Overloaded Operators#

The Gurobi .NET interface overloads several arithmetic and comparison operators. Overloaded arithmetic operators (+, -, *, /) are used to create linear, quadratic, and nonlinear expressions. Overloaded comparison operators (<=, >=, and ==) are used to build linear, quadratic, and nonlinear constraints.

Note that the results of overloaded comparison operators are generally never stored in user variables. They are immediately passed to GRBModel.AddConstr, GRBModel.AddQConstr, or GRBModel.AddNLConstr.

Nonlinear operators produce GRBNLExpr objects. Because nonlinear expressions never modify their operands, each nonlinear operator creates a new nonlinear expression from its arguments. Nonlinear comparison operators return an intermediate GRBTempNLConstr object that is normally passed directly to GRBModel.AddNLConstr.

Comparison operators involving a nonlinear expression and a scalar constant store the constant as the right-hand side of the resulting constraint (and, where needed, swap the direction of the inequality) rather than folding it into the expression tree. For example, sin(x) <= 1 is added with the sin(x) expression and the right-hand side value 1.

GRBTempConstr operator <=(GRBQuadExpr lhsExpr, GRBQuadExpr rhsExpr)#

Create an inequality constraint.

Parameters:
  • lhsExpr – Left-hand side of inequality constraint.

  • rhsExpr – Right-hand side of inequality constraint.

Returns:

A constraint of type GRBTempConstr. The result is typically immediately passed to method GRBModel.AddConstr.

GRBTempConstr operator >=(GRBQuadExpr lhsExpr, GRBQuadExpr rhsExpr)#

Create an inequality constraint.

Parameters:
  • lhsExpr – Left-hand side of inequality constraint.

  • rhsExpr – Right-hand side of inequality constraint.

Returns:

A constraint of type GRBTempConstr. The result is typically immediately passed to method GRBModel.AddConstr.

GRBTempConstr operator ==(GRBLinExpr lhsExpr, GRBLinExpr rhsExpr)#

Create an equality constraint.

Parameters:
  • lhsExpr – Left-hand side of equality constraint.

  • rhsExpr – Right-hand side of equality constraint.

Returns:

A constraint of type GRBTempConstr. The result is typically immediately passed to method GRBModel.AddConstr.

GRBTempNLConstr operator <=(GRBNLExpr lhsExpr, GRBNLExpr rhsExpr)#

Create an inequality constraint.

Parameters:
  • lhsExpr – Left-hand side of inequality constraint.

  • rhsExpr – Right-hand side of inequality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator <=(GRBNLExpr lhsExpr, double rhsValue)#

Create an inequality constraint.

Parameters:
  • lhsExpr – Left-hand side of inequality constraint.

  • rhsValue – Right-hand side value of the inequality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator <=(double lhsValue, GRBNLExpr rhsExpr)#

Create an inequality constraint.

Parameters:
  • lhsValue – Left-hand side value of the inequality constraint.

  • rhsExpr – Right-hand side of inequality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator >=(GRBNLExpr lhsExpr, GRBNLExpr rhsExpr)#

Create an inequality constraint.

Parameters:
  • lhsExpr – Left-hand side of inequality constraint.

  • rhsExpr – Right-hand side of inequality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator >=(GRBNLExpr lhsExpr, double rhsValue)#

Create an inequality constraint.

Parameters:
  • lhsExpr – Left-hand side of inequality constraint.

  • rhsValue – Right-hand side value of the inequality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator >=(double lhsValue, GRBNLExpr rhsExpr)#

Create an inequality constraint.

Parameters:
  • lhsValue – Left-hand side value of the inequality constraint.

  • rhsExpr – Right-hand side of inequality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator ==(GRBNLExpr lhsExpr, GRBNLExpr rhsExpr)#

Create an equality constraint.

Parameters:
  • lhsExpr – Left-hand side of equality constraint.

  • rhsExpr – Right-hand side of equality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator ==(GRBNLExpr lhsExpr, double rhsValue)#

Create an equality constraint.

Parameters:
  • lhsExpr – Left-hand side of equality constraint.

  • rhsValue – Right-hand side value of the equality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBTempNLConstr operator ==(double lhsValue, GRBNLExpr rhsExpr)#

Create an equality constraint.

Parameters:
  • lhsValue – Left-hand side value of the equality constraint.

  • rhsExpr – Right-hand side of equality constraint.

Returns:

A nonlinear constraint of type GRBTempNLConstr. The result is typically immediately passed to method GRBModel.AddNLConstr.

GRBLinExpr operator +(GRBLinExpr expr1, GRBLinExpr expr2)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • expr1 – First linear expression argument.

  • expr2 – Second linear expression argument.

Returns:

A linear expression that is equal to the sum of the two argument expressions.

GRBLinExpr operator +(GRBLinExpr expr, GRBVar var)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • expr – Linear expression argument.

  • var – Variable argument.

Returns:

A linear expression that is equal to the sum of the argument linear expression and the argument variable.

GRBLinExpr operator +(GRBVar var, GRBLinExpr expr)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • var – Variable argument.

  • expr – Linear expression argument.

Returns:

A linear expression that is equal to the sum of the argument linear expression and the argument variable.

GRBLinExpr operator +(GRBVar var1, GRBVar var2)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • var1 – First variable argument.

  • var2 – Second variable argument.

Returns:

A linear expression that is equal to the sum of the two argument variables.

GRBLinExpr operator +(double a, GRBVar var)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • a – Constant.

  • var – Variable.

Returns:

A linear expression that is equal to the sum of the constant and the variable argument.

GRBLinExpr operator +(GRBVar var, double a)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • var – Variable.

  • a – Constant.

Returns:

A linear expression that is equal to the sum of the constant and the variable argument.

GRBQuadExpr operator +(GRBQuadExpr expr1, GRBQuadExpr expr2)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • expr1 – First quadratic expression argument.

  • expr2 – Second quadratic expression argument.

Returns:

A quadratic expression that is equal to the sum of the two argument quadratic expressions.

GRBQuadExpr operator +(GRBQuadExpr expr, GRBVar var)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • expr – Quadratic expression argument.

  • var – Variable argument.

Returns:

A quadratic expression that is equal to the sum of the argument quadratic expression and the argument variable.

GRBQuadExpr operator +(GRBVar var, GRBQuadExpr expr)#

Create a new expression by adding a pair of Gurobi objects.

Parameters:
  • var – Variable argument.

  • expr – Quadratic expression argument.

Returns:

A quadratic expression that is equal to the sum of the argument quadratic expression and the argument variable.

GRBNLExpr operator +(GRBNLExpr expr1, GRBNLExpr expr2)#

Create a new nonlinear expression by adding a pair of Gurobi objects.

Parameters:
  • expr1 – First nonlinear expression argument.

  • expr2 – Second nonlinear expression argument.

Returns:

A nonlinear expression that is equal to the sum of the two argument expressions.

GRBLinExpr operator -(GRBLinExpr expr1, GRBLinExpr expr2)#

Create a new expression by subtracting one Gurobi object from another.

Parameters:
  • expr1 – First linear expression argument.

  • expr2 – Second linear expression argument.

Returns:

A linear expression that is equal to the first expression minus the second.

GRBQuadExpr operator -(GRBQuadExpr expr1, GRBQuadExpr expr2)#

Create a new expression by subtracting one Gurobi object from another.

Parameters:
  • expr1 – First quadratic expression argument.

  • expr2 – Second quadratic expression argument.

Returns:

A quadratic expression that is equal to the first expression minus the second.

GRBNLExpr operator -(GRBNLExpr expr1, GRBNLExpr expr2)#

Create a new nonlinear expression by subtracting one Gurobi object from another.

Parameters:
  • expr1 – First nonlinear expression argument.

  • expr2 – Second nonlinear expression argument.

Returns:

A nonlinear expression that is equal to the first expression minus the second.

GRBNLExpr operator -(GRBNLExpr expr)#

Create a new nonlinear expression by negating a nonlinear expression.

Parameters:

expr – Nonlinear expression argument.

Returns:

A nonlinear expression that is equal to the negation of the argument.

GRBLinExpr operator *(double multiplier, GRBLinExpr expr)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • multiplier – Multiplier for expression argument.

  • expr – Expression argument.

Returns:

A linear expression that is equal to the input expression times the input multiplier.

GRBLinExpr operator *(GRBLinExpr expr, double multiplier)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • expr – Linear expression argument.

  • multiplier – Multiplier for expression argument.

Returns:

A linear expression that is equal to the input expression times the input multiplier.

GRBLinExpr operator *(double multiplier, GRBVar var)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • multiplier – Multiplier for variable argument.

  • var – Variable argument.

Returns:

A linear expression that is equal to the input variable times the input multiplier.

GRBLinExpr operator *(GRBVar var, double multiplier)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • var – Variable argument.

  • multiplier – Multiplier for variable argument.

Returns:

A linear expression that is equal to the input variable times the input multiplier.

GRBQuadExpr operator *(double multiplier, GRBQuadExpr expr)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • multiplier – Multiplier for expression argument.

  • expr – Quadratic expression argument.

Returns:

A quadratic expression that is equal to the input expression times the input multiplier.

GRBQuadExpr operator *(GRBQuadExpr expr, double multiplier)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • expr – Quadratic expression argument.

  • multiplier – Multiplier for expression argument.

Returns:

A quadratic expression that is equal to the input expression times the input multiplier.

GRBQuadExpr operator *(GRBVar var1, GRBVar var2)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • var1 – First variable argument.

  • var2 – Second variable argument.

Returns:

A quadratic expression that is equal to the product of the two input variables.

GRBQuadExpr operator *(GRBVar var, GRBLinExpr expr)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • var – Input variable.

  • expr – Input linear expression.

Returns:

A quadratic expression that is equal to the input linear expression times the input variable.

GRBQuadExpr operator *(GRBLinExpr expr, GRBVar var)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • expr – Input linear expression.

  • var – Input variable.

Returns:

A quadratic expression that is equal to the input linear expression times the input variable.

GRBQuadExpr operator *(GRBLinExpr expr1, GRBLinExpr expr2)#

Create a new expression by multiplying a pair of Gurobi objects.

Parameters:
  • expr1 – First linear expression argument.

  • expr2 – Second linear expression argument.

Returns:

A quadratic expression that is equal to the product of the two input linear expressions.

GRBNLExpr operator *(GRBNLExpr expr1, GRBNLExpr expr2)#

Create a new nonlinear expression by multiplying a pair of Gurobi objects.

Parameters:
  • expr1 – First nonlinear expression argument.

  • expr2 – Second nonlinear expression argument.

Returns:

A nonlinear expression that is equal to the product of the two argument expressions.

GRBNLExpr operator *(GRBQuadExpr expr, GRBNLExpr nlExpr)#

Create a new nonlinear expression by multiplying a quadratic expression with a nonlinear expression. This makes products involving more than two variables (e.g., x1 * x2 * x3, which is parsed as (x1 * x2) * x3 with x1 * x2 being a quadratic expression) resolve to a nonlinear expression.

Parameters:
  • expr – Quadratic expression argument.

  • nlExpr – Nonlinear expression argument.

Returns:

A nonlinear expression that is equal to the product of the quadratic expression and the nonlinear expression.

GRBLinExpr operator /(GRBVar var, double divisor)#

Create a new expression by dividing a Gurobi variable by a constant.

Parameters:
  • var – Variable argument.

  • divisor – Divisor for variable argument.

Returns:

A linear expression that is equal to the input variable divided by the input divisor.

GRBNLExpr operator /(GRBVar var1, GRBVar var2)#

Create a new nonlinear expression by dividing a variable by another variable.

Parameters:
  • var1 – Numerator variable.

  • var2 – Denominator variable.

Returns:

A nonlinear expression that is equal to the quotient of the two argument variables.

GRBNLExpr operator /(GRBVar var, GRBLinExpr expr)#

Create a new nonlinear expression by dividing a variable by a linear expression.

Parameters:
  • var – Numerator variable.

  • expr – Denominator linear expression.

Returns:

A nonlinear expression that is equal to the quotient of the variable and the linear expression.

GRBNLExpr operator /(GRBVar var, GRBQuadExpr expr)#

Create a new nonlinear expression by dividing a variable by a quadratic expression.

Parameters:
  • var – Numerator variable.

  • expr – Denominator quadratic expression.

Returns:

A nonlinear expression that is equal to the quotient of the variable and the quadratic expression.

GRBNLExpr operator /(double a, GRBVar var)#

Create a new nonlinear expression by dividing a constant value by a variable.

Parameters:
  • a – Numerator constant value.

  • var – Denominator variable.

Returns:

A nonlinear expression that is equal to the quotient of the constant and the variable.

GRBNLExpr operator /(GRBLinExpr expr, GRBVar var)#

Create a new nonlinear expression by dividing a linear expression by a variable.

Parameters:
  • expr – Numerator linear expression.

  • var – Denominator variable.

Returns:

A nonlinear expression that is equal to the quotient of the linear expression and the variable.

GRBNLExpr operator /(GRBLinExpr expr1, GRBLinExpr expr2)#

Create a new nonlinear expression by dividing a linear expression by another linear expression.

Parameters:
  • expr1 – Numerator linear expression.

  • expr2 – Denominator linear expression.

Returns:

A nonlinear expression that is equal to the quotient of the two linear expressions.

GRBNLExpr operator /(GRBLinExpr expr, GRBQuadExpr qe)#

Create a new nonlinear expression by dividing a linear expression by a quadratic expression.

Parameters:
  • expr – Numerator linear expression.

  • qe – Denominator quadratic expression.

Returns:

A nonlinear expression that is equal to the quotient of the linear expression and the quadratic expression.

GRBNLExpr operator /(GRBQuadExpr expr, GRBVar var)#

Create a new nonlinear expression by dividing a quadratic expression by a variable.

Parameters:
  • expr – Numerator quadratic expression.

  • var – Denominator variable.

Returns:

A nonlinear expression that is equal to the quotient of the quadratic expression and the variable.

GRBNLExpr operator /(GRBQuadExpr expr, GRBLinExpr le)#

Create a new nonlinear expression by dividing a quadratic expression by a linear expression.

Parameters:
  • expr – Numerator quadratic expression.

  • le – Denominator linear expression.

Returns:

A nonlinear expression that is equal to the quotient of the quadratic expression and the linear expression.

GRBNLExpr operator /(GRBQuadExpr expr1, GRBQuadExpr expr2)#

Create a new nonlinear expression by dividing a quadratic expression by another quadratic expression.

Parameters:
  • expr1 – Numerator quadratic expression.

  • expr2 – Denominator quadratic expression.

Returns:

A nonlinear expression that is equal to the quotient of the two quadratic expressions.

GRBNLExpr operator /(GRBNLExpr expr1, GRBNLExpr expr2)#

Create a new nonlinear expression by dividing a nonlinear expression by another nonlinear expression.

Parameters:
  • expr1 – Numerator nonlinear expression.

  • expr2 – Denominator nonlinear expression.

Returns:

A nonlinear expression that is equal to the quotient of the two nonlinear expressions.

implicit cast#

GRBLinExpr GRBLinExpr(double value)#

Create an expression from an implicit cast (e.g., expr = 0.0).

Parameters:

value – Desired value for linear expression.

Returns:

A linear expression that is equal to specified constant.

GRBQuadExpr GRBQuadExpr(double value)#

Create an expression from an implicit cast (e.g., expr = 0.0).

Parameters:

value – Desired value for quadratic expression.

Returns:

A quadratic expression that is equal to specified constant.

GRBLinExpr GRBLinExpr(GRBVar var)#

Create an expression from an implicit cast (e.g., expr = x).

Parameters:

var – Variable for the linear expression.

Returns:

A linear expression that is equal to specified variable.

GRBQuadExpr GRBQuadExpr(GRBVar var)#

Create an expression from an implicit cast (e.g., expr = x).

Parameters:

var – Desired value for quadratic expression.

Returns:

A quadratic expression that is equal to specified variable.

GRBQuadExpr GRBQuadExpr(GRBLinExpr expr)#

Create an expression from an implicit cast (e.g., qexpr = lexpr).

Parameters:

expr – Desired value for quadratic expression.

Returns:

A quadratic expression that is equal to specified linear expression.

GRBNLExpr GRBNLExpr(double value)#

Create a nonlinear expression from an implicit cast (e.g., nlexpr = 1.0).

Parameters:

value – Desired value for nonlinear expression.

Returns:

A nonlinear expression that is equal to the specified constant.

GRBNLExpr GRBNLExpr(GRBVar var)#

Create a nonlinear expression from an implicit cast (e.g., nlexpr = x).

Parameters:

var – Variable for the nonlinear expression.

Returns:

A nonlinear expression that is equal to the specified variable.

GRBNLExpr GRBNLExpr(GRBLinExpr expr)#

Create a nonlinear expression from an implicit cast (e.g., nlexpr = lexpr).

Parameters:

expr – Linear expression for the nonlinear expression.

Returns:

A nonlinear expression that is equal to the specified linear expression.

GRBNLExpr GRBNLExpr(GRBQuadExpr expr)#

Create a nonlinear expression from an implicit cast (e.g., nlexpr = qexpr).

Parameters:

expr – Quadratic expression for the nonlinear expression.

Returns:

A nonlinear expression that is equal to the specified quadratic expression.