Try our new documentation site (beta).


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 by starting with an empty expression (using the GRBLinExpr constructor), and then adding 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.

To add a linear constraint to your model, you generally build one or two linear expression objects (expr1 and expr2) and then pass them to GRBModel.addConstr. To give a few examples:

<span>$</span>\displaystyle \begin{array}{l}
\mathrm{model.addConstr}(expr1, GRB.LESS\_EQUAL, expr2) \
\mathrm{model.addConstr}(expr1, GRB.EQUAL, 1) \
\end{array}<span>$</span>
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).



Subsections

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search

Gurobi Optimization