/
fromOneEpisodeData.py
424 lines (349 loc) · 16.4 KB
/
fromOneEpisodeData.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# Copyright (c) 2023, 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.
from datetime import datetime, timedelta
import os
import numpy as np
import copy
import warnings
from typing import Union, Tuple
from pathlib import Path
from grid2op.Exceptions import (
ChronicsError, ChronicsNotFoundError
)
from grid2op.Chronics.gridValue import GridValue
from grid2op.dtypes import dt_int, dt_float
from grid2op.Episode import EpisodeData
TYPE_EP_DATA_INGESTED = Union[str, Path, EpisodeData, Tuple[str, str]]
class FromOneEpisodeData(GridValue):
"""This class allows to use the :class:`grid2op.Chronics.handlers.BaseHandler` to read back data
stored in :class:`grid2op.Episode.EpisodeData`
It can be used if you want to loop indefinitely through one episode.
.. versionadded:: 1.9.4
TODO there will be "perfect" forecast, as original forecasts are not stored !
.. warning::
Original forecasts are not stored by the runner. This is why you cannot use
the same information as available in the original "obs.simulate".
However, you can still use PERFECT FORECAST if you want to by providing the extra
parameters "list_perfect_forecasts=[forecast_horizon_1, forecast_horizon_2, etc.]"
when you build this class. (see examples below)
.. danger::
If you want the created environment to be exactly that the original environment, make
sure to generate data using a "do nothing" agent.
If the agent modified the injections (*eg* with redispatching, curtailment or storage)
then the resulting time series will "embed" these modifications: they will
NOT match the orignal implementation
.. danger::
If you load an episode data with an opponent, make sure also to build your environment with
:class:`grid2op.Opponent.FromEpisodeDataOpponent` and assign `opponent_attack_cooldown=1`
(see example below) otherwise you might end up with different time series than what you
initially had in the EpisodeData.
.. note::
As this class reads from the hard drive an episode that has been played, we strongly encourage
you to build this class with a complete episode (and not using an agent that games over after a
few steps), for example by using the "RecoPowerlineAgent" and the `NO_OVERFLOW_DISCONNECTION`
parameters (see example below)
.. seealso::
:class:`grid2op.Chronics.FromMultiEpisodeData` if you want to use multiple episode data
Examples
---------
You can use this class this way:
First, you generate some data by running an episode with do nothing or reco powerline agent,
preferably episode that go until the end of your time series
.. code-block:: python
import grid2op
from grid2op.Runner import Runner
from grid2op.Agent import RecoPowerlineAgent
path_agent = ....
env_name = "l2rpn_case14_sandbox" # or any other name
env = grid2op.make(env_name, etc.)
# optional (change the parameters to allow the )
param = env.parameters
param.NO_OVERFLOW_DISCONNECTION = True
env.change_parameters(param)
env.reset()
# end optional
runner = Runner(**env.get_params_for_runner(),
agentClass=RecoPowerlineAgent)
runner.run(nb_episode=1,
path_save=path_agent)
And then you can load it back and run the exact same environment with the same
time series, the same attacks etc. with:
.. code-block:: python
import grid2op
from grid2op.Chronics import FromOneEpisodeData
from grid2op.Opponent import FromEpisodeDataOpponent
from grid2op.Episode import EpisodeData
path_agent = .... # same as above
env_name = .... # same as above
# path_agent is the path where data coming from a grid2op runner are stored
# NB it should come from a do nothing agent, or at least
# an agent that does not modify the injections (no redispatching, curtailment, storage)
li_episode = EpisodeData.list_episode(path_agent)
ep_data = li_episode[0]
env = grid2op.make(env_name,
chronics_class=FromOneEpisodeData,
data_feeding_kwargs={"ep_data": ep_data},
opponent_class=FromEpisodeDataOpponent,
opponent_attack_cooldown=1,
)
# ep_data can be either a tuple of 2 elements (like above)
# or a full path to a saved episode
# or directly an object of type EpisodeData
obs = env.reset()
# and now you can use "env" as any grid2op environment.
If you want to include perfect forecast (unfortunately you cannot retrieve the original forecasts)
you can do:
.. code-block:: python
# same as above
env = grid2op.make(env_name,
chronics_class=FromOneEpisodeData,
data_feeding_kwargs={"ep_data": ep_data,
"list_perfect_forecasts": (5, 10, 15)},
opponent_class=FromEpisodeDataOpponent,
opponent_attack_cooldown=1,
)
# it creates an environment with perfect forecasts available for the next step (5),
# the step afterwards (10) and again the following one (15)
.. seealso::
:class:`grid2op.Opponent.FromEpisodeDataOpponent`
"""
MULTI_CHRONICS = False
def __init__(
self,
path, # can be None !
ep_data: TYPE_EP_DATA_INGESTED,
time_interval=timedelta(minutes=5),
sep=";", # here for compatibility with grid2op, but not used
max_iter=-1,
start_datetime=datetime(year=2019, month=1, day=1),
chunk_size=None,
list_perfect_forecasts=None, # TODO
**kwargs, # unused
):
GridValue.__init__(
self,
time_interval=time_interval,
max_iter=max_iter,
start_datetime=start_datetime,
chunk_size=chunk_size,
)
self.path = path
if self.path is not None:
# logger: this has no impact
pass
if isinstance(ep_data, EpisodeData):
self._episode_data = ep_data
elif isinstance(ep_data, (str, Path)):
try:
self._episode_data = EpisodeData.from_disk(*os.path.split(ep_data))
except Exception as exc_:
raise ChronicsError("Impossible to build the FromOneEpisodeData with the `ep_data` provided.") from exc_
elif isinstance(ep_data, (tuple, list)):
if len(ep_data) != 2:
raise ChronicsError("When you provide a tuple, or a list, FromOneEpisodeData can only be used if this list has length 2. "
f"Length {len(ep_data)} found.")
try:
self._episode_data = EpisodeData.from_disk(*ep_data)
except Exception as exc_:
raise ChronicsError("Impossible to build the FromOneEpisodeData with the `ep_data` provided.") from exc_
else:
raise ChronicsError("FromOneEpisodeData can only read data either directly from an EpisodeData, "
"from a path pointing to one, or from a tuple")
self.current_inj = None
if list_perfect_forecasts is not None:
self.list_perfect_forecasts = list_perfect_forecasts
else:
self.list_perfect_forecasts = []
self._check_list_perfect_forecasts()
def _check_list_perfect_forecasts(self):
if not self.list_perfect_forecasts:
return
self.list_perfect_forecasts = [int(el) for el in self.list_perfect_forecasts]
for horizon in self.list_perfect_forecasts:
tmp = horizon * 60. / self.time_interval.total_seconds()
if tmp - int(tmp) != 0:
raise ChronicsError(f"All forecast horizons should be multiple of self.time_interval (and given in minutes), found {horizon}")
for h_id, horizon in enumerate(self.list_perfect_forecasts):
if horizon * 60 != (h_id + 1) * (self.time_interval.total_seconds()):
raise ChronicsError("For now all horizons should be consecutive, you cannot 'skip' a forecast: (5, 10, 15) "
"is ok but (5, 15, 30) is NOT.")
def initialize(
self,
order_backend_loads,
order_backend_prods,
order_backend_lines,
order_backend_subs,
names_chronics_to_backend=None,
):
# set the current path of the time series
self.n_gen = len(order_backend_prods)
self.n_load = len(order_backend_loads)
self.n_line = len(order_backend_lines)
self.curr_iter = 0
self.current_inj = None
# TODO check if consistent, and compute the order !
# when there are no maintenance / hazards, build this only once
self._no_mh_time = np.full(self.n_line, fill_value=-1, dtype=dt_int)
self._no_mh_duration = np.full(self.n_line, fill_value=0, dtype=dt_int)
def load_next(self):
obs = self._episode_data.observations[self.curr_iter]
self.current_datetime += self.time_interval
self.curr_iter += 1
res = {}
# load the injection
dict_inj, prod_v = self._load_injection(obs)
res["injection"] = dict_inj
# load maintenance
res["maintenance"] = obs.time_next_maintenance == 0
maintenance_time = 1 * obs.time_next_maintenance
maintenance_duration = 1 * obs.duration_next_maintenance
self.current_inj = res
return (
self.current_datetime,
res,
maintenance_time,
maintenance_duration,
self._no_mh_duration,
prod_v,
)
def max_timestep(self):
if self.max_iter > 0:
return min(self.max_iter, len(self._episode_data))
return len(self._episode_data)
def next_chronics(self):
self.current_datetime = self.start_datetime
self.curr_iter = 0
def done(self):
# I am done if the part I control is "over"
if self._max_iter > 0 and self.curr_iter > self._max_iter:
return True
if self.curr_iter > len(self._episode_data):
return True
return False
def check_validity(self, backend):
warning_msg = ("An action modified the injection with {}, resulting data might be "
"different from original data used in the generation of the initial EpisodeData.")
redisp_issued = False
sto_issued = False
curt_issued = False
for act in self._episode_data.actions:
if act._modif_redispatch:
if not redisp_issued:
warnings.warn(warning_msg.format("redispatching"))
redisp_issued = True
if act._modif_storage:
if not sto_issued:
warnings.warn(warning_msg.format("storage"))
sto_issued = True
if act._modif_curtailment:
if not curt_issued:
warnings.warn(warning_msg.format("curtailment"))
curt_issued = True
return True
def _aux_forecasts(self, h_id, dict_, key,
for_handler, base_handler, handlers):
if for_handler is not None:
tmp_ = for_handler.forecast(h_id, self.current_inj, dict_, base_handler, handlers)
if tmp_ is not None:
dict_[key] = dt_float(1.0) * tmp_
def forecasts(self):
"""Retrieve PERFECT forecast from this time series generator.
.. danger::
These are **perfect forecast** and **NOT** the original forecasts.
Notes
-----
As in grid2op the forecast information is not stored by the runner,
it is NOT POSSIBLE to retrieve the forecast informations used by the
"original" env (the one that generated the EpisodeData).
This class however, thanks to the `list_perfect_forecasts` kwarg you
can set at building time, can generate perfect forecasts: the agent will
see into the future if using these forecasts.
"""
if not self.list_perfect_forecasts:
return []
res = []
for h_id, h in enumerate(self.list_perfect_forecasts):
res_d = {}
obs = self._episode_data.observations[min(self.curr_iter + h_id, len(self._episode_data) - 1)]
# load the injection
dict_inj, prod_v = self._load_injection(obs)
dict_inj["prod_v"] = prod_v
res_d["injection"] = dict_inj
forecast_datetime = self.current_datetime + timedelta(minutes=h)
res.append((forecast_datetime, res_d))
return res
def get_kwargs(self, dict_):
dict_["ep_data"] = copy.deepcopy(self._episode_data)
# dict_["list_perfect_forecasts"] = copy.deepcopy(self.list_perfect_forecasts)
return dict_
def get_id(self) -> str:
if self.path is not None:
return self.path
else:
# TODO EpisodeData.path !!!
return ""
def shuffle(self, shuffler=None):
# TODO
pass
def sample_next_chronics(self, probabilities=None):
# TODO
pass
def seed(self, seed):
# nothing to do in this case, environment is purely deterministic
super().seed(seed)
def _load_injection(self, obs):
dict_ = {}
prod_v = None
tmp_ = obs.load_p
if tmp_ is not None:
dict_["load_p"] = dt_float(1.0) * tmp_
tmp_ = obs.load_q
if tmp_ is not None:
dict_["load_q"] = dt_float(1.0) * tmp_
tmp_ = obs.gen_p
if tmp_ is not None:
dict_["prod_p"] = dt_float(1.0) * tmp_
tmp_ = obs.gen_v
if tmp_ is not None:
prod_v = dt_float(1.0) * tmp_
return dict_, prod_v
def _init_date_time(self): # from csv handler
if os.path.exists(os.path.join(self.path, "start_datetime.info")):
with open(os.path.join(self.path, "start_datetime.info"), "r") as f:
a = f.read().rstrip().lstrip()
try:
tmp = datetime.strptime(a, "%Y-%m-%d %H:%M")
except ValueError:
tmp = datetime.strptime(a, "%Y-%m-%d")
except Exception:
raise ChronicsNotFoundError(
'Impossible to understand the content of "start_datetime.info". Make sure '
'it\'s composed of only one line with a datetime in the "%Y-%m-%d %H:%M"'
"format."
)
self.start_datetime = tmp
self.current_datetime = tmp
if os.path.exists(os.path.join(self.path, "time_interval.info")):
with open(os.path.join(self.path, "time_interval.info"), "r") as f:
a = f.read().rstrip().lstrip()
try:
tmp = datetime.strptime(a, "%H:%M")
except ValueError:
tmp = datetime.strptime(a, "%M")
except Exception:
raise ChronicsNotFoundError(
'Impossible to understand the content of "time_interval.info". Make sure '
'it\'s composed of only one line with a datetime in the "%H:%M"'
"format."
)
self.time_interval = timedelta(hours=tmp.hour, minutes=tmp.minute)
def fast_forward(self, nb_timestep):
for _ in range(nb_timestep):
self.load_next()
# for this class I suppose the real data AND the forecast are read each step
self.forecasts()