PySCIPOpt icon indicating copy to clipboard operation
PySCIPOpt copied to clipboard

How to collect (all) feasible solutions during SCIP solve?

Open XiangyuYang-Opt opened this issue 4 years ago • 6 comments

Is there any way to collect possibly all the feasible solutions during solve?

XiangyuYang-Opt avatar Dec 03 '21 08:12 XiangyuYang-Opt

Hi, have a look at the "getSols" function in scip.pyx, that should be what you are looking for, additionally have a look at some additional information.

CGraczyk avatar Dec 03 '21 11:12 CGraczyk

this is related to #248

mattmilten avatar Dec 03 '21 12:12 mattmilten

This issue does not seem to be fully resolved by referring to #248.

XiangyuYang-Opt avatar Dec 06 '21 03:12 XiangyuYang-Opt

import pyscipopt as pso
from pyscipopt import Model

model = Model()
#The first case: cannot output the collected feasible solutions
x = model.addVar('x', vtype = 'I')
y = model.addVar('y', vtype = 'I')
model.setObjective(x + 2*y)
model.addCons(-4*x - 2*y  <= -5)
#The second case: can successfully output the collected feasible solutions
#x = model.addVar('x', vtype = 'I')
#y = model.addVar('y', vtype = 'I')
#z = model.addVar('z', vtype = 'I')
#model.setObjective(-x - 4*y - 3*z)
#model.addCons(2*x + 2*y + z <= 5)
#model.addCons(x + 2*y + 2*z <= 6)
model.optimize()
model.setBoolParam("constraints/countsols/collect", True)

nsols = model.getNSols()
sols = model.getSols()

print('nsol =', nsols)
print('sols =', sols)

As noted in the above example, the first given MIP instance cannot output the collected feasible solutions while the second makes it. Could anyone explain this?

XiangyuYang-Opt avatar Dec 06 '21 07:12 XiangyuYang-Opt

@CGraczyk, could you please have another look? It seems the first model does indeed not populate the solution pool properly.

mattmilten avatar Dec 06 '21 17:12 mattmilten

Hi, i will have a look at this, though i will not be able to attend to this issue specifically until beginning of next year. Maybe you can have a closer look at the issue referred to by @mattmilten and go from there if you need an immediate solution.

CGraczyk avatar Dec 07 '21 16:12 CGraczyk

I have also encountered this issue. From a few limited experiences, it seems that whenever it's the trivial heuristic that finds the optimal solution, the solution pool is not properly populated.

I encountered a similar issue with a solution limit not being respected whenever the solutions were found by the trivial heuristic. I imposed a limit of 1 solution, and the trivial heuristic finds 4. Link for the gitlab issue: link

It seems consistent with the example provided as well. The trivial heuristic finds the only solution in the first example, but other heuristics are responsible for the second example. Does this seem plausible, @XiangyuYang-Opt, @CGraczyk, @mattmilten?

EDIT: This also reproduces the error.

m = Model()
x = m.addVar()
m.setObjective(x)
m.optimize()

nsols = m.getNSols()
sols = m.getSols()

print('nsol =', nsols)
print('sols =', sols)

Outputs:

image

Joao-Dionisio avatar Jun 09 '23 16:06 Joao-Dionisio

Hello, @XiangyuYang-Opt! I think the issue is that the function getSols prints the solution of the transformed problem. SCIP enters a presolving stage, where it looks for ways to make the problem simpler. In very simple instances, this means that all the variables might actually get deleted, which is happening in your case.

I've now changed the getSols function so that it prints the solution with respect to the original variables.

Joao-Dionisio avatar Oct 06 '23 16:10 Joao-Dionisio