Movatterモバイル変換


[0]ホーム

URL:


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

Concepts

Features

Reference

Gurobi
Back to top

gurobipy.LinExpr#

classLinExpr#

Gurobi linear expression object. A linear expression consists of aconstant term, plus a list of coefficient-variable pairs that capturethe linear terms. Linear expressions are used to build linear objectiveand constraints. They are temporary objects that typically have shortlifespans.

You generally build linear expressions using overloaded operators. Forexample, ifx is aVar object, thenx+1 is aLinExpr object. Expressions can be built from constants(e.g.,expr=0), variables (e.g.,expr=1*x+2*y), orfrom other expressions (e.g.,expr2=2*expr1+x, orexpr3=expr1+2*expr2). You can also modify existing expressions(e.g.,expr+=x, orexpr2-=expr1).

The full list of overloaded operators onLinExpr objects isas follows:+,+=,-,-=,*,*=,/, and** (only for exponent 2). In Python parlance, we’ve defined thefollowingLinExpr functions:__add__,__radd__,__iadd__,__sub__,__rsub__,__isub__,__neg__,__mul__,__rmul__,__imul__,__div__, and__pow__.

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

You can also useadd oraddTerms to modifyexpressions. TheLinExpr() constructor can also beused to build expressions. Another option isquicksum; it isa more efficient version of the Pythonsum function. Terms can beremoved 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 theLinExpr() constructor is generally going to befastest, followed by theaddTerms method, and then thequicksum function.

To add a linear constraint to your model, you generally build one or twolinear expression objects (expr1 andexpr2) and then use anoverloaded comparison operator to build an argument forModel.addConstr. To give a few examples:

model.addConstr(expr1<=expr2)model.addConstr(expr1==1)model.addConstr(2*x+3*y<=4)

Once you add a constraint to your model, subsequent changes to theexpression object you used to build the constraint will not change theconstraint (you would useModel.chgCoeff for that).

Individual terms in a linear expression can be queried using thegetVar,getCoeff, andgetConstantmethods. You can query the number of terms in the expression using thesize method.

Note that a linear expression may contain multiple terms that involvethe same variable. These duplicate terms are merged when creating aconstraint from an expression, but they may be visible when inspectingindividual terms in the expression (e.g., when usinggetVar).

LinExpr(arg1=0.0,arg2=None)#

Linear expression constructor. For linear expressions of moderate size(only a few terms) and for the ease of usage, you should generally useoverloaded operators instead of the explicit constructor to build linearexpression objects.

This constructor takes multiple forms. You can initialize a linearexpression using a constant (LinExpr(2.0)), a variable(LinExpr(x)), an expression (LinExpr(2*x)), a pair of listscontaining coefficients and variables, respectively(LinExpr([1.0,2.0],[x,y])), or a list of coefficient-variabletuples (LinExpr([(1.0,x),(2.0,y),(1.0,z)])).

Returns:

A linear expression object.

Example:
expr=LinExpr(2.0)expr=LinExpr(2*x)expr=LinExpr([1.0,2.0],[x,y])expr=LinExpr([(1.0,x),(2.0,y),(1.0,z)])
add(expr,mult=1.0)#

Add one linear expression into another. Upon completion, the invokinglinear expression will be equal to the sum of itself and the argumentexpression.

Parameters:
  • expr – Linear expression to add.

  • mult – (optional) Multiplier for argument expression.

Example:
e1=x+ye1.add(z,3.0)
addConstant(c)#

Add a constant into a linear expression.

Parameters:

c – Constant to add to expression.

Example:
expr=x+2*yexpr.addConstant(0.1)
addTerms(coeffs,vars)#

Add new terms into a linear expression.

Parameters:
  • coeffs – Coefficients for new terms; either a list of coefficientsor a single coefficient. The two arguments must have the same size.

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

Example:
expr.addTerms(1.0,x)expr.addTerms([2.0,3.0],[y,z])
clear()#

Set a linear expression to 0.

Example:
expr.clear()
copy()#

Copy a linear expression

Returns:

Copy of input expression.

Example:
e0=2*x+3e1=e0.copy()
getConstant()#

Retrieve the constant term from a linear expression.

Returns:

Constant from expression.

Example:
e=2*x+3print(e.getConstant())
getCoeff(i)#

Retrieve the coefficient from a single term of the expression.

Returns:

Coefficient for the term at indexi in the expression.

Example:
e=x+2*y+3print(e.getCoeff(1))
getValue()#

Compute the value of an expression using the current solution.

Returns:

The value of the expression.

Example:
obj=model.getObjective()print(obj.getValue())
getVar(i)#

Retrieve the variable object from a single term of the expression.

Returns:

Variable for the term at indexi in the expression.

Example:
e=x+2*y+3print(e.getVar(1))
linTerms()#

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

Example:
>>>e=x+2*y+z-1.0>>>forcoeff,varine.linTerms():>>>print(coeff,var.VarName)1.0 x2.0 y1.0 z
remove(item)#

Remove a term from a linear expression.

Parameters:

item – Ifitem is an integer, then the term stored at indexitem of the expression is removed. Ifitem is a Var, then allterms that involveitem are removed.

Example:
e=x+2*y+3e.remove(x)
size()#

Retrieve the number of terms in the linear expression (not including theconstant).

Returns:

Number of terms in the expression.

Example:
e=x+2*y+3print(e.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+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+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+y>=1)

Help and Feedback

On this page

[8]ページ先頭

©2009-2025 Movatter.jp