【问题标题】:R linear programming set up in linprog package ignores constraint (less than or equal to) using solveLP在 linprog 包中设置的 R 线性规划使用solveLP忽略约束(小于或等于)
【发布时间】:2013-12-28 21:47:30
【问题描述】:

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

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; 

【问题讨论】:

    标签: r statistics linear-programming convex-optimization


    【解决方案1】:

    与许多线性编程库一样, 有隐含的非负约束,y&gt;=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
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2023-02-22
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多