gurobipy.MQConstr#
- class MQConstr#
Gurobi matrix quadratic constraint object. An
MQConstrobject is an array-like data structure that represents multiple quadratic constraints (in contrast to aQConstrobject, which represents a single quadratic constraint). It behaves similar to NumPy’sndarrays, 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. 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 quadratic constraint attributes. Consider the QCRHS attribute. The values for a matrix quadratic constraint
mqccan 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 NumPyndarraythat has the same shape asqmc. An attribute can be set, using a standard assignment statement (e.g.,mqc.qcrhs = b), withbbeing either anndarraywith 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.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.QCRHS).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(qconstrlist)#
Convert a list of quadratic constraints into an
MQConstrobject. The shape is inferred from the contents of the list - a list ofQConstrobjects produces a 1-DMQConstrobject, a list of lists ofQConstrobjects 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
AttributeErrorif the requested attribute doesn’t exist or can’t be queried. Raises aGurobiErrorif there is a problem with theMQConstrobject (e.g., it was removed from the model).The result is returned as a NumPy
ndarraywith the same shape as theMQConstrobject.- Parameters:
attrname – The attribute being queried.
- Returns:
ndarrayof 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).Raises an
AttributeErrorif the specified attribute doesn’t exist or can’t be set. Raises aGurobiErrorif there is a problem with theMQConstrobject (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 theMQConstrobject. 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
QConstrobjects.- Returns:
List of
QConstrobjects.- Example:
mqc = model.addConstr(x * y <= b) qconstrlist = mqc.tolist() # Do something with the QConstr corresponding to mqc[3] print(qconstrlist[3])