/
test_noisy_obs.py
215 lines (188 loc) · 7.74 KB
/
test_noisy_obs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
import numpy as np
import unittest
import warnings
import pdb
import grid2op
from grid2op.Runner import Runner
from grid2op.Observation import CompleteObservation
from grid2op.Observation import NoisyObservation
class TestNoisy(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make(
"educ_case14_storage", test=True, observation_class=NoisyObservation
)
self.env.seed(0)
self.env.set_id(0)
self.obs = self.env.reset()
def tearDown(self) -> None:
self.env.close()
def test_create_ok(self):
# simply test the creation
pass
def _obs_equals(self, obs1, obs2):
assert np.all(obs1.load_p == obs2.load_p)
assert np.all(obs1.load_q == obs2.load_q)
assert np.all(obs1.gen_p == obs2.prod_p)
assert np.all(obs1.gen_q == obs2.prod_q)
assert np.all(obs1.a_or == obs2.a_or)
assert np.all(obs1.a_ex == obs2.a_ex)
assert np.all(obs1.p_or == obs2.p_or)
assert np.all(obs1.p_ex == obs2.p_ex)
assert np.all(obs1.q_or == obs2.q_or)
assert np.all(obs1.q_ex == obs2.q_ex)
assert np.all(obs1.storage_power == obs2.storage_power)
def test_getobs_sameres(self):
# simply test the creation
obs0 = self.env.get_obs()
obs1 = self.env.get_obs()
assert np.all(obs0.load_p == obs1.load_p)
assert np.any(obs0.load_p != self.env.backend.load_p)
assert np.all(obs0.load_q == obs1.load_q)
assert np.any(obs0.load_q != self.env.backend.load_q)
assert np.all(obs0.gen_p == obs1.prod_p)
assert np.any(obs0.gen_p != self.env.backend.prod_p)
assert np.all(obs0.gen_q == obs1.prod_q)
assert np.any(obs0.gen_q != self.env.backend.prod_q)
assert np.all(obs0.a_or == obs1.a_or)
assert np.any(obs0.a_or != self.env.backend.a_or)
assert np.all(obs0.a_ex == obs1.a_ex)
assert np.any(obs0.a_ex != self.env.backend.a_ex)
assert np.all(obs0.p_ex == obs1.p_ex)
assert np.any(obs0.p_ex != self.env.backend.p_ex)
assert np.all(obs0.q_ex == obs1.q_ex)
assert np.any(obs0.q_ex != self.env.backend.q_ex)
assert np.all(obs0.p_or == obs1.p_or)
assert np.any(obs0.p_or != self.env.backend.p_or)
assert np.all(obs0.q_or == obs1.q_or)
assert np.any(obs0.q_or != self.env.backend.q_or)
assert np.all(obs0.storage_power == obs1.storage_power)
assert np.any(obs0.storage_power != self.env._storage_power)
def test_seed_works(self):
self.env.seed(0)
self.env.set_id(0)
obs = self.env.reset()
self._obs_equals(obs, self.obs)
def test_seed_independant_previous(self):
"""test that the seed of a given episode is independant on what happened in the previous"""
obs, *_ = self.env.step(self.env.action_space())
obs, *_ = self.env.step(self.env.action_space())
obs, *_ = self.env.step(self.env.action_space())
obs = self.env.reset()
self.env.seed(0)
self.env.set_id(0)
as_ref = self.env.reset() # should match self.obs
self._obs_equals(as_ref, self.obs)
# don't do anything (instead of 3 steps)
as_obs = self.env.reset()
self._obs_equals(obs, as_obs) # should match the case where I did 3 steps
def test_with_copy(self):
env_cpy = self.env.copy()
obs, *_ = self.env.step(self.env.action_space())
obs, *_ = self.env.step(self.env.action_space())
obs, *_ = self.env.step(self.env.action_space())
obs = self.env.reset()
obs_cpy = env_cpy.reset()
self._obs_equals(obs_cpy, obs)
obs, *_ = self.env.step(self.env.action_space())
obs_cpy, *_ = env_cpy.step(self.env.action_space())
self._obs_equals(obs_cpy, obs)
def test_simulate(self):
sim_o, *_ = self.obs.simulate(self.env.action_space())
assert type(sim_o).env_name == "educ_case14_storage"
assert isinstance(sim_o, CompleteObservation)
# test that it is reproducible
self.env.seed(0)
self.env.set_id(0)
as_ref = self.env.reset() # should match self.obs
sim_o2, *_ = as_ref.simulate(self.env.action_space())
self._obs_equals(sim_o2, sim_o)
# test that it is the same as non stochastic observation
# (simulate is based on forecast, not on actual environment state)
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
env = grid2op.make(
"educ_case14_storage", test=True, observation_class=CompleteObservation
)
env.seed(0)
env.set_id(0)
obs = env.reset()
sim_o3, *_ = obs.simulate(self.env.action_space())
self._obs_equals(sim_o3, sim_o)
def test_runner(self):
runner = Runner(**self.env.get_params_for_runner())
# check it's the same when seed is the same
res = runner.run(
nb_episode=1,
max_iter=10,
episode_id=[0],
env_seeds=[0],
add_detailed_output=True,
)
res2 = runner.run(
nb_episode=1,
max_iter=10,
episode_id=[0],
env_seeds=[0],
add_detailed_output=True,
)
self._obs_equals(res[0][-1].observations[0], self.obs)
for el in range(10):
obs1 = res[0][-1].observations[el]
obs2 = res2[0][-1].observations[el]
self._obs_equals(obs1, obs2)
# check it's different when seed is different
res3 = runner.run(
nb_episode=1,
max_iter=10,
episode_id=[0],
env_seeds=[1],
add_detailed_output=True,
)
for el in range(10):
obs1 = res[0][-1].observations[el]
obs3 = res3[0][-1].observations[el]
with self.assertRaises(AssertionError):
self._obs_equals(obs1, obs3)
class TestNoisyDiffParams(TestNoisy):
def setUp(self) -> None:
kwargs_observation = {"sigma_load_p": 1.0, "sigma_gen_p": 0.1}
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make(
"educ_case14_storage",
test=True,
observation_class=NoisyObservation,
kwargs_observation=kwargs_observation,
)
self.env.seed(0)
self.env.set_id(0)
self.obs = self.env.reset()
def test_param_working(self):
# change the kwargs to make sure it has an impact
kwargs_observation = {"sigma_load_p": 0.1, "sigma_gen_p": 1.0}
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
env = grid2op.make(
"educ_case14_storage",
test=True,
observation_class=NoisyObservation,
kwargs_observation=kwargs_observation,
)
env.seed(0)
env.set_id(0)
obs = env.reset()
with self.assertRaises(AssertionError):
self._obs_equals(obs, self.obs)
# TODO next: have a powerflow there to compute the outcome of the state
# after the modification
if __name__ == "__main__":
unittest.main()