'Cplex Python Gaps

How can I print the gaps(each iteration) like OPL's engine in Python? I want to keep the gaps like this:

     Nodes                                         Cuts/
   Node  Left     Objective  IInf  Best Integer    Best Bound    ItCnt     Gap

      0     0     5627,2325   167                   5627,2325       11         
*     0+    0                         8610,2984     5627,2325            34,65%
      0     0     5627,2325   186     8610,2984     Cuts: 157      158   34,65%
      0     0     5627,2325   186     8610,2984      Cuts: 38      188   34,65%
*     0+    0                         8100,9408     5627,2325            30,54%
*     0+    0                         7058,3430     5627,2325            20,28%
      0     2     5627,2325   186     7058,3430     5678,4051      188   19,55%
Elapsed time = 1,00 sec. (349,63 ticks, tree = 0,02 MB, solutions = 3)
*    86+   13                         7029,5430     5678,5253            19,22%
*   128+   12                         7026,1830     5678,5253            19,18%
*   170+   12                         7025,9430     5724,8207            18,52%
*   227+   78                         6972,1152     5757,6981            17,42%
*   415+  157                         6907,3358     5757,6981            16,64%
*   562+  241                         6787,0188     5757,6981            15,17%

I am using the solve_details.gap, but it returns the solution gap.

>>> model.solve_details.gap


Solution 1:[1]

In Easy optimization with python I shared get solution 1 by 1

from docplex.mp.model import Model
from docplex.mp.progress import *

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.parameters.mip.limits.solutions=1

while (1==1):
    sol=mdl.solve(log_output=False)
    for v in mdl.iter_integer_vars():
       print(v," = ",v.solution_value)

    print("objective = ",sol.get_objective_value())
    print("best bound = ",mdl.solve_details.best_bound)
    print("mip gap = ",mdl.solve_details.mip_relative_gap)  
       
    print("status : ",mdl.solve_details.status)   
    if ("optimal solution" in str(mdl.solve_details.status)):
        break

that could help you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Fleischer