Skip to content

Configurable overcurrent protections (per line, per side, multi-stage) - #797

Open
BDonnot wants to merge 9 commits into
dev_1.12.7from
claude/grid2op-overcurrent-refactor-tst3t6
Open

BDonnot wants to merge 9 commits into
dev_1.12.7from
claude/grid2op-overcurrent-refactor-tst3t6

Conversation

@BDonnot

@BDonnot BDonnot commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator
added removed
Python (grid2op package) +1652 -156
Tests (grid2op/tests) +897 -50
Docs + changelog + notebooks +161 -0
total +2710 -206

What

Overcurrent protections become a first-class, configurable part of the environment (grid2op.Environment.protection), replacing the hard-coded "hard / soft overflow" logic of Backend.next_grid_state.

  • Model: a Protection is placed on one side ("or" / "ex") of one powerline, with a limit in A, a delay in steps (0 = instantaneous) and an in_service status. They are stored as a structure of arrays (ProtectionConfig, read-only except in_service) with one counter per protection (ProtectionState); the trip logic is vectorized (protection_solver.py).
  • Engagement: a protection is engaged when the current on its side is above PROTECTION_THRESHOLD * limit (new parameter, SOFT_OVERFLOW_THRESHOLD becomes a deprecated alias of it). It trips when it has been engaged for strictly more than delay consecutive steps.
  • Consistency rules, checked when the protections are set, per line side: no two equal limits, no two equal delays, a higher limit needs a strictly lower delay. The protection with the lowest limit (= longest delay) is the reference protection of the side.
  • rho: rho_or / rho_ex = current / limit of the reference protection of the side (0 without protection), rho = max(rho_or, rho_ex); timestep_overflow counts the steps with rho > 1.
  • Time before trip: time_before_protection_trip (and _or / _ex, plus protection_steps_before_trip per protection) gives the number of steps before a protection disconnects the line if the currents stay the same. It is unambiguous when several delayed stages are on the same side, unlike timestep_protection_engaged (kept for compatibility: maximum counter, also per side).
  • No protection at reset: the observation returned by env.reset is the initial state of the grid, no protection acts on it whatever its delay (previously the hard overflow could disconnect lines there).
  • Configuration: env.set_protections(...), env.add_protection(...), env.set_protection_in_service(...), a protections.json file in the environment directory, carried to env.copy, obs.simulate, obs.get_forecast_env, the runner and multiprocessing.

Backward compatibility

When no protections are given, the "legacy" ones are built from the thermal limits and the parameters (legacy_from_parameters, env.init_protection_legacy()): per line, on the "or" side, a reference protection at th with delay NB_TIMESTEP_OVERFLOW_ALLOWED and an instantaneous one at HARD_OVERFLOW_THRESHOLD / PROTECTION_THRESHOLD * th (omitted when NB_TIMESTEP_OVERFLOW_ALLOWED is 0). They follow set_thermal_limit and parameter changes.

This was checked against the code before this PR on stressed scenarios over every combination of NB_TIMESTEP_OVERFLOW_ALLOWED in {0..3}, SOFT_OVERFLOW_THRESHOLD in {0.95, 1.0, 1.1}, HARD_OVERFLOW_THRESHOLD in {1.3, 2.0} and NO_OVERFLOW_DISCONNECTION on/off (line status, disc_lines, rho, timestep_overflow, timestep_protection_engaged, simulate): identical on every real step. The only differences come from the hard overflow no longer acting during env.reset.

Breaking changes / deprecations

  • [BREAKING] no protection acts during env.reset (see above); test_resest_options.test_hard_overflow is updated accordingly.
  • [BREAKING] dynamic line rating is removed from the backend: Backend.update_thermal_limit is no longer called, update_thermal_limit_from_vect does nothing. DLR belongs to the protections (out of scope here).
  • [BREAKING] Backend.set_thermal_limit does nothing (with a DeprecationWarning) once the backend has exposed its thermal limits; it still works while thermal_limit_a is unset so a backend can use it in load_grid.
  • [BREAKING] private env attributes _protection_counter, _hard_overflow_threshold, _nb_ts_max_protection_counter are replaced by _protection_config / _protection_state (the backend conformance tests in BaseBackendTest.py are updated).
  • [DEPRECATION] SOFT_OVERFLOW_THRESHOLD, HARD_OVERFLOW_THRESHOLD, NB_TIMESTEP_OVERFLOW_ALLOWED, the thermal limits (env.set_thermal_limit, the "thermal limit" reset option, thermal_limit_a, config.py thermal_limits), Backend.get_thermal_limit / get_relative_flow / get_line_overflow.

Not in this PR (TODO in the CHANGELOG)

  • Let the backend expose default protections when they are not in protections.json (as it exposes the thermal limits today): to be tracked in an issue referencing this PR.
  • Dynamic line rating as a protection with its own logic.
  • The new per-line observation attributes are copied with the observation but are not part of its vector representation (gym spaces and saved episodes unchanged).

Tests

  • New grid2op/tests/test_protections.py (config, consistency rules, reference, solver, legacy mapping, ex side, multi-stage, in_service, NO_OVERFLOW_DISCONNECTION, copy, simulate / forecast env, runner, protections.json, no protection at reset, observation attributes including the 3-stage delays 4/1/0 case).
  • Locally: the full test_generic list and the CI module groups pass on the branch before the merge of dev_1.12.7; after the merge, the protection, overflow, reset, simulator and new dispatch tests pass (164 tests).

Generated by Claude Code

BDonnot and others added 9 commits September 26, 2026 06:53
Add grid2op/Environment/protection: a frozen Protection dataclass, a
structure-of-arrays ProtectionConfig (only in_service is mutable), a
per-protection ProtectionState and a vectorized solver used by the
cascading failure loop of Backend.next_grid_state.

