Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# GRBLinExpr

class GRBLinExpr

  Gurobi linear expression object. A linear expression consists of a constant term, plus a list of coefficient-variable pairs that capture the linear terms. Linear expressions are used to build constraints. They are temporary objects that typically have short lifespans.

  The `GRBLinExpr` class is a sub-class of the abstract base class `GRBExpr`.

  You generally build linear expressions using overloaded operators. For example, if `x` is a `GRBVar` object, then `x + 1` is a `GRBLinExpr` object. Expressions can be built from constants (e.g., `expr = 0`), variables (e.g., `expr = 1 * x + 2 * y`), or from other expressions (e.g., `expr2 = 2 * expr1 + x`, or `expr3 = expr1 + 2 * expr2`). You can also modify existing expressions (e.g., `expr += x`, or `expr2 -= expr1`).

  Another option for building expressions is to use the `addTerms` method, which adds an array of new terms at once. Terms can also be removed from an expression, using `remove`.

  Note that the cost of building expressions depends heavily on the approach you use. While you can generally ignore this issue when building small expressions, you should be aware of a few efficiency issues when building large expressions:

  - You should avoid using `expr = expr + x` in a loop. It will lead to runtimes that are quadratic in the number of terms in the expression.

  - Using `expr += x` (or `expr -= x`) is much more efficient than `expr = expr + x`. Building a large expression by looping over `+=` statements is reasonably efficient, but it isn’t the most efficient approach.

  - The most efficient way to build a large expression is to make a single call to `addTerms`.

  To add a linear constraint to your model, you generally build one or two linear expression objects (`expr1` and `expr2`) and then use an overloaded comparison operator to build an argument for `GRBModel::addConstr`. To give a few examples:

      model.addConstr(expr1 <= expr2)
      model.addConstr(expr1 == 1)
      model.addConstr(2*x + 3*y <= 4)

  Once you add a constraint to your model, subsequent changes to the expression object you used to build the constraint will not change the constraint (you would use `GRBModel::chgCoeff` for that).

  Individual terms in a linear expression can be queried using the `getVar`, `getCoeff`, and `getConstant` methods. You can query the number of terms in the expression using the `size` method.

  Note that a linear expression may contain multiple terms that involve the same variable. These duplicate terms are merged when creating a constraint from an expression, but they may be visible when inspecting individual terms in the expression (e.g., when using `getVar`).

  GRBLinExpr GRBLinExpr(double constant = 0.0)

    Linear expression constructor that creates a constant linear expression.

    Parameters:

      constant – (optional) Constant value for expression.

    Returns:

      A constant expression object.

  GRBLinExpr GRBLinExpr(GRBVar var, double coeff = 1.0)

    Linear expression constructor that creates an expression with one term.

    Parameters:

      - var – Variable for expression term.

      - coeff – (optional) Coefficient for expression term.

    Returns:

      An expression object containing one linear term.

  void addTerms(const double *coeffs, const GRBVar *vars, int count)

    Add new terms into a linear expression.

    Parameters:

      - coeffs – Coefficients for new terms.

      - vars – Variables for new terms.

      - count – Number of terms to add to the expression.

  void clear()

    Set a linear expression to 0.

    You should use the overloaded `expr = 0` instead. The `clear` method is mainly included for consistency with our interfaces to non-overloaded languages.

  double getConstant()

    Retrieve the constant term from a linear expression.

    Returns:

      Constant from expression.

  double getCoeff(int i)

    Retrieve the coefficient from a single term of the expression.

    Parameters:

      i – Index for coefficient of interest.

    Returns:

      Coefficient for the term at index `i` in the expression.

  double getValue()

    Compute the value of a linear expression for the current solution.

    Returns:

      Value of the expression for the current solution.

  GRBVar getVar(int i)

    Retrieve the variable object from a single term of the expression.

    Parameters:

      i – Index for term of interest.

    Returns:

      Variable for the term at index `i` in the expression.

  GRBLinExpr operator=(const GRBLinExpr &rhs)

    Set an expression equal to another expression.

    Parameters:

      rhs – Source expression.

    Returns:

      New expression object.

  GRBLinExpr operator+(const GRBLinExpr &rhs)

    Add one expression into another, producing a result expression.

    Parameters:

      rhs – Expression to add.

    Returns:

      Expression object which is equal the sum of the invoking expression and the argument expression.

  GRBLinExpr operator-(const GRBLinExpr &rhs)

    Subtract one expression from another, producing a result expression.

    Parameters:

      rhs – Expression to subtract.

    Returns:

      Expression object which is equal the invoking expression minus the argument expression.

  void operator+=(const GRBLinExpr &expr)

    Add an expression into the invoking expression.

    Parameters:

      expr – Expression to add.

  void operator-=(const GRBLinExpr &expr)

    Subtract an expression from the invoking expression.

    Parameters:

      expr – Expression to subtract.

  void operator*=(double multiplier)

    Multiply the invoking expression by a constant.

    Parameters:

      multiplier – Constant multiplier.

  void remove(int i)

    Remove the term stored at index `i` of the expression.

    Parameters:

      i – The index of the term to be removed.

  boolean remove(GRBVar var)

    Remove all terms associated with variable `var` from the expression.

    Parameters:

      var – The variable whose term should be removed.

    Returns:

      Returns `true` if the variable appeared in the linear expression (and was removed).

  unsigned int size()

    Retrieve the number of terms in the linear expression (not including the constant).

    Returns:

      Number of terms in the expression.

Help and Feedback

Press `/` or click to search this documentation | Gurobi website | Gurobi community forum
Go to other Gurobi documentation | Gurobi download | Contact support