I am using \'lpSolveAPI\' to solve multiple binary linear programming problems in R. How do I get it too return ALL the variable combinations for a maximizing problem.
I
Discovered that this was dupe with: Get multiple solutions for 0/1-Knapsack MILP with lpSolveAPI
Here is the code I used to solve this adapted from the link:
# find first solution
status<-solve(lpmodel)
sols<-list() # create list for more solutions
obj0<-get.objective(lpmodel) # Find values of best solution (in this case four)
counter <- 0 #construct a counter so you wont get more than 100 solutions
# find more solutions
while(counter < 100) {
sol <- get.variables(lpmodel)
sols <- rbind(sols,sol)
add.constraint(lpmodel,2*sol-1,"<=", sum(sol)-1)
rc<-solve(lpmodel)
if (status!=0) break;
if (get.objective(lpmodel)
This finds all six solutions:
> sols
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
sol 1 0 0 1 0 1 0 1
sol 1 0 0 1 0 1 1 0
sol 0 1 0 1 0 1 0 1
sol 0 1 0 1 0 1 1 0
sol 0 1 0 0 1 1 1 0
sol 0 1 0 0 1 1 0 1
Sorry for the dupe everyone. If anyone knows another way built into the 'lpSolveAPI' package I'd still love to know.