GRBQuadExpr#

class GRBQuadExpr#
Gurobi quadratic expression object. A quadratic expression consists of a linear expression, plus a list of coefficientvariablevariable 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 subclass of the abstract base classGRBExpr
.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 * 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
). 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
in a loop. It will lead to runtimes that are quadratic in the number of terms in the expression.Using
expr += x*x
(orexpr = x*x
) is much more efficient thanexpr = expr + x*x
. Building a large expression by looping over+=
statements 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
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 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
method. To query the constant and linear terms associated with a quadratic expression, first obtain the linear portion of the quadratic expression usinggetLinExpr
, and then use thegetConstant
,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 terms in the expression (e.g., when using
getVar1
andgetVar2
).
GRBQuadExpr GRBQuadExpr(double constant = 0.0)#
Quadratic expression constructor that creates a constant quadratic expression.
 Parameters:
constant – (optional) Constant value for expression.
 Returns:
A constant expression object.

GRBQuadExpr GRBQuadExpr(GRBVar var, double coeff = 1.0)#
Quadratic expression constructor that creates an expression with one term.
 Parameters:
var – Variable for expression term.
coeff – (optional) Coefficient for expression term.
 Returns:
An expression object containing one quadratic term.

GRBQuadExpr GRBQuadExpr(GRBLinExpr linexpr)#
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.

void addTerm(double coeff, GRBVar var)#
Add a new linear term into a quadratic expression.
 Parameters:
coeff – Coefficient for new linear term.
var – Variable for new linear term.

void addTerm(double coeff, GRBVar var1, GRBVar var2)#
Add a new quadratic term into a quadratic expression.
 Parameters:
coeff – Coefficient for new quadratic term.
var1 – Variable for new quadratic term.
var2 – Variable for new quadratic term.

void addTerms(const double *coeffs, const GRBVar *vars, int count)#
Add new linear terms into a quadratic expression.
 Parameters:
coeffs – Coefficients for new linear terms.
vars – Variables for new linear terms.
count – Number of linear terms to add to the quadratic expression.

void addTerms(const double *coeffs, const GRBVar *vars1, const GRBVar *vars2, int count)#
Add new quadratic terms into a quadratic expression.
 Parameters:
coeffs – Coefficients for new quadratic terms.
vars1 – First variables for new quadratic terms.
vars2 – Second variables for new quadratic terms.
count – Number of quadratic terms to add to the quadratic expression.

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 nonoverloaded languages.

double getCoeff(int i)#
Retrieve the coefficient from a single quadratic term of the quadratic expression.
 Parameters:
i – Index for coefficient of interest.
 Returns:
Coefficient for the quadratic term at index
i
in the quadratic 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.
 Returns:
Linear expression associated with the quadratic expression.

double getValue()#
Compute the value of a quadratic expression for the current solution.
 Returns:
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.
 Parameters:
i – Index for term of interest.
 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.
 Parameters:
i – Index for term of interest.
 Returns:
Second variable for the quadratic term at index
i
in the quadratic expression.

GRBQuadExpr operator=(const GRBQuadExpr &rhs)#
Set a quadratic expression equal to another quadratic expression.
 Parameters:
rhs – Source quadratic expression.
 Returns:
New quadratic expression object.

GRBQuadExpr operator+(const GRBQuadExpr &rhs)#
Add one expression into another, producing a result expression.
 Parameters:
rhs – Expression to add.
 Returns:
Expression object which is equal the sum of the invoking expression and the argument expression.

GRBQuadExpr operator(const GRBQuadExpr &rhs)#
Subtract one expression from another, producing a result expression.
 Parameters:
rhs – Expression to subtract.
 Returns:
Expression object which is equal the invoking expression minus the argument expression.

void operator+=(const GRBQuadExpr &expr)#
Add an expression into the invoking expression.
 Parameters:
expr – Expression to add.

void operator=(const GRBQuadExpr &expr)#
Subtract an expression from the invoking expression.
 Parameters:
expr – Expression to subtract.

void operator*=(double multiplier)#
Multiply the invoking expression by a constant.
 Parameters:
multiplier – Constant multiplier.

void remove(int i)#
Remove the quadratic term stored at index
i
of the expression. Parameters:
i – The index of the term to be removed.

boolean remove(GRBVar var)#
Remove all quadratic terms associated with variable
var
from the quadratic expression. Parameters:
var – The variable whose term should be removed.
 Returns:
Returns
true
if the variable appeared in the quadratic expression (and was removed).

unsigned int size()#
Retrieve the number of quadratic terms in the quadratic expression.
 Returns:
Number of quadratic terms in the expression.