how to remove and add back a constraint?
Answeredimport gurobipy as grb
model = grb.read('model.lp')
model.optimize()
c = model.getConstrByName('budget')
c.__dict__
# this shows NULL object : {'_cmodel': <capsule object NULL at 0x1063e6420>, '_rowno': 0}
# is that a problem?
model.remove(c)
# looking a new lp I can confirm that this deletes the constraint.
Now how can I add back constraint `c` ?
-
Gurobi does lazy updates to the model. You won't see that this constraint was deleted until the update is processed. To force the update to process immediately, call model.update().
0 -
I understand. My question is, how can I add this constraint back to the model?
0 -
If you want to remove a constraint and add it later, you need to get the elements of a constraint. For a linear constraint, it looks like this:
c = m.getConstrs()[0] # for example
lhs, sense, rhs, name = m.getRow(c), c.Sense, c.RHS, c.ConstrName
m.remove(c)
# Optional, to show the current state
m.update()
m.printStats()
# Add the constraint
c = m.addConstr(lhs, sense, rhs, name)
# Also optional, to show the current state
m.update()
m.printStats()2 -
That's helpful, thanks.
But if I want to add constraint back in a .NET environment, how can I do to get the lhs of the constraint c?
I found that there's no attribute 'LHS' in a constraint of .NET environment so I don't know how to do that....
Thanks!0 -
Hi,
You can get the LHS by using the getRow() function.
Best regards,
Jaromił1 -
Hello,
It seems
c = m.addConstr(lhs, sense, rhs, name)
is no longer supported in the newest version of gurobi. What should I do to add back a constraint in this case?
0 -
Hi Fei,
Model.addLConstr should solve your problem.
- Riley
0
Please sign in to leave a comment.
Comments
7 comments