gurobipy.QConstr#
- class QConstr#
Gurobi quadratic constraint object. Quadratic constraints are always associated with a particular model. You create a quadratic constraint object by adding a quadratic constraint to a model (using
Model.addQConstr), rather than by using aQConstrconstructor.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,Model.writeon the associated model.We should point out a few things about quadratic constraint attributes. Consider the
qcrhsattribute. Its value can be queried usingqconstr.qcrhs. The Gurobi library ignores letter case in attribute names, so it can also be queried asqconstr.QCRHS. It can be set using a standard assignment statement (e.g.,qconstr.qcrhs = 0). 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., theqcpiattribute), so attempts to assign new values to them will raise an exception.You can also use
QConstr.getAttr/QConstr.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.
- getAttr(attrname)#
Query the value of a quadratic constraint attribute.
Raises an
AttributeErrorif the requested attribute doesn’t exist or can’t be queried. Raises aGurobiErrorif there is a problem with theQConstrobject (e.g., it was removed from the model).- Parameters:
attrname – The attribute being queried.
- Returns:
The current value of the requested attribute.
- Example:
print(qconstr.getAttr(GRB.Attr.QCSense)) print(qconstr.getAttr("qcsense"))
- setAttr(attrname, newvalue)#
Set the value of a 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 theQConstrobject (e.g., it was removed from the model).- Parameters:
attrname – The attribute being modified.
newvalue – The desired new value of the attribute.
- Example:
constr.setAttr(GRB.Attr.QCRHS, 0.0) constr.setAttr("qcrhs", 0.0)