Skip to content

Commit af1a3a0

Browse files
committed
fixing issue #713
Signed-off-by: DONNOT Benjamin <[email protected]>
1 parent b905090 commit af1a3a0

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

grid2op/Environment/environment.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# you can obtain one at http://mozilla.org/MPL/2.0/.
66
# SPDX-License-Identifier: MPL-2.0
77
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
8+
import datetime
89
import os
910
import copy
1011
import warnings
@@ -413,13 +414,22 @@ def _init_backend(
413414
names_chronics_to_backend = self.backend.names_target_to_source
414415

415416
self.chronics_handler = chronics_handler
417+
if self._init_obs is not None:
418+
# env init from an observation, I must update date and time
419+
# from the chronics handler
420+
delta_t_step = datetime.timedelta(minutes=float(self._init_obs.delta_time))
421+
init_dt = self._init_obs.get_time_stamp() - delta_t_step
422+
self.chronics_handler._real_data.start_datetime = init_dt
423+
self.chronics_handler._real_data.current_datetime = init_dt
424+
self.chronics_handler._real_data.time_interval = delta_t_step
416425
self.chronics_handler.initialize(
417426
self.name_load,
418427
self.name_gen,
419428
self.name_line,
420429
self.name_sub,
421430
names_chronics_to_backend=names_chronics_to_backend,
422431
)
432+
423433
# new in grdi2op 1.10.2: used
424434
self.chronics_handler.action_space = self._helper_action_env
425435
self._names_chronics_to_backend = names_chronics_to_backend
@@ -1381,7 +1391,6 @@ def reset(self,
13811391
else:
13821392
# reset previous max iter to value set with `env.set_max_iter(...)` (or -1 by default)
13831393
self.chronics_handler._set_max_iter(self._max_iter)
1384-
13851394
self.chronics_handler.next_chronics()
13861395
self.chronics_handler.initialize(
13871396
self.backend.name_load,

grid2op/Observation/baseObservation.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5029,9 +5029,17 @@ def _make_env_from_arays(self,
50295029
load_q=load_q,
50305030
prod_p=prod_p,
50315031
prod_v=prod_v,
5032-
maintenance=maintenance)
5032+
maintenance=maintenance,
5033+
)
50335034
ch.max_iter = ch.real_data.max_iter
50345035

5036+
5037+
delta_t_step = datetime.timedelta(minutes=float(self.delta_time))
5038+
init_dt = self.get_time_stamp() - delta_t_step
5039+
ch._real_data.start_datetime = init_dt
5040+
ch._real_data.current_datetime = init_dt
5041+
ch._real_data.time_interval = delta_t_step
5042+
50355043
backend = self._obs_env.backend.copy_public()
50365044
backend._is_loaded = True
50375045
nb_highres_called = self._obs_env.highres_sim_counter.nb_highres_called

grid2op/tests/test_issue_713.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) 2025, RTE (https://www.rte-france.com)
2+
# See AUTHORS.txt and https://github.com/Grid2Op/grid2op/pull/319
3+
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
4+
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
5+
# you can obtain one at http://mozilla.org/MPL/2.0/.
6+
# SPDX-License-Identifier: MPL-2.0
7+
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
8+
9+
import warnings
10+
import grid2op
11+
from grid2op.Action import BaseAction
12+
import unittest
13+
14+
15+
class TestIssue713(unittest.TestCase):
16+
def setUp(self) -> None:
17+
with warnings.catch_warnings():
18+
warnings.filterwarnings("ignore")
19+
self.env = grid2op.make("educ_case14_storage",
20+
test=True,
21+
action_class=BaseAction,
22+
_add_to_name=type(self).__name__)
23+
self.init_obs = self.env.reset(seed=0, options={"time serie id":0})
24+
return super().setUp()
25+
26+
def test_proper_date_time_after_reset(self):
27+
for_env = self.init_obs.get_forecast_env()
28+
for_obs = for_env.reset()
29+
self.assertEqual(for_obs.hour_of_day, 0)
30+
self.assertEqual(for_obs.minute_of_hour, 0)
31+
self.assertEqual(for_obs.year, 2019)
32+
self.assertEqual(for_obs.day, 12)
33+
self.assertEqual(for_obs.day_of_week, 5)
34+
35+
def test_proper_date_time_after_step(self):
36+
for_env = self.init_obs.get_forecast_env()
37+
for_obs = for_env.reset()
38+
for_obs, *_ = for_env.step(self.env.action_space())
39+
self.assertEqual(for_obs.hour_of_day, 0)
40+
self.assertEqual(for_obs.minute_of_hour, 5)
41+
self.assertEqual(for_obs.year, 2019)
42+
self.assertEqual(for_obs.day, 12)
43+
self.assertEqual(for_obs.day_of_week, 5)
44+
45+
def test_proper_date_time_wuithout_reset(self):
46+
for_env = self.init_obs.get_forecast_env()
47+
for_obs = for_env.get_obs()
48+
self.assertEqual(for_obs.hour_of_day, 0)
49+
self.assertEqual(for_obs.minute_of_hour, 0)
50+
self.assertEqual(for_obs.year, 2019)
51+
self.assertEqual(for_obs.day, 12)
52+
self.assertEqual(for_obs.day_of_week, 5)
53+
54+
for_obs, *_ = for_env.step(self.env.action_space())
55+
self.assertEqual(for_obs.hour_of_day, 0)
56+
self.assertEqual(for_obs.minute_of_hour, 5)
57+
self.assertEqual(for_obs.year, 2019)
58+
self.assertEqual(for_obs.day, 12)
59+
self.assertEqual(for_obs.day_of_week, 5)
60+
61+
62+
if __name__ == "__main__":
63+
unittest.main()

grid2op/typing_variables.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"hazards",
4242
"maintenance",
4343
"shunt",
44-
"detach_load"],
44+
"detach_load",
45+
"detach_gen",
46+
"detach_storage"],
4547
Any]
4648
# TODO improve that (especially the Any part)
4749

0 commit comments

Comments
 (0)