R中的优化:最大化和最小化多个变量 [英] Optimization in R: Maximizing and Minimizing Many Variables

查看:179
本文介绍了R中的优化:最大化和最小化多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含70种食物和每种食物营养价值信息(蛋白质/盎司,脂肪/盎司,加仑/盎司等)的数据集,以及食物的成本/盎司。我试图弄清楚 - 给定一个预算在$ - 食物(和每种食物的最佳组合)的最佳组合是最大化蛋白质,减少脂肪,减少卡路里等。我的目标是做到这一点通过一系列的价格点,并绘制每个价格点。



我发现了一大堆不同的包,可以帮助这里: http://cran.r-project.org/web/views/Optimization.html 。然而,我是一名初学者,不确定哪些方面最有帮助/从哪里开始 - 会喜欢熟悉解决这类优化问题的人的一些建议。

>解决方案

这被称为饮食问题,它是线性编程的流行介绍(参见,例如我在饮食问题中发现的第一次Google打击)。通过诸如 lpSolve 等软件包的线性规划解算器可用于解决饮食问题的许多变体。

例如,

,请考虑上述链接中问题的版本,您可以从中选择以下食物:

 (食物< - 数据帧(Food = c(玉米,2%牛奶,小麦面包),CostPerServing = c(.18,.23,.05),维生素A = c(107,500,0),卡路里= c(72,121,65)))
#食物CostPerServing维生素A卡路里
#1玉米0.18 107 72
#2 2%牛奶0.23 500 121
#3小麦面包0.05 0 65

假设你想要找出每种食物的份数,成本受制于总卡路里必须介于2000和2500之间以及维生素A的量必须介于5000和50000之间。如果您定义了变量X1,X2和X2,那么您的目标是.18 * X1 + .23 * X2 + .05 * X3是变量的线性函数。同样,你的约束条件是变量的线性函数;例如,卡路里数量的下限是形式72 * X1 + 121 * X2 + 65 * X3> = 2000的限制。 <$>来自 lpSolve 包的函数c $ c> lp 包含一个向量,该向量表示目标值中的系数,以及有关约束的信息矩阵,每个约束的方向以及每个约束的右侧)。对于所述的问题,这可能是:

$ p $ lt; code> library(lpSolve)
mod < - lp(min ,#min / max
food $ CostPerServing,#Objective
rbind(食物$维他命A,食物$维生素A,食物$卡路里,食物$卡路里),#约束矩阵
c(> = ,< =,> =,< =),#约束方向
c(5000,50000,2000,2500))

在模型解决后,您可以查看目标函数和值:

pre > 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

满足约束条件的最低成本为2.91美元,您可以通过使用0份玉米,10份2%牛奶和12.15份小麦面包达到此目的。这产生了5000单位的维生素A和2000卡路里。

I have a dataset with 70 foods and information about each food's nutritional value (protein/oz., fat/oz., cals/oz., etc.), as well as the food's cost/oz. I am trying to figure out--given a set budget in $--what the best combination of foods (and the amt. of each food) would be to maximize protein, minimize fat, minimize calories, etc. I aim to do this across a series of price points, and to plot each.

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'm a beginner and am not sure what would be most helpful/where to start - would love some suggestions from anyone familiar with solving these kinds of optimization problems.

解决方案

This is called the diet problem and it is a popular introduction to linear programming (see, e.g., the first Google hit I found for the diet problem). Linear programming solvers through packages such as lpSolve can be used to solve many variants of the diet problem.

For instance, consider the version of the problem in the link above, where you have the following foods to choose from:

(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

Assume you wanted to wanted to find the number of servings of each food that minimize total cost subject to the constraint that the total calories must be between 2000 and 2500 and the amount of Vitamin A must be between 5000 and 50000. If you defined variables X1, X2, and X2, then your objective is .18*X1 + .23*X2 + .05*X3, a linear function of the variables. Similarly each of your constraints in a linear function of the variables; for instance, the lower bound on the number of calories is a constraint of the form 72*X1 + 121*X2 + 65*X3 >= 2000.

The lp function from the lpSolve package takes as input a vector indicating the coefficients in the objective value, and information about constraints (a constraint matrix, the direction of each constraint, and the right-hand side of each constraint). For the stated problem, this 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 solved, 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 would be $2.91, and you can achieve this by using 0 servings of corn, 10 servings of 2% milk, and 12.15 servings of wheat bread. This yields exactly 5000 units of Vitamin A and exactly 2000 calories.

这篇关于R中的优化:最大化和最小化多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