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.
- GRBQuadExpr GRBQuadExpr(double a)#
Quadratic expression constructor that creates a constant quadratic expression.
- Returns:
A quadratic expression object.
- 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.
- 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.
- 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.
- 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.
- void AddConstant(double c)#
Add a constant into a quadratic expression.
- Parameters:
c – Constant to add to expression.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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).
- 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.
- double Value#
(Property) The value of an expression for the current solution.