2

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.

lbrandao
  • 95
  • 10

1 Answers1

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.

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22
  • I'm afraid I'm facing another issue. The solution pool works but my objective function ( solver.Minimize(C) ) instead of decreasing over time is, in fact, increasing. I've added the code " while solver.NextSolution(): localtime = solver.wall_time() makespan_evolution[localtime] = C.solution_value()" right after the solver.Solve(). What I'm I doing wrong? Thanks in advance. – lbrandao Jan 11 '21 at 17:06
  • I suppose it prints out solutions in the reverse order they were found. – Laurent Perron Jan 11 '21 at 18:19
  • You're right @LaurentPerron! However, the loop on NextSolution() is not able to access/output the optimal value (I know, in advance, the optimal values of my MIP problem). For example, if the optimal value is 262. The loop outputs: 263, 264, 268, ... In other words, it "skips" 262. Any suggestion on how to also "grab" that value? Thank you. – lbrandao Jan 11 '21 at 20:20
  • Forgot to mention that I tried to reach the optimal value using solver.Objective().Value() but it also returns the "263". – lbrandao Jan 11 '21 at 20:58
  • The first (optimal) solution is reached just after `Solve()`. Then previous solutions can be reached through `NextSolution()`. – Laurent Perron Jan 11 '21 at 21:28