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.

In .NET languages that support operator overloading, you generally build quadratic expressions using overloaded operators. For example, if x is a GRBVar object, then x * x is a GRBQuadExpr object. Expressions can be built from constants (e.g., expr = 0), variables (e.g., expr = 1 * x * x + 2 * x * y), or from other expressions (e.g., expr2 = 2 * expr1 + x, or expr3 = expr1 + 2 * expr2). You can also modify existing expressions (e.g., expr += x * x, or expr2 -= expr1).

The other option for building expressions is to start with an empty expression (using the GRBQuadExpr constructor), and then add 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).

Note that the cost of building expressions 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 using expr = expr + x*x or expr += x*x in a loop. It will lead to runtimes that are quadratic in the number of terms in the expression.

  • Using AddTerm in 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 to AddTerms.

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 <= qexpr2)
model.AddQConstr(qexpr1 == 1)
model.AddQConstr(2*x*x + 3*y*y <= 4)

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 property. To query the constant and linear terms associated with a quadratic expression, first obtain the linear portion of the quadratic expression using LinExpr, and then use the Constant, GetCoeff, or GetVar 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.

Returns:

An empty expression object.

Example:
// Create empty quadratic expression
GRBQuadExpr qexpr = new GRBQuadExpr();
GRBQuadExpr GRBQuadExpr(double a)#

Quadratic expression constructor that creates a constant quadratic expression.

Returns:

A quadratic expression object.

Example:
// Create constant quadratic expression with value 1.0
GRBQuadExpr qexpr = new GRBQuadExpr(1.0);
GRBQuadExpr GRBQuadExpr(GRBLinExpr orig)#

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

Parameters:

orig – Existing linear expression to copy.

Returns:

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

Quadratic expression constructor that copies an existing quadratic expression.

Parameters:

orig – Existing expression to copy.

Returns:

A copy of the input expression object.

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

Parameters:

le – Linear expression to add.

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

Parameters:

qe – Quadratic expression to add.

Example:
// Add x^2 + y to quadratic expression
qexpr.Add(x*x + y);
void AddConstant(double c)#

Add a constant into a quadratic expression.

Parameters:

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.

Parameters:
  • 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.

Parameters:
  • 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.

Parameters:
  • 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.

Parameters:
  • 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.

Parameters:
  • 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.

Parameters:
  • 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.

You should use the overloaded expr = 0 instead. The clear method is mainly included for consistency with our interfaces to non-overloaded languages.

Example:
qexpr.Clear();
double GetCoeff(int i)#

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

Returns:

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);
GRBVar GetVar1(int i)#

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

Returns:

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 var = qexpr.GetVar1(0);
GRBVar GetVar2(int i)#

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

Returns:

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 var = qexpr.GetVar2(0);
GRBLinExpr LinExpr#

(Property) 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.

Example:
// Get linear part of quadratic expression
GRBLinExpr lexpr = qexpr.LinExpr;
void MultAdd(double m, GRBLinExpr le)#

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

Parameters:
  • m – Constant multiplier for added expression.

  • le – Linear expression to add.

Example:
// Add 2.0 * (x + y) to quadratic expression
qexpr.MultAdd(2.0, x + y);
void MultAdd(double m, GRBQuadExpr qe)#

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

Parameters:
  • m – Constant multiplier for added expression.

  • qe – Quadratic expression to add.

Example:
// Add 2.0 * (x^2 + x*y) to quadratic expression
qexpr.MultAdd(2.0, x*x + x*y);
void Remove(int i)#

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

Parameters:

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.

Parameters:

var – The variable whose quadratic term should be removed.

Returns:

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

Example:
// Remove all terms holding variable x from quadratic expression
bool removed = qexpr.Remove(x);
int Size#

(Property) The number of quadratic terms in the quadratic expression. Use GRBQuadExpr.LinExpr to retrieve constant or linear terms from the quadratic expression.

Example:
int s = qexpr.Size;
double Value#

(Property) The value of an expression for the current solution.

Example:
double val = qexpr.Value;