Skip to content

Commit b072657

Browse files
committed
fix some issues in the tests
Signed-off-by: DONNOT Benjamin <[email protected]>
1 parent 89d75b1 commit b072657

File tree

4 files changed

+64
-43
lines changed

4 files changed

+64
-43
lines changed

grid2op/Action/_backendAction.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,14 @@ def set_redispatch(self, new_redispatching):
669669
This is called by the environment, do not alter.
670670
"""
671671
self.prod_p.change_val(new_redispatching)
672+
673+
def set_storage(self, new_storage):
674+
"""
675+
.. warning:: /!\\\\ Internal, do not use unless you know what you are doing /!\\\\
676+
677+
This is called by the environment, do not alter.
678+
"""
679+
self.storage_power.set_val(new_storage)
672680

673681
def _aux_iadd_inj(self, dict_injection):
674682
"""

grid2op/Environment/baseEnv.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,7 @@ def _aux_apply_redisp(self,
31033103
gen_curtailed: np.ndarray,
31043104
except_: List[Exception],
31053105
powerline_status):
3106+
cls = type(self)
31063107
is_illegal_redisp = False
31073108
is_done = False
31083109
is_illegal_reco = False
@@ -3121,12 +3122,12 @@ def _aux_apply_redisp(self,
31213122
action.reset_cache_topological_impact()
31223123
action = self._action_space({})
31233124
_ = action.get_topological_impact(powerline_status, _store_in_cache=True, _read_from_cache=False)
3124-
if type(self).dim_alerts:
3125+
if cls.dim_alerts:
31253126
action.raise_alert = orig_action.raise_alert
31263127
is_illegal_redisp = True
31273128
except_.append(except_tmp)
31283129

3129-
if type(self).n_storage > 0:
3130+
if cls.n_storage > 0:
31303131
# TODO curtailment: cancel it here too !
31313132
self._storage_current_charge[:] = self._storage_previous_charge
31323133
self._amount_storage -= self._amount_storage_prev
@@ -3155,7 +3156,7 @@ def _aux_apply_redisp(self,
31553156
action.reset_cache_topological_impact()
31563157
res_action = self._action_space({})
31573158
_ = res_action.get_topological_impact(powerline_status, _store_in_cache=True, _read_from_cache=False)
3158-
if type(self).dim_alerts:
3159+
if cls.dim_alerts:
31593160
res_action.raise_alert = action.raise_alert
31603161
is_illegal_redisp = True
31613162
except_.append(except_tmp)
@@ -3179,7 +3180,7 @@ def _aux_apply_redisp(self,
31793180
action.reset_cache_topological_impact()
31803181
res_action = self._action_space({})
31813182
_ = res_action.get_topological_impact(powerline_status, _store_in_cache=True, _read_from_cache=False)
3182-
if type(self).dim_alerts:
3183+
if cls.dim_alerts:
31833184
res_action.raise_alert = action.raise_alert
31843185
except_.append(except_tmp)
31853186
else:
@@ -3196,19 +3197,22 @@ def _aux_update_backend_action(self,
31963197
# the environment must make sure it's a zero-sum action.
31973198
# same kind of limit for the storage
31983199
res_exc_ = None
3199-
# cancel the redisp tag (storage should be kept for topology)
3200+
# cancel the redisp and storage tags (set later in the code)
32003201
tag_redisp = action._modif_redispatch
3202+
tag_storage = action._modif_storage
32013203
action._modif_redispatch = False
3204+
action._modif_storage = False
32023205
# cancel the values
3203-
action._redispatch[:] = 0.0 # redispatch is added in _aux_apply_redisp a bit later in the code
3204-
action._storage_power[:] = self._storage_power
3206+
action._redispatch[:] = 0.0 # redispatch is added after everything in the code (even after the opponent)
3207+
action._storage_power[:] = 0.0 # storage is also added after everything
32053208
# add the action
32063209
self._backend_action += action
32073210
# put initial value
32083211
action._storage_power[:] = action_storage_power
32093212
action._redispatch[:] = init_disp
32103213
# put back the tags
32113214
action._modif_redispatch = tag_redisp
3215+
action._modif_storage = tag_storage
32123216
return res_exc_
32133217

32143218
def _update_alert_properties(self, action, lines_attacked, subs_attacked):
@@ -3689,6 +3693,7 @@ def step(self, action: BaseAction) -> Tuple[BaseObservation,
36893693
# TODO storage: check the original action, even when replaced by do nothing is not modified
36903694
self._backend_action += self._env_modification
36913695
self._backend_action.set_redispatch(self._actual_dispatch)
3696+
self._backend_action.set_storage(self._storage_power)
36923697

36933698
# now get the new generator voltage setpoint
36943699
voltage_control_act = self._voltage_control(action, prod_v_chronics)

grid2op/tests/test_detached_properly_updated.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ def setUp(self) -> None:
2222
test=True,
2323
allow_detachment=True,
2424
action_class=CompleteAction)
25-
self.env.reset(seed=0, options={"time serie id": 0})
26-
obs = self.env.reset()
25+
parameters = self.env.parameters
26+
parameters.ENV_DOES_REDISPATCHING = False
27+
self.env.change_parameters(parameters)
28+
self.env.change_forecast_parameters(parameters)
29+
# values are hard coded for time serie id = 1, do not modify
30+
self.obs = self.env.reset(seed=0, options={"time serie id": 1})
2731
return super().setUp()
2832

2933
def tearDown(self) -> None:
@@ -38,31 +42,34 @@ def test_disco_gen(self):
3842
"set_bus": {"generators_id": [(gen_id, -1)]}
3943
}
4044
))
41-
assert not done
45+
assert not done, info["exception"]
46+
assert not info["exception"]
4247
assert obs.gen_detached[gen_id]
4348
assert obs.gen_p[gen_id] == 0.
4449
assert obs.gen_v[gen_id] == 0.
4550
assert obs.gen_q[gen_id] == 0.
4651
assert obs.gen_theta[gen_id] == 0., f"{obs.gen_theta[gen_id]} vs 0."
47-
assert abs(obs.gen_p_detached[gen_id] - 79.8) <= 1e-5
52+
assert abs(obs.gen_p_detached[gen_id] - 79.8) <= 1e-5, f"{obs.gen_p_detached[gen_id]} vs {79.8}"
4853

4954
obs1, _, done, info = self.env.step(self.env.action_space({}))
50-
assert not done
55+
assert not done, info["exception"]
56+
assert not info["exception"]
5157
assert obs.gen_detached[gen_id]
5258
assert obs1.gen_p[gen_id] == 0.
5359
assert obs1.gen_v[gen_id] == 0.
5460
assert obs1.gen_q[gen_id] == 0.
5561
assert obs1.gen_theta[gen_id] == 0., f"{obs1.gen_theta[gen_id]} vs 0."
56-
assert abs(obs1.gen_p_detached[gen_id] - 80.5) <= 1e-5
62+
assert abs(obs1.gen_p_detached[gen_id] - 80.5) <= 1e-5, f"{obs.gen_p_detached[gen_id]} vs {80.5}"
5763

5864
obs2, _, done, info = self.env.step(self.env.action_space({}))
59-
assert not done
65+
assert not done, info["exception"]
66+
assert not info["exception"]
6067
assert obs.gen_detached[gen_id]
6168
assert obs2.gen_p[gen_id] == 0.
6269
assert obs2.gen_v[gen_id] == 0.
6370
assert obs2.gen_q[gen_id] == 0.
6471
assert obs2.gen_theta[gen_id] == 0., f"{obs2.gen_theta[gen_id]} vs 0."
65-
assert abs(obs2.gen_p_detached[gen_id] - 80.5) <= 1e-5
72+
assert abs(obs2.gen_p_detached[gen_id] - 80.5) <= 1e-5, f"{obs.gen_p_detached[gen_id]} vs {80.5}"
6673

6774
def test_disco_load(self):
6875
"""test i can disconnect a load"""

grid2op/tests/test_detached_simulate.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
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
9+
1010
import grid2op
11-
from grid2op.Agent import BaseAgent
1211
from grid2op.Action import CompleteAction
1312
from grid2op.Chronics import ChangeNothing
1413
import unittest
@@ -29,12 +28,14 @@ def setUp(self):
2928
chronics_class=ChangeNothing,
3029
data_feeding_kwargs={"h_forecast": [5, 10, 15, 20, 25, 30]},
3130
)
32-
paramerters = self.env.parameters
33-
paramerters.MAX_SUB_CHANGED = 99999
34-
paramerters.MAX_LINE_STATUS_CHANGED = 99999
35-
paramerters.NB_TIMESTEP_COOLDOWN_LINE = 0
36-
paramerters.NB_TIMESTEP_COOLDOWN_SUB = 0
37-
self.env.change_parameters(paramerters)
31+
parameters = self.env.parameters
32+
parameters.MAX_SUB_CHANGED = 99999
33+
parameters.MAX_LINE_STATUS_CHANGED = 99999
34+
parameters.NB_TIMESTEP_COOLDOWN_LINE = 0
35+
parameters.NB_TIMESTEP_COOLDOWN_SUB = 0
36+
parameters.ENV_DOES_REDISPATCHING = False
37+
self.env.change_parameters(parameters)
38+
self.env.change_forecast_parameters(parameters)
3839
self.init_obs = self.env.reset(seed=0, options={"time serie id": 0})
3940

