Gurobi status code
AnsweredHi everyone,
I would like to know if there is a proper way to convert the integer status codes into strings ?
I'm using a handmade dictionary (in Python), but would like to know if there is a pre-built feature to do this.
Thank you!
-
Official comment
The full list of status codes is documented at https://www.gurobi.com/documentation/current/refman/optimization_status_codes.html. There is nothing wrong with writing your own dictionary to convert the code values to strings. The status codes are normally hardcoded during compilation and the values (e.g, OPTIMAL=2) will not be changed in the future. However, we have occasionally extended the list in the past with additional values for new major releases.
The Gurobi Python API provides class constants that can also be used (GRB.OPTIMAL, etc.) so your dictionary definition could look like this:
status = { "LOADED" : GRB.LOADED, "OPTIMAL" : GRB.OPTIMAL, .... }
-
Official comment
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 why not try our AI Gurobot?. -
You can hack this using the Python
__dict__
method:
sc = gurobipy.StatusConstClass
d = {sc.__dict__[k]: k for k in sc.__dict__.keys() if k[0] >= 'A' and k[0] <= 'Z'}Then the dictionary `d` is now:
{1: 'LOADED', 2: 'OPTIMAL', 3: 'INFEASIBLE', 4: 'INF_OR_UNBD', 5: 'UNBOUNDED', 6: 'CUTOFF', 7: 'ITERATION_LIMIT', 8: 'NODE_LIMIT', 9: 'TIME_LIMIT', 10: 'SOLUTION_LIMIT', 11: 'INTERRUPTED', 12: 'NUMERIC', 13: 'SUBOPTIMAL', 14: 'INPROGRESS', 15: 'USER_OBJ_LIMIT'}
2 -
Hi,
Seems that we're getting an attibute error in the latest version:
module 'gurobipy' has no attibute 'StatusConstClass'
Importing the class directly fixed it.
from gurobipy._statusconst import StatusConstClass
status_dict = {value: name for name, value in StatusConstClass.__dict__.items() if not name.startswith('__') and not callable(value)}
status_dict.get(code, "UNKNOWN STATUS CODE")0 -
Hi,
The gurobipy module was restructured a bit for version 12.0. This should do the trick in version 12.0:
from gurobipy import GRB
d = {getattr(GRB.Status, k): k for k in GRB.Status.__dir__() if "A" <= k[0] <= "Z"}Best regards,
Maliheh
1 -
Hi, thanks, working well :)
0
Post is closed for comments.
Comments
6 comments