Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

python - Google OR-Tools (using SCIP solver) - How to access the intermediate solutions found by the solver?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.5k users

...