Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Optimizer Reference Manual
Light LogoDark Logo

Concepts

Features

Reference

Gurobi
Back to top

gurobipy.QuadExpr#

classQuadExpr#

Gurobi quadratic expression object. A quadratic expression consists of alinear expression plus a list of coefficient-variable-variable triplesthat capture the quadratic terms. Quadratic expressions are used tobuild quadratic objective functions and quadratic constraints. They aretemporary objects that typically have short lifespans.

You generally build quadratic expressions using overloaded operators.For example, ifx is aVar object, thenx*x is aQuadExpr 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 full list of overloaded operators onQuadExpr objects isas follows:+,+=,-,-=,*,*=, and/. InPython parlance, we’ve defined the followingQuadExpr functions:__add__,__radd__,__iadd__,__sub__,__rsub__,__isub__,__neg__,__mul__,__rmul__,__imul__, and__div__.

We’ve also overloaded the comparison operators (==,<=, and>=), to make it easierto build constraints from quadratic expressions.

You can usequicksum to build quadratic expressions; it is amore efficient version of the Pythonsum function. You can also useadd oraddTerms to modify expressions. Terms canbe removed from an expression usingremove.

Given all these options for building expressions, you may wonder whichis fastest. For small expressions, you won’t need to worry aboutperformance differences between them. If you are building lots of verylarge expressions (100s of terms), you will find that a single call toaddTerms is 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 ortwo quadratic expression objects (qexpr1 andqexpr2) and thenuse 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 theexpression object you used to build the constraint will have no effecton that constraint.

Individual quadratic terms in a quadratic expression can be queriedusing thegetVar1,getVar2, andgetCoeff methods. You can query the number of quadratic termsin the expression using thesize method. To query theconstant and linear terms associated with a quadratic expression, usegetLinExpr to obtain the linear portion of the quadraticexpression, and then use thegetVar,getCoeff, andgetConstant methods on thisLinExpr object. Note that a quadratic expression may containmultiple terms that involve the same variable pair. These duplicateterms are merged when creating a constraint from an expression, but theymay be visible when inspecting individual terms in the expression (e.g.,when usinggetVar1 andgetVar2).

QuadExpr(expr=None)#

Quadratic expression constructor. Note that you should generally useoverloaded operators instead of the explicit constructor to buildquadratic expression objects.

Parameters:

expr – (optional) Initial value of quadratic expression. Can be aLinExpr or aQuadExpr. If no argument is specified, the initialexpression 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 alinear or a quadratic expression. Upon completion, the invokingquadratic expression will be equal to the sum of itself and the argumentexpression.

Parameters:
  • expr – Linear or quadratic expression to add.

  • mult – (optional) Multiplier for argument expression.

Example:
expr=x*x+2*y*yexpr.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+zexpr.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 coefficientsor a single coefficient. The arguments must have the same size.

  • vars – Variables for new terms; either a list of variables or asingle variable. The arguments must have the same size.

  • vars2 – (optional) Variables for new quadratic terms; either a listof variables or a single variable. Only present when you are addingquadratic 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+ze1=e0.copy()
getCoeff(i)#

Retrieve the coefficient from a single term of the expression.

Returns:

Coefficient for the quadratic term at indexi in theexpression.

Example:
expr=x*x+2*y*y+zprint(expr.getCoeff(1))
getLinExpr()#

A quadratic expression is represented as a linear expression, plus alist of quadratic terms. This method retrieves the linear expressionassociated with the quadratic expression.

Returns:

Linear expression from quadratic expression.

Example:
expr=x*x+2*y*y+zle=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 quadraticexpression.

Returns:

First variable associated with the quadratic term at indexiin the quadratic expression.

Example:
expr=x*x+2*y*y+zprint(expr.getVar1(1))
getVar2(i)#

Retrieve the second variable for a single quadratic term of thequadratic expression.

Returns:

Second variable associated with the quadratic term at indexi in the quadratic expression.

Example:
expr=x*x+2*y*y+zprint(expr.getVar2(1))
linTerms()#

Return an iterator over linear terms in the expression. Each element inthe iteration is a tuple of the form(coefficient,variable). Theconstant and the quadratic terms are not included in the iteration.

Example:
>>>e=x*z+y**2+z+y+1.0>>>forcoeff,varine.linTerms():>>>print(coeff,var.VarName)1.0 z1.0 y
quadTerms()#

Return an iterator over quadratic terms in the expression. Each elementin the iteration is a tuple of the form(coefficient,var1,var2).The constant and the linear terms are not included in the iteration.

Example:
>>>e=x*z+y**2+z+y+1.0>>>forcoeff,var1,var2ine.quadTerms():>>>print(coeff,var1.VarName,var2.VarName)1.0 x z1.0 y y
remove(item)#

Remove a term from a quadratic expression.

Parameters:

item – Ifitem is an integer, then the quadratic term stored atindexitem of the expression is removed. Ifitem is a Var, thenall quadratic terms that involveitem are removed.

Example:
expr=x*x+2*y*y+zexpr.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+zprint(expr.size())
__eq__()#

Overloads the== operator, creating aTempConstr objectthat captures an equality constraint. The result is typicallyimmediately passed toModel.addConstr.

Returns:

ATempConstr object.

Example:
m.addConstr(x*x+y*y==1)
__le__()#

Overloads the<= operator, creating aTempConstr objectthat captures an inequality constraint. The result is typicallyimmediately passed toModel.addConstr.

Returns:

ATempConstr object.

Example:
m.addConstr(x*x+y*y<=1)
__ge__(arg)#

Overloads the>= operator, creating aTempConstr objectthat captures an inequality constraint. The result is typicallyimmediately passed toModel.addConstr.

Returns:

ATempConstr object.

Example:
m.addConstr(x*x+y*y>=1)

Help and Feedback

On this page

[8]ページ先頭

©2009-2025 Movatter.jp