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

How to turn a binary matrix into a data.frame in R? lpSolveAPI

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 的 data.frame 吗?

这是一个选项,我们创建一个 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