R:如何解决以下线性规划问题

问题描述

我对解决以下线性规划问题很感兴趣。

在这个玩具示例中,第二个约束告诉我 x1 <= -1,即 x1 必须为负,因此 x1 的最小值应该为负。使用 lpSolveAPI,我编写了这个玩具示例。

library(lpSolveAPI)
my.lp <- make.lp(nrow = 2,ncol = 2)
set.column(my.lp,1,c(1,2))
set.column(my.lp,2,c(3,0))
set.objfn(my.lp,0))
set.constr.type(my.lp,rep("<=",2))
set.rhs(my.lp,c(-4,-2))
set.bounds(my.lp,lower = c(-Inf,-Inf),upper = c(Inf,Inf))
> my.lp
Model name: 
            C1    C2        
Minimize     1     0        
R1           1     3  <=  -4
R2           2     0  <=  -2
Kind       Std   Std        
Type      Real  Real        
Upper      Inf   Inf        
Lower     -Inf  -Inf 

然而,在 R 中解决这个线性规划问题给了我

> solve(my.lp)
[1] 3
> get.variables(my.lp)
[1]   3.694738e-57 -2.681562e+154
> get.objective(my.lp)
[1] 1e+30

get.objective(my.lp) 返回 1e+30 的值 x1,这显然不满足第二个约束。我专门使用了 set.bounds 以便 x1,x2 可以在实线上取任何值,但我仍然没有得到负数。哪里出错了?

解决方法

library(CVXR)

x1 <- Variable(1)
x2 <- Variable(1)

# Problem definition
objective <- Minimize(x1)
constraints <- list(x1 + 3*x2 <= -4,2*x1 + 0*x2  <= -2)
prob <- Problem(objective,constraints)

# Problem solution
sol <- solve(prob)

sol$value
# [1] -Inf

sol$status
# [1] "unbounded"