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.

In .NET languages that support operator overloading, 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).

The other option for building expressions is to start with an empty expression (using the GRBLinExpr constructor), and then add terms. Terms can be added individually (using AddTerm) or in groups (using AddTerms or MultAdd). Terms can also be removed from an expression, using Remove.

Note that it is much more efficient to build a linear expression by modifying an existing expression (using AddTerm or AddTerms) rather than creating a new one (using overloaded operators). The statement expr = expr + x, for example, creates a brand new expression to represent the sum. This incurs a cost for building the new expression, and it requires the previous expression to be reclaimed via garbage collection. The AddTerm or AddTerms methods just append new terms to an existing expression object.

Individual terms in a linear expression can be queried using the GetVar and GetCoeff methods. The constant can be queried using the Constant property. You can query the number of terms in the expression using the Size property.

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).



Subsections