-
Notifications
You must be signed in to change notification settings - Fork 4
fix: extra output comparison #240
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
Merged
+144
−4
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d902167
Support comparison operators in extra-output expressions (#237)
claude 77cb379
Revert version bump, document extra-output comparison fix in changelog
claude e095198
Apply suggestion from @aoustry
aoustry 58c4f3d
Apply suggestion from @aoustry
aoustry 25fab1c
fix: filter solution/dual arrays to own model components in extra out…
aoustry 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -162,3 +162,97 @@ def test_extra_output_abs_round_on_variable() -> None: | |||||
| ) | ||||||
| assert abs_shift == pytest.approx(2.3) | ||||||
| assert rounded == pytest.approx(3.0) | ||||||
|
|
||||||
|
|
||||||
| def test_extra_output_min_on_variable() -> None: | ||||||
| """ | ||||||
| min(variable, parameter) is allowed in extra outputs and evaluated | ||||||
| element-wise post-solve. | ||||||
| """ | ||||||
| from gems.expression import param, var | ||||||
| from gems.expression.expression import literal, minimum | ||||||
| from gems.model.model import model | ||||||
| from gems.model.parameter import float_parameter | ||||||
| from gems.model.variable import float_variable | ||||||
| from gems.simulation import TimeBlock, build_problem | ||||||
| from gems.study import ConstantData, DataBase, Study, System, create_component | ||||||
|
|
||||||
| SIMPLE_MODEL = model( | ||||||
| id="SIMPLE_MIN", | ||||||
| parameters=[float_parameter("cap")], | ||||||
| variables=[float_variable("a", lower_bound=literal(5), upper_bound=literal(5))], | ||||||
| extra_outputs={"capped": minimum(var("a"), param("cap"))}, | ||||||
| ) | ||||||
|
|
||||||
| database = DataBase() | ||||||
| comp = create_component(model=SIMPLE_MODEL, id="comp_1") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| database.add_data("comp_1", "cap", ConstantData(3.0)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| OTHER_MODEL = model( | ||||||
| id="OTHER_MODEL", | ||||||
| parameters=[float_parameter("p")], | ||||||
| variables=[float_variable("b", lower_bound=literal(1), upper_bound=literal(1))], | ||||||
| ) | ||||||
| other_comp = create_component(model=OTHER_MODEL, id="comp_2") | ||||||
| database.add_data("comp_2", "p", ConstantData(0.0)) | ||||||
|
|
||||||
| system = System("test_min_extra") | ||||||
| system.add_component(comp) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| system.add_component(other_comp) | ||||||
|
|
||||||
| problem = build_problem( | ||||||
| Study(system, database), TimeBlock(1, [0]), scenario_ids=list(range(1)) | ||||||
| ) | ||||||
| problem.solve(solver_name="highs") | ||||||
|
|
||||||
| df = SimulationTableBuilder().build(problem) | ||||||
| capped = ( | ||||||
| df.component("comp_1").output("capped").value(time_index=0, scenario_index=0) | ||||||
| ) | ||||||
| # min(a=5, cap=3) == 3 | ||||||
| assert capped == pytest.approx(3.0) | ||||||
|
|
||||||
|
|
||||||
| def test_extra_output_comparison() -> None: | ||||||
| """ | ||||||
| Comparison operators (>=, <=) are allowed in extra outputs and evaluated | ||||||
| post-solve as a float indicator: 1.0 where the condition holds, 0.0 | ||||||
| otherwise. Previously these raised NotImplementedError. | ||||||
| """ | ||||||
| from gems.expression import param, var | ||||||
| from gems.expression.expression import Comparator, ComparisonNode, literal | ||||||
| from gems.model.model import model | ||||||
| from gems.model.parameter import float_parameter | ||||||
| from gems.model.variable import float_variable | ||||||
| from gems.simulation import TimeBlock, build_problem | ||||||
| from gems.study import ConstantData, DataBase, Study, System, create_component | ||||||
|
|
||||||
| SIMPLE_MODEL = model( | ||||||
| id="SIMPLE_CMP", | ||||||
| parameters=[float_parameter("threshold")], | ||||||
| variables=[float_variable("a", lower_bound=literal(5), upper_bound=literal(5))], | ||||||
| extra_outputs={ | ||||||
| # a (=5) >= threshold (=3) -> 1.0 | ||||||
| "ge": ComparisonNode(var("a"), param("threshold"), Comparator.GREATER_THAN), | ||||||
| # a (=5) <= threshold (=3) -> 0.0 | ||||||
| "le": ComparisonNode(var("a"), param("threshold"), Comparator.LESS_THAN), | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
| database = DataBase() | ||||||
| comp = create_component(model=SIMPLE_MODEL, id="comp_1") | ||||||
| database.add_data("comp_1", "threshold", ConstantData(3.0)) | ||||||
|
|
||||||
| system = System("test_comparison_extra") | ||||||
| system.add_component(comp) | ||||||
|
|
||||||
| problem = build_problem( | ||||||
| Study(system, database), TimeBlock(1, [0]), scenario_ids=list(range(1)) | ||||||
| ) | ||||||
| problem.solve(solver_name="highs") | ||||||
|
|
||||||
| df = SimulationTableBuilder().build(problem) | ||||||
| ge = df.component("comp_1").output("ge").value(time_index=0, scenario_index=0) | ||||||
| le = df.component("comp_1").output("le").value(time_index=0, scenario_index=0) | ||||||
| assert ge == pytest.approx(1.0) | ||||||
| assert le == pytest.approx(0.0) | ||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.