- protections on the "or" and/or "ex" side, any number per line, each
  with its own threshold (x thermal limit), delay and in_service status
- default config built from HARD/SOFT_OVERFLOW_THRESHOLD and
  NB_TIMESTEP_OVERFLOW_ALLOWED, rebuilt when parameters change: same
  behaviour as before
- env.set_protections / add_protection / set_protection_in_service /
  get_protections, protections.json in the env folder
- carried to env.copy, obs.simulate, obs.get_forecast_env, the runner
  and multiprocessing (pickle keeps the read only flags)
- obs.protection_counters / protection_line_id / protection_side;
  timestep_protection_engaged is the per-line max of the counters
- Backend.get_line_flow_ex and get_line_flows_both_sides
- MaskedEnvironment and EpisodeReboot migrated; old private attributes
  _protection_counter, _hard_overflow_threshold and
  _nb_ts_max_protection_counter removed

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
- the observation returned by env.reset is the initial state of the grid:
  no protection trips on it (previously the hard overflow could), whatever
  its delay, and counters start at 0
- rename default_from_parameters to legacy_from_parameters and document
  the mapping convention of the legacy parameters
- add env.init_protection_legacy(parameters=None)
- explicit tests for both

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
…tection

- a protection has a limit in A (instead of a multiplier of the thermal
  limit); it is engaged when the current on its side is above
  PROTECTION_THRESHOLD * limit
- new parameter PROTECTION_THRESHOLD; SOFT_OVERFLOW_THRESHOLD becomes a
  deprecated alias of it (still read from json files)
- protections on the same side of a powerline must be consistent: no
  equal limits, no equal delays, a higher limit needs a strictly lower
  delay (checked when the configuration is built)
- the protection with the lowest limit of each side is its reference
  protection (ProtectionConfig.reference_limits / is_reference);
  env.get_thermal_limit returns the reference limits
- legacy mapping (legacy_from_parameters) now takes the thermal limits:
  reference protection at th with delay NB_TIMESTEP_OVERFLOW_ALLOWED,
  instantaneous one at HARD / PROTECTION_THRESHOLD * th (omitted when
  NB_TIMESTEP_OVERFLOW_ALLOWED is 0), rebuilt when the thermal limits or
  the parameters change
- MaskedEnvironment adapted, tests rewritten for absolute limits

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
- rho_or / rho_ex: current divided by the limit of the reference
  protection of each side (0 on a side without protection); rho is the
  maximum of both, a_or / thermal_limit with the legacy protections
- timestep_overflow counts the consecutive steps with rho > 1 (any side)
- timestep_protection_engaged_or / _ex (per side maximum counter),
  timestep_protection_engaged is the maximum of both sides
- time_before_protection_trip (_or / _ex): steps before a protection
  disconnects the powerline if the currents stay above the limits of the
  protections currently engaged (-1 if none), unambiguous with several
  delayed stages on the same side; protection_steps_before_trip per
  protection
- the per line attributes are copied with the observation but are not
  part of its vector representation

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
- the environment no longer calls Backend.update_thermal_limit (DLR);
  Backend.update_thermal_limit_from_vect does nothing
- Backend.set_thermal_limit does nothing (with a DeprecationWarning) once
  the backend has exposed its thermal limits; grid2op no longer calls it
  (env, obs env, observation space, simulator, BackendConverter)
- CloseToOverflowReward reads the limits from the environment
- deprecation notes: env.set_thermal_limit / get_thermal_limit, the
  "thermal limit" reset option, obs.thermal_limit, Runner thermal_limit_a,
  Backend.get_thermal_limit / get_relative_flow / get_line_overflow,
  simulator update_thermal_limit; MaskedEnvironment points to
  set_protection_in_service
- backend conformance tests updated: the cascade tests set the limits on
  the environment, set_thermal_limit is checked to be a no-op

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
- docs/user/environment.rst: absolute limits, consistency rules,
  reference protection, legacy mapping, new observation attributes,
  no protection at reset, DLR removed from the backend
- CHANGELOG: added / updated / breaking / deprecation entries, and TODO
  items for backend-exposed protections and DLR as a protection

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
- test_resest_options: a line above the hard limit is not disconnected by
  env.reset anymore (no protection acts on the initial state), it is at
  the first step
- test_diff_backend: the simulator keeps the limits in its observation,
  not in its backend (Backend.set_thermal_limit is a no-op): check the
  observation limits and the rho of the predictions instead

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Benjamin Donnot <benjamin.donnot@rte-france.com>
Conflicts resolved:
- baseEnv.py: keep the protection imports next to the new dispatch
  imports (the redispatch error message now lives in
  Environment/dispatch)
- docs/user/environment.rst: keep both new sections (redispatch solver
  from dev_1.12.7, overcurrent protections from this branch)
- CHANGELOG.rst: 1.12.6 is released, the protection entries move to the
  1.12.7 section

The version markers added by this branch (versionadded / deprecated /
"since grid2op ...") are retargeted from 1.12.6 to 1.12.7.

Assisted-by: Claude Code (Claude Opus 5.5)
Claude-Session: https://claude.ai/code/session_01Rq4DmgaczxciTCF6xsR4GA
Signed-off-by: Claude <noreply@anthropic.com>
@codacy-production

codacy-production Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Not up to standards ⛔

🟢 Coverage 96.09% diff coverage · +0.26% coverage variation

Metric Results
Coverage variation ✅ +0.26% coverage variation (-1.00%)
Diff coverage ✅ 96.09% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (ad26e9b) 25969 21318 82.09%
Head commit (8130337) 26535 (+566) 21851 (+533) 82.35% (+0.26%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#797) 640 615 96.09%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

This branch has not been deployed

No deployments
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.

2 participants