From b3c9ab28b1226f051d060d4f7e95c7aff3e57a66 Mon Sep 17 00:00:00 2001 From: DONNOT Benjamin Date: Thu, 19 Oct 2023 17:18:37 +0200 Subject: [PATCH] adressing issue #66, now reproduced --- lightsim2grid/lightSimBackend.py | 8 ++++++-- lightsim2grid/tests/test_dist_slack_backend.py | 2 +- lightsim2grid/tests/test_issue_66.py | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lightsim2grid/lightSimBackend.py b/lightsim2grid/lightSimBackend.py index 2d502d9..6cb4945 100644 --- a/lightsim2grid/lightSimBackend.py +++ b/lightsim2grid/lightSimBackend.py @@ -820,8 +820,12 @@ def apply_action(self, backendAction): raise BackendError(f"{exc_}") if self.__has_storage: - self._grid.update_storages_p(backendAction.storage_power.changed, - backendAction.storage_power.values) + try: + self._grid.update_storages_p(backendAction.storage_power.changed, + backendAction.storage_power.values) + except RuntimeError as exc_: + # modification of power of disconnected storage has no effect in lightsim2grid + pass # handle shunts if type(self).shunts_data_available: diff --git a/lightsim2grid/tests/test_dist_slack_backend.py b/lightsim2grid/tests/test_dist_slack_backend.py index 5025a5c..966c0ea 100644 --- a/lightsim2grid/tests/test_dist_slack_backend.py +++ b/lightsim2grid/tests/test_dist_slack_backend.py @@ -98,7 +98,7 @@ def test_after_runner(self): runner_ds = Runner(**self.env_ds.get_params_for_runner()) res_ss = runner_ss.run(nb_episode=1, max_iter=self.max_iter_real, add_detailed_output=True) res_ds = runner_ds.run(nb_episode=1, max_iter=self.max_iter_real, add_detailed_output=True) - assert res_ss[0][3] == res_ds[0][3] # same number of steps survived + assert res_ss[0][3] == res_ds[0][3], f"{res_ss[0][3]} vs {res_ds[0][3]}" # same number of steps survived assert res_ss[0][2] != res_ds[0][2] # not the same reward ep_ss = res_ss[0][-1] ep_ds = res_ds[0][-1] diff --git a/lightsim2grid/tests/test_issue_66.py b/lightsim2grid/tests/test_issue_66.py index aa41873..3804613 100644 --- a/lightsim2grid/tests/test_issue_66.py +++ b/lightsim2grid/tests/test_issue_66.py @@ -9,6 +9,7 @@ import unittest import warnings from lightsim2grid import LightSimBackend +from grid2op.Action import PlayableAction import grid2op class Issue66Tester(unittest.TestCase): @@ -16,7 +17,8 @@ class Issue66Tester(unittest.TestCase): def setUp(self) -> None: with warnings.catch_warnings(): warnings.filterwarnings("ignore") - self.env = grid2op.make("l2rpn_case14_sandbox", test=True, backend=LightSimBackend()) + self.env = grid2op.make("educ_case14_storage", test=True, backend=LightSimBackend(), + action_class=PlayableAction) return super().setUp() def tearDown(self) -> None: @@ -67,6 +69,20 @@ def test_change_bus_gen(self): obs, reward, done, info = self.env.step(act) assert done + def test_disco_storage(self): + """test i can disconnect a storage unit""" + obs = self.env.reset() + act = self.env.action_space({"set_bus": {"storages_id": [(0, -1)]}}) + obs, reward, done, info = self.env.step(act) + assert not done + # should not raise any RuntimeError + + act = self.env.action_space({"storage_p": [(0, -1)]}) + obs, reward, done, info = self.env.step(act) + assert not done + # should not raise any RuntimeError + + if __name__ == "__main__": unittest.main() \ No newline at end of file