Serialize/deserialize Gurobi models
AnsweredHello Gurobi Team,
I would like to serialize/deserialize Gurobi models, similar to Python's pickle/dill dump and load functionalities.
When I run dill on a gurobipy Model, I am running into
RecursionError: maximum recursion depth exceeded while calling a Python object
which I cannot fix by adjusting
sys.setrecursionlimit(...)
What is your recommended strategy for serialization/deserialization?
I am trying to avoid writing my own wrappers that would capture everything that lives outside of the common file formats.
Thank you
-
You can save a Gurobi model as an MPS file by calling
model.write("model.mps")
# Read it later
model = gp.read("model.mps")This will preserve variable and constraint names which you can then use to identify them.
I'm not sure pickling is supported or recommended.
Cheers,
David0 -
Thanks, David. We're aware of the common formats supported by the write function.
However, we'd like to avoid separately storing all other Gurobi model attributes that are not part of, e.g., MPS files (for example, solver hyperparameters, variable and constraint attributes, etc.).
Is there no other way to dump a model and load it back into memory? For example, models may be run as part of a cron job over night. If there is something going wrong, we'd like to be able to investigate the model as-is - without having to fully reconstruct it from scratch using MPS files and separate attribute files we'd have to manage ourselves.
0 -
We replied to this query via a separate internal ticket.
I post the answer from Alison for others in the forum:
It sounds like simply storing the MPS model file is insufficient for this case. You noted that you also wanted to store additional attribute information. There are two additional files that I recommend you output and store:
-
Attribute file (ATTR). The Gurobi attribute file serves as a repository for storing and retrieving attribute information related to a model. This information can be either specified by the user or generated during a previous solution call. This is the main information our customers look for in the attributes If they are available, this file stores the following key attributes:
- X - This is the primal solution from the last optimization call.
- Start - It keeps all the stored MIP start vectors.
- It also stores the following attributes, if available: Partition, VarHintVal, VarHintPri, BranchPriority, Lazy, VTag, CTag, QCTag, VBasis, CBasis, PStart, and DStart.
- Parameter file (PRM). The Gurobi parameter file stores the parameter-value pairs specified in the model object.
Cheers,
David0 -
Attribute file (ATTR). The Gurobi attribute file serves as a repository for storing and retrieving attribute information related to a model. This information can be either specified by the user or generated during a previous solution call. This is the main information our customers look for in the attributes If they are available, this file stores the following key attributes:
Please sign in to leave a comment.
Comments
3 comments