Gurobi Cookbook - Relaxed Function (3/5)

Obtaining the lp relaxation of a MIP

The function relaxed of the model object creates a copy of the problem with the intgrality constraints relaxed.

relaxed = m.relax()
Optimize a model with 637 rows, 120 columns and 14280 nonzeros
Coefficient statistics:
  Matrix range    [1e+00, 1e+00]
  Objective range [1e+00, 1e+00]
  Bounds range    [1e+00, 1e+00]
  RHS range       [1e+00, 1e+01]
Presolve time: 0.01s
Presolved: 120 rows, 757 columns, 14400 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0   -0.0000000e+00   0.000000e+00   1.752000e+03      0s
     414    1.7142857e+01   0.000000e+00   0.000000e+00      0s

Solved in 414 iterations and 0.04 seconds
Optimal objective  1.714285714e+01


The relax function returns a fresh of the original model. The original model will still have the original constraints. You can see this by querying the attribute isMIP.

cplex users

In the cplex interactive shell, this corresponds to the command change problem lp except that this command will modify the global model instead of returning a new model

m.isMIP, relaxed.isMIP
(1, 0)


The dual price is available only for continous models.

Attempting to query this attribute for MIPs (or an continuous model without a solution) will result in the exception

GurobiError: Unable to retrieve attribute 'Pi'

If you are unsure that the model has integer variables, you can check with m.isMIP