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

回眸只為那壹抹淺笑 提交于 2020-01-05 07:15:02

问题


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))

I turned all my ads vectors into a single data.frame df$ads, is there any way to transform this matrix too? I tried using

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

but lpSolveAPI recognizes that length is different:Error in add.constraint(lprec1, diag(nrow(df), "<=", df$ads) : the length of ‘xt’ is not equal to the number of decision variables in the model but there are 33 decision variables and the nrow(df) are 33... Is there any way to binarize without having to make this matrix?

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

The length is different sizes, is there any way to turn these vectors into a single data.frame with length = 33?


回答1:


Here is an option, where we create a list of vectors, loop through the sequence of list and assign the constraints

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


来源:https://stackoverflow.com/questions/59491515/how-to-turn-a-binary-matrix-into-a-data-frame-in-r-lpsolveapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!