-
Notifications
You must be signed in to change notification settings - Fork 113
/
test_Agent.py
365 lines (326 loc) · 15.3 KB
/
test_Agent.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# 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 time
import warnings
import pandapower as pp
import unittest
from grid2op.tests.helper_path_test import *
import grid2op
from grid2op.Exceptions import *
from grid2op.Agent import (
PowerLineSwitch,
TopologyGreedy,
DoNothingAgent,
RecoPowerlineAgent,
FromActionsListAgent,
)
from grid2op.Parameters import Parameters
from grid2op.dtypes import dt_float
from grid2op.Agent import RandomAgent
import pdb
DEBUG = False
if DEBUG:
print("pandapower version : {}".format(pp.__version__))
class TestAgent(HelperTests, unittest.TestCase):
def setUp(self):
"""
The case file is a representation of the case14 as found in the ieee14 powergrid.
:return:
"""
super().setUp()
param = Parameters()
param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True})
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("rte_case14_redisp", test=True, param=param, _add_to_name=type(self).__name__)
def tearDown(self):
self.env.close()
super().tearDown()
def _aux_test_agent(self, agent, i_max=30):
done = False
i = 0
beg_ = time.perf_counter()
cum_reward = dt_float(0.0)
obs = self.env.get_obs()
reward = 0.0
time_act = 0.0
all_acts = []
while not done:
# print("_______________")
beg__ = time.perf_counter()
act = agent.act(obs, reward, done)
all_acts.append(act)
end__ = time.perf_counter()
obs, reward, done, info = self.env.step(
act
) # should load the first time stamp
time_act += end__ - beg__
cum_reward += reward
i += 1
if i > i_max:
break
end_ = time.perf_counter()
if DEBUG:
li_text = [
"Env: {:.2f}s",
"\t - apply act {:.2f}s",
"\t - run pf: {:.2f}s",
"\t - env update + observation: {:.2f}s",
"\t - time env obs space: {:.2f}s",
"BaseAgent: {:.2f}s",
"Total time: {:.2f}s",
"Cumulative reward: {:1f}",
]
msg_ = "\n".join(li_text)
print(
msg_.format(
self.env._time_apply_act
+ self.env._time_powerflow
+ self.env._time_extract_obs, # env
self.env._time_apply_act, # apply act
self.env._time_powerflow, # run pf
self.env._time_extract_obs, # env update + obs
self.env.observation_space._update_env_time, # time get topo vect
time_act,
end_ - beg_,
cum_reward,
)
)
return i, cum_reward, all_acts
def test_0_donothing(self):
agent = DoNothingAgent(self.env.action_space)
with warnings.catch_warnings():
warnings.filterwarnings("error")
i, cum_reward, all_acts = self._aux_test_agent(agent)
assert i == 31, "The powerflow diverged before step 30 for do nothing"
expected_reward = dt_float(35140.027)
expected_reward = dt_float(35140.03125 / 12.)
assert (
np.abs(cum_reward - expected_reward, dtype=dt_float) <= self.tol_one
), f"The reward has not been properly computed {cum_reward} instead of {expected_reward}"
def test_1_powerlineswitch(self):
agent = PowerLineSwitch(self.env.action_space)
with warnings.catch_warnings():
warnings.filterwarnings("error")
i, cum_reward, all_acts = self._aux_test_agent(agent)
assert (
i == 31
), "The powerflow diverged before step 30 for powerline switch agent"
# switch to using df_float in the reward, change then the results
expected_reward = dt_float(35147.55859375)
expected_reward = dt_float(35147.7685546 / 12.)
assert (
np.abs(cum_reward - expected_reward) <= self.tol_one
), f"The reward has not been properly computed {cum_reward} instead of {expected_reward}"
def test_2_busswitch(self):
agent = TopologyGreedy(self.env.action_space)
with warnings.catch_warnings():
warnings.filterwarnings("error")
i, cum_reward, all_acts = self._aux_test_agent(agent, i_max=10)
assert i == 11, "The powerflow diverged before step 10 for greedy agent"
# i have more actions now, so this is not correct (though it should be..
# yet a proof that https://github.com/rte-france/Grid2Op/issues/86 is grounded
expected_reward = dt_float(12075.389)
expected_reward = dt_float(12277.632)
expected_reward = dt_float(12076.35644531 / 12.)
assert (
np.abs(cum_reward - expected_reward) <= self.tol_one
), f"The reward has not been properly computed {cum_reward} instead of {expected_reward}"
class TestMake2Agents(HelperTests, unittest.TestCase):
def test_2random(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
env = grid2op.make("rte_case5_example", test=True, _add_to_name=type(self).__name__)
env2 = grid2op.make("rte_case14_realistic", test=True, _add_to_name=type(self).__name__)
agent = RandomAgent(env.action_space)
agent2 = RandomAgent(env2.action_space)
# test i can reset the env
obs = env.reset()
obs2 = env2.reset()
# test the agent can act
act = agent.act(obs, 0.0, False)
act2 = agent2.act(obs2, 0.0, False)
# test the env can step
_ = env.step(act)
_ = env2.step(act2)
env.close()
env2.close()
class TestSeeding(HelperTests, unittest.TestCase):
def test_random(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case5_example", test=True, _add_to_name=type(self).__name__) as env:
obs = env.reset()
my_agent = RandomAgent(env.action_space)
my_agent.seed(0)
nb_test = 100
res = np.zeros(nb_test, dtype=int)
res2 = np.zeros(nb_test, dtype=int)
res3 = np.zeros(nb_test, dtype=int)
for i in range(nb_test):
res[i] = my_agent.my_act(obs, 0.0, False)
my_agent.seed(0)
for i in range(nb_test):
res2[i] = my_agent.my_act(obs, 0.0, False)
my_agent.seed(1)
for i in range(nb_test):
res3[i] = my_agent.my_act(obs, 0.0, False)
# the same seeds should produce the same sequence
assert np.all(res == res2)
# different seeds should produce different sequence
assert np.any(res != res3)
class TestRecoPowerlineAgent(HelperTests, unittest.TestCase):
def test_reco_simple(self):
param = Parameters()
param.NO_OVERFLOW_DISCONNECTION = True
param.NB_TIMESTEP_COOLDOWN_LINE = 1
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case5_example", test=True, param=param, _add_to_name=type(self).__name__) as env:
my_agent = RecoPowerlineAgent(env.action_space)
obs = env.reset()
assert np.sum(obs.time_before_cooldown_line) == 0
obs, reward, done, info = env.step(
env.action_space({"set_line_status": [(1, -1)]})
)
assert np.sum(obs.time_before_cooldown_line) == 1
# the agent should do nothing, as the line is still in cooldown
act = my_agent.act(obs, reward, done)
assert not act.as_dict()
obs, reward, done, info = env.step(act)
# now cooldown is over
assert np.sum(obs.time_before_cooldown_line) == 0
act2 = my_agent.act(obs, reward, done)
ddict = act2.as_dict()
assert "set_line_status" in ddict
assert "nb_connected" in ddict["set_line_status"]
assert "connected_id" in ddict["set_line_status"]
assert ddict["set_line_status"]["nb_connected"] == 1
assert ddict["set_line_status"]["connected_id"][0] == 1
def test_reco_more_difficult(self):
param = Parameters()
param.NO_OVERFLOW_DISCONNECTION = True
param.NB_TIMESTEP_COOLDOWN_LINE = 3
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case5_example", test=True, param=param, _add_to_name=type(self).__name__) as env:
my_agent = RecoPowerlineAgent(env.action_space)
obs = env.reset()
obs, reward, done, info = env.step(
env.action_space({"set_line_status": [(1, -1)]})
)
obs, reward, done, info = env.step(
env.action_space({"set_line_status": [(2, -1)]})
)
# the agent should do nothing, as the line is still in cooldown
act = my_agent.act(obs, reward, done)
assert not act.as_dict()
obs, reward, done, info = env.step(act)
act = my_agent.act(obs, reward, done)
assert not act.as_dict()
obs, reward, done, info = env.step(act)
# now in theory i can reconnect the first one
act2 = my_agent.act(obs, reward, done)
ddict = act2.as_dict()
assert "set_line_status" in ddict
assert "nb_connected" in ddict["set_line_status"]
assert "connected_id" in ddict["set_line_status"]
assert ddict["set_line_status"]["nb_connected"] == 1
assert ddict["set_line_status"]["connected_id"][0] == 1
# but i will not implement it on the grid
obs, reward, done, info = env.step(env.action_space())
act3 = my_agent.act(obs, reward, done)
ddict3 = act3.as_dict()
assert len(my_agent.tested_action) == 2
# and it turns out i need to reconnect the first one first
assert "set_line_status" in ddict3
assert "nb_connected" in ddict3["set_line_status"]
assert "connected_id" in ddict3["set_line_status"]
assert ddict3["set_line_status"]["nb_connected"] == 1
assert ddict3["set_line_status"]["connected_id"][0] == 1
obs, reward, done, info = env.step(act3)
act4 = my_agent.act(obs, reward, done)
ddict4 = act4.as_dict()
assert len(my_agent.tested_action) == 1
# and it turns out i need to reconnect the first one first
assert "set_line_status" in ddict4
assert "nb_connected" in ddict4["set_line_status"]
assert "connected_id" in ddict4["set_line_status"]
assert ddict4["set_line_status"]["nb_connected"] == 1
assert ddict4["set_line_status"]["connected_id"][0] == 2
class TestFromList(HelperTests, unittest.TestCase):
def test_agentfromlist_empty(self):
param = Parameters()
param.NO_OVERFLOW_DISCONNECTION = True
param.NB_TIMESTEP_COOLDOWN_LINE = 3
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case5_example", test=True, param=param, _add_to_name=type(self).__name__) as env:
agent = FromActionsListAgent(env.action_space, action_list=[])
obs = env.reset()
# should do nothing
act = agent.act(obs, 0.0, False)
obs, reward, done, info = env.step(act)
assert act.can_affect_something() is False
act = agent.act(obs, 0.0, False)
obs, reward, done, info = env.step(act)
assert act.can_affect_something() is False
def test_agentfromlist(self):
param = Parameters()
param.NO_OVERFLOW_DISCONNECTION = True
param.NB_TIMESTEP_COOLDOWN_LINE = 3
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case5_example", test=True, param=param, _add_to_name=type(self).__name__) as env:
agent = FromActionsListAgent(
env.action_space,
action_list=[env.action_space({"set_line_status": [(0, +1)]})],
)
obs = env.reset()
# should do nothing
act = agent.act(obs, 0.0, False)
obs, reward, done, info = env.step(act)
assert act == env.action_space({"set_line_status": [(0, +1)]})
act = agent.act(obs, 0.0, False)
obs, reward, done, info = env.step(act)
assert act.can_affect_something() is False
def test_agentfromlist_creation_fails(self):
param = Parameters()
param.NO_OVERFLOW_DISCONNECTION = True
param.NB_TIMESTEP_COOLDOWN_LINE = 3
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case5_example", test=True, param=param, _add_to_name=type(self).__name__) as env:
with self.assertRaises(AgentError):
# action_list should be an iterable
agent = FromActionsListAgent(env.action_space, action_list=1)
with self.assertRaises(AgentError):
# action_list should contain only actions
agent = FromActionsListAgent(env.action_space, action_list=[1])
with grid2op.make(
"l2rpn_case14_sandbox", test=True, param=param,
_add_to_name=type(self).__name__
) as env2:
with self.assertRaises(AgentError):
# action_list should contain only actions from a compatible environment
agent = FromActionsListAgent(
env.action_space,
action_list=[
env2.action_space({"set_line_status": [(0, +1)]})
],
)
with grid2op.make(
"rte_case5_example", test=True, param=param, _add_to_name="toto"
) as env3:
# this should work because it's the same underlying grid
agent = FromActionsListAgent(
env.action_space,
action_list=[env3.action_space({"set_line_status": [(0, +1)]})],
)
if __name__ == "__main__":
unittest.main()