【发布时间】:2021-05-04 11:56:00
【问题描述】:
给定两个未知变量x = (x1, x2),我想找到x1, x2 的最小值,这样一组约束Ax >= c 成立。这是一个玩具示例,我分别对x1 和x2 进行了优化(后来我意识到这不是正确的方法!):
我用 R 编码了这个:
library(lpSolveAPI)
c_vec <- c(0, 0, -0.42, 0.42, -0.81)
A_mat_col1 <- c(16, -15, -2, 3, 0.027)
A_mat_col2 <- c(15, 13, 16, 12, 13)
my.lp <- make.lp(nrow = 5, ncol = 2)
set.constr.type(my.lp, rep(">=", 5))
set.rhs(my.lp, c_vec)
set.column(my.lp, 1, A_mat_col1)
set.column(my.lp, 2, A_mat_col2)
set.bounds(my.lp, lower = rep(-Inf, 2), upper = rep(Inf, 2))
> my.lp
Model name:
C1 C2
Minimize 0 0
R1 16 15 >= 0
R2 -15 13 >= 0
R3 -2 16 >= -0.42
R4 3 12 >= 0.42
R5 0.027 13 >= -0.81
Kind Std Std
Type Real Real
Upper Inf Inf
Lower -Inf -Inf
然后循环最小化x1和x2如下:
lower <- vector()
for(i in 1:2){
set.objfn(my.lp, make_basis(i, p = 2))
lp.control(my.lp, sense = "min")
# If return value is 0, then problem is solved successfully
if(solve(my.lp) == 0){
lower[i] <- get.objective(my.lp)
# If return value is 3, then it's unbounded and I set lower bound to -Inf
}else if(solve(my.lp) == 3){
lower[i] <- -Inf
}
}
> lower
[1] -Inf 0.02876712
所以x1的最小值是-Inf,x2的最小值是0.02876712。但是,这不满足约束集。例如,第一个约束是16x1 + 15x2 >= 0,由于x1 是-Inf,那么结果将为负数。所以不满足第一个约束。
所以现在我想也许我应该解决以下问题:
有没有办法在 R 中优化多个目标?
【问题讨论】:
标签: r optimization mathematical-optimization linear-programming