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 a QConstr object, which represents a single quadratic constraint). It behaves similar to NumPy’s ndarrays, 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 with Model.addConstr, using overloaded comparison operators on matrix variables, matrix linear expressions, and matrix 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, or Model.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 using mc.QCRHS. The Gurobi library ignores letter case in attribute names, so it can also be queried as mc.qcrhs. Attribute values are returned as a NumPy ndarray that has the same shape as qmc. An attribute can be set, using a standard assignment statement (e.g., mqc.qcrhs = b), with b being either an ndarray 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 the GRB.Attr class (e.g., GRB.Attr.QCRHS).

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 correponding to mqc[3]
print(qconstrlist[3])
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 a GurobiError if there is a problem with the MQConstr object (e.g., it was removed from the model).

The result is returned as a NumPy ndarray with the same shape as the MQConstr 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 (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 MQConstr 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 MQConstr 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