Concepts
Features
Reference
Gurobi constraint object. Constraints are always associated with aparticular model. You create a constraint object by adding a constraintto a model (usingModel.addConstr), rather than by using aConstr constructor.
Constraint objects have a number of attributes.Some constraint attributes can only be queried, while others can also beset. Recall that the Gurobi Optimizer employs a lazy update approach, sochanges to attributes don’t take effect until the next call toModel.update,Model.optimize, orModel.write on the associated model.
We should point out a few things about constraint attributes. Consider therhs attribute. Its value can be queried usingconstr.rhs. TheGurobi library ignores letter case in attribute names, so it can also bequeried asconstr.rhs. It can be set using a standard assignmentstatement (e.g.,constr.rhs=0). However, as mentioned earlier,attribute modification is done in a lazy fashion, so you won’t see theeffect of the change immediately. And some attributes can not be set (e.g.,thePi attribute), so attempts to assign new values to them willraise an exception.
You can also useConstr.getAttr/Constr.setAttr to access attributes. The attributename can be passed to these routines as a string, or you can use theconstants defined in theGRB.Attr class (e.g.,GRB.Attr.RHS).
The full list of attributescan be found in theAttributes section of thisdocument. Examples of how to query and set attributes can also be foundinthis section.
Query the value of a constraint attribute.
Raises anAttributeError if the requested attribute doesn’t exist orcan’t be queried. Raises aGurobiError if there is a problem withtheConstr object (e.g., it was removed from the model).
attrname – The attribute being queried.
The current value of the requested attribute.
print(constr.getAttr(GRB.Attr.Slack))print(constr.getAttr("slack"))
This property returns the current index, or order, of the constraint inthe underlying constraint matrix.
Note that the index of a constraint may change after subsequent modelmodifications.
-2: removed, -1: not in model, otherwise: index of theconstraint in the model
c=model.getConstrs()[0]print(c.index)# Index will be 0
Check whether two constraint objects refer to the same constraint.
constr2 – The other constraint.
Boolean result indicates whether the two constraint objectsrefer to the same model constraint.
print(model.getConstrs()[0].sameAs(model.getConstrs()[1]))
Set the value of a constraint attribute. Note that, due to our lazy update approach,the change won’t actually take effect until you update the model (usingModel.update), optimize the model (usingModel.optimize), or write the model to disk (usingModel.write).
Raises anAttributeError if the specified attribute doesn’t exist orcan’t be set. Raises aGurobiError if there is a problem with theConstr object (e.g., it was removed from the model).
attrname – The attribute being modified.
newvalue – The desired new value of the attribute.
constr.setAttr(GRB.Attr.RHS,0.0)constr.setAttr("rhs",0.0)
Help and Feedback