# Gurobi retrieve final optimality gap

(Sang Woo Park) #1

Hello,

I am currently using Gurobi within CVX.
I was able to solve the problem but was wondering how I could retrieve the final optimality gap?
I would like to store it in a Matlab variable but cannot find a way to refer it.
Below, I am attaching an example. The final gap is 0.0000% and I would like a way to store that value.
Thank you very much in advance!

## Calling Gurobi 7.52: 310 variables, 200 equality constraints

Gurobi optimizer, licensed to CVX for CVX
Optimize a model with 200 rows, 310 columns and 780 nonzeros
Variable types: 200 continuous, 110 integer (110 binary)
Coefficient statistics:
Matrix range [1e+00, 2e+00]
Objective range [5e-01, 5e-01]
Bounds range [1e+00, 1e+00]
RHS range [2e+00, 2e+00]
Found heuristic solution: objective 0.0000000
Presolve removed 108 rows and 254 columns
Presolve time: 0.00s
Presolved: 92 rows, 56 columns, 276 nonzeros
Variable types: 0 continuous, 56 integer (56 binary)

Root relaxation: objective -2.300000e+01, 11 iterations, 0.00 seconds

Nodes    |    Current Node    |     Objective Bounds      |     Work

Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time

0     0  -23.00000    0   10    0.00000  -23.00000      -     -    0s

H 0 0 -16.0000000 -23.00000 43.8% - 0s
0 0 -18.75000 0 17 -16.00000 -18.75000 17.2% - 0s
0 0 -17.50000 0 34 -16.00000 -17.50000 9.38% - 0s
0 0 -17.00000 0 32 -16.00000 -17.00000 6.25% - 0s
0 0 -17.00000 0 14 -16.00000 -17.00000 6.25% - 0s
0 0 -17.00000 0 12 -16.00000 -17.00000 6.25% - 0s
H 0 0 -17.0000000 -17.00000 0.00% - 0s

Cutting planes:
Zero half: 2

Explored 1 nodes (154 simplex iterations) in 0.11 seconds
Thread count was 4 (of 4 available processors)

Solution count 3: -17 -16 0

## Optimal solution found (tolerance 1.00e-04) Best objective -1.700000000000e+01, best bound -1.700000000000e+01, gap 0.0000%

Status: Solved
Optimal value (cvx_optval): +17

(Mark L. Stone) #2

http://cvxr.com/cvx/doc/solver.html#controlling-precision
Upon solution of your model, the tolerance level the solver has achieved is returned in the cvx_slvtol variable.

I don’t know whether cvx_slvtol is the achieved final optimality gap when Gurobi is called to solve a mixed-integer problem. if not, the only other thing I can think of is to use the MATLAB diary command to save the solver output to a file, then call a tool (script, program, etc.) with MATLAB’s system command to extract the final optimality gap from the screen output and save it into a MATLAB variable.

(Sang Woo Park) #3

Hello Mark,

I tried cvx_slvtol but it seems that it does not correspond to the optimality gap.
Instead what I just found is that we can manually calculate the gap by doing the following:
(cvx_optbnd - cvx_optval)/cvx_optval*100

Best,
SangWoo

(Mark L. Stone) #4

cvx_optbnd doesn’t seem to be documented anywhere.

(Sang Woo Park) #5

You are right. I also haven’t seen it anywhere before.
But when I run cvx now, it automatically creates cvx_optbnd…