diff --git a/doc/release-notes.md b/doc/release-notes.md index 2a88e23f..5aae1f38 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -132,6 +132,7 @@ linopy 1.0 makes the strict arithmetic convention the only behaviour and puts de - `Model.from_spec` no longer refuses an infinite parameter times a variable absent there, where no row is built. ([\#1143](https://github.com/PyPSA/linopy/pull/1143)) - `Model.from_spec` adds no constraint where every variable of it is masked, where it raised that both sides are constant. ([\#1154](https://github.com/PyPSA/linopy/pull/1154)) - `Model.from_spec` no longer lets a `where` guard what a `shift`, window, `sum`, `count` or `at()` lookup reads at another coordinate, so a non-finite value there raises instead of entering a row or a `when:`. ([\#1173](https://github.com/PyPSA/linopy/pull/1173)) +- `Model.from_spec` checks a case's value, `otherwise:` included, only where that case applies, and skips a case that applies nowhere, so a guard like `cap == inf` no longer leaves `otherwise:` refused there. ([\#1163](https://github.com/PyPSA/linopy/pull/1163)) - `LinearExpression(data, model)` no longer raises on a `.sel` over full coordinates. ([\#1144](https://github.com/PyPSA/linopy/pull/1144)) - `LinearExpression.from_constant` builds on the model's store, so a sparse model's constant stays sparse and adding it emits no `PerformanceWarning`. ([\#1145](https://github.com/PyPSA/linopy/pull/1145)) - `Model.from_spec(..., sparse=True)` keeps a `shift` whose offset differs per entity sparse, without a `PerformanceWarning`. ([\#1145](https://github.com/PyPSA/linopy/pull/1145)) diff --git a/linopy/spec/evaluator.py b/linopy/spec/evaluator.py index c67a7a24..bbd1c7c8 100644 --- a/linopy/spec/evaluator.py +++ b/linopy/spec/evaluator.py @@ -4,7 +4,8 @@ import functools import operator -from collections.abc import Callable, Sequence +from collections.abc import Callable, Iterator, Sequence +from dataclasses import replace from typing import TypeAlias, TypeVar, assert_never, cast import numpy as np @@ -93,11 +94,7 @@ def _evaluated(node: ms.Expression, ctx: Context) -> Value: if isinstance(node, ms.WindowSum): return _moved(node, _operand(node.operand, widened(node, ctx)), ctx, None) if isinstance(node, ms.Cases): - regions = ( - _in_region(evaluate(region.value, ctx), evaluate_where(region.when, ctx)) - for region in node.regions - ) - return functools.reduce(lambda a, b: operator.add(*_paired(a, b)), regions) + return _cases(node, ctx) assert_never(node) @@ -124,18 +121,24 @@ def _data_under(node: ms.Expression, ctx: Context) -> None: shift or window. A use of every other operand is consumed unbuilt. """ if isinstance(node, ms.Cases): - for region in node.regions: - evaluate_where(region.when, ctx) + for region, rows in _regions(node, ctx): + _data_of(region.value, _narrowed(ctx, rows)) + return if isinstance(node, ms.Translate | ms.WindowSum): _partition(node.partition, ctx) _amount(_amount_of(node), ctx) inner = widened(node, ctx) for operand in ms.children(node): - if not ms.carries_variable(operand): - evaluate(operand, inner) - continue - ctx.memo.used(operand) - _data_under(operand, inner) + _data_of(operand, inner) + + +def _data_of(operand: ms.Expression, ctx: Context) -> None: + """Evaluate *operand* if it holds no variable, else what it reads beside its terms.""" + if not ms.carries_variable(operand): + evaluate(operand, ctx) + return + ctx.memo.used(operand) + _data_under(operand, ctx) def _sum( @@ -366,6 +369,42 @@ def _in_region(value: Value, rows: xr.DataArray) -> Value: return as_expression(value).where(rows, 0) +def _cases(node: ms.Cases, ctx: Context) -> Value: + """ + The cases of *node* added up, each its value where it applies and a hard + zero elsewhere. + + A case's value is evaluated and checked only where it applies. A case + that applies nowhere is not evaluated, as its value is never read: a + zero over the dimensions of *node* stands in for it. + """ + parts = [ + _in_region(evaluate(region.value, _narrowed(ctx, rows)), rows) + for region, rows in _regions(node, ctx) + ] + if len(parts) < len(node.regions): + parts.append(_zeros(node, ctx)) + total = functools.reduce(lambda a, b: operator.add(*_paired(a, b)), parts) + if not ctx.solved and ms.carries_variable(node) and isinstance(total, xr.DataArray): + return operators.constant_term(ctx.model, total) + return total + + +def _regions(node: ms.Cases, ctx: Context) -> Iterator[tuple[ms.Region, xr.DataArray]]: + """The cases of *node* that apply somewhere, each with where it applies; a use of every other is consumed unread.""" + for region in node.regions: + rows = evaluate_where(region.when, ctx) + if bool(rows.any()): + yield region, rows + else: + ctx.memo.skipped(region.value) + + +def _narrowed(ctx: Context, rows: xr.DataArray) -> Context: + """*ctx* checking only the rows *rows* keeps of the ones it checks, so a case's value is refused only where it applies, as :func:`_marks` reads it.""" + return replace(ctx, rows=rows if ctx.rows is None else ctx.rows & rows) + + def _amount(amount: int | str, ctx: Context) -> operators.Amount: """An amount, a ``refused`` hole moving nothing: no live row reads it, as :func:`refuse_holes` checks.""" if not isinstance(amount, str): diff --git a/test/test_spec_builder.py b/test/test_spec_builder.py index dcfa10b9..3d2a937a 100644 --- a/test/test_spec_builder.py +++ b/test/test_spec_builder.py @@ -44,6 +44,7 @@ import linopy # noqa: E402 from linopy import Model # noqa: E402 +from linopy.constants import TERM_DIM # noqa: E402 from linopy.spec import SpecDataError, evaluate # noqa: E402 from linopy.spec.api import lower # noqa: E402 from linopy.testing import ( # noqa: E402 @@ -629,6 +630,124 @@ def test_arithmetic_is_not_refused_on_a_row_masked_out() -> None: assert int(m.constraints["row"].labels.sel(t=0)) == -1 +def guarded(row: str, cases: dict[str, Any], otherwise: Any) -> dict[str, Any]: + """:func:`quotient` with a row reading the named expression ``used``, defined by *cases*.""" + used = {"dims": ["t"], "cases": cases, "otherwise": otherwise} + return with_(quotient(row), expressions={"used": used}) + + +UNRATED = {"unrated": {"when": "a == inf", "expression": 1}} +RATED = {"rated": {"when": "a < inf", "expression": "a * b"}} + + +@pytest.mark.parametrize( + ("cases", "otherwise"), + [ + pytest.param(UNRATED, "a * b", id="otherwise"), + pytest.param(RATED, 1, id="case"), + ], +) +def test_a_case_value_is_not_refused_where_another_case_applies( + cases: dict[str, Any], otherwise: Any +) -> None: + """``a * b`` is inf * 0 on ``t=0``, which a case claims: a value is read only within its region (#1148).""" + spec = guarded("x <= used", cases, otherwise) + m = Model.from_spec(spec, INF_AT_0) + row = m.constraints["row"] + assert row.rhs.values.tolist() == [1.0, 4.0, 5.0] + assert row.coeffs.squeeze().values.tolist() == [1.0, 1.0, 1.0] + folded = evaluate(spec, m, "used", sources=INF_AT_0)["used"] + assert folded.values.tolist() == [1.0, 4.0, 5.0] + + +def test_a_case_term_is_not_refused_where_another_case_applies() -> None: + spec = guarded( + "used <= 3", {"unrated": {"when": "a == inf", "expression": "x"}}, "a * b * x" + ) + row = Model.from_spec(spec, INF_AT_0).constraints["row"] + assert row.coeffs.where(row.vars != -1).sum(TERM_DIM).values.tolist() == [1, 4, 5] + assert row.rhs.values.tolist() == [3.0, 3.0, 3.0] + + +SHIFTED_PRODUCT = "shift(a * b, along=t, offset=1, edge=0)" + + +@pytest.mark.parametrize( + ("cases", "otherwise"), + [ + pytest.param({"high": {"when": "b > 4", "expression": 1}}, "a * b", id="own"), + pytest.param( + {"pos": {"when": "b > 0", "expression": SHIFTED_PRODUCT}}, 0, id="shift" + ), + pytest.param(UNRATED, SHIFTED_PRODUCT, id="otherwise-shift"), + pytest.param( + UNRATED, "sum_back(a * b, along=t, window=2)", id="otherwise-window" + ), + ], +) +def test_a_case_value_is_refused_where_it_is_read( + cases: dict[str, Any], otherwise: Any +) -> None: + """``a * b`` is inf * 0 on ``t=0``, read there by the case that applies, or from ``t=1`` by a shift or window.""" + spec = guarded("x <= used", cases, otherwise) + with pytest.raises(SpecDataError, match=r"constraint 'row'.*not finite at t=0"): + Model.from_spec(spec, INF_AT_0) + + +@pytest.mark.parametrize( + ("row", "dims", "case", "rhs"), + [ + pytest.param( + "x <= used", + ["t"], + "sum_back(b, along=t, window=5)", + [0.0, 4.0, 5.0], + id="unread", + ), + pytest.param( + "x <= sum(used, over=s)", ["t", "s"], "y", [0.0, 8.0, 10.0], id="zero" + ), + ], +) +def test_a_case_that_applies_nowhere_is_not_evaluated( + row: str, dims: list[str], case: str, rhs: list[float] +) -> None: + """No ``b`` is negative, so a window wider than ``t`` is never read, and the case is zero over its dimensions.""" + spec = with_( + quotient(row), + dimensions={"s": {"dtype": "str"}}, + variables={"y": {"dims": ["s"], "bounds": {"lower": 0, "upper": 1}}}, + expressions={ + "used": { + "dims": dims, + "cases": {"neg": {"when": "b < 0", "expression": case}}, + "otherwise": "b", + } + }, + ) + built = Model.from_spec(spec, {**INF_AT_0, "s": GROUPS}).constraints["row"] + assert built.rhs.values.tolist() == rhs + + +def test_a_case_value_over_an_empty_dimension_is_read_within_its_region() -> None: + """A sum over a dimension without a coordinate builds no term but reads its data, a case's value within its region.""" + spec = with_( + quotient("x <= sum(used, over=s)"), + dimensions={"s": {"dtype": "int"}}, + variables={"y": {"dims": ["s"], "bounds": {"lower": 0, "upper": 1}}}, + expressions={ + "used": { + "dims": ["t", "s"], + "cases": {"unrated": {"when": "a == inf", "expression": "y"}}, + "otherwise": "a * b * y", + } + }, + ) + data = {**INF_AT_0, "s": pd.Index([], dtype=int, name="s")} + row = Model.from_spec(spec, data).constraints["row"] + assert row.rhs.values.tolist() == [0.0, 0.0, 0.0] + + GATED_ON = { "variables": { "on": {