gpt4 book ai didi

linprog 包中设置的 R 线性规划使用 solveLP 忽略约束(小于或等于)

转载 作者:行者123 更新时间:2023-12-02 04:41:10 25 4
gpt4 key购买 nike

我正在使用 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 and 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;

最佳答案

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

solveLP 似乎不允许负解:您可以将问题转换为只有非负值(将 y1 替换为 u1-v1,将 y2 替换为 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
)

关于linprog 包中设置的 R 线性规划使用 solveLP 忽略约束(小于或等于),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20819743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com