-
Notifications
You must be signed in to change notification settings - Fork 79
feat(piecewise): partial active gate support via active_gate helper (#796)
#797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """ | ||
| Legacy helper for padding a partial piecewise ``active`` gate. | ||
|
|
||
| This module is a temporary stopgap. Under the planned v1 arithmetic | ||
| semantics (#717) the bare idiom ``active.reindex(coords).fillna(fill_value)`` | ||
| is correct on its own, so :func:`active_gate` is expected to be deprecated | ||
| and this file removed once v1 lands. Keeping it isolated makes that | ||
| removal a single-file delete. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from linopy.piecewise import _to_linexpr, _warn_evolving_api | ||
|
|
||
| if TYPE_CHECKING: | ||
| from linopy.expressions import LinearExpression | ||
| from linopy.types import LinExprLike | ||
|
|
||
|
|
||
| def active_gate( | ||
| active: LinExprLike, | ||
| coords: Mapping[Any, Any], | ||
| fill_value: float = 1, | ||
| ) -> LinearExpression: | ||
| r""" | ||
| Pad a partial ``active`` gate to full coverage for piecewise gating. | ||
|
|
||
| Reindexes ``active`` to ``coords`` and fills missing/masked entries with | ||
| ``fill_value`` (``1`` = always active, ``0`` = always off), so a gate | ||
| defined over only a subset of :meth:`~linopy.Model.add_piecewise_formulation`'s | ||
| coordinate does not force the uncovered entries to zero. Equivalent to the | ||
| v1 idiom ``active.reindex(coords).fillna(fill_value)`` but correct under | ||
| legacy too (see the module docstring). | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| gate = active_gate(status, {"component": components}) | ||
| m.add_piecewise_formulation((power, xs), (fuel, ys), active=gate) | ||
|
|
||
| Parameters | ||
| ---------- | ||
| active : Variable or LinearExpression | ||
| The (possibly partial) gate expression. | ||
| coords : mapping of dim to labels | ||
| Reindex target, passed straight to ``reindex``; unlisted dims | ||
| broadcast. | ||
| fill_value : float, default 1 | ||
| Value for missing/masked entries (``1`` = on, ``0`` = off). | ||
|
|
||
| Returns | ||
| ------- | ||
| LinearExpression | ||
| The padded gate, suitable to pass as ``active=``. | ||
|
|
||
| Warns | ||
| ----- | ||
| EvolvingAPIWarning | ||
| Part of the evolving piecewise API; may be refined. | ||
| """ | ||
| _warn_evolving_api( | ||
| "active_gate", | ||
| "piecewise: active_gate is a new API; its signature and the way it " | ||
| "resolves missing/masked entries may be refined in minor releases. " | ||
| "It is primarily a legacy stopgap and may be removed once legacy " | ||
| "semantics are dropped. This warning fires once per session; " | ||
| "silence with " | ||
| '`warnings.filterwarnings("ignore", category=linopy.EvolvingAPIWarning)`.', | ||
| ) | ||
| gate = _to_linexpr(active).reindex(coords) | ||
| term_dims = [d for d in gate.vars.dims if d not in gate.coord_dims] | ||
| present = (gate.vars >= 0).any(term_dims) | ||
| return gate.where(present, fill_value) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.