linopy.model.Model.linexpr

Contents

linopy.model.Model.linexpr#

Model.linexpr(*args)#

Create a linopy.LinearExpression from argument list.

Parameters:

args (tuples of (coefficients, variables) or tuples of) – coordinates and a function If args is a collection of coefficients-variables-tuples, the resulting linear expression is built with the function LinearExpression.from_tuples. In this case, each tuple represents on term in the linear expression, which can span over multiple dimensions:

  • coefficientsint/float/array_like

    The coefficient(s) in the term, if the coefficients array contains dimensions which do not appear in the variables, the variables are broadcasted.

  • variablesstr/array_like/linopy.Variable

    The variable(s) going into the term. These may be referenced by name.

If args is a collection of coordinates with an appended function at the end, the function LinearExpression.from_rule is used to build the linear expression. Then, the argument are expected to contain:

  • rulecallable

    Function to be called for each combinations in coords. The first argument of the function is the underlying linopy.Model. The following arguments are given by the coordinates for accessing the variables. The function has to return a ScalarLinearExpression. Therefore use the direct getter when indexing variables.

  • coordscoordinate-like

    Coordinates to be processed by xarray.DataArray. For each combination of coordinates, the function rule is called. The order and size of coords has to be same as the argument list followed by model in function rule.

Returns:

linopy.LinearExpression

Examples

For creating an expression from tuples:

>>> from linopy import Model
>>> import pandas as pd
>>> m = Model()
>>> x = m.add_variables(pd.Series([0, 0]), 1, name="x")
>>> y = m.add_variables(4, pd.Series([8, 10]), name="y")
>>> expr = m.linexpr((10, "x"), (1, "y"))

For creating an expression from a rule:

>>> m = Model()
>>> coords = pd.RangeIndex(10), ["a", "b"]
>>> a = m.add_variables(coords=coords)
>>> def rule(m, i, j):
...     return a.at[i, j] + a.at[(i + 1) % 10, j]
...
>>> expr = m.linexpr(rule, coords)

See also

LinearExpression.from_tuples, LinearExpression.from_rule