Summary
In Gurobi 14.0, state the nonlinear relationship directly as a nonlinear constraint, with any sense and right-hand side, for example sin(x) + y^3 <= 2. You no longer need a resultant variable, and the same applies to nonlinear objectives.
In Gurobi 12 and 13, nonlinear relationships were stated as general nonlinear constraints of the form y = f(x), where y is a resultant variable. In Gurobi 11 and earlier, only univariate function constraints such as y = exp(x) were available.
Function constraints and general nonlinear constraints are deprecated in Gurobi 14.0. New code should use nonlinear constraints, and existing code should be migrated before those interfaces are removed in a future version.
How nonlinear modeling differs by version
Each generation widened what you can state directly, and 14.0 is the first where the constraint has its own sense and right-hand side.
| Gurobi version | Constraint type | Form you can state | How Gurobi solves it | Status in 14.0 |
| 14.0 | Nonlinear constraint (addNLConstr, or addConstr in Python) | f(x) <= b, f(x) >= b, or f(x) = b, with f multivariate; nonlinear objectives are also supported | Global spatial branch-and-bound, or local nonlinear barrier | Recommended |
| 12.0 and 13.0 | General nonlinear constraint (addGenConstrNL, or addConstr(y == f(x)) in Python) | y = f(x) only, with f multivariate and y a resultant variable | Global spatial branch-and-bound; 13.0 adds the local nonlinear barrier | Deprecated |
| 11.0 | Function constraint (addGenConstrExp, addGenConstrLog, and similar) | y = f(x) with one predefined univariate function per constraint | Piecewise-linear approximation, or exact treatment with FuncNonlinear=1 | Deprecated |
Quadratic and bilinear constraints are a separate, fully supported constraint type in every version and are not affected by these changes.
For more details see Gurobi 14.0 release notes, Gurobi 13.0 release notes, Gurobi 12.0 release notes, Gurobi 11.0 release notes.
Details on nonlinear constraints in Gurobi 14.0
A nonlinear constraint in 14.0 has three parts: a nonlinear expression f(x) on the left-hand side, a sense (<=, >=, or =), and a constant right-hand side. The expression can combine any number of variables with arithmetic operators and the supported functions: square root, powers, signed power, exponential, logarithms (base e, 2, and 10), sine, cosine, tangent, hyperbolic tangent, and the logistic function.
The sense, right-hand side, and name can be queried and changed after creation through the NLCSense, NLCRHS, and NLCName attributes, and NLCSlack reports the slack in the current solution. See Nonlinear Constraint Attributes for the full list.
The examples below all add the constraint sin(x) + y <= 1. Python and .NET let you write the expression with operators. C++ and Java (and C) take the expression as an expression tree encoded in three arrays: opcode, data, and parent.
Python
Model.addConstr accepts nonlinear expressions in any form and with any sense, and returns an NLConstr object. Model.setObjective accepts nonlinear expressions too.
import gurobipy as gp
from gurobipy import GRB, nlfunc
model = gp.Model()
x = model.addVar(lb=-1.0, ub=2.0, name="x")
y = model.addVar(lb=0.0, ub=2.0, name="y")
# Nonlinear inequality, no resultant variable needed
nlc = model.addConstr(nlfunc.sin(x) + y <= 1.0, name="nlc")
# Nonlinear objective
model.setObjective(nlfunc.exp(x) - y, GRB.MINIMIZE)
model.optimize()For explicit control, Model.addNLConstr(expr, sense, rhs, name) takes the three parts separately, and Model.addNLConstrAdv takes an expression tree.
C++
GRBVar x = model.addVar(-1.0, 2.0, 0.0, GRB_CONTINUOUS, "x");
GRBVar y = model.addVar( 0.0, 2.0, 0.0, GRB_CONTINUOUS, "y");
// Expression tree for sin(x) + y
// node 0: PLUS (root)
// node 1: SIN (child of 0)
// node 2: VARIABLE x (child of 1)
// node 3: VARIABLE y (child of 0)
int opcode[4] = {GRB_OPCODE_PLUS, GRB_OPCODE_SIN,
GRB_OPCODE_VARIABLE, GRB_OPCODE_VARIABLE};
double data[4] = {-1.0, -1.0, (double) x.index(), (double) y.index()};
int parent[4] = {-1, 0, 1, 0};
GRBNLConstr nlc = model.addNLConstr(4, opcode, data, parent,
GRB_LESS_EQUAL, 1.0, "nlc");Java
GRBVar x = model.addVar(-1.0, 2.0, 0.0, GRB.CONTINUOUS, "x");
GRBVar y = model.addVar( 0.0, 2.0, 0.0, GRB.CONTINUOUS, "y");
// Expression tree for sin(x) + y
int[] opcode = new int[] {GRB.OPCODE_PLUS, GRB.OPCODE_SIN,
GRB.OPCODE_VARIABLE, GRB.OPCODE_VARIABLE};
double[] data = new double[] {-1.0, -1.0, x.index(), y.index()};
int[] parent = new int[] {-1, 0, 1, 0};
GRBNLConstr nlc = model.addNLConstr(opcode, data, parent,
GRB.LESS_EQUAL, 1.0, "nlc");.NET (C#)
New in 14.0, C# and Visual Basic support expression-based nonlinear modeling through the GRBNLExpr class. Functions without a C# operator, such as Sin, Sqrt, Log, and Pow, are static methods on GRBNLExpr.
GRBVar x = model.AddVar(-1.0, 2.0, 0.0, GRB.CONTINUOUS, "x");
GRBVar y = model.AddVar( 0.0, 2.0, 0.0, GRB.CONTINUOUS, "y");
GRBNLConstr nlc = model.AddNLConstr(GRBNLExpr.Sin(x) + y <= 1.0, "nlc");An expression-tree overload, AddNLConstr(opcode, data, parent, sense, rhs, name), is also available.
In the object-oriented APIs, nonlinear constraints are removed with the model's remove method and their left-hand side is retrieved with getNLConstrLHS. Complete programs are in the nlconstr examples for each language.
Earlier versions in brief
Before 14.0, every nonlinear relationship had to be an equality that defines a resultant variable, so the same constraint sin(x) + y <= 1 needs an extra variable and, in older versions, an extra constraint.
Gurobi 12.0 and 13.0: general nonlinear constraints
Gurobi 12.0 was the first version to introduce spatial branch-and-bound as the default method for solving nonlinear optimization problems beyond quadratic problems.
In terms of the API, versions 12.0 and 13.0 support multivariate expressions, but only the form y = f(x) is accepted. Inequalities are modeled by bounding the resultant variable.
# z = sin(x) + y, with z <= 1 enforced through its upper bound
z = model.addVar(lb=-GRB.INFINITY, ub=1.0, name="z")
gc = model.addConstr(z == nlfunc.sin(x) + y) # returns a GenConstrIn C, C++, Java, and .NET the equivalent call is addGenConstrNL(z, opcode, data, parent, name). These versions have no nonlinear objective; you should minimize a resultant variable instead.
Gurobi 11.0 and earlier: function constraints
Note that versions 11.0 or earlier are no longer supported. We only add this example here for informational purposes, to better understand what has changed from historical versions. With versions 11.0 or earlier, each constraint linked exactly two variables through one predefined function, so composite expressions had to be built up one function at a time. More importantly, the underlying algorithms were by default, static piecewise-linear approximations.
# s = sin(x), then a linear constraint on s
s = model.addVar(lb=-1.0, ub=1.0, name="s")
model.addGenConstrSin(x, s)
model.addConstr(s + y <= 1.0)
Migrating existing code to 14.0
Deprecated interfaces keep working in 14.0, so migration can be gradual, with two exceptions described under "What to check" below.
| If your code uses | Replace it with |
| Function constraints: addGenConstrExp, addGenConstrLog, addGenConstrSin, addGenConstrPow, and similar. General nonlinear constraints: addGenConstrNL(y, ...) |
A nonlinear constraint on the same expression. Python, e.g.: addConstr(y == nlfunc.exp(x)). Other APIs: addNLConstr. |
| A resultant variable whose bound enforces an inequality | The inequality itself: f(x) <= b |
| A resultant variable used as the objective | A nonlinear objective: setObjective in Python, setNLObj in the other APIs |
| Parameters and attributes FuncPieces, FuncPieceError, FuncPieceLength, FuncPieceRatio, FuncNonlinear | Nothing. They apply only to function constraints, so remove them once those constraints are gone |
| GenConstr objects and general constraint attributes for nonlinear constraints | NLConstr objects for Python (GRBNLConstr for other APIs), the NLC attributes, getNLConstrs, and getNLConstrLHS |
A typical Python change removes the resultant variable entirely:
# Gurobi 12 and 13
z = model.addVar(lb=-GRB.INFINITY, ub=1.0, name="z")
model.addConstr(z == nlfunc.sin(x) + y)becomes
# Gurobi 14
model.addConstr(nlfunc.sin(x) + y <= 1.0)in Gurobi 14.0.
What to check
- Python return types change without a code change. model.addConstr(y == nlfunc.sin(x)) returned a GenConstr (or MGenConstr) in 12 and 13. In 14 the same line creates the nonlinear constraint y - sin(x) == 0 and returns an NLConstr (or MNLConstr). Update any code that reads general constraint attributes from the result or looks for these constraints in Model.getGenConstrs().
- Local optimization no longer accepts general nonlinear constraints. If you solve to local optimality through the OptimalityTarget parameter, constraints added with addGenConstrNL (13.0 and older versions syntax) must be converted to nonlinear constraints before the model will solve that way in 14.0.
- Find deprecated calls before upgrading. Run your Python code on Gurobi 13 with warnings enabled (python -W default or python -X dev) and fix each deprecation warning.
- Supporting several versions at once. In Python, addConstr(y == f(x)) runs on 12, 13, and 14. However, inequalities, constant right-hand sides, and nonlinear objectives require 14, so guard them with a version check if the same code must run on older releases.
Best practices
These apply to 14.0 and, unless noted, to general nonlinear constraints in 12 and 13 as well.
- Give every variable in a nonlinear expression finite, tight bounds. Global optimization branches on and relaxes over variable domains, so loose or infinite bounds weaken the relaxation and slow the search. Remember that the default lower bound is 0 and the default upper bound is infinite.
- Keep arguments inside each function's domain with your own bounds. Gurobi implicitly enforces a lower bound of 1e-10 on logarithm arguments and 0 on square-root arguments, and keeps divisors out of the interval [-1e-10, 1e-10]. Values that close to the edge are numerically fragile, so set a realistic bound, such as a minimum of 1e-3 on a denominator.
- Respect the restrictions on powers. In x^y, either the base or the exponent must be a constant, and a constant base must be strictly positive. If both are variables and x > 0, model the term as exp(y * log(x)).
- State inequalities as inequalities (starting with version 14.0). If the model only needs one direction, write f(x) <= b instead of an equality plus a bound. The feasible region is often convex in that form, for example exp(x) - y <= 0, where the equality y = exp(x) is not. After a solve, the IsConvex model attribute reports whether Gurobi detected the presolved model as convex.
- Write the full expression instead of chaining auxiliary variables. Gurobi decomposes a multivariate expression itself and can use the structure it sees. Hand-built chains of single-function constraints hide that structure.
- Leave quadratic terms quadratic. Products of two variables and squares belong in linear or quadratic constraints, which Gurobi handles with dedicated algorithms. For products of three or more variables, see How do I model multilinear terms in Gurobi?
- Choose global or local optimization deliberately. Global optimization uses spatial branch-and-bound, supports integer variables, and returns a proven bound. Local optimization (13.0 and later) uses the nonlinear barrier solver, is limited to continuous models with differentiable functions, and returns a locally optimal point without a global guarantee. Select between them with the OptimalityTarget parameter.
- Watch the scaling. Functions such as exp turn modest inputs into very large values. Choose units so that variables and expression values stay within a few orders of magnitude of 1, and follow the guidelines for numerical issues.
- Use piecewise-linear constraints only when you want an approximation. Explicit piecewise-linear constraints remain fully supported and are a good fit for tabulated data, but they are no longer the way to model a closed-form function.
Further information
- Nonlinear Functions: expression trees, operation codes, and the domain rules for each function
- Additions, Changes, and Removals in Gurobi 14.0: the full list of new nonlinear methods per API and the deprecated functionality
- Nonlinear Constraint Attributes
- NLExpr and NLConstr in the Python API reference
- GRBNLConstr for C++ and GRBNLConstr for .NET
- Nlconstr examples in C, C++, C#, Java, MATLAB, Python, and Visual Basic
- How do I divide by a variable in Gurobi?
- Why does Gurobi report that my convex model is non-convex?
- How do I model logical and general expressions?
- How do I model multilinear terms in Gurobi?