5

Is there a way to make lpSolve return multiple solutions? In below case i want (5,0) and (0,5) both.

If lpSolve cannot do that then is there any other R package which will return all possible solutions of an integral linear optimization program?

 library("lpSolve")
  A=matrix (c(1, 1), nrow=1, byrow=TRUE)

  b=(5)
  signs='=='
  c_=c(1,1)
  res = lpSolve::lp('max', c_, A, signs, b,  all.int = TRUE)
  res$solution

=======================================================================

I would also like to know why lpSolve package provides all possible solutions if all decision variables are binary. Why cannot it repeat the same when all variables are integer...

Nicolas Grebille
  • 1,332
  • 8
  • 15
user2543622
  • 5,760
  • 25
  • 91
  • 159
  • I feel that someone must have found a way to work around this. lets hope i get that through the bounty – user2543622 Feb 21 '15 at 01:12
  • You can do this with CPLEX, but it is not completely free (unless you can obtain an academic license). Would this option be of interest to you? – Ioannis Feb 22 '15 at 20:57
  • thanks for the input. But no. I cannot use cplex. It has to be R – user2543622 Feb 23 '15 at 03:05
  • I was talking about the R API of CPLEX. Still R, but CPLEX instead of lpsolve. Otherwise if you would like to use lpsolve you can formulate your problem with integer variables as one with binary variables, but this will increase the number of variables. If you are interested, I can provide an answer. – Ioannis Feb 23 '15 at 12:52
  • regarding binary variables, i am already trying that. If possible please answer http://stackoverflow.com/questions/28666795/r-lpsolve-binary-find-all-possible-solutions. Regarding R API of cplex: if it is R API then do i have to worry about whether cplex is free of cost or not? – user2543622 Feb 23 '15 at 14:26

1 Answers1

3

Code:

library(lpSolveAPI)

vBiv_of_v <- function (nbits,v){
   taillev<-length(v)
   taillevBivalent<-nbits*taillev
   vBivalent<-rep(0,taillevBivalent)

   for(iLg in seq(1,taillev)) {
     iCoef<-1
     for(iDelta in seq(1,nbits)){
       vBivalent[(iLg-1)*nbits+iDelta]<- iCoef*v[iLg]
       iCoef<-iCoef*2
     }
   }
   vBivalent
}

vBiv_to_v <- function (nbits,vBivalent) {
   taillevBivalent<-length(vBivalent)
   taillev<-taillevBivalent/nbits

   v<-rep(0,taillev)
   for(iLg in seq(1,taillev)) {
     for(iDelta in seq(1,nbits)){
       v[iLg]<-v[iLg]+2^(iDelta-1)*vBivalent[(iLg-1)*nbits+iDelta]
     }
   }
   v
}
nbVariable<-2
nbBits=3
nbVariableBivalentes<-nbVariable*nbBits
f.obj<-rep(0,nbVariableBivalentes)
mylp <- make.lp(0, nbVariableBivalentes)
set.objfn(mylp,f.obj)
add.constraint(mylp, vBiv_of_v(nbBits,c(1,1)), "=", 5)
set.type(mylp, 1:nbVariableBivalentes , type = "binary")

repeat {
  status<-solve(mylp)

  if(status == 0) {
    last_sol<-get.variables(mylp)

    vRes<-vBiv_to_v(nbBits,last_sol)
    cat(vRes[1],vRes[2],"\n")

    #add cutting
    new_rhs <- 0;
    f.condSup<-rep(0,nbVariableBivalentes)
    for (iCol in 1:nbVariableBivalentes) {
      f.condSup[iCol] <-  2 * last_sol[iCol] - 1
      new_rhs <- new_rhs + last_sol[iCol];
    }
    add.constraint(mylp, f.condSup, "<=", new_rhs - 1)
  }
  else if(status == 2) {
    cat("No more solution.\n") 
    break
  }
}

Result:

5 0
4 1
3 2
1 4
2 3
0 5
No more solution.
Community
  • 1
  • 1
V. Michel
  • 1,599
  • 12
  • 14