4041
def tearDown(self):
@@ -48,71 +49,71 @@ def test_detach_gen(self, tol=1e-5):
4849
act_deco = self.env.action_space({"set_bus": {"generators_id": [(gen_id, -1)]}})
4950
act_reco = self.env.action_space({"set_bus": {"generators_id": [(gen_id, +1)]}})
5051
next_obs, reward, done, info = self.env.step(act_deco)
51-
assert not done
52-
assert len(info["exception"]) == 0
52+
assert not done, info["exception"]
53+
assert len(info["exception"]) == 0, info["exception"]
5354
assert next_obs.gen_detached[gen_id]
5455
assert next_obs.gen_v[gen_id] == 0.
5556
assert next_obs.gen_p[gen_id] == 0.
5657

5758
# test simulate
5859
next_obs_sim00, reward, done, info = next_obs.simulate(self.env.action_space())
59-
assert not done
60-
assert len(info["exception"]) == 0
60+
assert not done, info["exception"]
61+
assert len(info["exception"]) == 0, info["exception"]
6162
assert next_obs_sim00.gen_detached[gen_id]
6263
assert next_obs_sim00.gen_v[gen_id] == 0.
6364
assert next_obs_sim00.gen_p[gen_id] == 0.
6465
next_obs_sim0, reward, done, info = next_obs.simulate(act_reco)
65-
assert not done
66-
assert len(info["exception"]) == 0
66+
assert not done, info["exception"]
67+
assert len(info["exception"]) == 0, info["exception"]
6768
assert not next_obs_sim0.gen_detached[gen_id]
6869
assert np.abs(next_obs_sim0.gen_v[gen_id] - normal_v[gen_id]) <= tol, f"error {np.abs(next_obs_sim0.gen_v[gen_id] - normal_v[gen_id]).max()}"
6970
assert np.abs(next_obs_sim0.gen_p[gen_id] - normal_p[gen_id]) <= tol, f"error {np.abs(next_obs_sim0.gen_p[gen_id] - normal_p[gen_id]).max()}"
7071
next_obs_sim1, reward, done, info = next_obs.simulate(act_deco)
71-
assert not done
72-
assert len(info["exception"]) == 0
72+
assert not done, info["exception"]
73+
assert len(info["exception"]) == 0, info["exception"]
7374
assert next_obs_sim1.gen_detached[gen_id]
7475
assert next_obs_sim1.gen_v[gen_id] == 0.
7576
assert next_obs_sim1.gen_p[gen_id] == 0.
7677
next_obs_sim2, reward, done, info = next_obs.simulate(self.env.action_space())
77-
assert not done
78-
assert len(info["exception"]) == 0
78+
assert not done, info["exception"]
79+
assert len(info["exception"]) == 0, info["exception"]
7980
assert next_obs_sim2.gen_detached[gen_id]
8081
assert next_obs_sim2.gen_v[gen_id] == 0.
8182
assert next_obs_sim2.gen_p[gen_id] == 0.
8283
next_obs_sim3, reward, done, info = next_obs.simulate(act_reco)
83-
assert not done
84-
assert len(info["exception"]) == 0
84+
assert not done, info["exception"]
85+
assert len(info["exception"]) == 0, info["exception"]
8586
assert not next_obs_sim3.gen_detached[gen_id]
8687
assert np.abs(next_obs_sim3.gen_v[gen_id] - normal_v[gen_id]) <= tol, f"error {np.abs(next_obs_sim3.gen_v[gen_id] - normal_v[gen_id]).max()}"
8788
assert np.abs(next_obs_sim3.gen_p[gen_id] - normal_p[gen_id]) <= tol, f"error {np.abs(next_obs_sim3.gen_p[gen_id] - normal_p[gen_id]).max()}"
8889
# now change the setpoint
8990
next_obs_sim4, reward, done, info = next_obs.simulate(act_reco + self.env.action_space({"injection": {"prod_v": normal_v * 1.01}}))
90-
assert not done
91-
assert len(info["exception"]) == 0
91+
assert not done, info["exception"]
92+
assert len(info["exception"]) == 0, info["exception"]
9293
assert not next_obs_sim4.gen_detached[gen_id]
9394
assert np.abs(next_obs_sim4.gen_v[gen_id] - 1.01 * normal_v[gen_id]) <= tol, f"error {np.abs(next_obs_sim4.gen_v[gen_id] - 1.01 * normal_v[gen_id]).max()}"
9495
assert np.abs(next_obs_sim4.gen_p[gen_id] - normal_p[gen_id]) <= tol, f"error {np.abs(next_obs_sim4.gen_p[gen_id] - normal_p[gen_id]).max()}"
9596
# disco
9697
next_obs_sim5, reward, done, info = next_obs.simulate(act_deco)
97-
assert not done
98-
assert len(info["exception"]) == 0
98+
assert not done, info["exception"]
99+
assert len(info["exception"]) == 0, info["exception"]
99100
assert next_obs_sim5.gen_detached[gen_id]
100101
assert next_obs_sim5.gen_v[gen_id] == 0.
101102
assert next_obs_sim5.gen_p[gen_id] == 0.
102103

