Concepts
Features
Reference
Gurobi matrix quadratic constraint object. AnMQConstr object is anarray-like data structure that represents multiple quadratic constraints(in contrast to aQConstr object, which represents a singlequadratic constraint). It behaves similar to NumPy’sndarrays,e.g., it has a shape and can be indexed and sliced. Matrix quadraticconstraints are always associated with a particular model. You typicallycreate these objects withModel.addConstr, using overloadedcomparison operators onmatrixvariables,matrixlinearexpressions, andmatrixquadraticexpressions.
Quadratic constraint objects have a number of attributes.Some constraint attributes can only be queried, while otherscan also be set. Recall that the Gurobi optimizer employs a lazy updateapproach, so changes to attributes don’t take effect until the next calltoModel.update,Model.optimize, orModel.write on the associated model.
We should point out a few things about quadratic constraint attributes.Consider theQCRHS attribute. The values for a matrix quadraticconstraintmqc can be queried usingmc.QCRHS. The Gurobi libraryignores letter case in attribute names, so it can also be queried asmc.qcrhs. Attribute values are returned as a NumPyndarray thathas the same shape asqmc. An attribute can be set, using a standardassignment statement (e.g.,mqc.qcrhs=b), withb being eitheranndarray with the appropriate shape, or a scalar which is thenapplied to all of the associated quadratic constraints. However, asmentioned earlier, attribute modification is done in a lazy fashion, soyou won’t see the effect of the change immediately. And some attributescan not be set (e.g., theQCPi attribute), so attempts to assignnew values to them will raise an exception.
You can also useMQConstr.getAttr/MQConstr.setAttr to access attributes. Theattribute name can be passed to these routines as a string, or you canuse the constants defined in theGRB.Attr class (e.g.,GRB.Attr.QCRHS).
The full list of attributescan be found in theAttributes section of thisdocument. Examples of how to query and set attributes can also be foundinthis section.
Convert a list of quadratic constraints into anMQConstr object. Theshape is inferred from the contents of the list - a list ofQConstrobjects produces a 1-DMQConstr object, a list of lists ofQConstr objects produces a 2-DMQConstr, etc.
constrlist – A list of QConstr objects to populate the returnedMQConstr.
MQConstr object corresponding to the input constraints.
qconstrs=model.getQConstrs()mqc=MQConstr.fromlist(qconstrs)# 1-D MQConstr
Query the value of an attribute for a matrix quadratic constraint. Thefull list of available attributes can be found in theAttributes section.
Raises anAttributeError if the requested attribute doesn’t exist orcan’t be queried. Raises aGurobiError if there is a problem withtheMQConstr object (e.g., it was removed from the model).
The result is returned as a NumPyndarray with the same shape as theMQConstr object.
attrname – The attribute being queried.
ndarray of current values for the requested attribute.
mqc=model.addConstr(x**2+y<=1)qcrhs=mc.getAttr("QCRHS")
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 (usingModel.update), optimize the model (usingModel.optimize), or write the model to disk (usingModel.write).
Raises anAttributeError if the specified attribute doesn’t exist orcan’t be set. Raises aGurobiError if there is a problem with theMQConstr object (e.g., it was removed from the model).
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 tohave the right shape.
mqc=model.addConstr(x*y-x-y<=0)mqc.setAttr("QCRHS",np.arange(x.size))mqc.setAttr(GRB.Attr.RHS,1.0)# broadcast scalar
Help and Feedback