Skip to content

fix: compensate the load injections of an action in the dispatch (#777) - #790

Merged
BDonnot merged 2 commits into
Grid2op:dev_1.12.7from
fahad-ee:fix/777-load-injection-dispatch
Nov 13, 2026
Merged

BDonnot merged 2 commits into
Grid2op:dev_1.12.7from
fahad-ee:fix/777-load-injection-dispatch

Conversation

@fahad-ee

@fahad-ee fahad-ee commented Nov 9, 2026 •

Copy link
Copy Markdown
added removed
Python (grid2op package) +143 -6
Tests (grid2op/tests) +469 -1
Docs + changelog + notebooks +9 -0
total +621 -7

Fixes #777

What was wrong

An action that sets load_p through its "injection" key changed the grid, but the change was never declared to the dispatch. The slack generator silently took it, without any pmax or ramp check, and without an error.

What this PR does

  • Adds a "load" source to dispatch_contributions (new field load_injection_mw in RedispatchState).
  • New LoadInjectionModule computes it from the difference between the loads set by the action and the loads of the time series. Like storage and detachment, the solver gets the change since the previous step, so the generators come back to normal when the injection stops.
  • Loads that the time series does not set keep their injected value, so a persistent injection stays compensated (per load state load_injection_excess).
  • Detached loads and nan entries (not modified) are ignored.
  • _ObsEnv treats all loads as set by the forecasts.
  • FeasibilityGuard also counts this source.
  • If the generators cannot follow, the episode ends with ImpossibleRedispatching.

Behavior notes (questions for the maintainers)

  • This can turn some previously accepted load injections into a game over. I did not add a parameter to disable it. Is that acceptable?
  • Grids without redispatching data and without storage units are unchanged.
  • prod_p injections are out of scope.
  • The changelog entry is under a new [1.12.7] section because the top section is already dated. Please tell me if it should go elsewhere.

Tests

  • New test_issue_777.py (14 tests): the reproduction of the issue, increase and decrease, ramp and pmax limits, too large increase ends the episode, dispatch returns to zero, persistent injection, detached load.
  • 3 of the main tests fail without the fix.
  • test_redispatch_solver.py is updated for the new source.
  • On Linux, 219 existing related tests (redispatch, curtailment, storage, detachment, simulate, forecast) pass.
  • I did not run the full CI suite locally.

…d2op#777)

The change of load_p made by an action through its injection key is now declared to the dispatch as a new load source of imbalance. The generators compensate it within pmax and ramps, and the episode ends with ImpossibleRedispatching if they cannot follow. Adds a regression test and a changelog entry.

Fixes Grid2op#777

Signed-off-by: Engr. Muhammad Fahad <muhammadfahadx2.0@gmail.com>
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

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.

@BDonnot

BDonnot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Hello,

Thanks for helping me fix this issue 😊

I'll review and check why a bit complains.

Best

Benjamin

@BDonnot BDonnot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If feasible I think supporting the forecasts (ob's.simulate and ob's.get_forecast_env()) would je really nice.

Also I would like to see runner and env.copy tested on the python tests.

Let me know if something is blocking.

Thanks

Comment thread grid2op/Environment/baseEnv.py Outdated
"""
Loads that have a new active consumption in the time series this step.

NB this is overidden in _ObsEnv where the consumption of all the loads is read from the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this feature of changing the load does not work in obs.simulate(...) ?

from grid2op.Exceptions import ImpossibleRedispatching


class TestIssue777LoadInjectionDispatch(unittest.TestCase):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall for this whole file, the obs.simulate is not tested.
It would also be good to include small test for:

  • copied environment: for example make a load change before,m the copy, copy, then a) a normal action behaves normally b) another reduce the load action behaves normally. Another example would be to make sure that after a copy a load modification is properly implemented in the new env. Properly implemented here means "lead to the same results as in the environment not copied". Another test is also to make sure the initial env and the copy are not sharing any load modification data
  • runner (here it's pretty simple: make a runner, an agent that does some load modification at each step and check the results are correct with the same agent on the same env)
  • obs.get_forecast_env() (check the env behaves normally, you might need l2rpn_idf_2023 for this test otherwise the forecast env is pretty limited (only one step) )

Beside that it looks well covered.

Thanks

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I added tests for all the cases you listed:

  • obs.simulate: increase, decrease, too large change (ImpossibleRedispatching), the real env is not modified by a simulate, and a real injection followed by a simulated do nothing step.
  • env.copy(): load change before the copy, then a normal action and a reduce load action give the same results as in the original env, a new load change gives the same result, a too large change ends the copy, and the injection data is not shared (modified in one, unchanged in the other).
  • Runner: an agent that changes a load at each step; the dispatch follows the load change at each step, and the results (rewards, dispatch) are the same as a loop on the env.
  • obs.get_forecast_env() on l2rpn_idf_2023: same checks, plus the dispatch coming back after the injection stops.
    Please tell me if you want other cases.

…injections (Grid2op#777)

Load injections also work in obs.simulate, in copied environments, with the Runner and in the forecast environment. Clarify the comments about simulate and add the tests asked in the review.

Signed-off-by: Engr. Muhammad Fahad <muhammadfahadx2.0@gmail.com>

@BDonnot BDonnot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@BDonnot
BDonnot merged commit ad26e9b into Grid2op:dev_1.12.7 Nov 13, 2026
19 of 21 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.

2 participants