gurobipy.MGenConstr#
- class MGenConstr#
Gurobi matrix general constraint object. An
MGenConstrobject is an array-like data structure that represents multiple general constraints (in contrast to aGenConstrobject, which represents a single general constraint). It behaves similar to NumPy’sndarrays, e.g., it has a shape and can be indexed and sliced. Matrix general constraints are always associated with a particular model. Currently only theModel.addGenConstrIndicatormethod producesMGenConstrobjects 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, orModel.writeon 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.getGenConstrIndicatorto 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
AttributeErrorif the requested attribute doesn’t exist or can’t be queried. Raises aGurobiErrorif there is a problem with theMGenConstrobject (e.g., it was removed from the model).The result is returned as a NumPy
ndarraywith the same shape as theMGenConstrobject.- Parameters:
attrname – The attribute being queried.
- Returns:
ndarrayof 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 (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
AttributeErrorif the specified attribute doesn’t exist or can’t be set. Raises aGurobiErrorif there is a problem with theMGenConstrobject (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 theMGenConstrobject. 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()