Skip to content

Commit 02a71af

Browse files
committed
fix some bugs with forecasted env + add tests for the previous known connected state
Signed-off-by: DONNOT Benjamin <[email protected]>
1 parent a27c850 commit 02a71af

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Native multi agents support:
140140
- [FIXED] a bug in "obs.simulate" and "obs.get_forecast_env" : when a line was disconnected
141141
and the user tried to reconnect it (without specifying on which bus) it could do something
142142
different than "env.step" (with the same action)
143+
- [FIXED] a powerflow is run when the environment is first created even before the initial "env.step"
144+
function is called. This is to ensure proper behaviour if env is used without being reset.
143145
- [ADDED] possibility to set the "thermal limits" when calling `env.reset(..., options={"thermal limit": xxx})`
144146
- [ADDED] possibility to retrieve some structural information about elements with
145147
with `gridobj.get_line_info(...)`, `gridobj.get_load_info(...)`, `gridobj.get_gen_info(...)`

grid2op/Environment/_env_prev_state.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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.
88

9+
import copy
910
from typing import Optional, Type, Any
1011
import numpy as np
1112
from grid2op.Space import GridObjects
@@ -103,6 +104,8 @@ def update_from_backend(self,
103104

104105
def update_from_other(self,
105106
other : "_EnvPreviousState"):
107+
if not self._can_modif:
108+
raise Grid2OpException(f"Impossible to modifiy this _EnvPreviousState")
106109
for attr_nm in ["_load_p",
107110
"_load_q",
108111
"_gen_p",
@@ -115,7 +118,7 @@ def update_from_other(self,
115118
tmp = getattr(self, attr_nm)
116119
if tmp.size > 1:
117120
# works only for array of size 2 or more
118-
tmp[:] = getattr(other, attr_nm)
121+
tmp[:] = copy.deepcopy(getattr(other, attr_nm))
119122
else:
120123
setattr(self, attr_nm, getattr(other, attr_nm))
121124

grid2op/Environment/baseEnv.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,6 @@ def _has_been_initialized(self):
13801380
if np.min([self.n_line, self.n_gen, self.n_load, self.n_sub]) <= 0:
13811381
raise EnvironmentError("Environment has not been initialized properly")
13821382
self._backend_action_class = _BackendAction.init_grid(bk_type, _local_dir_cls=self._local_dir_cls)
1383-
self._backend_action = self._backend_action_class()
13841383

13851384
# initialize maintenance / hazards
13861385
self._time_next_maintenance = np.full(bk_type.n_line, -1, dtype=dt_int)
@@ -1480,20 +1479,23 @@ def _has_been_initialized(self):
14801479
if self._init_obs is None:
14811480
# regular environment, initialized from scratch
14821481
try:
1482+
self.backend.runpf(is_dc=self._parameters.ENV_DC)
14831483
self._previous_conn_state.update_from_backend(self.backend)
14841484
except Exception as exc_:
14851485
# nothing to do in this case
14861486
self.logger.warning(f"Impossible to retrieve the initial state of the grid before running the initial powerflow: {exc_}")
14871487
self._previous_conn_state._topo_vect[:] = 1 # I force assign everything to busbar 1 by default...
14881488
self._cst_prev_state_at_init = copy.deepcopy(self._previous_conn_state)
1489-
self._cst_prev_state_at_init.prevent_modification()
1489+
self._backend_action = self._backend_action_class()
14901490
else:
14911491
# environment initialized from an observation, eg forecast_env
1492+
# update the backend
1493+
self._backend_action = self.backend.update_from_obs(self._init_obs)
1494+
self._backend_action.last_topo_registered.values[:] = self._init_obs._prev_conn._topo_vect
14921495
self._cst_prev_state_at_init = copy.deepcopy(self._init_obs._prev_conn)
1493-
self._cst_prev_state_at_init.prevent_modification()
1494-
self._previous_conn_state.update_from_other(self._cst_prev_state_at_init)
1495-
# TODO this has to be done also in "_reset_to_orig_state" but I don't know why...
1496+
self._previous_conn_state.update_from_other(self._init_obs._prev_conn)
14961497

1498+
self._cst_prev_state_at_init.prevent_modification()
14971499
# update backend_action with the "last known" state
14981500
self._backend_action.last_topo_registered.values[:] = self._previous_conn_state._topo_vect
14991501

grid2op/Environment/environment.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -534,14 +534,6 @@ def _init_backend(
534534
# reset everything to be consistent
535535
self._reset_vectors_and_timings()
536536

537-
# 1.11 and detachement: forecasted env
538-
if self._init_obs is not None:
539-
# update the backend
540-
self._backend_action = self.backend.update_from_obs(self._init_obs)
541-
self._backend_action.last_topo_registered.values[:] = self._init_obs._prev_conn._topo_vect
542-
self._cst_prev_state_at_init.force_update(self._init_obs._prev_conn)
543-
self._previous_conn_state.update_from_other(self._init_obs._prev_conn)
544-
545537
def max_episode_duration(self):
546538
"""
547539
Return the maximum duration (in number of steps) of the current episode.

0 commit comments

Comments
 (0)