-
Notifications
You must be signed in to change notification settings - Fork 112
/
multiFolder.py
779 lines (636 loc) · 30 KB
/
multiFolder.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# 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 os
import json
import warnings
import numpy as np
from datetime import timedelta, datetime
from grid2op.dtypes import dt_int, dt_float
from grid2op.Exceptions import ChronicsNotFoundError, ChronicsError
from grid2op.Chronics.gridValue import GridValue
from grid2op.Chronics.gridStateFromFile import GridStateFromFile
class Multifolder(GridValue):
"""
The classes :class:`GridStateFromFile` and :class:`GridStateFromFileWithForecasts` implemented the reading of a
single folder representing a single episode.
This class is here to "loop" between different episode. Each one being stored in a folder readable by
:class:`GridStateFromFile` or one of its derivate (eg. :class:`GridStateFromFileWithForecasts`).
Chronics are always read in the alpha-numeric order for this class. This means that if the folder is not modified,
the data are always loaded in the same order, regardless of the :class:`grid2op.Backend`, :class:`grid2op.BaseAgent` or
:class:`grid2op.Environment`.
.. note::
Most grid2op environments, by default, use this type of "chronix", read from the hard drive.
Attributes
-----------
gridvalueClass: ``type``, optional
Type of class used to read the data from the disk. It defaults to :class:`GridStateFromFile`.
data: :class:`GridStateFromFile`
Data that will be loaded and used to produced grid state and forecasted values.
path: ``str``
Path where the folders of the episodes are stored.
sep: ``str``
Columns separtor, forwarded to :attr:`Multifolder.data` when it's built at the beginning of each episode.
subpaths: ``list``
List of all the episode that can be "played". It's a sorted list of all the directory in
:attr:`Multifolder.path`. Each one should contain data in a format that is readable by
:attr:`MultiFolder.gridvalueClass`.
"""
MULTI_CHRONICS = True
def __init__(
self,
path,
time_interval=timedelta(minutes=5),
start_datetime=datetime(year=2019, month=1, day=1),
gridvalueClass=GridStateFromFile,
sep=";",
max_iter=-1,
chunk_size=None,
filter_func=None,
**kwargs
):
self._kwargs = kwargs
GridValue.__init__(
self,
time_interval=time_interval,
max_iter=max_iter,
chunk_size=chunk_size,
start_datetime=start_datetime,
)
self.gridvalueClass = gridvalueClass
self.data = None
self.path = os.path.abspath(path)
self.sep = sep
self.init_subpath()
if len(self.subpaths) == 0:
raise ChronicsNotFoundError(
'Not chronics are found in "{}". Make sure there are at least '
"1 chronics folder there.".format(self.path)
)
# TODO clarify that
# np.random.shuffle(self.subpaths)
self.chunk_size = chunk_size
# for saving
self._order_backend_loads = None
self._order_backend_prods = None
self._order_backend_lines = None
self._order_backend_subs = None
self._names_chronics_to_backend = None
# improving looping strategy
if filter_func is None:
self._filter = self._default_filter
else:
if not callable(filter_func):
raise ChronicsError(
"The filtering function you provided ("
"kwargs: filter_func) is not callable."
)
self._filter = filter_func
self._prev_cache_id = 0
self._order = None
def init_subpath(self):
"""
Read the content of the main directory and initialize the `subpaths`
where the data could be located.
This is usefull, for example, if you generated data and want to be able to use them.
**NB** this has no effect until :attr:`Multifolder.reset` is called.
.. warning::
By default, it will only consider data that are present at creation time. If you add data after, you need
to call this function (and do a reset)
Examples
---------
A "typical" usage of this function can be the following workflow.
Start a script to train an agent (say "train_agent.py"):
.. code-block:: python
import os
import grid2op
from lightsim2grid import LightSimBackend # highly recommended for speed !
env_name = "l2rpn_wcci_2022" # only compatible with what comes next (at time of writing)
env = grid2op.make(env_name, backend=LightSimBackend())
# now train an agent
# see l2rpn_baselines package for more information, for example
# l2rpn-baselines.readthedocs.io/
from l2rpn_baselines.PPO_SB3 import train
nb_iter = 10000 # train for that many iterations
agent_name = "WhaetverIWant" # or any other name
agent_path = os.path.expand("~") # or anywhere else on your computer
trained_agent = train(env,
iterations=nb_iter,
name=agent_name,
save_path=agent_path)
On another script (say "generate_data.py"), you can generate more data:
.. code-block:: python
import grid2op
env_name = "l2rpn_wcci_2022" # only compatible with what comes next (at time of writing)
env = grid2op.make(env_name)
env.generate_data(nb_year=50) # generates 50 years of data
# (takes roughly 50s per week, around 45mins per year, in this case 50 * 45 mins = lots of minutes)
Let the script to generate the data run normally (don't interupt it).
And from time to time, in the script "train_agent.py" you can do:
.. code-block:: python
# reload the generated data
env.chronics_handler.init_subpath()
env.chronics_handler.reset()
# retrain the agent taking into account new data
trained_agent = train(env,
iterations=nb_iter,
name=agent_name,
save_path=agent_path,
load_path=agent_path
)
# the script to generate data is still running, you can reload some data again
env.chronics_handler.init_subpath()
env.chronics_handler.reset()
# retrain the agent
trained_agent = train(env,
iterations=nb_iter,
name=agent_name,
save_path=agent_path,
load_path=agent_path
)
# etc.
Both scripts you run "at the same time" for it to work efficiently.
To recap:
- script "generate_data.py" will... generate data
- these data will be reloaded from time to time by the script "train_agent.py"
.. warning::
Do not delete data between calls to `env.chronics_handler.init_subpath()` and `env.chronics_handler.reset()`,
and even less so during training !
If you want to delete data (for example not to overload your hard drive) you should remove them
right before calling `env.chronics_handler.init_subpath()`.
"""
try:
self.subpaths = [
os.path.join(self.path, el)
for el in os.listdir(self.path)
if os.path.isdir(os.path.join(self.path, el)) and (el != "__pycache__") and (not el.startswith("."))
]
self.subpaths.sort()
self.subpaths = np.array(self.subpaths)
except FileNotFoundError as exc_:
raise ChronicsError(
'Path "{}" doesn\'t exists.'.format(self.path)
) from exc_
self._order = None # to trigger a "reset" when chronix will next be loaded
def get_kwargs(self, dict_):
if self._filter != self._default_filter:
dict_["filter_func"] = self._filter
def available_chronics(self):
"""return the list of available chronics.
Examples
--------
# TODO
"""
return self.subpaths[self._order]
def _default_filter(self, x):
"""
default filter used at the initialization. It keeps only the first data encountered.
"""
return True
def set_filter(self, filter_fun):
"""
Assign a filtering function to remove some chronics from the next time a call to "reset_cache" is called.
**NB** filter_fun is applied to all element of :attr:`Multifolder.subpaths`. If ``True`` then it will
be put in cache, if ``False`` this data will NOT be put in the cache.
**NB** this has no effect until :attr:`Multifolder.reset` is called.
Examples
--------
Let's assume in your chronics, the folder names are "Scenario_august_dummy", and
"Scenario_february_dummy". For the sake of the example, we want the environment to loop
only through the month of february, because why not. Then we can do the following:
.. code-block:: python
import re
import grid2op
env = grid2op.make("l2rpn_neurips_2020_track1", test=True) # don't add "test=True" if
# you don't want to perform a test.
# check at which month will belong each observation
for i in range(10):
obs = env.reset()
print(obs.month)
# it always alternatively prints "8" (if chronics if from august) or
# "2" if chronics is from february)
# to see where the chronics are located
print(env.chronics_handler.subpaths)
# keep only the month of february
env.chronics_handler.set_filter(lambda path: re.match(".*february.*", path) is not None)
env.chronics_handler.reset() # if you don't do that it will not have any effect
for i in range(10):
obs = env.reset()
print(obs.month)
# it always prints "2" (representing february)
"""
self._filter = filter_fun
def next_chronics(self):
self._prev_cache_id += 1
# TODO implement the shuffling indeed.
# if self._prev_cache_id >= len(self._order):
# self.space_prng.shuffle(self._order)
self._prev_cache_id %= len(self._order)
def sample_next_chronics(self, probabilities=None):
"""
This function should be called before "next_chronics".
It can be used to sample non uniformly for the next next chronics.
Parameters
-----------
probabilities: ``np.ndarray``
Array of integer with the same size as the number of chronics in the cache.
If it does not sum to one, it is rescaled such that it sums to one.
Returns
-------
selected: ``int``
The integer that was selected.
Examples
--------
Let's assume in your chronics, the folder names are "Scenario_august_dummy", and
"Scenario_february_dummy". For the sake of the example, we want the environment to loop
75% of the time to the month of february and 25% of the time to the month of august.
.. code-block:: python
import grid2op
env = grid2op.make("l2rpn_neurips_2020_track1", test=True) # don't add "test=True" if
# you don't want to perform a test.
# check at which month will belong each observation
for i in range(10):
obs = env.reset()
print(obs.month)
# it always alternatively prints "8" (if chronics if from august) or
# "2" if chronics is from february) with a probability of 50% / 50%
env.seed(0) # for reproducible experiment
for i in range(10):
_ = env.chronics_handler.sample_next_chronics([0.25, 0.75])
obs = env.reset()
print(obs.month)
# it prints "2" with probability 0.75 and "8" with probability 0.25
"""
self._prev_cache_id = -1
if probabilities is None:
probabilities = np.ones(self._order.shape[0])
try:
probabilities = np.array(probabilities, dtype=dt_float)
except Exception as exc_:
raise ChronicsError("Impossible to convert the probablities given to an array of float") from exc_
sum_prob = probabilities.sum()
if abs(sum_prob) <= 1e-5:
raise ChronicsError("Impossible to use the given probabilities argument, it sums to 0. (or close enough to it)")
# make sure it sums to 1
probabilities /= sum_prob
# take one at "random" among these
selected = self.space_prng.choice(self._order, p=probabilities)
id_sel = np.where(self._order == selected)[0]
self._prev_cache_id = selected - 1
return id_sel
def reset(self):
"""
Rebuilt the :attr:`Multifolder._order`. This should be called after a call to :func:`Multifolder.set_filter`
is performed.
.. warning:: This "reset" is different from the `env.reset`. It should be only called after the function to set
the filtering function has been called.
This "reset" only reset which chronics are used for the environment.
Returns
-------
new_order: ``numpy.ndarray``, dtype: str
The selected chronics paths after a call to this method.
Notes
-----
Except explicitly mentioned, for example by :func:`Multifolder.set_filter` you should not use this
function. This will erased every selection of chronics, every shuffle etc.
"""
self._order = []
self._prev_cache_id = 0
for i, path in enumerate(self.subpaths):
if not self._filter(path):
continue
self._order.append(i)
if len(self._order) == 0:
raise ChronicsError(
'Impossible to initialize the Multifolder. Your "filter_fun" filters out all the '
"possible scenarios."
)
self._order = np.array(self._order)
return self.subpaths[self._order]
def initialize(
self,
order_backend_loads,
order_backend_prods,
order_backend_lines,
order_backend_subs,
names_chronics_to_backend=None,
):
self._order_backend_loads = order_backend_loads
self._order_backend_prods = order_backend_prods
self._order_backend_lines = order_backend_lines
self._order_backend_subs = order_backend_subs
self._names_chronics_to_backend = names_chronics_to_backend
self.n_gen = len(order_backend_prods)
self.n_load = len(order_backend_loads)
self.n_line = len(order_backend_lines)
if self._order is None:
# initialize the cache
self.reset()
id_scenario = self._order[self._prev_cache_id]
this_path = self.subpaths[id_scenario]
self.data = self.gridvalueClass(
time_interval=self.time_interval,
sep=self.sep,
path=this_path,
max_iter=self.max_iter,
chunk_size=self.chunk_size,
**self._kwargs
)
if self.seed is not None:
max_int = np.iinfo(dt_int).max
seed_chronics = self.space_prng.randint(max_int)
self.data.seed(seed_chronics)
self.data.initialize(
order_backend_loads,
order_backend_prods,
order_backend_lines,
order_backend_subs,
names_chronics_to_backend=names_chronics_to_backend,
)
def done(self):
"""
Tells the :class:`grid2op.Environment` if the episode is over.
Returns
-------
res: ``bool``
Whether or not the episode, represented by :attr:`MultiFolder.data` is over.
"""
return self.data.done()
def load_next(self):
"""
Load the next data from the current episode. It loads the next time step for the current episode.
Returns
-------
See the return type of :class:`GridStateFromFile.load_next` (or of :attr:`MultiFolder.gridvalueClass` if it
has been changed) for more information.
"""
return self.data.load_next()
def check_validity(self, backend):
"""
This method check that the data loaded can be properly read and understood by the :class:`grid2op.Backend`.
Parameters
----------
backend: :class:`grid2op.Backend`
The backend used for the experiment.
Returns
-------
See the return type of :class:`GridStateFromFile.check_validity` (or of :attr:`MultiFolder.gridvalueClass` if it
has been changed) for more information.
"""
return self.data.check_validity(backend)
def forecasts(self):
"""
The representation of the forecasted grid state(s), if any.
Returns
-------
See the return type of :class:`GridStateFromFile.forecasts` (or of :attr:`MultiFolder.gridvalueClass` if it
has been changed) for more information.
"""
return self.data.forecasts()
def tell_id(self, id_num, previous=False):
"""
This tells this chronics to load for the next episode.
By default, if id_num is greater than the number of episode, it is equivalent at restarting from the first
one: episode are played indefinitely in the same order.
Parameters
----------
id_num: ``int`` | ``str``
Id of the chronics to load.
previous:
Do you want to set to the previous value of this one or not (note that in general you want to set to
the previous value, as calling this function as an impact only after `env.reset()` is called)
"""
if isinstance(id_num, str):
# new accepted behaviour starting 1.6.4
# new in version 1.6.5: you only need to specify the chronics folder id and not the full path
found = False
for internal_id_, number in enumerate(self._order):
if (
self.subpaths[number] == id_num
or os.path.join(self.path, id_num) == self.subpaths[number]
):
self._prev_cache_id = internal_id_
found = True
if not found:
raise ChronicsError(
f'Impossible to find the chronics with id "{id_num}". The call to '
f"`env.chronics_handler.tell_id(...)` cannot be performed."
)
else:
# default behaviour prior to 1.6.4
self._prev_cache_id = id_num
self._prev_cache_id %= len(self._order)
if previous:
self._prev_cache_id -= 1
self._prev_cache_id %= len(self._order)
def get_id(self) -> str:
"""
Full absolute path of the current folder used for the current episode.
Returns
-------
res: ``str``
Path from which the data are generated for the current episode.
"""
return self.subpaths[self._order[self._prev_cache_id]]
def max_timestep(self):
return self.data.max_timestep()
def shuffle(self, shuffler=None):
"""
This method is used to have a better control on the order in which the subfolder containing the episode are
processed.
It can focus the evaluation on one specific folder, shuffle the folders, use only a subset of them etc. See the
examples for more information.
Parameters
----------
shuffler: ``object``
Shuffler should be a function that is called on :attr:`MultiFolder.subpaths` that will shuffle them.
It can also be used to remove some path if needed (see example).
Returns
--------
new_order: ``numpy.ndarray``, dtype: str
The order in which the chronics will be looped through
Examples
---------
If you want to simply shuffle the data you can do:
.. code-block:: python
# create an environment
import numpy as np
import grid2op
env_name = "l2rpn_case14_sandbox"
env = grid2op.make(env_name)
# shuffle the chronics (uniformly at random, without duplication)
env.chronics_handler.shuffle()
# use the environment as you want, here do 10 episode with the selected data
for i in range(10):
obs = env.reset()
print(f"Path of the chronics used: {env.chronics_handler.data.path}")
done = False
while not done:
act = ...
obs, reward, done, info = env.step(act)
# re shuffle them (still uniformly at random, without duplication)
env.chronics_handler.shuffle()
# use the environment as you want, here do 10 episode with the selected data
for i in range(10):
obs = env.reset()
print(f"Path of the chronics used: {env.chronics_handler.data.path}")
done = False
while not done:
act = ...
obs, reward, done, info = env.step(act)
If you want to use only a subset of the path, say for example the path with index 1, 5, and 6
.. code-block:: python
# create an environment
import numpy as np
import grid2op
env_name = "l2rpn_case14_sandbox"
env = grid2op.make(env_name)
# select the chronics (here 5 at random amongst the 10 "last" chronics of the environment)
nb_chron = len(env.chronics_handler.chronics_used)
chron_id_to_keep = np.random.choice(np.arange(nb_chron - 10, nb_chron), size=5, replace=True)
env.chronics_handler.shuffle(lambda x: chron_id_to_keep)
# use the environment as you want, here do 10 episode with the selected data
for i in range(10):
obs = env.reset()
print(f"Path of the chronics used: {env.chronics_handler.data.path}")
done = False
while not done:
act = ...
obs, reward, done, info = env.step(act)
# re shuffle them (uniformly at random, without duplication, among the chronics "selected" above.)
env.chronics_handler.shuffle()
# use the environment as you want, here do 10 episode with the selected data
for i in range(10):
obs = env.reset()
print(f"Path of the chronics used: {env.chronics_handler.data.path}")
done = False
while not done:
act = ...
obs, reward, done, info = env.step(act)
.. warning:: Though it is possible to use this "shuffle" function to only use some chronics, we highly
recommend you to have a look at the sections :ref:`environment-module-chronics-info` or
:ref:`environment-module-train-val-test`. It is likely that you will find better way to do
what you want to do there. Use this last example with care then.
.. warning:: As stated on the :func:`MultiFolder.reset`, any call to `env.chronics_handler.reset`
will remove anything related to shuffling, including the selection of chronics !
"""
if shuffler is None:
def shuffler(x):
return x[self.space_prng.choice(len(x), size=len(x), replace=False)]
self._order = 1 * shuffler(self._order)
return self.subpaths[self._order]
@property
def chronics_used(self):
"""return the full path of the chronics currently in use."""
return self.subpaths[self._order]
def set_chunk_size(self, new_chunk_size):
self.chunk_size = new_chunk_size
def split_and_save(self, datetime_beg, datetime_end, path_out):
"""
This function allows you to split the data (keeping only the data between datetime_beg and datetime_end) and to
save it on your local machine. This is espacially handy if you want to extract only a piece of the dataset we
provide for example.
Parameters
----------
datetime_beg: ``dict``
Keys are the name id of the scenarios you want to save. Values
are the corresponding starting date and time (in "%Y-%m-ùd %H:%M"
format). See example for more information.
datetime_end: ``dict``
keys must be the same as in the "datetime_beg" argument.
See example for more information
path_out: ``str``
The path were the data will be stored.
Examples
---------
Here is a short example on how to use it
.. code-block:: python
import grid2op
import os
env = grid2op.make()
env.chronics_handler.real_data.split_and_save({"004": "2019-01-08 02:00",
"005": "2019-01-30 08:00",
"006": "2019-01-17 00:00",
"007": "2019-01-17 01:00",
"008": "2019-01-21 09:00",
"009": "2019-01-22 12:00",
"010": "2019-01-27 19:00",
"011": "2019-01-15 12:00",
"012": "2019-01-08 13:00",
"013": "2019-01-22 00:00"},
{"004": "2019-01-11 02:00",
"005": "2019-02-01 08:00",
"006": "2019-01-18 00:00",
"007": "2019-01-18 01:00",
"008": "2019-01-22 09:00",
"009": "2019-01-24 12:00",
"010": "2019-01-29 19:00",
"011": "2019-01-17 12:00",
"012": "2019-01-10 13:00",
"013": "2019-01-24 00:00"},
path_out=os.path.join("/tmp"))
"""
if not isinstance(datetime_beg, dict):
datetime_beg_orig = datetime_beg
datetime_beg = {}
for subpath in self.subpaths:
id_this_chron = os.path.split(subpath)[-1]
datetime_beg[id_this_chron] = datetime_beg_orig
if not isinstance(datetime_end, dict):
datetime_end_orig = datetime_end
datetime_end = {}
for subpath in self.subpaths:
id_this_chron = os.path.split(subpath)[-1]
datetime_end[id_this_chron] = datetime_end_orig
seed_chronics_all = {}
for subpath in self.subpaths:
id_this_chron = os.path.split(subpath)[-1]
if not id_this_chron in datetime_beg:
continue
tmp = self.gridvalueClass(
time_interval=self.time_interval,
sep=self.sep,
path=subpath,
max_iter=self._max_iter,
chunk_size=self.chunk_size,
)
seed_chronics = None
if self.seed is not None:
max_int = np.iinfo(dt_int).max
seed_chronics = self.space_prng.randint(max_int)
tmp.seed(seed_chronics)
seed_chronics_all[subpath] = seed_chronics
tmp.initialize(
self._order_backend_loads,
self._order_backend_prods,
self._order_backend_lines,
self._order_backend_subs,
self._names_chronics_to_backend,
)
path_out_chron = os.path.join(path_out, id_this_chron)
tmp.split_and_save(
datetime_beg[id_this_chron], datetime_end[id_this_chron], path_out_chron
)
meta_params = {}
meta_params["datetime_beg"] = datetime_beg
meta_params["datetime_end"] = datetime_end
meta_params["path_out"] = path_out
meta_params["all_seeds"] = seed_chronics_all
try:
with open(
os.path.join(path_out, "split_and_save_meta_params.json"),
"w",
encoding="utf-8",
) as f:
json.dump(obj=meta_params, fp=f, sort_keys=True, indent=4)
except Exception as exc_:
warnings.warn(
'Impossible to save the "metadata" for the chronics with error:\n"{}"'
"".format(exc_)
)
def fast_forward(self, nb_timestep):
self.data.fast_forward(nb_timestep)