Skip to content

Complementary devs for floor, ceil, min, and max operations#186

Merged
aoustry merged 4 commits intomainfrom
claude/implement-operator-visitors-olesZ
Mar 24, 2026
Merged

Complementary devs for floor, ceil, min, and max operations#186
aoustry merged 4 commits intomainfrom
claude/implement-operator-visitors-olesZ

Conversation

@aoustry
Copy link
Copy Markdown
Collaborator

@aoustry aoustry commented Mar 24, 2026

Summary

This PR extends expression support in two areas:

  1. Parsing – adds visitor methods for two grammar rules (TimeShiftExpr and TimeIndexExpr) that were previously unhandled, enabling syntax such as (expr)[t-1] and (expr)[1].
  2. Linearization – makes floor, ceil, min, and max operators usable inside linear expressions when all their operands are constant, instead of always raising an error.

Changes

src/gems/expression/parsing/parse_expression.py

  • visitTimeShiftExpr: handles (expr)[t±N] — applies a time shift to an arbitrary expression node; simplifies to the expression itself when the shift is 0.
  • visitTimeIndexExpr: handles (expr)[k] — evaluates an expression at an absolute time index.

src/gems/simulation/linearize.py

  • floor / ceil: visit the operand; if it is purely constant, return math.floor / math.ceil of its value; raise ValueError otherwise.
  • maximum / minimum: visit all operands; if none contain variable terms, return the constant max / min; raise ValueError otherwise.
  • Added import math.

Test coverage

  • tests/unittests/expressions/parsing/test_expression_parsing.py: four new parametrized cases covering [t-1], [t], [t-1] with ceil, and [1].
  • tests/unittests/expressions/visitor/test_linearization.py: eight new tests — constant floor/ceil/max/min succeed, variable floor/ceil/max/min raise with message matching "non-constant".

Test plan

  • pytest tests/unittests/expressions/parsing/test_expression_parsing.py
  • pytest tests/unittests/expressions/visitor/test_linearization.py

claude added 4 commits March 24, 2026 16:09
These operators are valid in a linear expression when all operands are
constant. Previously they raised unconditionally; now they evaluate to
a constant result, and only raise ValueError when a non-constant
(variable-containing) operand is present.

https://claude.ai/code/session_01DL3tB7dnWvHsB8dNYBBNxZ
…itor

Adds support for parenthesized-expression time-shift/index syntax
such as `(ceil(p/q))[t-1]`, which previously failed to parse because
the ANTLR visitor lacked handlers for the timeShiftExpr and
timeIndexExpr grammar rules. The new methods mirror the existing
visitTimeShift and visitTimeIndex logic, using ctx.expr().accept(self)
instead of _convert_identifier() to obtain the base expression.

https://claude.ai/code/session_01So7yDQ3KiPj8oDa4xYnhUK
@aoustry aoustry marked this pull request as ready for review March 24, 2026 16:40
@aoustry aoustry changed the title Allow linearization of constant floor, ceil, min, and max operations Complementary devs for floor, ceil, min, and max operations Mar 24, 2026
@aoustry aoustry merged commit bd319d8 into main Mar 24, 2026
2 checks passed
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