gurobipy.MConstr#
- class MConstr#
Gurobi matrix constraint object. An
MConstr
object is an array-like data structure that represents multiple linear constraints (in contrast to aConstr
object, which represents a single constraint). It behaves similar to NumPy’sndarray
s, e.g., it has a shape and can be indexed and sliced. Matrix constraints are always associated with a particular model. You typically create these objects withModel.addConstr
, using overloaded comparison operators onmatrix variables
andlinear matrix expressions
, or with the methodModel.addMConstr
.Constraint objects have a number of attributes. The full list can be found in the Attributes section of this document. Some constraint attributes can only be queried, while others can also be set. Recall that the Gurobi Optimizer employs a lazy update approach, so changes to attributes don’t take effect until the next call to
Model.update
,Model.optimize
, orModel.write
on the associated model.We should point out a few things about constraint attributes. Consider the
rhs
attribute. The values for a matrix constraintmc
can be queried usingmc.rhs
. The Gurobi library ignores letter case in attribute names, so it can also be queried asmc.RHS
. Attribute values 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
with the appropriate shape, or a scalar which is then applied to all of the associated constraints. However, as mentioned earlier, attribute modification is done in a lazy fashion, so you won’t see the effect of the change immediately. And some attributes can not be set (e.g., the Pi attribute), so attempts to assign new values to them will raise an exception.You can also use
MConstr.getAttr
/MConstr.setAttr
to access attributes. The attribute name can be passed to these routines as a string, or you can use the constants defined in theGRB.Attr
class (e.g.,GRB.Attr.RHS
).- fromlist(constrlist)#
Convert a list of constraints into an
MConstr
object. The shape is inferred from the contents of the list - a list ofConstr
objects produces a 1-DMConstr
object, a list of lists ofConstr
objects produces a 2-DMConstr
, etc.- Parameters:
constrlist – A list of Constr objects to populate the returned MConstr.
- Returns:
MConstr object corresponding to the input constraints.
- Example:
constrs = model.getConstrs() mc = MConstr.fromlist(constrs) # 1-D MConstr
- getAttr(attrname)#
Query the value of an attribute for a matrix constraint. The full list of available attributes can be found in the Attributes section.
Raises an
AttributeError
if the requested attribute doesn’t exist or can’t be queried. Raises aGurobiError
if there is a problem with theMConstr
object (e.g., it was removed from the model).The result is returned as a NumPy
ndarray
with the same shape as theMConstr
object.- Parameters:
attrname – The attribute being queried.
- Returns:
ndarray
of current values for the requested attribute.- Example:
mc = model.addConstr(A @ x <= b) rhs = mc.getAttr("RHS")
- setAttr(attrname, newvalue)#
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 (using
Model.update
), optimize the model (usingModel.optimize
), or write the model to disk (usingModel.write
).The full list of available attributes can be found in the Attributes section.
Raises an
AttributeError
if the specified attribute doesn’t exist or can’t be set. Raises aGurobiError
if there is a problem with theMConstr
object (e.g., it was removed from the model).- Parameters:
attrname – The attribute being modified.
newvalue –
ndarray
of desired new values for the attribute. The shape must be the same as theMConstr
object. Alternatively, you can pass a scalar argument, which will automatically be promoted to have the right shape.
- Example:
mc = model.addConstr(A @ x <= b) mc.setAttr("RHS", np.arange(A.shape[0])) mc.setAttr(GRB.Attr.RHS, 0.0) # broadcast