Skip to content

Enable complex variable bounds expressions, thanks to operator expansion#191

Open
aoustry wants to merge 7 commits intomainfrom
claude/expand-variable-bounds-PkM2W
Open

Enable complex variable bounds expressions, thanks to operator expansion#191
aoustry wants to merge 7 commits intomainfrom
claude/expand-variable-bounds-PkM2W

Conversation

@aoustry
Copy link
Copy Markdown
Collaborator

@aoustry aoustry commented Mar 26, 2026

Summary

Enhanced variable bound handling in the optimization module to properly expand operators and validate that bounds are constant expressions without decision variables. The objective is to enable the treatment of complex operators (TimeShift, AllTimeSum) inside bounds expression of variables. Complex E2E tests, using AntaresLegacy Thermal Cluster model, are added.

Key Changes

  • Added expand_operators() calls after instantiating lower and upper bound expressions to ensure all operators are properly resolved before evaluation
  • Replaced _compute_expression_value() calls with linearize_expression() for bound evaluation, which provides better control over expression analysis
  • Added validation to ensure variable bounds are constant expressions (no decision variable terms), raising descriptive ValueError messages when non-constant bounds are detected
  • Updated both lower and upper bound processing to follow the same validation pattern
  • E2E tests added.

Implementation Details

  • The operator expansion step ensures that complex bound expressions are simplified before linearization
  • Using linearize_expression() instead of direct value computation allows us to inspect the expression structure via terms and constant attributes
  • Validation checks that ub_linear.terms and lb_linear.terms are empty, ensuring bounds contain no decision variables
  • Error messages clearly indicate which variable has invalid bounds and what the constraint is

claude and others added 7 commits March 26, 2026 19:09
…tion

TimeShiftNode (and other time operators) in bound expressions caused
NotImplementedError when EvaluationVisitor was called directly.

Apply the same expand_operators + linearize_expression pattern already
used for constraint expressions:
- In _create_variables: expand bound expressions after instantiation, then
  evaluate via linearize_expression().constant instead of
  _compute_expression_value (which uses EvaluationVisitor, incapable of
  handling ProblemParameterNode produced by expansion).
- In _create_constraint: same fix for constraint lower/upper bounds,
  addressing the pre-existing TODO comment about time operators in bounds.

Both sites now also guard against bounds that contain decision-variable
terms, which would be a modelling error.

https://claude.ai/code/session_01YBA2zGVryRxA9DdGk1kvFv
Undo the change to _create_constraint — constraint bounds evaluation
should remain as-is using _compute_expression_value.
Only variable bounds need the expand + linearize treatment.

https://claude.ai/code/session_01YBA2zGVryRxA9DdGk1kvFv
- Add missing blank lines between fixture definitions (2 required by Black)
- Remove blank line between @pytest.mark.parametrize decorator and function definition
- Reformat long line in ref_gen3 assignment to fit within 88-char limit
- Strip trailing whitespace from blank lines inside function body

https://claude.ai/code/session_01QnDQUdSWzeHFXweSAzkQ8h
Reformat multi-line assignment of is_in_objective to satisfy Black's
style requirements.

https://claude.ai/code/session_01QnDQUdSWzeHFXweSAzkQ8h
Black 23.7.0 (used in CI) differs from local version:
- optimization.py: revert is_in_objective assignment to subscript-style multi-line format
- test_operators_v1.py: remove blank line after function signature before body

https://claude.ai/code/session_01QnDQUdSWzeHFXweSAzkQ8h
@aoustry aoustry changed the title Improve variable bound validation with operator expansion Enable complex variable bounds expressions, thanks to operator expansion Mar 27, 2026
@aoustry aoustry marked this pull request as ready for review March 27, 2026 12:30
@aoustry aoustry requested a review from tbittar March 27, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants