Retrieving MVar from Model read from file
AnsweredAfter loading a Gurobi model from file (e.g. m = gp.read('my_model.mps')), I am unable to retrieve matrix variables.
For example, if the model had MVar with name "X" when it was previously constructed and saved, reloading the model and doing m.getVars() will return a list of every scalar entry in X (and all other matrix variables), and m.getVarByName("X") returns None.
How can I retrieve my MVars?
-
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?. -
Apparently it is a new feature, but it would be really nice to be able to retrieve them as a matrix.
1 -
Unfortunately, this is not how gurobipy and Gurobi work. The Python API in gurobipy is, by design, just a layer around the C API of Gurobi. And the C API does not understand the concept of matrix variables. The writing and reading of model files happens on the C layer. Hence, the structure of matrix variables is not contained in the *.mps files.
Regards,
Tobias
0 -
I have come across the same issue. What works well for me is creating a model with the same variables (MVars), then reading the values to model from the *.sol file.
In Python:
model = gb.Model()
var = model.addMVar((correct, dimensions), name="var")
model.update() # prepare the variables to be accessible to read
model.read("solution.sol") # load up the values
model.optimize() # without constraints, this is almost instant
print(val.X) # val.X can now be accessed0
Post is closed for comments.
Comments
4 comments