GRBNLExpr#

GRBNLExpr#

Gurobi nonlinear expression object. A nonlinear expression stores an expression tree, and makes it possible to formulate nonlinear programming (NLP) and mixed integer nonlinear programming (MINLP) models in the .NET API using symbolic math.

You create GRBNLExpr objects by starting from GRBVar objects (or by using the GRBVar.nl property) and applying overloaded operators, the nonlinear functions hosted on this class (e.g., Sqrt, Sin), or a combination of both. The resulting object can be passed to GRBModel.AddNLConstr to add a nonlinear constraint to a model, or to GRBModel.SetNLObj to set a nonlinear objective.

In .NET languages that support operator overloading, you generally build nonlinear expressions using overloaded operators, matching the style of the linear and quadratic expression classes. For example, if x and y are GRBVar objects, then Sqrt(x), x / y, Log(x) * x, and x * x * x are all GRBNLExpr objects. Operations that have no corresponding C# operator (e.g., Sqrt, Log, Logistic) are provided as static factory methods on this class.

Implicit conversions from constants, variables, and linear or quadratic expressions make it possible to mix these objects with GRBNLExpr in operator expressions. For example, x / (1 + x) builds the \(x / (1 + x)\) expression, and Pow(x, 2) + 1 == y * y builds a constraint.

Overloaded comparison operators (<=, >=, and ==) produce an intermediate GRBTempNLConstr object that is normally passed directly to GRBModel.AddNLConstr to add a nonlinear constraint to a model. When one side of a comparison is a plain scalar, the scalar is stored as the right-hand side value of the constraint; when both sides are expressions, their difference is folded into the expression tree.

Nonlinear expressions are temporary objects that typically have short lifespans. Once you add a nonlinear constraint to a model, subsequent changes to the expression object you used to build the constraint will have no effect on that constraint.

The expression tree stored in a nonlinear expression can be exchanged with the C API form using ToArrayRepr and FromArrayRepr. The expression tree format is described in the nonlinear programming section of the manual.

Nonlinear expressions never store the value of the expression for the current solution; to evaluate a nonlinear expression, retrieve its expression tree and evaluate it on the solution point stored in the model.

GRBNLExpr FromConstant(double arg)#

Create a nonlinear expression from a constant value.

Parameters:

arg – Constant value.

Returns:

A new constant nonlinear expression.

Example:
GRBNLExpr e = GRBNLExpr.FromConstant(2.0);
GRBNLExpr FromVar(GRBVar arg)#

Create a nonlinear expression from a variable.

Parameters:

arg – Variable.

Returns:

A new nonlinear expression representing the variable.

Example:
GRBVar x = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x");
GRBNLExpr e = GRBNLExpr.FromVar(x);
GRBNLExpr FromLinExpr(GRBLinExpr arg)#

Create a nonlinear expression from a linear expression.

Parameters:

arg – Linear expression.

Returns:

A new nonlinear expression representing the linear expression.

Example:
GRBLinExpr lexpr = x + y;
GRBNLExpr e = GRBNLExpr.FromLinExpr(lexpr);
GRBNLExpr FromQuadExpr(GRBQuadExpr arg)#

Create a nonlinear expression from a quadratic expression.

Parameters:

arg – Quadratic expression.

Returns:

A new nonlinear expression representing the quadratic expression.

Example:
GRBQuadExpr qexpr = x * x + y * y;
GRBNLExpr e = GRBNLExpr.FromQuadExpr(qexpr);
GRBNLExpr FromPlus(params GRBNLExpr[] args)#

Create a nonlinear expression that is the sum of the given arguments.

Parameters:

args – Nonlinear expressions to add.

Returns:

A new nonlinear expression that is the sum of the arguments.

Example:
GRBNLExpr e = GRBNLExpr.FromPlus(Sqrt(x), x * y);
GRBNLExpr FromMultiply(params GRBNLExpr[] args)#

Create a nonlinear expression that is the product of the given arguments.

Parameters:

args – Nonlinear expressions to multiply.

Returns:

A new nonlinear expression that is the product of the arguments.

Example:
GRBNLExpr e = GRBNLExpr.FromMultiply(x, Log(y), z * z);
GRBNLExpr Sqrt(GRBNLExpr arg)#

Create a nonlinear expression that is the square root of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the square root of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Sqrt(x);
GRBNLExpr Sin(GRBNLExpr arg)#

Create a nonlinear expression that is the sine of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the sine of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Sin(x);
GRBNLExpr Cos(GRBNLExpr arg)#

Create a nonlinear expression that is the cosine of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the cosine of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Cos(x);
GRBNLExpr Tan(GRBNLExpr arg)#

