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 classGRBExpr
.In .NET languages that support operator overloading, you generally build quadratic expressions using overloaded operators. For example, if
x
is aGRBVar
object, thenx * x
is aGRBQuadExpr
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
, orexpr3 = expr1 + 2 * expr2
). You can also modify existing expressions (e.g.,expr += x * x
, orexpr2 -= 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 (usingAddTerm
) or in groups (usingAddTerms
orMultAdd
). Terms can also be removed from an expression (usingRemove
).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
orexpr += 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
andqexpr2
) and then use an overloaded comparison operator to build an argument forGRBModel.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
, andGetCoeff
methods. You can query the number of quadratic terms in the expression using theSize
property. To query the constant and linear terms associated with a quadratic expression, first obtain the linear portion of the quadratic expression usingLinExpr
, and then use theConstant
,GetCoeff
, orGetVar
on the resultingGRBLinExpr
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
andGetVar2
).- 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();
' Create empty quadratic expression Dim qexpr As GRBQuadExpr = 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);
' Create constant quadratic expression with value 1.0 Dim qexpr As GRBQuadExpr = 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);
' Create a quadratic expression from a linear expression Dim qexpr Ad GRBQuadExpr = 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);
' Copy quadratic expression Dim qexpr As GRBQuadExpr = 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);
' 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);
' 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);
' 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);
' 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);
' 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);
' Add 2.0 x + 3.0 y + 4.0 z to quadratic expression Dim coeffs As Double() = New Double() {2.0, 3.0, 4.0} Dim vars As GRBVar() = New GRBVar() {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
andlen
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);
' Add 2.0 x + 3.0 y to quadratic expression Dim coeffs As Double() = New Double() {2.0, 3.0, 4.0} Dim vars As GRBVar() = New GRBVar() {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);
' Add 2.0 x * y + 3.0 y * z + 4.0 z^2 to quadratic expression Dim coeffs As Double() = New Double() {2.0, 3.0, 4.0} Dim vars1 As GRBVar() = New GRBVar() {x, y, z} Dim vars2 As GRBVar() = New GRBVar(){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
andlen
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);
' Add 2.0 x * y + 3.0 y * z to quadratic expression Dim coeffs As Double() = New Double() {2.0, 3.0, 4.0} Dim vars1 As GRBVar() = New GRBVar() {x, y, z} Dim vars2 As GRBVar() = New GRBVar(){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. Theclear
method is mainly included for consistency with our interfaces to non-overloaded languages.- Example:
qexpr.Clear();
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);
' Get coefficient of first quadratic term of quadratic expression Dim coeff as Double = 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);
' Get first variable of first quadratic term of quadratic expression Dim var As GRBVar = 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);
' Get second variable of first quadratic term of quadratic expression Dim var As GRBVar = 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;
' Get linear part of quadratic expression Dim lexpr As GRBLinExpr = 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);
' 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);
' 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);
' 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);
' Remove all terms holding variable x from quadratic expression Dim removed As Boolean = 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;
Dim s As Integer = qexpr.Size
- double Value#
(Property) The value of an expression for the current solution.
- Example:
double val = qexpr.Value;
Dim val As Double = qexpr.Value