Concepts
Features
Reference
Gurobi matrix constraint object. AnMConstr object is an array-likedata structure that represents multiple linear constraints (in contrastto aConstr object, which represents a single constraint).It behaves similar to NumPy’sndarrays, e.g., it has a shape andcan be indexed and sliced. Matrix constraints are always associated witha particular model. You typically create these objects withModel.addConstr, using overloaded comparison operators onmatrixvariables andlinearmatrixexpressions, or with the methodModel.addMConstr.
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. Considertherhs attribute. The values for a matrix constraintmc can bequeried usingmc.rhs. The Gurobi library ignores letter case inattribute names, so it can also be queried asmc.RHS. Attributevalues are returned as a NumPyndarray that has the same shape asmc. An attribute can be set, using a standard assignment statement(e.g.,constr.rhs=b), withb being either anndarray withthe appropriate shape, or a scalar which is then applied to all of theassociated constraints. However, as mentioned earlier, attributemodification is done in a lazy fashion, so you won’t see the effect ofthe 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 useMConstr.getAttr/MConstr.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.
Convert a list of constraints into anMConstr object. The shape isinferred from the contents of the list - a list ofConstr objectsproduces a 1-DMConstr object, a list of lists ofConstr objectsproduces a 2-DMConstr, etc.
constrlist – A list of Constr objects to populate the returnedMConstr.
MConstr object corresponding to the input constraints.
constrs=model.getConstrs()mc=MConstr.fromlist(constrs)# 1-D MConstr
Query the value of an attribute for a matrix constraint.
Raises anAttributeError if the requested attribute doesn’t exist orcan’t be queried. Raises aGurobiError if there is a problem withtheMConstr object (e.g., it was removed from the model).
The result is returned as a NumPyndarray with the same shape as theMConstr object.
attrname – The attribute being queried.
ndarray of current values for the requested attribute.
mc=model.addConstr(A@x<=b)rhs=mc.getAttr("RHS")
Set the value of a matrix 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 theMConstr object (e.g., it was removed from the model).
attrname – The attribute being modified.
newvalue –ndarray of desired new values for the attribute.The shape must be the same as theMConstr object. Alternatively, youcan pass a scalar argument, which will automatically be promoted to havethe right shape.
mc=model.addConstr(A@x<=b)mc.setAttr("RHS",np.arange(A.shape[0]))mc.setAttr(GRB.Attr.RHS,0.0)# broadcast
Help and Feedback