Create a nonlinear expression that is the tangent of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the tangent of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Tan(x);
GRBNLExpr Exp(GRBNLExpr arg)#

Create a nonlinear expression that is the exponential of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the exponential of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Exp(x);
GRBNLExpr Log(GRBNLExpr arg)#

Create a nonlinear expression that is the natural logarithm of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the natural logarithm of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Log(x);
GRBNLExpr Log2(GRBNLExpr arg)#

Create a nonlinear expression that is the base-2 logarithm of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the base-2 logarithm of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Log2(x);
GRBNLExpr Log10(GRBNLExpr arg)#

Create a nonlinear expression that is the base-10 logarithm of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the base-10 logarithm of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Log10(x);
GRBNLExpr Logistic(GRBNLExpr arg)#

Create a nonlinear expression that is the logistic function of the argument. The logistic function is \(f(z) = 1 / (1 + e^{-z})\).

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the logistic function of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Logistic(x);
GRBNLExpr Tanh(GRBNLExpr arg)#

Create a nonlinear expression that is the hyperbolic tangent of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the hyperbolic tangent of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Tanh(x);
GRBNLExpr Square(GRBNLExpr arg)#

Create a nonlinear expression that is the square of the argument.

Parameters:

arg – Argument nonlinear expression.

Returns:

A new nonlinear expression that is the square of the argument.

Example:
GRBNLExpr e = GRBNLExpr.Square(x);
GRBNLExpr Signpow(GRBNLExpr @base, GRBNLExpr exponent)#

Create a nonlinear expression that is sign(base) times the absolute value of base raised to the power exponent.

Parameters:
  • base – Base of the signpow expression.

  • exponent – Exponent of the signpow expression.

Returns:

A new signpow nonlinear expression.

Example:
GRBNLExpr e = GRBNLExpr.Signpow(x, 2.0 / 3.0);
GRBNLExpr Pow(GRBNLExpr @base, GRBNLExpr exponent)#

Create a nonlinear expression that is the base raised to the given exponent.

Parameters:
  • base – Base nonlinear expression.

  • exponent – Exponent nonlinear expression.

Returns:

A new nonlinear expression for the power function.

Example:
GRBNLExpr e = GRBNLExpr.Pow(x, y);
GRBNLExpr Pow(GRBNLExpr @base, double exponent)#

Create a nonlinear expression that is the base raised to the given constant exponent. Raising to the power 2 produces a square node.

Parameters:
  • base – Base nonlinear expression.

  • exponent – Exponent value.

Returns:

A new nonlinear expression for the power function.

Example:
GRBNLExpr e = GRBNLExpr.Pow(x, 2.0);
GRBNLExpr Pow(double @base, GRBNLExpr exponent)#

Create a nonlinear expression that is the constant base raised to the given exponent.

Parameters:
  • base – Constant base value.

  • exponent – Exponent nonlinear expression.

Returns:

A new nonlinear expression for the power function.

Example:
GRBNLExpr e = GRBNLExpr.Pow(2.0, y);
GRBNLExpr FromArrayRepr(int[] opcode, object[] data, int[] parent)#

Build a nonlinear expression from the array form of an expression tree (as produced by ToArrayRepr or by the C API routine GRBgetnlconstr). For variable nodes the corresponding entry of data must contain the GRBVar object, for constant nodes a double. Entries for all other nodes are ignored.

Parameters:
  • opcode – Array containing the operation codes for the nodes.

  • data – Array containing the auxiliary data for each node.

  • parent – Array providing the parent index of the nodes.

Returns:

A new nonlinear expression described by the three arrays.

Example:
GRBNLExpr e = GRBNLExpr.FromArrayRepr(opcode, data, parent);
void ToArrayRepr(out int[] opcode, out object[] data, out int[] parent)#

Produce the three arrays (opcode, data, parent) used by the C API routines GRBaddnlconstr/GRBgetnlconstr to represent the expression tree described by this nonlinear expression. The traversal is depth-first, pre-order. For variable nodes the data entries are the GRBVar objects, for constant nodes they are the constant values (as double), and for all other nodes the default data value -1.0 is used.

Parameters:
  • opcode – Output array of operation codes for the nodes.

  • data – Output array of data values for the nodes.

  • parent – Output array of parent indices for the nodes (-1 for the root).

Example:
int[] opcode;
object[] data;
int[] parent;
expr.ToArrayRepr(out opcode, out data, out parent);
GRBNLExpr Copy()#

Copy a nonlinear expression.

Returns:

A nonlinear expression describing the same expression tree as this one.

Example:
GRBNLExpr e2 = expr.Copy();
string ToString()#

Return a string representation of the nonlinear expression in infix form.

Returns:

Infix string representation of the expression tree.

Example:
string s = expr.ToString();