我在 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;