Python API - MGenConstr#

class MGenConstr#

Gurobi matrix general constraint object. An MGenConstr object is an array-like data structure that represents multiple general constraints (in contrast to a GenConstr object, which represents a single general constraint). It behaves similar to NumPy’s ndarrays, e.g., it has a shape and can be indexed and sliced. Matrix general constraints are always associated with a particular model. Currently only the Model.addGenConstrIndicator method produces MGenConstr objects when matrix-friendly objects are used as inputs.

General constraint objects have a number of attributes. The full list can be found in the Attributes section of this document. Some general 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.

Note that in most cases to query general constraint data, you will need to index into the MGenConstr object and pass individual GenConstr objects to methods such as Model.getGenConstrIndicator to read back general constraint data.

fromlist(genconstrlist)#

Convert a list of general constraints into an MGenConstr object. The shape is inferred from the contents of the list - a list of GenConstr objects produces a 1-D MGenConstr object, a list of lists of GenConstr objects produces a 2-D MGenConstr, etc.

Parameters:

genconstrlist – A list of GenConstr objects to populate the returned MGenConstr.

Returns:

MGenConstr object corresponding to the input general constraints.

Example:
gc0, gc1, gc2, gc3 = model.getGenConstrs()
mgc_1d = MGenConstr.fromlist([gc0, gc1, gc2, gc3])  # 1-D MGenConstr
mgc_2d = MGenConstr.fromlist([[gc0, gc1], [gc2, gc3]])  # 2-D MGenConstr
getAttr(attrname)#

Query the value of an attribute for a matrix general 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 MGenConstr object (e.g., it was removed from the model).

The result is returned as a NumPy ndarray with the same shape as the MGenConstr object.

Parameters:

attrname – The attribute being queried.

Returns:

ndarray of current values for the requested attribute.

Example:
mgc = model.addGenConstrIndicator(z, 1.0, A @ x <= b)
model.computeIIS()
iis = mc.getAttr("IISGenConstr")
setAttr(attrname, newvalue)#

Set the value of a matrix general 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 MGenConstr 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 MGenConstr object. Alternatively, you can pass a scalar argument, which will automatically be promoted to have the right shape.

Example:
mgc = model.addGenConstrIndicator(z, 1.0, A @ x <= b)
iis = mc.setAttr("IISGenConstrForce", 1)
model.computeIIS()
tolist()#

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

Returns:

List of GenConstr objects.

Example:
mgc = model.addGenConstrIndicator(z, True, A @ x <= b)
genconstrlist = mgc.tolist()