gurobipy.QuadExpr#
- class QuadExpr#
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.
You generally build quadratic expressions using overloaded operators. For example, if
xis aVarobject, thenx * xis aQuadExprobject. 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 full list of overloaded operators on
QuadExprobjects is as follows:+,+=,-,-=,*,*=, and/. In Python parlance, we’ve defined the followingQuadExprfunctions:__add__,__radd__,__iadd__,__sub__,__rsub__,__isub__,__neg__,__mul__,__rmul__,__imul__, and__div__.We’ve also overloaded the comparison operators (
==,<=, and>=), to make it easier to build constraints from quadratic expressions.You can use
quicksumto build quadratic expressions; it is a more efficient version of the Pythonsumfunction. You can also useaddoraddTermsto modify expressions. Terms can be removed from an expression usingremove.Given all these options for building expressions, you may wonder which is fastest. For small expressions, you won’t need to worry about performance differences between them. If you are building lots of very large expressions (100s of terms), you will find that a single call to
addTermsis fastest. Next would be a call toquicksum, followed by a series of calls toexpr.add(x*x).To add a quadratic constraint to your model, you generally build one or two quadratic expression objects (
qexpr1andqexpr2) and then use an overloaded comparison operator to build an argument forModel.addConstr. To give a few examples:model.addConstr(qexpr1 <= qexpr2) model.addConstr(qexpr1 == 1) model.addConstr(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, andgetCoeffmethods. You can query the number of quadratic terms in the expression using thesizemethod. To query the constant and linear terms associated with a quadratic expression, usegetLinExprto obtain the linear portion of the quadratic expression, and then use thegetVar,getCoeff, andgetConstantmethods on thisLinExprobject. Note that a quadratic expression may contain multiple terms that involve the same variable pair. These duplicate terms are merged when creating a constraint from an expression, but they may be visible when inspecting individual terms in the expression (e.g., when usinggetVar1andgetVar2).- QuadExpr(expr=None)#
Quadratic expression constructor. Note that you should generally use overloaded operators instead of the explicit constructor to build quadratic expression objects.
- Parameters:
expr – (optional) Initial value of quadratic expression. Can be a
LinExpror aQuadExpr. If no argument is specified, the initial expression value is 0.- Returns:
A quadratic expression object.
- Example:
expr = QuadExpr() expr = QuadExpr(2*x) expr = QuadExpr(x*x + y+y)
- add(expr, mult=1.0)#
Add an expression into a quadratic expression. Argument can be either a linear or a quadratic expression. Upon completion, the invoking quadratic expression will be equal to the sum of itself and the argument expression.
- Parameters:
expr – Linear or quadratic expression to add.
mult – (optional) Multiplier for argument expression.
- Example:
expr = x * x + 2 * y * y expr.add(z * z, 3.0)
- addConstant(c)#
Add a constant into a quadratic expression.
- Parameters:
c – Constant to add to expression.
- Example:
expr = x * x + 2 * y * y + z expr.addConstant(0.1)
- addTerms(coeffs, vars, vars2=None)#
Add new linear or quadratic terms into a quadratic expression.
- Parameters:
coeffs – Coefficients for new terms; either a list of coefficients or a single coefficient. The arguments must have the same size.
vars – Variables for new terms; either a list of variables or a single variable. The arguments must have the same size.
vars2 – (optional) Variables for new quadratic terms; either a list of variables or a single variable. Only present when you are adding quadratic terms. The arguments must have the same size.
- Example:
expr.addTerms(1.0, x) expr.addTerms([2.0, 3.0], [y, z]) expr.addTerms([2.0, 3.0], [x, y], [y, z])
- clear()#
Set a quadratic expression to 0.
- Example:
expr.clear()
- copy()#
Copy a quadratic expression
- Returns:
Copy of input expression.
- Example:
e0 = x * x + 2 * y * y + z e1 = e0.copy()
- getCoeff(i)#
Retrieve the coefficient from a single term of the expression.
- Returns:
Coefficient for the quadratic term at index
iin the expression.- Example:
expr = x * x + 2 * y * y + z print(expr.getCoeff(1))
- 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 from quadratic expression.
- Example:
expr = x * x + 2 * y * y + z le = expr.getLinExpr()
- getValue()#
Compute the value of an expression using the current solution.
- Returns:
The value of the expression.
- Example:
obj = model.getObjective() print(obj.getValue())
- getVar1(i)#
Retrieve the first variable for a single quadratic term of the quadratic expression.
- Returns:
First variable associated with the quadratic term at index
iin the quadratic expression.- Example:
expr = x * x + 2 * y * y + z print(expr.getVar1(1))
- getVar2(i)#
Retrieve the second variable for a single quadratic term of the quadratic expression.
- Returns:
Second variable associated with the quadratic term at index
iin the quadratic expression.- Example:
expr = x * x + 2 * y * y + z print(expr.getVar2(1))
- remove(item)#
Remove a term from a quadratic expression.
- Parameters:
item – If
itemis an integer, then the quadratic term stored at indexitemof the expression is removed. Ifitemis a Var, then all quadratic terms that involveitemare removed.- Example:
expr = x * x + 2 * y * y + z expr.remove(x)
- size()#
Retrieve the number of quadratic terms in the expression.
- Returns:
Number of quadratic terms in the expression.
- Example:
expr = x * x + 2 * y * y + z print(expr.size())
- __eq__()#
Overloads the
==operator, creating aTempConstrobject that captures an equality constraint. The result is typically immediately passed toModel.addConstr.- Returns:
A
TempConstrobject.- Example:
m.addConstr(x*x + y*y == 1)
- __le__()#
Overloads the
<=operator, creating aTempConstrobject that captures an inequality constraint. The result is typically immediately passed toModel.addConstr.- Returns:
A
TempConstrobject.- Example:
m.addConstr(x*x + y*y <= 1)
- __ge__(arg)#
Overloads the
>=operator, creating aTempConstrobject that captures an inequality constraint. The result is typically immediately passed toModel.addConstr.- Returns:
A
TempConstrobject.- Example:
m.addConstr(x*x + y*y >= 1)