How to get lpsolveAPI to return all possible solutions?

后端 未结 1 2004
感情败类
感情败类 2021-01-25 22:03

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

相关标签:
1条回答
  • 2021-01-25 23:02

    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)<obj0) break;
      counter <- counter + 1
    }
    sols
    

    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.

    0 讨论(0)
提交回复
热议问题