How to save model construction in a file to be able to update later
AnsweredI want to save the model construction (constraints, objective function) in a file, because the model construction alone took 5 hours. However, the model is currently infeasible so it will require changes. Can I save what I have now and retreive later to be able to update it with less time? or do I need to reconstrcut the model from scratch everytime?
I tried model.write("model.sol") doesn't work because I don't have results.
I tried .mst --> got this error"
GurobiError: No MIP start available-
Hi,
The Gurobi model object can be written into a file in one of the following four formats:
For the scenario you described, it makes sense to save the model in an MPS or an LP format by calling the model.write("model.mps") or model.write("model.lp") method. You can then read the model file in by calling model.read("model.mps") or model.read("model.lp") method. Make sure all the variables and constraints have explicit names before writing the model into a file.
Please check the links provided to learn more about how different file formats differ.
Best regards,
Maliheh
0 -
Hey Maliheh,
I have a question regarding your answer.
Assume I have saved the model with variables named X , Y and various constraints in .mps format in a file named a.mps. I want to load/open the model for modification in another file (could be anything such as python file). How can I access the model X and Y variables and constraints after I opened it?
Thanks,
Farzane
0 -
Hi Farzane,
How can I access the model X and Y variables and constraints after I opened it?
You can use the global function gurobipy.read() function to read the model file in as a Model object. You can then rely on methods such as Model.getConstrs() and Model.getVars() to retrieve the list of all linear constraints and variables, respectively, in the model. You can then access each constraint or variable information using the available linear constraint and variable attributes.
To access a linear constraint or a variable by name, you can use the methods Model.getConstrByName() and Model.getVarByName().
To retrieve the list of quadratic constraints or general constraints, you can use the methods Model.getQConstrs() and Model.getGenConstrs().
Best regards,
Maliheh
0
Please sign in to leave a comment.
Comments
3 comments