Get multiple solutions for 0/1-Knapsack MILP with lpSolveAPI

China☆狼群 提交于 2019-12-06 02:26:07

Looks like that is broken. Here is a DIY approach for your specific model:

# first problem
rc<-solve(lp_model)
sols<-list()
obj0<-get.objective(lp_model)
# find more solutions
while(TRUE) {
   sol <- round(get.variables(lp_model))
   sols <- c(sols,list(sol))
   add.constraint(lp_model,2*sol-1,"<=", sum(sol)-1)
   rc<-solve(lp_model)
   if (rc!=0) break;
   if (get.objective(lp_model)<obj0-1e-6) break;
}
sols

The idea is to cut off the current integer solution by adding a constraint. Then resolve. Stop when no longer optimal or when the objective starts to deteriorate. Here is some math background.

You should see now:

> sols
[[1]]
[1] 1 0 1

[[2]]
[1] 0 1 1

Update

Below in the comments it was asked why coefficients of the cut have the form 2*sol-1. Again have a look at the derivation. Here is a counter example:

           C1   C2        
Maximize    0   10        
R1          1    1  <=  10
Kind      Std  Std        
Type      Int  Int        
Upper       1    1        
Lower       0    0       

With "my" cuts this will yield:

> sols
[[1]]
[1] 0 1

[[2]]
[1] 1 1

while using the suggested "wrong" cuts will give just:

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