3
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?

  • I tried to use as.table (diag (nrow (df)) too, Is it impossible to isolate each line of a matrix with a function? – Vinicius Soares Dec 26 '19 at 19:04
  • I tried to use as.data.frame (diag (nrow (df)) too – Vinicius Soares Dec 26 '19 at 19:06
  • What package are you using? What kind of variable is lprec1? Please check the output of class(lprec1), str(lprec1), and attributes(lprec1). – BigFinger Dec 26 '19 at 19:51
  • lprec1 is a lpSolve linear program model object from lpSolveAPI package. class(lprec1): "lpExtPtr" – Vinicius Soares Dec 26 '19 at 20:43
  • Looking at the documentation of add.constraint(), it requires argument "rhs" to be a single numeric value. In your case it is as.vector(df$ads), which I suspect has length > 1. This is likely a source of error. This user guide may help you figure what you need to do: http://civil.colorado.edu/~balajir/CVEN5393/R-sessions/sess1/lpSolveAPI-vignettes.pdf – BigFinger Dec 26 '19 at 21:09
  • if rhs is a single numeric value, cannot pass a data.frame as an argument? a data.frame is a vector set – Vinicius Soares Dec 27 '19 at 15:02
  • It depends on the implementation of the package whether you can make such a substitution. I don't know the answer. A "single numeric value" is in fact a numeric vector of length 1. A data frame is a list with particular attributes. Numeric vectors and lists are 2 different built-in data types in the R language. – BigFinger Dec 27 '19 at 16:30
  • What is 'ads1', 'ads2' etc – akrun Dec 27 '19 at 23:04
  • it was a vector with 1 numeric value inside, i turned all ads1 ads2 ads3 into df$ads line 1 is ads1 line 2 is ads2 etc – Vinicius Soares Dec 28 '19 at 16:30

1 Answers1

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
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @ViniciusSoares Please check my example, where I created the 'ads' as the same length as the number of rows or the length of lst1 – akrun Dec 27 '19 at 23:20
  • Error in abs(xt) : non-numeric argument to mathematical function – Vinicius Soares Dec 28 '19 at 14:13
  • @ViniciusSoares. Not with the example I posted – akrun Dec 28 '19 at 17:34
  • @ViniciusSoares. Not clear about the error as the `lst1` length is the same as the number of elements in 'ads' vector. Do you have `ads` as a `list`? – akrun Dec 28 '19 at 18:28
  • error: Error in lst1 [[i]]: subscript out of bounds, but if I keep giving ctrl + enter the code it creates lprec1, but the restriction of lst1 [[i]] doesn't work – Vinicius Soares Dec 28 '19 at 18:35
  • i used tidyverse tribble, to turn ads into a dataframe column, – Vinicius Soares Dec 28 '19 at 18:36
  • class(df) [1] "tbl_df" "tbl" "data.frame" – Vinicius Soares Dec 28 '19 at 18:42
  • @ViniciusSoares Can you show your code with `for` loop – akrun Dec 28 '19 at 18:43
  • @ViniciusSoares Can you try `for(i in seq_along(lst1)) add.constraint(lprec1, lst1[[i]], "<=", ads[[i]])`. Here, I am using `ads[[i]]]` or `df[[i]]` – akrun Dec 28 '19 at 18:50
  • library(lpSolveAPI) lprec1 <- make.lp(0,nrow(df)) lp.control(lprec1,sense='max') set.type(lprec1, columns=c(seq(from=1,nrow(df),by=1)), type =c("integer")) a<-diag(nrow(df)) lst1 <- asplit(matrix(a, ncol = nrow(df), byrow = TRUE), 1) for(i in seq_along(a)) add.constraint(lprec1, lst1[[i]], "<=", df$ads) print(solve(lprec1)) print(get.variables(lprec1)) print(get.objective(lprec1)) – Vinicius Soares Dec 28 '19 at 18:52
  • @ViniciusSoares. What is the length of `df$ads` it needs to be either 1 or length of each list element i.e `length(lst[[1]])` – akrun Dec 28 '19 at 18:53
  • the length of df$ads and lst1 is the same – Vinicius Soares Dec 28 '19 at 18:56
  • @ViniciusSoares. In that case you need `for(i in seq_along(lst1)) add.constraint(lprec1, lst1[[i]], "<=", df$ads[i])` – akrun Dec 28 '19 at 18:56
  • Funny, it works perfectly if I ignore the error message "Error in lst1 [[i]]: subscript out of bounds" and keep pressing ctrl + enter, but if I try to run all the code at once R doesn't ignore this error , can you tell me why this happens? – Vinicius Soares Dec 28 '19 at 19:06
  • @ViniciusSoares. It could be based on the how you are indexing. If you check my code and the vectors, each index is correctly applied – akrun Dec 28 '19 at 19:07