Optimization in R: Maximizing and Minimizing Many Variables

I have a dataset with 70 foods and information about each nutritional value (protein / ounce, fat / ounce, cal / ounce, etc.) as well as the cost / ounce of food. I'm trying to figure out - given a given $ budget - that the best combination of foods (and the amount of each meal) would be to maximize protein, minimize fat, minimize calories, etc. I intend to do this across a series of price points and for each of them.

I found a whole bunch of different packages that could help with this here: http://cran.r-project.org/web/views/Optimization.html . However, I am a beginner and not sure if it would be very helpful / where to start - would love to get some suggestions from those familiar with solving such optimization problems.

+3


source to share


1 answer


This is called the diet problem and it is a popular introduction to linear programming (see, for example, the first Google hit I found for the diet problem ). Linear software solvers through packages such as lpSolve

can be used to solve many variations of the dietary problem.

For example, consider the version of the problem in the link above, where you can select the following products:

(food <- data.frame(Food=c("Corn", "2% Milk", "Wheat Bread"), CostPerServing=c(.18, .23, .05), VitaminA=c(107, 500, 0), Calories=c(72, 121, 65)))
#          Food CostPerServing VitaminA Calories
# 1        Corn           0.18      107       72
# 2     2% Milk           0.23      500      121
# 3 Wheat Bread           0.05        0       65

      

Suppose you wanted to find the number of servings of each food that minimizes the total cost, subject to the restriction that the total number of calories should be between 2,000 and 2,500, and the amount of vitamin A should be between 5,000 and 50,000. If you are certain X1 variables, X2 and X2, then your goal is .18 * X1 +.23 * X2 +.05 * X3, a linear function of variables. Likewise, each of your constraints is in a linear function of variables; for example, the lower limit of the number of calories is the limit of the form 72 * X1 + 121 * X2 + 65 * X3> = 2000.

The function lp

from the package lpSolve

takes at the input a vector indicating the coefficients in an objective value, and information about the constraints (a matrix of constraints, the direction of each constraint and the right side of each constraint). For the specified problem, it would be:



library(lpSolve)
mod <- lp("min",  # min/max
          food$CostPerServing,  # Objective
          rbind(food$VitaminA, food$VitaminA, food$Calories, food$Calories),  # Constraint matrix
          c(">=", "<=", ">=", "<="),  # Constraint directions
          c(5000, 50000, 2000, 2500))

      

After the model has decided, you can look at the objective function and values:

mod$objval
# [1] 2.907692
mod$solution
# [1]  0.00000 10.00000 12.15385
sum(food$VitaminA * mod$solution)
# [1] 5000
sum(food$Calories * mod$solution)
# [1] 2000

      

The cheapest cost to meet the constraints is $ 2.91, and you can achieve this with 0 servings of corn, 10 servings of 2% milk, and 12.15 servings of wheat bread. This provides exactly 5000 units of vitamin A and exactly 2000 calories.

+4


source







All Articles