103104
# reco again (and check the setpoint is not the last one used in obs.simulate but is the one of the observation)
104105
next_obs_sim6, reward, done, info = next_obs.simulate(act_reco)
105-
assert not done
106-
assert len(info["exception"]) == 0
106+
assert not done, info["exception"]
107+
assert len(info["exception"]) == 0, info["exception"]
107108
assert not next_obs_sim6.gen_detached[gen_id]
108109
assert np.abs(next_obs_sim6.gen_v[gen_id] - normal_v[gen_id]) <= tol, f"error {np.abs(next_obs_sim6.gen_v[gen_id] - normal_v[gen_id]).max()}"
109110
assert np.abs(next_obs_sim6.gen_p[gen_id] - normal_p[gen_id]) <= tol, f"error {np.abs(next_obs_sim6.gen_p[gen_id] - normal_p[gen_id]).max()}"
110111

111112

112113
# test step
113114
next_next_obs, reward, done, info = self.env.step(act_reco)
114-
assert not done
115-
assert len(info["exception"]) == 0
115+
assert not done, info["exception"]
116+
assert len(info["exception"]) == 0, info["exception"]
116117
assert not next_next_obs.gen_detached[gen_id]
117118
assert np.abs(next_next_obs.gen_v[gen_id] - normal_v[gen_id]) <= tol, f"error {np.abs(next_next_obs.gen_v[gen_id] - normal_v[gen_id]).max()}"
118119
assert np.abs(next_next_obs.gen_p[gen_id] - normal_p[gen_id]) <= tol, f"error {np.abs(next_next_obs.gen_p[gen_id] - normal_p[gen_id]).max()}"

0 commit comments

Comments
 (0)