# Pyomo mipgap status monitoring

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.

• Is there no solver log to watch? (Note Jupyter notebooks may not always show this; so try to run locally from the command line). Oct 20 at 11:42
• 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. Oct 23 at 9:08

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.

• 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! Oct 23 at 9:14
• 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. Oct 23 at 9:46
• 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? Oct 25 at 15:12
• 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/… Oct 25 at 16:27
• 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? Oct 26 at 17:24

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%.

• 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 Oct 28 at 17:46
• It works fine with glpk. Didn't test on other one. Oct 29 at 3:56
• 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% Nov 2 at 11:13