Cannot access MIPGap attribute
回答済みI use Python 3.7.9 and Gurobi 9.1.0. When I create a model object `prob` and solve it, I cannot access the MIPGap attribute. This is a minimal reproducible example.
import gurobipy as gp
from gurobipy import GRB
import numpy as np# https://stackoverflow.com/questions/36988341/how-to-get-the-relative-mip-optimality-gap-after-timelimit-is-met
# get MIPGap by prob.optimize() -> prob.MIPGapprob = gp.Model("lp")
a = prob.addVar()
b = prob.addVar()prob.setObjective(3*a+2*b, GRB.MAXIMIZE)
# prob.Params.NonConvex = 2
prob.addConstr(a + b <= 3)
prob.addConstr(2*a + b <= 4)prob.optimize()
print("prob.status", prob.status)
print("a", a.x)
print("b", b.x)
print("MIPGap", prob.MIPGap)
(My use case is that I am solving a nonconvex quadratic problem and I want to terminate after a time limit before the solver can solve to optimality and I know the MIPGap -- the error bound -- at the time of termination.) The output is:
Using license file /Users/ja3484/gurobi.lic
Academic license - for non-commercial use only - expires 2022-10-02
Gurobi Optimizer version 9.1.0 build v9.1.0rc0 (mac64)
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0x8d50bd05
Coefficient statistics:
Matrix range [1e+00, 2e+00]
Objective range [2e+00, 3e+00]
Bounds range [0e+00, 0e+00]
RHS range [3e+00, 4e+00]
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzerosIteration Objective Primal Inf. Dual Inf. Time
0 5.0000000e+30 3.500000e+30 5.000000e+00 0s
2 7.0000000e+00 0.000000e+00 0.000000e+00 0sSolved in 2 iterations and 0.00 seconds
Optimal objective 7.000000000e+00
prob.status 2
a 1.0
b 2.0
Traceback (most recent call last):
File "test.py", line 24, in <module>
print("MIPGap", prob.MIPGap)
File "src/gurobipy/model.pxi", line 343, in gurobipy.gurobipy.Model.__getattr__
File "src/gurobipy/model.pxi", line 1842, in gurobipy.gurobipy.Model.getAttr
File "src/gurobipy/attrutil.pxi", line 100, in gurobipy.gurobipy.__getattr
AttributeError: Unable to retrieve attribute 'MIPGap'
In particular, this is the problem:
AttributeError: Unable to retrieve attribute 'MIPGap'
I used to be able to do this before but I don't know why I can't do this anymore.
Thanks for your help!
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
The example problem is a continuous linear problem, so it is not solved as a MIP and there is no MIPGap to report. The model status is \(\texttt{GRB.OPTIMAL}\), meaning the problem was solved to optimality. You can check if a problem is solved as a MIP by querying the value of the IsMIP model attribute:
print("IsMIP:", prob.IsMIP)If you were to change the model so Gurobi solves the problem as a MIP (e.g., by changing a variable to be integer or introducing a non-convex quadratic constraint like you mentioned), you should be able to query the MIP gap.
I used to be able to do this before but I don't know why I can't do this anymore.
Do you mean you used to be able to query the MIP gap of a continuous linear problem like this? It's expected that the MIP gap is not available for such problems.
0
投稿コメントは受け付けていません。
コメント
2件のコメント