问题
I'm new to Google OR-Tools.
Using Python, I implemented a MIP model with SCIP as a solver. The objective function is for minimization (solver.Minimize(C)) and I am accessing the final solution through solver.Objective().Value().
However, I also need to access the intermediate solutions that the solver finds, before reaching the final one, and their timestamp. (The final goal is to plot a graph with the solutions evolution through time).
I tried to use a while loop:
solutions_evolution = {} # dict to store miliseconds (as keys) and solutions (as values)
localtime = (solver.wall_time()) #in miliseconds
limit_break = localtime + 60*5*1000 #sets a time limit of 5 minutes
while (localtime <= limit_break) & (status != pywraplp.Solver.OPTIMAL & status != pywraplp.Solver.FEASIBLE):
new_localtime = (solver.wall_time())
solutions_evolution[new_localtime] = solver.Objective().Value()
localtime = new_localtime
But it's not working since solver.Objective().Value() gives only the final solution.
I've been struggling for days. Can anyone please help me? Thank you.
回答1:
In non C++ languages, you cannot access the solver object, and incumbent callbacks are not accessible in python.
You can use solution pool though with SCIP.
Just run Solve()
, then loop on NextSolution()
.
If your problem is purely integral, you can use the CP-SAT solver (directly, not through the linear solver wrapper). It supports solution callback in python.
来源:https://stackoverflow.com/questions/65664045/google-or-tools-using-scip-solver-how-to-access-the-intermediate-solutions-f