gurobipy.MNLConstr#
- class MNLConstr#
Gurobi matrix nonlinear constraint object. An
MNLConstrobject is an array-like data structure that represents multiple nonlinear constraints (in contrast to anNLConstrobject, which represents a single nonlinear constraint). It behaves similarly to NumPy’sndarrays, e.g., it has a shape and can be indexed and sliced. Matrix nonlinear constraints are always associated with a particular model. You typically create these objects withModel.addConstrorModel.addNLConstr.Nonlinear 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 nonlinear constraint attributes. Consider the NLCRHS attribute. The values for a matrix nonlinear constraint
mnlccan be queried usingmnlc.NLCRHS. The Gurobi library ignores letter case in attribute names, so it can also be queried asmnlc.nlcrhs. Attribute values are returned as a NumPyndarraythat has the same shape asmnlc. An attribute can be set, using a standard assignment statement (e.g.,mnlc.nlcrhs = b), withbbeing either anndarraywith the appropriate shape, or a scalar which is then applied to all of the associated nonlinear 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 cannot be set (e.g., the NLCSlack attribute), so attempts to assign new values to them will raise an exception.You can also use
MNLConstr.getAttr/MNLConstr.setAttrto access attributes. The attribute name can be passed to these methods as a string, or you can use the constants defined in theGRB.Attrclass (e.g.,GRB.Attr.NLCRHS).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(nlconstrlist)#
Converts a list of nonlinear constraints into an
MNLConstrobject. The shape is inferred from the contents of the list - a list ofNLConstrobjects produces a 1-DMNLConstrobject, a list of lists ofNLConstrobjects produces a 2-DMNLConstr, etc.- Parameters:
nlconstrlist – A list of
NLConstrobjects to populate the returnedMNLConstr.- Returns:
MNLConstrobject corresponding to the input constraints.- Example:
nlconstrs = model.getNLConstrs() mnlc = MNLConstr.fromlist(nlconstrs) # 1-D MNLConstr
- getAttr(attrname)#
Returns the value of an attribute for a matrix nonlinear 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 theMNLConstrobject (e.g., it was removed from the model).The result is returned as a NumPy
ndarraywith the same shape as theMNLConstrobject.- Parameters:
attrname – The attribute being queried.
- Returns:
ndarrayof current values for the requested attribute.- Example:
mnlc = model.addConstr(y == gp.nlfunc.exp(x)) nlcrhs = mnlc.getAttr("NLCRHS")
- setAttr(attrname, newvalue)#
Sets the value of a matrix nonlinear 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 theMNLConstrobject (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 theMNLConstrobject. Alternatively, you can pass a scalar argument, which will automatically be promoted to have the right shape.
- Example:
mnlc = model.addConstr(y == gp.nlfunc.exp(x)) mnlc.setAttr("NLCRHS", np.arange(x.size)) mnlc.setAttr(GRB.Attr.NLCRHS, 1.0) # broadcast scalar
- tolist()#
Returns the nonlinear constraints associated with this matrix nonlinear constraint as a list of individual
NLConstrobjects.- Returns:
List of
NLConstrobjects.- Example:
mnlc = model.addConstr(y == gp.nlfunc.exp(x)) nlconstrlist = mnlc.tolist() # Do something with the NLConstr corresponding to mnlc[3] print(nlconstrlist[3])