【发布时间】:2020-04-22 13:51:15
【问题描述】:
我正在尝试通过使用线性规划来解决优化问题。我有一份我知道多种营养素含量的产品清单。然后,目标是找到最接近特定营养素需求的产品组合。在 R 中,我编写了一个能够解决此问题的脚本。
library(lpSolveAPI)
# Required nutrients
nitrogen = 1500
phosphate = 530
# Product properties (sample dataset, this will later be expanded with more products and nutrients)
products <- data.frame(
p1 = c(0.2, 0.1),
p2 = c(0.8, 0),
p3 = c(0.15, 0.2),
p4 = c(0.1, 0.25),
p5 = c(0, 0.4)
)
# Create model
model <- make.lp(nrow = 2, ncol = ncol(products))
# Add the products as decisions
for (p in 1:ncol(products)) {
set.column(model, p, products[, p])
}
# Set the required amount as constraint
set.constr.value(model, rhs = c(nitrogen, phosphate))
set.constr.type(model, types = c(2,2))
# Set the objective function
set.objfn(model, rep(1, ncol(products)))
lp.control(model, sense='min')
solve(model)
get.objective(model)
但是,我现在想添加限制,即不能使用超过一定数量(例如 2 个)的产品。我正在考虑添加一个二进制约束,但不知道如何实现它。我能够发现的唯一选择是将决策变量设置为二进制,但这不能选择使用产品的多个单位。
那么如何添加限制以不使用超过 2 或 3 个产品?
【问题讨论】:
标签: r optimization constraints linear-programming lpsolve