If you are using the Python or LP- or MPS-file interface to Cplex or Gurobi (e.g., solver_io=‘python’, solver_io=‘lp’) you should find the absolute gap stored as results.solution.gap. However, the solution gets taken off of the results object and loaded into the model by default, so you will need to disable this by calling solve with load_solutions=False. E.g.,
results = opt.solve(model, load_solutions=False)
if len(results.solution) > 0:
# you may need to relax these checks in certain cases
assert str(results.solver.status) == ‘ok’
assert str(results.solver.termination_condition) == ‘optimal'
absgap = results.solution(0).gap
# now load the solution
model.solutions.load_from(results)
If you are using the AMPL interface to a solver (e.g., gurobi_ampl), the gap is often not reported by default in the standardized solution format returned by the ASL library. Sometimes you can tell solvers to return it as a suffix, which you load into the Pyomo model with the solution by adding a
Suffix component to the model before the solve. See the ‘return_mipgap’ command-line option for gurobi_ampl as an example.