gurobipy.MConstr#
- class MConstr#
Gurobi matrix constraint object. An
MConstrobject is an array-like data structure that represents multiple linear constraints (in contrast to aConstrobject, which represents a single constraint). It behaves similar to NumPy’sndarrays, 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 variablesandlinear matrix expressions, or with the methodModel.addMConstr.Constraint objects have a number of attributes. 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.writeon the associated model.We should point out a few things about constraint attributes. Consider the
rhsattribute. The values for a matrix constraintmccan 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 NumPyndarraythat has the same shape asmc. An attribute can be set, using a standard assignment statement (e.g.,constr.rhs = b), withbbeing either anndarraywith 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.setAttrto access attributes. The attribute name can be passed to these routines as a string, or you can use the constants defined in theGRB.Attrclass (e.g.,GRB.Attr.RHS).The full list of attributes can be found in the Attributes section of this document. Examples of how to query and set attributes can also be found in this section.
- fromlist(constrlist)#
Convert a list of constraints into an
MConstrobject. The shape is inferred from the contents of the list - a list ofConstrobjects produces a 1-DMConstrobject, a list of lists ofConstrobjects 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.
Raises an
AttributeErrorif the requested attribute doesn’t exist or can’t be queried. Raises aGurobiErrorif there is a problem with theMConstrobject (e.g., it was removed from the model).The result is returned as a NumPy
ndarraywith the same shape as theMConstrobject.- Parameters:
attrname – The attribute being queried.
- Returns:
ndarrayof 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).Raises an
AttributeErrorif the specified attribute doesn’t exist or can’t be set. Raises aGurobiErrorif there is a problem with theMConstrobject (e.g., it was removed from the model).- Parameters:
attrname – The attribute being modified.
newvalue –
ndarrayof desired new values for the attribute. The shape must be the same as theMConstrobject. 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