lumix.core.expressions.LXLinearExpression

class lumix.core.expressions.LXLinearExpression(terms=<factory>, constant=0.0, _multi_terms=<factory>)[source]

Type-safe linear expression builder with multi-model support.

Represents: sum(coeff[i] * var[i]) + constant

Examples:

expr = LXLinearExpression()
expr.add_term(production, 1.0)
expr.add_term(inventory, -1.0)
expr.constant(100)

# Multi-model
expr = LXLinearExpression()
expr.sum_over(duty, where=lambda driver, date: date.is_weekend)
Parameters:
__init__(terms=<factory>, constant=0.0, _multi_terms=<factory>)

Methods

__init__([terms, constant, _multi_terms])

add_constant(value)

Add constant to expression.

add_multi_term(var[, coeff, where])

Add multi-indexed variable to expression.

add_term(var[, coeff, where])

Add term with coefficient (constant or function).

copy()

Create a deep copy of this expression.

sum_over(var[, where])

Syntactic sugar for summing over all dimensions of a variable.

Attributes

terms: Dict[str, Tuple[LXVariable, Callable[[TModel], float]], Tuple[Callable[[TModel], bool]]]
constant: float = 0.0
__deepcopy__(memo)[source]

Custom deepcopy that handles variables and lambda functions.

This method enables what-if analysis on expressions by: 1. Deep copying all variables in the expression 2. Safely copying coefficient and filter lambda functions 3. Preserving the expression structure

Parameters:

memo – Dictionary for tracking circular references during deepcopy

Returns:

Deep copy of this expression with all dependencies resolved

add_term(var, coeff=1.0, where=None)[source]

Add term with coefficient (constant or function).

Parameters:
Return type:

Self

Returns:

Self for chaining

add_multi_term(var, coeff=<function LXLinearExpression.<lambda>>, where=None)[source]

Add multi-indexed variable to expression.

Parameters:
Return type:

Self

Returns:

Self for chaining

Example:

expr.add_multi_term(
    duty,
    coeff=lambda driver, date: driver.cost * date.multiplier,
    where=lambda driver, date: date.is_weekend
)
sum_over(var, where=None)[source]

Syntactic sugar for summing over all dimensions of a variable.

Parameters:
Return type:

Self

Returns:

Self for chaining

Example:

# Sum all driver duties (over all drivers and dates)
expr.sum_over(duty)

# Sum duties for all drivers on a specific date
expr.sum_over(duty, where=lambda d, dt: dt == specific_date)

Note

Currently sums over all dimensions. Future enhancement could add selective dimension summing (e.g., sum only over drivers, not dates).

add_constant(value)[source]

Add constant to expression.

Parameters:

value (float) – Constant value

Return type:

Self

Returns:

Self for chaining

__add__(other)[source]

Enable expr1 + expr2 or expr + constant.

Parameters:

other (Union[float, Self]) – Expression or constant to add

Return type:

Self

Returns:

Self for chaining

__mul__(scalar)[source]

Enable scalar * expression.

Parameters:

scalar (float) – Scalar multiplier

Return type:

Self

Returns:

Self for chaining

copy()[source]

Create a deep copy of this expression.

Return type:

Self

Returns:

New expression with same terms and constant

__init__(terms=<factory>, constant=0.0, _multi_terms=<factory>)