gurobipy.MNLConstr#

class MNLConstr#

Gurobi matrix nonlinear constraint object. An MNLConstr object is an array-like data structure that represents multiple nonlinear constraints (in contrast to an NLConstr object, which represents a single nonlinear constraint). It behaves similarly to NumPy’s ndarrays, 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 with Model.addConstr or Model.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, or Model.write on 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 mnlc can be queried using mnlc.NLCRHS. The Gurobi library ignores letter case in attribute names, so it can also be queried as mnlc.nlcrhs. Attribute values are returned as a NumPy ndarray that has the same shape as mnlc. An attribute can be set, using a standard assignment statement (e.g., mnlc.nlcrhs = b), with b being either an ndarray with 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.setAttr to access attributes. The attribute name can be passed to these methods as a string, or you can use the constants defined in the GRB.Attr class (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 MNLConstr object. The shape is inferred from the contents of the list - a list of NLConstr objects produces a 1-D MNLConstr object, a list of lists of NLConstr objects produces a 2-D MNLConstr, etc.

Parameters:

nlconstrlist – A list of NLConstr objects to populate the returned MNLConstr.

Returns:

MNLConstr object 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 AttributeError if the requested attribute doesn’t exist or can’t be queried. Raises a GurobiError if there is a problem with the MNLConstr object (e.g., it was removed from the model).

The result is returned as a NumPy ndarray with the same shape as the MNLConstr object.

Parameters:

attrname – The attribute being queried.

Returns:

ndarray of 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 (using Model.optimize), or write the model to disk (using Model.write).

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 MNLConstr object (e.g., it was removed from the model).

Parameters:
  • attrname – The attribute being modified.

  • newvalue – ndarray of desired new values for the attribute. The shape must be the same as the MNLConstr object. 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 NLConstr objects.

Returns:

List of NLConstr objects.

Example:
mnlc = model.addConstr(y == gp.nlfunc.exp(x))
nlconstrlist = mnlc.tolist()
# Do something with the NLConstr corresponding to mnlc[3]
print(nlconstrlist[3])