Java API - 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.

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:

model.addConstr(expr1, GRB.LESS_EQUAL, expr2)
model.addConstr(expr1, GRB.EQUAL, 1)

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()#

Linear expression constructor that creates an empty linear expression.

Return value:

An empty expression object.

GRBLinExpr GRBLinExpr(GRBLinExpr le)#

Linear expression constructor that copies an existing linear expression.

Arguments:

le – Existing expression to copy.

Return value:

A copy of the input expression object.

void add(GRBLinExpr le)#

Add one linear expression into another. Upon completion, the invoking linear expression will be equal to the sum of itself and the argument expression.

Arguments:

le – Linear expression to add.

void addConstant(double c)#

Add a constant into a linear expression.

Arguments:

c – Constant to add to expression.

void addTerm(double coeff, GRBVar var)#

Add a single term into a linear expression.

Arguments:
  • coeff – Coefficient for new term.

  • var – Variable for new term.

void addTerms(double[] coeffs, GRBVar[] vars)#

Add a list of terms into a linear expression. Note that the lengths of the two argument arrays must be equal.

Arguments:
  • coeffs – Coefficients for new terms.

  • vars – Variables for new terms.

void addTerms(double[] coeffs, GRBVar[] vars, int start, int len)#

Add new terms into a linear expression. This signature allows you to use arrays to hold the coefficients and variables that describe the terms in an array without being forced to add a term for each entry in the array. The start and len arguments allow you to specify which terms to add.

Arguments:
  • coeffs – Coefficients for new terms.

  • vars – Variables for new terms.

  • start – The first term in the list to add.

  • len – The number of terms to add.

void clear()#

Set a linear expression to 0.

double getConstant()#

Retrieve the constant term from a linear expression.

Return value:

Constant from expression.

double getCoeff(int i)#

Retrieve the coefficient from a single term of the expression.

Arguments:

i – Index for coefficient of interest.

Return value:

Coefficient for the term at index i in the expression.

double getValue()#

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

Return value:

Value of the expression for the current solution.

GRBVar getVar(int i)#

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

Arguments:

i – Index for term of interest.

Return value:

Variable for the term at index i in the expression.

void multAdd(double m, GRBLinExpr le)#

Add a constant multiple of one linear expression into another. Upon completion, the invoking linear expression is equal the sum of itself and the constant times the argument expression.

Arguments:
  • m – Constant multiplier for added expression.

  • le – Linear expression to add.

void remove(int i)#

Remove the term stored at index i of the expression.

Arguments:

i – The index of the term to be removed.

boolean remove(GRBVar var)#

Remove all terms associated with variable var from the expression.

Arguments:

var – The variable whose term should be removed.

Return value:

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

int size()#

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

Return value:

Number of terms in the expression.