使用R lpSolve在线性编程中添加1000个约束

时间:2013-08-28 17:13:44

标签: r

我正在尝试实施一个大线性模型,但我遇到了问题 添加更多变量/约束。例如,我想添加约束而不添加 矩阵中的其他零(0)(f.con),例如:

library(linprog)
f.obj <- c(3,24,79,140,230,306,338,
           26,78,145,226,309,336,354,
           146,250,312,364,417,496,508,
           314,382,424,472,503,525,548)
f.con <- matrix (c(1,1,1,1,1,1,1, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0,
                   0,0,0,0,0,0,0, 1,1,1,1,1,1,1, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0,
                   0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 1,1,1,1,1,1,1, 0,0,0,0,0,0,0,
                   0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 1,1,1,1,1,1,1)             
                 ,nrow=4, byrow=TRUE)
f.rhs <- c(10,22,8,30)
f.dir <- c("<=", "<=","<=","<=")
lp ("max", f.obj, f.con, f.dir, f.rhs)

n<-lp ("max", f.obj, f.con, f.dir, f.rhs)$solution
n

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你是在追求这样的事情:

makef.con <- function(f.obj,f.dir){
  k      <- length(f.dir)
  Nconst <- length(f.obj) / k
  Diag   <- diag(k)
  c(matrix(rep(Diag, each = Nconst), nrow = k, byrow = TRUE))
}
all(makef.con(f.obj,f.dir) == f.con)
[1] TRUE

虽然说实话,我不熟悉你在做什么,所以你可能需要玩这个以便它能够根据你的需要进行推广