如何将二进制矩阵转换为R中的data.frame? lpSolveAPI

时间:2019-12-26 17:25:39

标签: r optimization lpsolve

library(lpSolveAPI)
lprec1 <- make.lp(0,nrow(df) 

  add.constraint(lprec1, c(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,0,0,0,0), "<=", as.numeric(ads1)) 
  add.constraint(lprec1, c(0,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,0,0,0), "<=", as.numeric(ads2)) 
  add.constraint(lprec1, c(0,0,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,0,0), "<=", as.numeric(ads3)) 
  add.constraint(lprec1, c(0,0,0,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,0), "<=", as.numeric(ads4)) 
  add.constraint(lprec1, c(0,0,0,0,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), "<=", as.numeric(ads5)) 
  add.constraint(lprec1, c(0,0,0,0,0,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), "<=", as.numeric(ads6)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,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), "<=", as.numeric(ads7)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,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), "<=", as.numeric(ads8)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,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), "<=", as.numeric(ads9)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads10)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads11)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads12)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads13)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads14)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads15)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads16)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads17)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads18)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads19)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads20)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads21)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads22)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads23)) 
  add.constraint(lprec1, c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0), "<=", as.numeric(ads24)) 
  add.constraint(lprec1, c(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,0,0,0,0,0,0,0,0), "<=", as.numeric(ads25)) 
  add.constraint(lprec1, c(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,0,0,0,0,0,0,0), "<=", as.numeric(ads26)) 
  add.constraint(lprec1, c(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,0,0,0,0,0,0), "<=", as.numeric(ads27)) 
  add.constraint(lprec1, c(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,0,0,0,0,0), "<=", as.numeric(ads28)) 
  add.constraint(lprec1, c(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,0,0,0,0), "<=", as.numeric(ads29))
  add.constraint(lprec1, c(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,0,1,0,0,0), "<=", as.numeric(ads30)) 
  add.constraint(lprec1, c(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,0,0,1,0,0), "<=", as.numeric(ads31))
  add.constraint(lprec1, c(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,0,0,0,1,0), "<=", as.numeric(ads32)) 
  add.constraint(lprec1, c(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,0,0,0,0,1), "<=", as.numeric(ads33)) 
solve(lprec1) 
print(get.variables(lprec1)) 
print(get.objective(lprec1))

我将所有 ads 向量都转换为单个data.frame df $ ads,是否也可以转换此矩阵?我尝试使用

add.constraint(lprec1, diag(nrow (df))), "<=", as.vector(df$ads))
solve(lprec1)

但是lpSolveAPI识别出长度是不同的: add.constraint(lprec1,diag(nrow(df),“ <=”,df $ ads)中的错误:  “ xt”的长度不等于模型中决策变量的数量,但是有33个决策变量,nrow(df)为33 ... 有什么方法可以使矩阵二值化吗?

length(diag(nrow(df))) = 361

长度是不同的大小,有什么方法可以将这些向量转换为单个数据帧,长度= 33?

1 个答案:

答案 0 :(得分:0)

这是一个选项,其中我们创建list个向量,遍历list的序列并分配约束

a <- as.vector(diag(5))
lst1 <- asplit(matrix(a, ncol = 5, byrow = TRUE), 1)

library(lpSolveAPI)
lprec1 <- make.lp(0, length(lst1)) 
ads <- c(0, 5, 1, -1, 0)
for(i in seq_along(lst1)) add.constraint(lprec1, lst1[[i]], "<=", ads[i])
solve(lprec1)
#[1] 2