gurobipy.MQConstr#
- class MQConstr#
Gurobi matrix quadratic constraint object. An
MQConstr
object is an array-like data structure that represents multiple quadratic constraints (in contrast to aQConstr
object, which represents a single quadratic constraint). It behaves similar to NumPy’sndarray
s, e.g., it has a shape and can be indexed and sliced. Matrix quadratic constraints are always associated with a particular model. You typically create these objects withModel.addConstr
, using overloaded comparison operators onmatrix variables
,matrix linear expressions
, andmatrix quadratic expressions
.Quadratic 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 quadratic constraint attributes. Consider the QCRHS attribute. The values for a matrix quadratic constraint
mqc
can be queried usingmc.QCRHS
. The Gurobi library ignores letter case in attribute names, so it can also be queried asmc.qcrhs
. Attribute values are returned as a NumPyndarray
that has the same shape asqmc
. An attribute can be set, using a standard assignment statement (e.g.,mqc.qcrhs = b
), withb
being either anndarray
with the appropriate shape, or a scalar which is then applied to all of the associated quadratic 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 QCPi attribute), so attempts to assign new values to them will raise an exception.You can also use
MQConstr.getAttr
/MQConstr.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.QCRHS
).- fromlist(qconstrlist)#
Convert a list of quadratic constraints into an
MQConstr
object. The shape is inferred from the contents of the list - a list ofQConstr
objects produces a 1-DMQConstr
object, a list of lists ofQConstr
objects produces a 2-DMQConstr
, etc.- Parameters:
constrlist – A list of QConstr objects to populate the returned MQConstr.
- Returns:
MQConstr object corresponding to the input constraints.
- Example:
qconstrs = model.getQConstrs() mqc = MQConstr.fromlist(qconstrs) # 1-D MQConstr
- getAttr(attrname)#
Query the value of an attribute for a matrix quadratic 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 theMQConstr
object (e.g., it was removed from the model).The result is returned as a NumPy
ndarray
with the same shape as theMQConstr
object.- Parameters:
attrname – The attribute being queried.
- Returns:
ndarray
of current values for the requested attribute.- Example:
mqc = model.addConstr(x**2 + y <= 1) qcrhs = mc.getAttr("QCRHS")
- setAttr(attrname, newvalue)#
Set the value of a matrix quadratic 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 theMQConstr
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 theMQConstr
object. Alternatively, you can pass a scalar argument, which will automatically be promoted to have the right shape.
- Example:
mqc = model.addConstr(x * y - x - y <= 0) mqc.setAttr("QCRHS", np.arange(x.size)) mqc.setAttr(GRB.Attr.RHS, 1.0) # broadcast scalar
- tolist()#
Return the quadratic constraints associated with this matrix quadratic constraint as a list of individual
QConstr
objects.- Returns:
List of
QConstr
objects.- Example:
mqc = model.addConstr(x * y <= b) qconstrlist = mqc.tolist() # Do something with the QConstr corresponding to mqc[3] print(qconstrlist[3])