2
$\begingroup$

Is there a way to monitor the value of the mipgap while running an optimization model Pyomo? I would like to know how far I am from the optimality gap. Here is my code:

solver = SolverFactory('cbc', executable='path/to/cbc.exe')
solver.options['mipgap'] = 1e-3
solver.options['tee'] = True
solver.options['keepfiles'] = True
results = solver.solve(Model, logfile='path/to/logfile.log')

I can report the last value of mipgap AFTER solver is finished as follows:

__data = results.Problem._list

LB = __data[0].lower_bound
UB = __data[0].upper_bound
__gap = abs(UB - LB) * 100/max(0.1, abs(UB))
print(__gap)

But this is not what I am looking for. I am looking for a sort of live streaming from solver which updates me about the current value of the mipgap; then I can determine how far I am from the optimality; or whether there is any progress in the model.

$\endgroup$
2
  • 1
    $\begingroup$ Is there no solver log to watch? (Note Jupyter notebooks may not always show this; so try to run locally from the command line). $\endgroup$ Oct 20, 2021 at 11:42
  • $\begingroup$ There is a log that is stored in logfile='path/to/logfile.log' but it is only accessible once the solver is done; i.e., cannot monitor the value of mip-gap while running solve method. $\endgroup$ Oct 23, 2021 at 9:08

2 Answers 2

4
$\begingroup$

Short answer: Add this line - solver.options['slog'] = 1, before calling the solve method.

Detailed answer: In terms of logging, a user needs to pass two arguments to the solver in order to obtain the required. The first argument is tee=True that enables the solver logs and the second argument is slog=1 that defines the logging level. MIP-gap is logged at the info level but the default setting is warning. So, when you override the default logging level on the solver with the argument slog, you get to see those details.

This page and some digging inside cbc's github helped me figure that out.

$\endgroup$
5
  • $\begingroup$ Many thanks, Mohit. I just added solver.options['slog'] = True; solver.options['tee'] = True; solver.options['keepfiles'] = True but still nothing happening while running solve! Is it logging the info somewhere or some file? Nothing is happening in the command window! $\endgroup$ Oct 23, 2021 at 9:14
  • $\begingroup$ I thought I am over-writing it by adding solver.options['tee'] = True; afterwards. But removing it and keeping only solver.options['slog'] = 1 did not change anything. $\endgroup$ Oct 23, 2021 at 9:46
  • $\begingroup$ The expectation with that is it should print the logs in real-time on your console, just to make sure can you confirm you're not using any kind of package to silence logs in your code at any point? $\endgroup$
    – mohit-mhjn
    Oct 25, 2021 at 15:12
  • $\begingroup$ I tested on my side, you can utilize these files for reference also another recommendation is to try slog=3 github.com/mohit-mhjn/py_optimization/tree/master/… $\endgroup$
    – mohit-mhjn
    Oct 25, 2021 at 16:27
  • $\begingroup$ Many thanks for the response, Mohit. As advised in the example, I added the command solver.options['slog'] = 1 (and 3) along with results = solver.solve(Model, logfile='path/to/logfile.log', tee=True) but all it did, printing out the final status, including gap of course, AFTER solution was obtained with the requested accuracy. I ran your code as well which did the same. By the way, as checked, I did not have any package to silence logs. By any chance, would it be possible to share a sort of screenshot from your screen to see how your code works? $\endgroup$ Oct 26, 2021 at 17:24
2
$\begingroup$

Try this: results = solver.solve(Model, logfile='path/to/logfile.log', tee=True)

with tee=True, solver will display the log at runtime and you will be able to see the IP solution with the gap%.

$\endgroup$
4
  • $\begingroup$ I have tried that Jatin. It prints out the result at the end! I suspect it has to do with solver, i.e., CBC, glpk, etc. I know CPLEX does report intermediate values of gap but not sure of the open source ones $\endgroup$ Oct 28, 2021 at 17:46
  • $\begingroup$ It works fine with glpk. Didn't test on other one. $\endgroup$ Oct 29, 2021 at 3:56
  • $\begingroup$ You mean, you can track how the gap is converging to the tolerance,? What I am looking for is something like the following: solver = SolverFactory('cbc', executable='path/to/cbc.exe') solver.options['mipgap'] = 1e-5 solver.options['tee'] = True solver.options['keepfiles'] = True results = solver.solve(Model, logfile='path/to/logfile.log') then while (NOT AFTER) running the solver, it prints out gap: 5%, gap: 2%, gap: 0.5%, gap: 0.02%, ..., gap: 0%,; then prints out the result upon gap turning to 0% $\endgroup$ Nov 2, 2021 at 11:13
  • $\begingroup$ I finally managed to make it happen and I thought it worth sharing: Before calling solver (CBC.exe) solver.options = {'sec': 50000, 'threads': 6, 'ratio': solver_gap_ratio, 'slog': 3} It then prints out all the info $\endgroup$ Apr 18, 2022 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.