How to add more constraints after loading the model from a ".mps" file
AnsweredAfter loading a Gurobi model from file (e.g. m = gp.read('my_model.mps')), I am unable to add more constraint to this model. It returns an error saying the variables are not defined. Is it possible to add constraints after loading the model from ".mps" file?
0
-
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?. -
When you read a model from a file, you have to get access to the variables first before adding a constraint.
If you know the names of the variables, one way of retrieving the variables and working with them is
import gurobipy as gp
# read model with only 2 variables "x" and "y"
m = gp.read("myLP.lp")
V = m.getVars()
myVars = {}
# create a dictionary {'x': <gurobi.Var x>, 'y': <gurobi.Var y>}
for v in V:
myVars[v.VarName] = v
# you can now access the constraints via their names
m.addConstr(myVars["x"] + myVars["y"] <= 0)If you don't have any information about the variable names, i.e., you don't know the names and/or their meaning, then I don't see a good way to work the variables when reading a model from a file.
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments