Overloaded Operators#

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

Note that the results of overloaded comparison operators are generally never stored in user variables. They are immediately passed to GRBModel::addConstr or GRBModel::addQConstr.

GRBTempConstr operator==(GRBQuadExpr lhsExpr, GRBQuadExpr 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 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 GRBModel::addConstr or GRBModel::addQConstr.

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 GRBModel::addConstr or GRBModel::addQConstr.

GRBLinExpr operator+(const GRBLinExpr &expr1, const GRBLinExpr &expr2)#

Overloaded operator on expression objects.

Add a pair of expressions.

Parameters:
  • expr1 – First expression to be added.

  • expr2 – Second expression to be added.

Returns:

Sum expression.

GRBLinExpr operator+(const GRBLinExpr &expr)#

Overloaded operator on expression objects.

Allow plus sign to be used before an expression.

Parameters:

expr – Expression.

Returns:

Result expression.

GRBLinExpr operator+(GRBVar x, GRBVar y)#

Overloaded operator on expression objects.

Add a pair of variables.

Parameters:
  • x – First variable to be added.

  • y – Second variable to be added.

Returns:

Sum expression.

GRBQuadExpr operator+(const GRBQuadExpr &expr1, const GRBQuadExpr &expr2)#

Overloaded operator on expression objects.

Add a pair of expressions.

Parameters:
  • expr1 – First expression to be added.

  • expr2 – Second expression to be added.

Returns:

Sum expression.

GRBQuadExpr operator+(const GRBQuadExpr &expr)#

Overloaded operator on expression objects.

Allow plus sign to be used before an expression.

Parameters:

expr – Expression.

Returns:

Result expression.

GRBLinExpr operator-(const GRBLinExpr &expr1, const GRBLinExpr &expr2)#

Overloaded operator on expression objects.

Subtract one expression from another.

Parameters:
  • expr1 – Start expression.

  • expr2 – Expression to be subtracted.

Returns:

Difference expression.

GRBLinExpr operator-(const GRBLinExpr &expr)#

Overloaded operator on expression objects.

Negate an expression.

Parameters:

expr – Expression.

Returns:

Negation of expression.

GRBQuadExpr operator-(const GRBQuadExpr &expr1, const GRBQuadExpr &expr2)#

Overloaded operator on expression objects.

Subtract one expression from another.

Parameters:
  • expr1 – Start expression.

  • expr2 – Expression to be subtracted.

Returns:

Difference expression.

GRBQuadExpr operator-(const GRBQuadExpr &expr)#

Overloaded operator on expression objects.

Negate an expression.

Parameters:

expr – Expression.

Returns:

Negation of expression.

GRBLinExpr operator*(GRBVar x, double a)#

Overloaded operator on expression objects.

Multiply a variable and a constant.

Parameters:
  • x – Variable.

  • a – Constant multiplier.

Returns:

Expression that represents the result of multiplying the variable by a constant.

GRBLinExpr operator*(double a, GRBVar x)#

Overloaded operator on expression objects.

Multiply a variable and a constant.

Parameters:
  • a – Constant multiplier.

  • x – Variable.

Returns:

Expression that represents the result of multiplying the variable by a constant.

GRBLinExpr operator*(const GRBLinExpr &expr, double a)#

Overloaded operator on expression objects.

Multiply an expression and a constant.

Parameters:
  • expr – Expression.

  • a – Constant multiplier.

Returns:

Expression that represents the result of multiplying the expression by a constant.

GRBLinExpr operator*(double a, const GRBLinExpr &expr)#

Overloaded operator on expression objects.

Multiply an expression and a constant.

Parameters:
  • a – Constant multiplier.

  • expr – Expression.

Returns:

Expression that represents the result of multiplying the expression by a constant.

GRBQuadExpr operator*(const GRBQuadExpr &expr, double a)#

Overloaded operator on expression objects.

Multiply an expression and a constant.

Parameters:
  • expr – Expression.

  • a – Constant multiplier.

Returns:

Expression that represents the result of multiplying the expression by a constant.

GRBQuadExpr operator*(double a, const GRBQuadExpr &expr)#

Overloaded operator on expression objects.

Multiply an expression and a constant.

Parameters:
  • a – Constant multiplier.

  • expr – Expression.

Returns:

Expression that represents the result of multiplying the expression by a constant.

GRBQuadExpr operator*(GRBVar x, GRBVar y)#

Overloaded operator on expression objects.

Multiply a pair of variables.

Parameters:
  • x – First variable.

  • y – Second variable.

Returns:

Expression that represents the result of multiplying the argument variables.

GRBQuadExpr operator*(GRBVar var, const GRBLinExpr &expr)#

Overloaded operator on expression objects.

Multiply an expression and a variable.

Parameters:
  • var – Variable.

  • expr – Expression.

Returns:

Expression that represents the result of multiplying the expression by a variable.

GRBQuadExpr operator*(const GRBLinExpr &expr, GRBVar var)#

Overloaded operator on expression objects.

Multiply an expression and a variable.

Parameters:
  • var – Variable.

  • expr – Expression.

Returns:

Expression that represents the result of multiplying the expression by a variable.

GRBQuadExpr operator*(const GRBLinExpr &expr1, const GRBLinExpr &expr2)#

Overloaded operator on expression objects.

Multiply a pair of expressions.

Parameters:
  • expr1 – First expression.

  • expr2 – Second expression.

Returns:

Expression that represents the result of multiplying the argument expressions.

GRBLinExpr operator/(GRBVar x, double a)#

Overloaded operator to divide a variable or expression by a constant.

Parameters:
  • x – Variable.

  • a – Constant divisor.

Returns:

Expression that represents the result of dividing the variable by a constant.

GRBLinExpr operator/(const GRBLinExpr &expr, double a)#

Overloaded operator to divide a variable or expression by a constant.

Parameters:
  • expr – Expression.

  • a – Constant divisor.

Returns:

Expression that represents the result of dividing the expression by a constant.

GRBLinExpr operator/(const GRBQuadExpr &expr, double a)#

Overloaded operator to divide a variable or expression by a constant.

Parameters:
  • expr – Expression.

  • a – Constant divisor.

Returns:

Expression that represents the result of dividing the expression by a constant.