2

我在 linprog R 包中使用 solveLP 来解决一个简单的线性规划问题:

minimize -x1-x2 
subject to 2*x1+x2+x3    =12
           x1+2*x2   +x4 = 9
           x1,x2,x3,x4 >=0

它具有双重等价物:

maximize 12*y1+9*y2 
subject to 2*y1+y2 <= -1
           y1+2*y2 <= -1
           y1,y2 <=0

如果我以原始形式陈述问题,我会得到正确的结果 (5,2,0,0)。但是当以对偶形式陈述问题时,前两个约束就被忽略了。我得到的结果 (0,0) 明显违反(2*y1+y2 <= -1 和 y1+2*y2 <= -1),我是否缺少额外的设置或参数?请看看下面的代码,让我知道你的想法:

require(linprog)
objVec <- c(-1,-1,0,0) 
rhsConstr <- c(12, 9,0,0,0,0) 
Amat <- rbind( c( 2, 1, 1, 0 ),
               c( 1, 2, 0, 1 ),
               c( 1, 0, 0, 0 ),
               c( 0, 1, 0, 0 ),
               c( 0, 0, 1, 0 ),
               c( 0, 0, 0, 1 ))
res <- solveLP( objVec, rhsConstr, Amat, maximum=FALSE, const.dir = c("==","==",">=",">=",">=",">=") , lpSolve=TRUE)
res$solution

# dual problem - this is where the problem is
objVec <- c(12,9) 
rhsConstr <- c(-1.0,-1.0,0,0) 
Amat <- rbind( c( 2, 1),
               c( 1, 2), 
               c( 1, 0),
               c( 0, 1))
res <- solveLP( objVec, rhsConstr, Amat, maximum=TRUE, const.dir = rep("<=",length(rhsConstr)))
res$solution

在正空间中,对偶问题确实给出了正确答案(1/3,1/3):

objVec <- c(12,9); 
rhsConstr <- c(1,1,0,0); 
Amat <- rbind( c( 2, 1), c( 1, 2), c( 1, 0), c( 0, 1)); 
res <- solveLP( objVec, rhsConstr, Amat, maximum=FALSE, const.dir = rep(">=",length(rhsConstr)) , lpSolve=TRUE); 
res$solution; 
4

1 回答 1

5

与许多线性编程库一样,存在隐含的非负约束y>=0:没有可行的解决方案(但我希望res$status指出这一点)。

solveLP似乎不允许负面解决方案:您可以将问题转换为仅具有非负值(替换y1u1-v1, y2with u2-v2)或使用另一个允许负值的包。

library(Rglpk)
objVec <- c(12,9) 
rhsConstr <- c(-1.0,-1.0,0,0) 
Amat <- rbind( c( 2, 1),
               c( 1, 2), 
               c( 1, 0),
               c( 0, 1))
Rglpk_solve_LP( 
  objVec, Amat, rep("<=",4), rhsConstr,
  bounds = list( lower = list( ind=c(1L,2L), val=c(-Inf,-Inf) ),
                 upper = list( ind=c(1L,2L), val=c( Inf, Inf) ) ), 
  max=TRUE
)
于 2013-12-29T02:50:25.143 回答