Concepts
Features
Reference
Gurobi nonlinear expression object. AnNLExpr stores anexpression tree, and makes it possible to formulate nonlinearprogramming (NLP) and mixed integer nonlinear programming (MINLP)models in Python using symbolic math.
You will generally build nonlinear expressions by starting fromVar objects and applying Python operators,nonlinear functions, or a combination of both.In general, anNLExpr will only be created for expressions thatcannot be captured by aLinExpr orQuadExpr.Specifically, formulating an expression which is not a polynomial ofdegree at most 2 will result in anNLExpr.
The following example lists the object types of various expressionsconstructed fromVar objects:
fromgurobipyimportnlfunc...x=model.addVar(name="x")# Vary=model.addVar(name="y")# Varz=model.addVar(name="z")# Varexpr1=2.0*x# LinExprexpr2=2.0*x*y# QuadExprexpr3=2.0*x*y*z# NLExprexpr4=x/y# NLExprexpr5=nlfunc.sin(x)# NLExpr
Nonlinear expressions are used to build nonlinear generalconstraints. They are typically temporary objects which are passedimmediately to eitherModel.addConstr orModel.addGenConstrNL to add a constraint to the model.Such constraints are always equality constraints with aresultantvariable on the left side of the expression. In Python code theseconstraints can be added using either method:
model.addConstr(z==nlfunc.sqrt(x+y))model.addGenConstrNL(z,x/y)
Nonlinear Inequality Constraints
It is not possible to use<= or>= operators to specifynonlinear inequality constraints usingNLExpr objects. However,you can formulate an equivalent constraint by creating a boundedresultant variable. For example, the following code constrains that\(log(x) \le 1\):
x=model.addVar()res=model.addVar(lb=-GRB.INFINITY,ub=1.0)model.addGenConstrNL(res,nlfunc.log(x))
More control over expression creation
If you want more control over the expression trees generated, you can‘opt-in’ to the nonlinear world using the.nl property ofVar andMVarobjects. This is considered advanced usage. In the vast majority ofcases, you do not need to consider how a nonlinear expression isrepresented internally.
The.nl property returns anNLExpr object representing itsobject. Any arithmetic operations involving that object will alsoresult in anNLExpr:
x=model.addVar()# Varexpr1=x+1.0# LinExprexpr2=x.nl# NLExprexpr3=x.nl+1.0# NLExpr
The implications are subtle, but can have an impact on how aconstraint is handled by the solver. For example, the following twoconstraints represent the same expression\((x + y)^2\) but havea different internal representation in the solver:
c1=model.addGenConstrNL(z,(x-y)**2)c2=model.addGenConstrNL(z,(x.nl-y.nl)**2)
Specifically, the first case captures a quadratic expression inexpanded form\(x^2 - 2xy + y^2\). The second case captures anonlinear expression with\(x - y\) as a node in the expressiontree which is explicitly squared.
Domain restrictions of operators
Some subtleties arise from certain arithmetic operators not being applicableto the whole domain that an operand may have (e.g., division by zero).Refer to the additional information for thedivide operator and thepower operator for all the details.
Returns a copy of this nonlinear expression
A newNLExpr object
Help and Feedback