/
test_attached_envs.py
292 lines (244 loc) · 10.5 KB
/
test_attached_envs.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# 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 warnings
import unittest
import grid2op
from grid2op.Action import (PowerlineSetAction, PlayableAction, DontAct)
from grid2op.Observation import CompleteObservation
from grid2op.Opponent import GeometricOpponent
import pdb
# TODO refactor to have 1 base class, maybe
class TestL2RPNNEURIPS2020_Track1(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("l2rpn_neurips_2020_track1", test=True)
self.env.seed(0)
_ = self.env.reset()
def test_elements(self):
assert self.env.n_sub == 36
assert self.env.n_line == 59
assert self.env.n_load == 37
assert self.env.n_gen == 22
assert self.env.n_storage == 0
def test_opponent(self):
assert issubclass(self.env._opponent_action_class, PowerlineSetAction)
assert self.env._opponent_action_space.n == self.env.n_line
def test_action_space(self):
assert issubclass(self.env.action_space.subtype, PlayableAction)
assert self.env.action_space.n == 494, f"{self.env.action_space.n} instead of 494"
def test_observation_space(self):
assert issubclass(self.env.observation_space.subtype, CompleteObservation)
size_th = 1266
assert self.env.observation_space.n == size_th, (
f"obs space size is {self.env.observation_space.n}, should be {size_th}"
)
def test_random_action(self):
"""test i can perform some step (random)"""
i = 0
for i in range(10):
act = self.env.action_space.sample()
obs, reward, done, info = self.env.step(act)
if done:
break
assert i >= 1, (
"could not perform the random action test because it games over first time step. "
"Please fix the test and try again"
)
class TestL2RPNICAPS2021(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("l2rpn_icaps_2021", test=True)
self.env.seed(0)
_ = self.env.reset()
def test_elements(self):
assert self.env.n_sub == 36
assert self.env.n_line == 59
assert self.env.n_load == 37
assert self.env.n_gen == 22
assert self.env.n_storage == 0
def test_opponent(self):
assert issubclass(self.env._opponent_action_class, PowerlineSetAction)
assert isinstance(self.env._opponent, GeometricOpponent)
assert self.env._opponent_action_space.n == self.env.n_line
def test_action_space(self):
assert issubclass(self.env.action_space.subtype, PlayableAction)
assert self.env.action_space.n == 519, (
f"act space size is {self.env.action_space.n}, should be {519}"
)
def test_observation_space(self):
assert issubclass(self.env.observation_space.subtype, CompleteObservation)
size_th = 1363
assert self.env.observation_space.n == size_th, (
f"obs space size is "
f"{self.env.observation_space.n}, "
f"should be {size_th}"
)
def test_random_action(self):
"""test i can perform some step (random)"""
i = 0
for i in range(10):
act = self.env.action_space.sample()
obs, reward, done, info = self.env.step(act)
if done:
break
assert i >= 1, (
"could not perform the random action test because it games over first time step. "
"Please fix the test and try again"
)
class TestL2RPNNEURIPS2020_Track2(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("l2rpn_neurips_2020_track2", test=True)
self.env.seed(2) # 0 or 1 breaks the test `test_random_action`
_ = self.env.reset()
def test_elements(self):
assert self.env.n_sub == 118
assert self.env.n_line == 186
assert self.env.n_load == 99
assert self.env.n_gen == 62
assert self.env.n_storage == 0
def test_opponent(self):
assert issubclass(self.env._opponent_action_class, DontAct)
assert self.env._opponent_action_space.n == 0
def test_action_space(self):
assert issubclass(self.env.action_space.subtype, PlayableAction)
assert self.env.action_space.n == 1500, f"{self.env.action_space.n} instead of 1500"
def test_observation_space(self):
assert issubclass(self.env.observation_space.subtype, CompleteObservation)
size_th = 3868
assert self.env.observation_space.n == size_th, (
f"obs space size is {self.env.observation_space.n}, should be {size_th}"
)
def test_random_action(self):
"""test i can perform some step (random)"""
i = 0
for i in range(10):
act = self.env.action_space.sample()
obs, reward, done, info = self.env.step(act)
if done:
break
assert i >= 1, (
"could not perform the random action test because it games over first 10 steps. "
"Please fix the test and try again"
)
class TestL2RPN_CASE14_SANDBOX(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("l2rpn_case14_sandbox", test=True)
self.env.seed(42)
_ = self.env.reset()
def test_elements(self):
assert self.env.n_sub == 14
assert self.env.n_line == 20
assert self.env.n_load == 11
assert self.env.n_gen == 6
assert self.env.n_storage == 0
def test_opponent(self):
assert issubclass(self.env._opponent_action_class, DontAct)
assert self.env._opponent_action_space.n == 0
def test_action_space(self):
assert issubclass(self.env.action_space.subtype, PlayableAction)
assert self.env.action_space.n == 166, f"{self.env.action_space.n} instead of 166"
def test_observation_space(self):
assert issubclass(self.env.observation_space.subtype, CompleteObservation)
size_th = 467
assert self.env.observation_space.n == size_th, (
f"obs space size is {self.env.observation_space.n}," f"should be {size_th}"
)
def test_random_action(self):
"""test i can perform some step (random)"""
i = 0
for i in range(10):
act = self.env.action_space.sample()
obs, reward, done, info = self.env.step(act)
if done:
break
assert i >= 1, (
"could not perform the random action test because it games over first time step. "
"Please fix the test and try again"
)
class TestEDUC_CASE14_REDISP(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("educ_case14_redisp", test=True)
self.env.seed(0)
_ = self.env.reset()
def test_elements(self):
assert self.env.n_sub == 14
assert self.env.n_line == 20
assert self.env.n_load == 11
assert self.env.n_gen == 6
assert self.env.n_storage == 0
def test_opponent(self):
assert issubclass(self.env._opponent_action_class, DontAct)
assert self.env._opponent_action_space.n == 0
def test_action_space(self):
assert issubclass(self.env.action_space.subtype, PlayableAction)
assert self.env.action_space.n == 26, f"{self.env.action_space.n} instead of 26"
def test_observation_space(self):
assert issubclass(self.env.observation_space.subtype, CompleteObservation)
size_th = 467
assert self.env.observation_space.n == size_th, (
f"obs space size is {self.env.observation_space.n}," f"should be {size_th}"
)
def test_random_action(self):
"""test i can perform some step (random)"""
i = 0
for i in range(10):
act = self.env.action_space.sample()
obs, reward, done, info = self.env.step(act)
if done:
break
assert i >= 1, (
"could not perform the random action test because it games over first time step. "
"Please fix the test and try again"
)
class TestEDUC_STORAGE(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("educ_case14_storage", test=True)
self.env.seed(0)
_ = self.env.reset()
def test_elements(self):
assert self.env.n_sub == 14
assert self.env.n_line == 20
assert self.env.n_load == 11
assert self.env.n_gen == 6
assert self.env.n_storage == 2
def test_opponent(self):
assert issubclass(self.env._opponent_action_class, DontAct)
assert self.env._opponent_action_space.n == 0
def test_action_space(self):
assert issubclass(self.env.action_space.subtype, PlayableAction)
assert self.env.action_space.n == 28, f"{self.env.action_space.n} instead of 28"
def test_observation_space(self):
assert issubclass(self.env.observation_space.subtype, CompleteObservation)
size_th = 475
assert self.env.observation_space.n == size_th, (
f"obs space size is {self.env.observation_space.n}," f"should be {size_th}"
)
def test_random_action(self):
"""test i can perform some step (random)"""
i = 0
for i in range(10):
act = self.env.action_space.sample()
obs, reward, done, info = self.env.step(act)
if done:
break
assert i >= 1, (
"could not perform the random action test because it games over first time step. "
"Please fix the test and try again"
)
if __name__ == "__main__":
unittest.main()