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.

Example:
// Create empty quadratic expression
GRBQuadExpr qexpr = new GRBQuadExpr();
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.

Example:
// Create a quadratic expression from a linear expression
GRBQuadExpr qexpr = new GRBQuadExpr(lexpr);
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.

Example:
// Copy quadratic expression
GRBQuadExpr copy = new GRBQuadExpr(qexpr);
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.

Example:
// Add linear expression lexpr to quadratic expression qexpr
qexpr.add(lexpr);
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.

Example:
// Add qexpr2 to quadratic expression qexpr1
qexpr1.add(qexpr2);
void addConstant(double c)#

Add a constant into a quadratic expression.

Arguments:

c – Constant to add to expression.

Example:
// Add 2.0 to quadratic expression
qexpr.addConstant(2.0);
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.

Example:
// Add 2.0 x to quadratic expression
qexpr.addTerm(2.0, x);
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.

Example:
// Add 2.0 x*y to quadratic expression
qexpr.addTerm(2.0, x, y);
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.

Example:
// Add 2.0 x + 3.0 y + 4.0 z to quadratic expression
double[] coeffs = {2.0, 3.0, 4.0};
GRBVar[] vars = {x, y, z};
qexpr.addTerms(coeffs, vars);
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.

Example:
// Add 2.0 x + 3.0 y  to quadratic expression
double[] coeffs = {2.0, 3.0, 4.0};
GRBVar[] vars = {x, y, z};
qexpr.addTerms(coeffs, vars, 0, 2);
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.

Example:
// Add 2.0 x * y + 3.0 y * z + 4.0 z^2 to quadratic expression
double[] coeffs = {2.0, 3.0, 4.0};
GRBVar[] vars1 = {x, y, z};
GRBVar[] vars2 = {y, z, z};
qexpr.addTerms(coeffs, vars1, vars2);
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.

Example:
// Add 2.0 x * y + 3.0 y * z to quadratic expression
double[] coeffs = {2.0, 3.0, 4.0};
GRBVar[] vars1 = {x, y, z};
GRBVar[] vars2 = {y, z, z};
qexpr.addTerms(coeffs, vars1, vars2, 0, 2);
void clear()#

Set a quadratic expression to 0.

Example:
qexpr.clear();
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.

Example:
// Get coefficient of first quadratic term of quadratic expression
double coeff = qexpr.getCoeff(0);
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.

Example:
// Get linear part of quadratic expression
GRBLinExpr lexpr = qexpr.getLinExpr();
double getValue()#

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

Return value:

Value of the expression for the current solution.

Example:
double val = qexpr.getValue();
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.

Example:
// Get first variable of first quadratic term of quadratic expression
GRBVar v = qexpr.getVar1(0);
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.

Example:
// Get second variable of first quadratic term of quadratic expression
GRBVar v = qexpr.getVar2(0);
void multAdd(double m, GRBLinExpr le)#

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

Arguments:
  • m – Constant multiplier for added expression.

  • le – Linear expression to add.

Example:
// Add 2.0 * linexpr to quadratic expression
qexpr.multAdd(2.0, linexpr);
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 to the sum of itself and the constant times the argument expression.

Example:
// Add 2.0 * qexpr2 to quadratic expression qexpr1
qexpr1.multAdd(2.0, qexpr2);
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.

Example:
// Remove first term of quadratic expression
qexpr.remove(0);
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).

Example:
// Remove all terms holding variable x from quadratic expression
boolean removed = qexpr.remove(x);
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.

Example:
int s = qexpr.size();