-
Notifications
You must be signed in to change notification settings - Fork 111
/
environment.py
1864 lines (1535 loc) · 75.5 KB
/
environment.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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 copy
import warnings
import numpy as np
import re
import grid2op
from grid2op.Opponent import OpponentSpace
from grid2op.dtypes import dt_float, dt_bool, dt_int
from grid2op.Action import (
ActionSpace,
BaseAction,
TopologyAction,
DontAct,
CompleteAction,
)
from grid2op.Exceptions import *
from grid2op.Observation import CompleteObservation, ObservationSpace, BaseObservation
from grid2op.Reward import FlatReward, RewardHelper, BaseReward
from grid2op.Rules import RulesChecker, AlwaysLegal, BaseRules
from grid2op.Backend import Backend
from grid2op.Chronics import ChronicsHandler
from grid2op.VoltageControler import ControlVoltageFromFile, BaseVoltageController
from grid2op.Environment.baseEnv import BaseEnv
from grid2op.Opponent import BaseOpponent, NeverAttackBudget
from grid2op.operator_attention import LinearAttentionBudget
class Environment(BaseEnv):
"""
This class is the grid2op implementation of the "Environment" entity in the RL framework.
Attributes
----------
name: ``str``
The name of the environment
action_space: :class:`grid2op.Action.ActionSpace`
Another name for :attr:`Environment.helper_action_player` for gym compatibility.
observation_space: :class:`grid2op.Observation.ObservationSpace`
Another name for :attr:`Environment.helper_observation` for gym compatibility.
reward_range: ``(float, float)``
The range of the reward function
metadata: ``dict``
For gym compatibility, do not use
spec: ``None``
For Gym compatibility, do not use
_viewer: ``object``
Used to display the powergrid. Currently properly supported.
"""
REGEX_SPLIT = r"^[a-zA-Z0-9]*$"
def __init__(
self,
init_env_path: str,
init_grid_path: str,
chronics_handler,
backend,
parameters,
name="unknown",
names_chronics_to_backend=None,
actionClass=TopologyAction,
observationClass=CompleteObservation,
rewardClass=FlatReward,
legalActClass=AlwaysLegal,
voltagecontrolerClass=ControlVoltageFromFile,
other_rewards={},
thermal_limit_a=None,
with_forecast=True,
epsilon_poly=1e-4, # precision of the redispatching algorithm we don't recommend to go above 1e-4
tol_poly=1e-2, # i need to compute a redispatching if the actual values are "more than tol_poly" the values they should be
opponent_space_type=OpponentSpace,
opponent_action_class=DontAct,
opponent_class=BaseOpponent,
opponent_init_budget=0.0,
opponent_budget_per_ts=0.0,
opponent_budget_class=NeverAttackBudget,
opponent_attack_duration=0,
opponent_attack_cooldown=99999,
kwargs_opponent={},
attention_budget_cls=LinearAttentionBudget,
kwargs_attention_budget={},
has_attention_budget=False,
logger=None,
kwargs_observation=None,
observation_bk_class=None,
observation_bk_kwargs=None,
highres_sim_counter=None,
_update_obs_after_reward=True,
_init_obs=None,
_raw_backend_class=None,
_compat_glop_version=None,
_read_from_local_dir=True, # TODO runner and all here !
_is_test=False,
):
BaseEnv.__init__(
self,
init_env_path=init_env_path,
init_grid_path=init_grid_path,
parameters=parameters,
thermal_limit_a=thermal_limit_a,
epsilon_poly=epsilon_poly,
tol_poly=tol_poly,
other_rewards=other_rewards,
with_forecast=with_forecast,
voltagecontrolerClass=voltagecontrolerClass,
opponent_space_type=opponent_space_type,
opponent_action_class=opponent_action_class,
opponent_class=opponent_class,
opponent_budget_class=opponent_budget_class,
opponent_init_budget=opponent_init_budget,
opponent_budget_per_ts=opponent_budget_per_ts,
opponent_attack_duration=opponent_attack_duration,
opponent_attack_cooldown=opponent_attack_cooldown,
kwargs_opponent=kwargs_opponent,
has_attention_budget=has_attention_budget,
attention_budget_cls=attention_budget_cls,
kwargs_attention_budget=kwargs_attention_budget,
logger=logger.getChild("grid2op_Environment")
if logger is not None
else None,
kwargs_observation=kwargs_observation,
observation_bk_class=observation_bk_class,
observation_bk_kwargs=observation_bk_kwargs,
highres_sim_counter=highres_sim_counter,
update_obs_after_reward=_update_obs_after_reward,
_init_obs=_init_obs,
_is_test=_is_test, # is this created with "test=True" # TODO not implemented !!
)
if name == "unknown":
warnings.warn(
'It is NOT recommended to create an environment without "make" and EVEN LESS '
"to use an environment without a name..."
)
self.name = name
self._read_from_local_dir = _read_from_local_dir
# for gym compatibility (initialized below)
# self.action_space = None
# self.observation_space = None
self.reward_range = None
self._viewer = None
self.metadata = None
self.spec = None
if _raw_backend_class is None:
self._raw_backend_class = type(backend)
else:
self._raw_backend_class = _raw_backend_class
self._compat_glop_version = _compat_glop_version
# for plotting
self._init_backend(
chronics_handler,
backend,
names_chronics_to_backend,
actionClass,
observationClass,
rewardClass,
legalActClass,
)
self._actionClass_orig = actionClass
self._observationClass_orig = observationClass
def _init_backend(
self,
chronics_handler,
backend,
names_chronics_to_backend,
actionClass,
observationClass,
rewardClass,
legalActClass,
):
"""
INTERNAL
.. warning:: /!\\\\ Internal, do not use unless you know what you are doing /!\\\\
Create a proper and valid environment.
"""
if isinstance(rewardClass, type):
if not issubclass(rewardClass, BaseReward):
raise Grid2OpException(
'Parameter "rewardClass" used to build the Environment should derived form '
'the grid2op.BaseReward class, type provided is "{}"'.format(
type(rewardClass)
)
)
else:
if not isinstance(rewardClass, BaseReward):
raise Grid2OpException(
'Parameter "rewardClass" used to build the Environment should derived form '
'the grid2op.BaseReward class, type provided is "{}"'.format(
type(rewardClass)
)
)
# backend
if not isinstance(backend, Backend):
raise Grid2OpException(
'Parameter "backend" used to build the Environment should derived form the '
'grid2op.Backend class, type provided is "{}"'.format(type(backend))
)
self.backend = backend
if self.backend.is_loaded and self._init_obs is None:
raise EnvError(
"Impossible to use the same backend twice. Please create your environment with a "
"new backend instance (new object)."
)
need_process_backend = False
if not self.backend.is_loaded:
# usual case: the backend is not loaded
# NB it is loaded when the backend comes from an observation for
# example
if self._read_from_local_dir:
# test to support pickle conveniently
self.backend._PATH_ENV = self.get_path_env()
# all the above should be done in this exact order, otherwise some weird behaviour might occur
# this is due to the class attribute
self.backend.set_env_name(self.name)
self.backend.load_grid(
self._init_grid_path
) # the real powergrid of the environment
try:
self.backend.load_redispacthing_data(self.get_path_env())
except BackendError as exc_:
self.backend.redispatching_unit_commitment_availble = False
warnings.warn(f"Impossible to load redispatching data. This is not an error but you will not be able "
f"to use all grid2op functionalities. "
f"The error was: \"{exc_}\"")
self.backend.load_storage_data(self.get_path_env())
exc_ = self.backend.load_grid_layout(self.get_path_env())
if exc_ is not None:
warnings.warn(
f"No layout have been found for you grid (or the layout provided was corrupted). You will "
f'not be able to use the renderer, plot the grid etc. The error was "{exc_}"'
)
self.backend.is_loaded = True
# alarm set up
self.load_alarm_data()
self.load_alert_data()
# to force the initialization of the backend to the proper type
self.backend.assert_grid_correct()
need_process_backend = True
self._handle_compat_glop_version(need_process_backend)
self._has_been_initialized() # really important to include this piece of code! and just here after the
# backend has loaded everything
self._line_status = np.ones(shape=self.n_line, dtype=dt_bool)
self._disc_lines = np.zeros(shape=self.n_line, dtype=dt_int) - 1
if self._thermal_limit_a is None:
self._thermal_limit_a = self.backend.thermal_limit_a.astype(dt_float)
else:
self.backend.set_thermal_limit(self._thermal_limit_a.astype(dt_float))
*_, tmp = self.backend.generators_info()
# rules of the game
self._check_rules_correct(legalActClass)
self._game_rules = RulesChecker(legalActClass=legalActClass)
self._game_rules.initialize(self)
self._legalActClass = legalActClass
# action helper
if not isinstance(actionClass, type):
raise Grid2OpException(
'Parameter "actionClass" used to build the Environment should be a type (a class) '
"and not an object (an instance of a class). "
'It is currently "{}"'.format(type(legalActClass))
)
if not issubclass(actionClass, BaseAction):
raise Grid2OpException(
'Parameter "actionClass" used to build the Environment should derived form the '
'grid2op.BaseAction class, type provided is "{}"'.format(
type(actionClass)
)
)
if not isinstance(observationClass, type):
raise Grid2OpException(
f'Parameter "observationClass" used to build the Environment should be a type (a class) '
f"and not an object (an instance of a class). "
f'It is currently : {observationClass} (type "{type(observationClass)}")'
)
if not issubclass(observationClass, BaseObservation):
raise Grid2OpException(
f'Parameter "observationClass" used to build the Environment should derived form the '
f'grid2op.BaseObservation class, type provided is "{type(observationClass)}"'
)
# action affecting the grid that will be made by the agent
bk_type = type(
self.backend
) # be careful here: you need to initialize from the class, and not from the object
self._rewardClass = rewardClass
self._actionClass = actionClass.init_grid(gridobj=bk_type)
self._actionClass._add_shunt_data()
self._actionClass._update_value_set()
self._observationClass = observationClass.init_grid(gridobj=bk_type)
self._complete_action_cls = CompleteAction.init_grid(gridobj=bk_type)
self._helper_action_class = ActionSpace.init_grid(gridobj=bk_type)
self._action_space = self._helper_action_class(
gridobj=bk_type,
actionClass=actionClass,
legal_action=self._game_rules.legal_action,
)
# action that affect the grid made by the environment.
self._helper_action_env = self._helper_action_class(
gridobj=bk_type,
actionClass=CompleteAction,
legal_action=self._game_rules.legal_action,
)
# handles input data
if not isinstance(chronics_handler, ChronicsHandler):
raise Grid2OpException(
'Parameter "chronics_handler" used to build the Environment should derived form the '
'grid2op.ChronicsHandler class, type provided is "{}"'.format(
type(chronics_handler)
)
)
if names_chronics_to_backend is None and type(self.backend).IS_BK_CONVERTER:
names_chronics_to_backend = self.backend.names_target_to_source
self.chronics_handler = chronics_handler
self.chronics_handler.initialize(
self.name_load,
self.name_gen,
self.name_line,
self.name_sub,
names_chronics_to_backend=names_chronics_to_backend,
)
self._names_chronics_to_backend = names_chronics_to_backend
self.delta_time_seconds = dt_float(self.chronics_handler.time_interval.seconds)
# this needs to be done after the chronics handler: rewards might need information
# about the chronics to work properly.
self._helper_observation_class = ObservationSpace.init_grid(gridobj=bk_type)
# FYI: this try to copy the backend if it fails it will modify the backend
# and the environment to force the deactivation of the
# forecasts
self._observation_space = self._helper_observation_class(
gridobj=bk_type,
observationClass=observationClass,
actionClass=actionClass,
rewardClass=rewardClass,
env=self,
kwargs_observation=self._kwargs_observation,
observation_bk_class=self._observation_bk_class,
observation_bk_kwargs=self._observation_bk_kwargs
)
# test to make sure the backend is consistent with the chronics generator
self.chronics_handler.check_validity(self.backend)
self._reset_storage() # this should be called after the self.delta_time_seconds is set
# reward function
self._reward_helper = RewardHelper(self._rewardClass, logger=self.logger)
self._reward_helper.initialize(self)
for k, v in self.other_rewards.items():
v.initialize(self)
# controller for voltage
if not issubclass(self._voltagecontrolerClass, BaseVoltageController):
raise Grid2OpException(
'Parameter "voltagecontrolClass" should derive from "ControlVoltageFromFile".'
)
self._voltage_controler = self._voltagecontrolerClass(
gridobj=bk_type,
controler_backend=self.backend,
actionSpace_cls=self._helper_action_class,
)
# create the opponent
# At least the 3 following attributes should be set before calling _create_opponent
self._create_opponent()
# create the attention budget
self._create_attention_budget()
# init the alert relate attributes
self._init_alert_data()
# performs one step to load the environment properly (first action need to be taken at first time step after
# first injections given)
self._reset_maintenance()
self._reset_redispatching()
self._reward_to_obs = {}
do_nothing = self._helper_action_env({})
*_, fail_to_start, info = self.step(do_nothing)
if fail_to_start:
raise Grid2OpException(
"Impossible to initialize the powergrid, the powerflow diverge at iteration 0. "
"Available information are: {}".format(info)
)
# test the backend returns object of the proper size
if need_process_backend:
self.backend.assert_grid_correct_after_powerflow()
# for gym compatibility
self.reward_range = self._reward_helper.range()
self._viewer = None
self.viewer_fig = None
self.metadata = {"render.modes": ["rgb_array"]}
self.spec = None
self.current_reward = self.reward_range[0]
self.done = False
# reset everything to be consistent
self._reset_vectors_and_timings()
def max_episode_duration(self):
"""
Return the maximum duration (in number of steps) of the current episode.
Notes
-----
For possibly infinite episode, the duration is returned as `np.iinfo(np.int32).max` which corresponds
to the maximum 32 bit integer (usually `2147483647`)
"""
tmp = dt_int(self.chronics_handler.max_episode_duration())
if tmp < 0:
tmp = dt_int(np.iinfo(dt_int).max)
return tmp
def set_max_iter(self, max_iter):
"""
Parameters
----------
max_iter: ``int``
The maximum number of iteration you can do before reaching the end of the episode. Set it to "-1" for
possibly infinite episode duration.
Notes
-------
Maximum length of the episode can depend on the chronics used. See :attr:`Environment.chronics_handler` for
more information
"""
self.chronics_handler.set_max_iter(max_iter)
@property
def _helper_observation(self):
return self._observation_space
@property
def _helper_action_player(self):
return self._action_space
def _handle_compat_glop_version(self, need_process_backend):
if (
self._compat_glop_version is not None
and self._compat_glop_version != grid2op.__version__
):
warnings.warn(
'You are using a grid2op "compatibility" environment. This means that some '
"feature will not be available. This feature is absolutely NOT recommended except to "
"read back data (for example with EpisodeData) that were stored with previous "
"grid2op version."
)
if need_process_backend:
self.backend.set_env_name(f"{self.name}_{self._compat_glop_version}")
cls_bk = type(self.backend)
cls_bk.glop_version = self._compat_glop_version
if cls_bk.glop_version == cls_bk.BEFORE_COMPAT_VERSION:
# oldest version: no storage and no curtailment available
# deactivate storage
# recompute the topology vector (more or less everything need to be adjusted...
stor_locs = [pos for pos in cls_bk.storage_pos_topo_vect]
for stor_loc in sorted(stor_locs, reverse=True):
for vect in [
cls_bk.load_pos_topo_vect,
cls_bk.gen_pos_topo_vect,
cls_bk.line_or_pos_topo_vect,
cls_bk.line_ex_pos_topo_vect,
]:
vect[vect >= stor_loc] -= 1
# deals with the "sub_pos" vector
for sub_id in range(cls_bk.n_sub):
if (cls_bk.storage_to_subid == sub_id).any():
stor_ids = np.where(cls_bk.storage_to_subid == sub_id)[0]
stor_locs = cls_bk.storage_to_sub_pos[stor_ids]
for stor_loc in sorted(stor_locs, reverse=True):
for vect, sub_id_me in zip(
[
cls_bk.load_to_sub_pos,
cls_bk.gen_to_sub_pos,
cls_bk.line_or_to_sub_pos,
cls_bk.line_ex_to_sub_pos,
],
[
cls_bk.load_to_subid,
cls_bk.gen_to_subid,
cls_bk.line_or_to_subid,
cls_bk.line_ex_to_subid,
],
):
vect[(vect >= stor_loc) & (sub_id_me == sub_id)] -= 1
# remove storage from the number of element in the substation
for sub_id in range(cls_bk.n_sub):
cls_bk.sub_info[sub_id] -= (cls_bk.storage_to_subid == sub_id).sum()
# remove storage from the total number of element
cls_bk.dim_topo -= cls_bk.n_storage
# recompute this private member
cls_bk._topo_vect_to_sub = np.repeat(
np.arange(cls_bk.n_sub), repeats=cls_bk.sub_info
)
self.backend._topo_vect_to_sub = np.repeat(
np.arange(cls_bk.n_sub), repeats=cls_bk.sub_info
)
new_grid_objects_types = cls_bk.grid_objects_types
new_grid_objects_types = new_grid_objects_types[
new_grid_objects_types[:, cls_bk.STORAGE_COL] == -1, :
]
cls_bk.grid_objects_types = 1 * new_grid_objects_types
self.backend.grid_objects_types = 1 * new_grid_objects_types
# erase all trace of storage units
cls_bk.set_no_storage()
Environment.deactivate_storage(self.backend)
if need_process_backend:
# the following line must be called BEFORE "self.backend.assert_grid_correct()" !
self.backend.storage_deact_for_backward_comaptibility()
# and recomputes everything while making sure everything is consistent
self.backend.assert_grid_correct()
type(self.backend)._topo_vect_to_sub = np.repeat(
np.arange(cls_bk.n_sub), repeats=cls_bk.sub_info
)
type(self.backend).grid_objects_types = new_grid_objects_types
def _voltage_control(self, agent_action, prod_v_chronics):
"""
INTERNAL
.. warning:: /!\\\\ Internal, do not use unless you know what you are doing /!\\\\
Update the environment action "action_env" given a possibly new voltage setpoint for the generators. This
function can be overide for a more complex handling of the voltages.
It must update (if needed) the voltages of the environment action :attr:`BaseEnv.env_modification`
Parameters
----------
agent_action: :class:`grid2op.Action.Action`
The action performed by the player (or do nothing is player action were not legal or ambiguous)
prod_v_chronics: ``numpy.ndarray`` or ``None``
The voltages that has been specified in the chronics
"""
volt_control_act = self._voltage_controler.fix_voltage(
self.current_obs, agent_action, self._env_modification, prod_v_chronics
)
return volt_control_act
def set_chunk_size(self, new_chunk_size):
"""
For an efficient data pipeline, it can be usefull to not read all part of the input data
(for example for load_p, prod_p, load_q, prod_v). Grid2Op support the reading of large chronics by "chunk"
of given size.
Reading data in chunk can also reduce the memory footprint, useful in case of multiprocessing environment while
large chronics.
It is critical to set a small chunk_size in case of training machine learning algorithm (reinforcement
learning agent) at the beginning when the agent performs poorly, the software might spend most of its time
loading the data.
**NB** this has no effect if the chronics does not support this feature.
**NB** The environment need to be **reset** for this to take effect (it won't affect the chronics already
loaded)
Parameters
----------
new_chunk_size: ``int`` or ``None``
The new chunk size (positive integer)
Examples
---------
Here is an example on how to use this function
.. code-block:: python
import grid2op
# I create an environment
env = grid2op.make("l2rpn_case14_sandbox", test=True)
env.set_chunk_size(100)
env.reset() # otherwise chunk size has no effect !
# and now data will be read from the hard drive 100 time steps per 100 time steps
# instead of the whole episode at once.
"""
if new_chunk_size is None:
self.chronics_handler.set_chunk_size(new_chunk_size)
return
try:
new_chunk_size = int(new_chunk_size)
except Exception as exc_:
raise Grid2OpException(
"Impossible to set the chunk size. It should be convertible a integer, and not"
'{}. The error was: \n"{}"'.format(new_chunk_size, exc_)
)
if new_chunk_size <= 0:
raise Grid2OpException(
'Impossible to read less than 1 data at a time. Please make sure "new_chunk_size"'
"is a positive integer."
)
self.chronics_handler.set_chunk_size(new_chunk_size)
def simulate(self, action):
"""
Another method to call `obs.simulate` to ensure compatibility between multi environment and
regular one.
Parameters
----------
action:
A grid2op action
Returns
-------
Same return type as :func:`grid2op.Environment.BaseEnv.step` or
:func:`grid2op.Observation.BaseObservation.simulate`
Notes
-----
Prefer using `obs.simulate` if possible, it will be faster than this function.
"""
return self.get_obs().simulate(action)
def set_id(self, id_):
"""
Set the id that will be used at the next call to :func:`Environment.reset`.
**NB** this has no effect if the chronics does not support this feature.
**NB** The environment need to be **reset** for this to take effect.
Parameters
----------
id_: ``int``
the id of the chronics used.
Examples
--------
Here an example that will loop 10 times through the same chronics (always using the same injection then):
.. code-block:: python
import grid2op
from grid2op import make
from grid2op.BaseAgent import DoNothingAgent
env = make("l2rpn_case14_sandbox") # create an environment
agent = DoNothingAgent(env.action_space) # create an BaseAgent
for i in range(10):
env.set_id(0) # tell the environment you simply want to use the chronics with ID 0
obs = env.reset() # it is necessary to perform a reset
reward = env.reward_range[0]
done = False
while not done:
act = agent.act(obs, reward, done)
obs, reward, done, info = env.step(act)
And here you have an example on how you can loop through the scenarios in a given order:
.. code-block:: python
import grid2op
from grid2op import make
from grid2op.BaseAgent import DoNothingAgent
env = make("l2rpn_case14_sandbox") # create an environment
agent = DoNothingAgent(env.action_space) # create an BaseAgent
scenario_order = [1,2,3,4,5,10,8,6,5,7,78, 8]
for id_ in scenario_order:
env.set_id(id_) # tell the environment you simply want to use the chronics with ID 0
obs = env.reset() # it is necessary to perform a reset
reward = env.reward_range[0]
done = False
while not done:
act = agent.act(obs, reward, done)
obs, reward, done, info = env.step(act)
"""
if isinstance(id_, str):
# new in grid2op 1.6.4
self.chronics_handler.tell_id(id_, previous=True)
return
try:
id_ = int(id_)
except Exception as exc_:
raise EnvError(
'the "id_" parameters should be convertible to integer and not be of type {}'
'with error \n"{}"'.format(type(id_), exc_)
)
self.chronics_handler.tell_id(id_ - 1)
def attach_renderer(self, graph_layout=None):
"""
This function will attach a renderer, necessary to use for plotting capabilities.
Parameters
----------
graph_layout: ``dict``
Here for backward compatibility. Currently not used.
If you want to set a specific layout call :func:`BaseEnv.attach_layout`
If ``None`` this class will use the default substations layout provided when the environment was created.
Otherwise it will use the data provided.
Examples
---------
Here is how to use the function
.. code-block:: python
import grid2op
# create the environment
env = grid2op.make("l2rpn_case14_sandbox")
if False:
# if you want to change the default layout of the powergrid
# assign coordinates (0., 0.) to all substations (this is a dummy thing to do here!)
layout = {sub_name: (0., 0.) for sub_name in env.name_sub}
env.attach_layout(layout)
# NB again, this code will make everything look super ugly !!!! Don't change the
# default layout unless you have a reason to.
# and if you want to use the renderer
env.attach_renderer()
# and now you can "render" (plot) the state of the grid
obs = env.reset()
done = False
reward = env.reward_range[0]
while not done:
env.render()
action = agent.act(obs, reward, done)
obs, reward, done, info = env.step(action)
"""
# Viewer already exists: skip
if self._viewer is not None:
return
# Do we have the dependency
try:
from grid2op.PlotGrid import PlotMatplot
except ImportError:
err_msg = (
"Cannot attach renderer: missing dependency\n"
"Please install matplotlib or run pip install grid2op[optional]"
)
raise Grid2OpException(err_msg) from None
self._viewer = PlotMatplot(self._observation_space)
self.viewer_fig = None
# Set renderer modes
self.metadata = {"render.modes": ["silent", "rgb_array"]} # "human",
def __str__(self):
return "<{} instance named {}>".format(type(self).__name__, self.name)
# TODO be closer to original gym implementation
def reset_grid(self):
"""
INTERNAL
.. warning:: /!\\\\ Internal, do not use unless you know what you are doing /!\\\\
This is automatically called when using `env.reset`
Reset the backend to a clean state by reloading the powergrid from the hard drive.
This might takes some time.
If the thermal has been modified, it also modify them into the new backend.
"""
self.backend.reset(
self._init_grid_path
) # the real powergrid of the environment
self.backend.assert_grid_correct()
if self._thermal_limit_a is not None:
self.backend.set_thermal_limit(self._thermal_limit_a.astype(dt_float))
self._backend_action = self._backend_action_class()
self.nb_time_step = -1 # to have init obs at step 1
do_nothing = self._helper_action_env({})
*_, fail_to_start, info = self.step(do_nothing)
if fail_to_start:
raise Grid2OpException(
"Impossible to initialize the powergrid, the powerflow diverge at iteration 0. "
"Available information are: {}".format(info)
)
# assign the right
self._observation_space.set_real_env_kwargs(self)
def add_text_logger(self, logger=None):
"""
Add a text logger to this :class:`Environment`
Logging is for now an incomplete feature, really incomplete (not used)
Parameters
----------
logger:
The logger to use
"""
self.logger = logger
return self
def reset(self) -> BaseObservation:
"""
Reset the environment to a clean state.
It will reload the next chronics if any. And reset the grid to a clean state.
This triggers a full reloading of both the chronics (if they are stored as files) and of the powergrid,
to ensure the episode is fully over.
This method should be called only at the end of an episode.
Examples
--------
The standard "gym loop" can be done with the following code:
.. code-block:: python
import grid2op
# create the environment
env = grid2op.make("l2rpn_case14_sandbox")
# and now you can "render" (plot) the state of the grid
obs = env.reset()
done = False
reward = env.reward_range[0]
while not done:
action = agent.act(obs, reward, done)
obs, reward, done, info = env.step(action)
"""
super().reset()
self.chronics_handler.next_chronics()
self.chronics_handler.initialize(
self.backend.name_load,
self.backend.name_gen,
self.backend.name_line,
self.backend.name_sub,
names_chronics_to_backend=self._names_chronics_to_backend,
)
self._env_modification = None
self._reset_maintenance()
self._reset_redispatching()
self._reset_vectors_and_timings() # it need to be done BEFORE to prevent cascading failure when there has been
self.reset_grid()
if self.viewer_fig is not None:
del self.viewer_fig
self.viewer_fig = None
# if True, then it will not disconnect lines above their thermal limits
self._reset_vectors_and_timings() # and it needs to be done AFTER to have proper timings at tbe beginning
# the attention budget is reset above
# reset the opponent
self._oppSpace.reset()
# reset, if need, reward and other rewards
self._reward_helper.reset(self)
for extra_reward in self.other_rewards.values():
extra_reward.reset(self)
# and reset also the "simulated env" in the observation space
self._observation_space.reset(self)
self._observation_space.set_real_env_kwargs(self)
self._last_obs = None # force the first observation to be generated properly
if self._init_obs is not None:
self._reset_to_orig_state(self._init_obs)
return self.get_obs()
def render(self, mode="rgb_array"):
"""
Render the state of the environment on the screen, using matplotlib
Also returns the Matplotlib figure
Examples
--------
Rendering need first to define a "renderer" which can be done with the following code:
.. code-block:: python
import grid2op
# create the environment
env = grid2op.make("l2rpn_case14_sandbox")
# if you want to use the renderer
env.attach_renderer()
# and now you can "render" (plot) the state of the grid
obs = env.reset()
done = False
reward = env.reward_range[0]
while not done:
env.render() # this piece of code plot the grid
action = agent.act(obs, reward, done)
obs, reward, done, info = env.step(action)
"""
# Try to create a plotter instance
# Does nothing if viewer exists
# Raises if matplot is not installed
self.attach_renderer()
# Check mode is correct
if mode not in self.metadata["render.modes"]:
err_msg = 'Renderer mode "{}" not supported. Available modes are {}.'
raise Grid2OpException(err_msg.format(mode, self.metadata["render.modes"]))
# Render the current observation
fig = self._viewer.plot_obs(
self.current_obs, figure=self.viewer_fig, redraw=True
)
# First time show for human mode
if self.viewer_fig is None and mode == "human":
fig.show()
else: # Update the figure content
fig.canvas.draw()
# Store to re-use the figure
self.viewer_fig = fig
# Return the rgb array
rgb_array = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8).reshape(self._viewer.height, self._viewer.width, 3)
return rgb_array
def _custom_deepcopy_for_copy(self, new_obj):
super()._custom_deepcopy_for_copy(new_obj)
new_obj.name = self.name
new_obj._read_from_local_dir = self._read_from_local_dir
new_obj.metadata = copy.deepcopy(self.metadata)
new_obj.spec = copy.deepcopy(self.spec)
new_obj._raw_backend_class = self._raw_backend_class
new_obj._compat_glop_version = self._compat_glop_version
new_obj._actionClass_orig = self._actionClass_orig
new_obj._observationClass_orig = self._observationClass_orig