Java API - GRBQuadExpr#

GRBQuadExpr#

Gurobi quadratic expression object. A quadratic expression consists of a linear expression, plus a list of coefficient-variable-variable triples that capture the quadratic terms. Quadratic expressions are used to build quadratic objective functions and quadratic constraints. They are temporary objects that typically have short lifespans.

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

You generally build quadratic expressions by starting with an empty expression (using the GRBQuadExpr constructor), and then adding terms. Terms can be added individually, using addTerm, or in groups, using addTerms, or multAdd. Quadratic terms can be removed from a quadratic expression using remove.

To add a quadratic constraint to your model, you generally build one or two quadratic expression objects (qexpr1 and qexpr2) and then use an overloaded comparison operator to build an argument for GRBModel.addQConstr. To give a few examples:

model.addQConstr(qexpr1, GRB.LESS_EQUAL, qexpr2)
model.addQConstr(qexpr1, GRB.EQUAL, 1)

Once you add a constraint to your model, subsequent changes to the expression object you used to build the constraint will have no effect on that constraint.

Individual quadratic terms in a quadratic expression can be queried using the getVar1, getVar2, and getCoeff methods. You can query the number of quadratic terms in the expression using the size method. To query the constant and linear terms associated with a quadratic expression, first obtain the linear portion of the quadratic expression using getLinExpr, and then use the getConstant, getCoeff, and getVar methods on the resulting GRBLinExpr object.

Note that a quadratic expression may contain multiple terms that involve the same variable pair. These duplicate terms are merged when creating the model objective from an expression, but they may be visible when inspecting individual quadratic terms in the expression (e.g., when using getVar1 and getVar2).

GRBQuadExpr GRBQuadExpr()#

Quadratic expression constructor that creates an empty quadratic expression.

Return value:

An empty expression object.

GRBQuadExpr GRBQuadExpr(GRBLinExpr le)#

Quadratic expression constructor that initializes a quadratic expression from an existing linear expression.

Arguments:

le – Existing linear expression to copy.

Return value:

Quadratic expression object whose initial value is taken from the input linear expression.

GRBQuadExpr GRBQuadExpr(GRBQuadExpr qe)#

Quadratic expression constructor that copies an existing quadratic expression.

Arguments:

qe – Existing expression to copy.

Return value:

A copy of the input expression object.

void add(GRBLinExpr le)#

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

Arguments:

le – Linear expression to add.

void add(GRBQuadExpr qe)#

Add a quadratic expression into a quadratic expression. Upon completion, the invoking quadratic expression will be equal to the sum of itself and the argument expression.

Arguments:

qe – Quadratic expression to add.

void addConstant(double c)#

Add a constant into a quadratic expression.

Arguments:

c – Constant to add to expression.

void addTerm(double coeff, GRBVar var)#

Add a single linear term (coeff*var) into a quadratic expression.

Arguments:
  • coeff – Coefficient for new term.

  • var – Variable for new term.

void addTerm(double coeff, GRBVar var1, GRBVar var2)#

Add a single quadratic term (coeff*var1*var2) into a quadratic expression.

Arguments:
  • coeff – Coefficient for new quadratic term.

  • var1 – First variable for new quadratic term.

  • var2 – Second variable for new quadratic term.

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

Add a list of linear terms into a quadratic 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 linear terms into a quadratic expression. This signature allows you to use arrays to hold the coefficients and variables that describe the linear 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 addTerms(double[] coeffs, GRBVar[] vars1, GRBVar[] vars2)#

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

Arguments:
  • coeffs – Coefficients for new quadratic terms.

  • vars1 – First variables for new quadratic terms.

  • vars2 – Second variables for new quadratic terms.

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

Add new quadratic terms into a quadratic 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 quadratic terms.

  • vars1 – First variables for new quadratic terms.

  • vars2 – Second variables for new quadratic terms.

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

  • len – The number of terms to add.

void clear()#

Set a quadratic expression to 0.

double getCoeff(int i)#

Retrieve the coefficient from a single quadratic term of the quadratic expression.

Arguments:

i – Index for coefficient of interest.

Return value:

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

GRBLinExpr getLinExpr()#

A quadratic expression is represented as a linear expression, plus a list of quadratic terms. This method retrieves the linear expression associated with the quadratic expression.

Return value:

Linear expression associated with the quadratic expression.

double getValue()#

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

Return value:

Value of the expression for the current solution.

GRBVar getVar1(int i)#

Retrieve the first variable object associated with a single quadratic term from the expression.

Arguments:

i – Index for term of interest.

Return value:

First variable for the quadratic term at index i in the quadratic expression.

GRBVar getVar2(int i)#

Retrieve the second variable object associated with a single quadratic term from the expression.

Arguments:

i – Index for term of interest.

Return value:

Second variable for the quadratic term at index i in the quadratic expression.

void multAdd(double m, GRBLinExpr le)#

Add a constant multiple of a linear expression into the quadratic expression. Upon completion, the invoking quadratic 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 multAdd(double m, GRBQuadExpr qe)#

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

Arguments:
  • m – Constant multiplier for added expression.

  • qe – Quadratic expression to add.

void remove(int i)#

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

Arguments:

i – The index of the quadratic term to be removed.

boolean remove(GRBVar var)#

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

Arguments:

var – The variable whose quadratic term should be removed.

Return value:

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

int size()#

Retrieve the number of quadratic terms in the quadratic expression. Use GRBQuadExpr.getLinExpr to retrieve constant or linear terms from the quadratic expression.

Return value:

Number of quadratic terms in the expression.