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 a Constr object, which represents a single constraint). It behaves similar to NumPy’s ndarrays, 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 with Model.addConstr, using overloaded comparison operators on matrix variables and linear matrix expressions, or with the method Model.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, or Model.write on the associated model.

We should point out a few things about constraint attributes. Consider the rhs attribute. The values for a matrix constraint mc can be queried using mc.rhs. The Gurobi library ignores letter case in attribute names, so it can also be queried as mc.RHS. Attribute values are returned as a NumPy ndarray that has the same shape as mc. An attribute can be set, using a standard assignment statement (e.g., constr.rhs = b), with b being either an ndarray 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 the GRB.Attr class (e.g., GRB.Attr.RHS).

tolist()#

Return the constraints associated with this matrix constraint as a list of individual Constr objects.

Returns:

List of Constr objects.

Example:
mc = model.addConstr(A @ x <= b)
constrlist = mc.tolist()
# Do something with the Constr correponding to mc[3]
print(constrlist[3])
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 a GurobiError if there is a problem with the MConstr object (e.g., it was removed from the model).

The result is returned as a NumPy ndarray with the same shape as the MConstr 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 (using Model.optimize), or write the model to disk (using Model.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 a GurobiError if there is a problem with the MConstr object (e.g., it was removed from the model).

Parameters:
  • attrname – The attribute being modified.

  • newvaluendarray of desired new values for the attribute. The shape must be the same as the MConstr 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