GRBLinExpr


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.

TheGRBLinExprclass is a sub-class of the abstract base classGRBExpr.

In .NET languages that support operator overloading, you generally build linear expressions using overloaded operators. For example, ifxis aGRBVarobject, thenx + 1is aGRBLinExprobject. 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, orexpr3 = expr1 + 2 * expr2). You can also modify existing expressions (e.g.,expr += x, orexpr2 -= expr1).

The other option for building expressions is to start with an empty expression (using theGRBLinExprconstructor), and then add terms. Terms can be added individually (usingAddTerm) or in groups (usingAddTermsorMultAdd). Terms can also be removed from an expression, usingRemove.

请注意,建筑表达的成本sions 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 usingexpr = expr + xorexpr += xin a loop. It will lead to runtimes that are quadratic in the number of terms in the expression.
  • UsingAddTermin a loop 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 toAddTerms.

Individual terms in a linear expression can be queried using theGetVarandGetCoeffmethods. The constant can be queried using theConstantproperty. You can query the number of terms in the expression using theSizeproperty.

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



Subsections