/
BaseObservation.py
723 lines (580 loc) · 32.6 KB
/
BaseObservation.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
# 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 copy
import numpy as np
from abc import abstractmethod
import pdb
from grid2op.dtypes import dt_int, dt_float, dt_bool
from grid2op.Exceptions import *
from grid2op.Space import GridObjects
# TODO be able to change reward here
# TODO make an action with the difference between the observation that would be an action.
# TODO have a method that could do "forecast" by giving the _injection by the agent, if he wants to make custom forecasts
# TODO finish documentation
# TODO fix "bug" when action not initalized, return nan in to_vect
class BaseObservation(GridObjects):
"""
Basic class representing an observation.
All observation must derive from this class and implement all its abstract methods.
Attributes
----------
action_helper: :class:`grid2op.Action.ActionSpace`
A reprensentation of the possible action space.
year: ``int``
The current year
month: ``int``
The current month (0 = january, 11 = december)
day: ``int``
The current day of the month
hour_of_day: ``int``
The current hour of the day
minute_of_hour: ``int``
The current minute of the current hour
day_of_week: ``int``
The current day of the week. Monday = 0, Sunday = 6
prod_p: :class:`numpy.ndarray`, dtype:float
The active production value of each generator (expressed in MW).
prod_q: :class:`numpy.ndarray`, dtype:float
The reactive production value of each generator (expressed in MVar).
prod_v: :class:`numpy.ndarray`, dtype:float
The voltage magnitude of the bus to which each generator is connected (expressed in kV).
load_p: :class:`numpy.ndarray`, dtype:float
The active load value of each consumption (expressed in MW).
load_q: :class:`numpy.ndarray`, dtype:float
The reactive load value of each consumption (expressed in MVar).
load_v: :class:`numpy.ndarray`, dtype:float
The voltage magnitude of the bus to which each consumption is connected (expressed in kV).
p_or: :class:`numpy.ndarray`, dtype:float
The active power flow at the origin end of each powerline (expressed in MW).
q_or: :class:`numpy.ndarray`, dtype:float
The reactive power flow at the origin end of each powerline (expressed in MVar).
v_or: :class:`numpy.ndarray`, dtype:float
The voltage magnitude at the bus to which the origin end of each powerline is connected (expressed in kV).
a_or: :class:`numpy.ndarray`, dtype:float
The current flow at the origin end of each powerline (expressed in A).
p_ex: :class:`numpy.ndarray`, dtype:float
The active power flow at the extremity end of each powerline (expressed in MW).
q_ex: :class:`numpy.ndarray`, dtype:float
The reactive power flow at the extremity end of each powerline (expressed in MVar).
v_ex: :class:`numpy.ndarray`, dtype:float
The voltage magnitude at the bus to which the extremity end of each powerline is connected (expressed in kV).
a_ex: :class:`numpy.ndarray`, dtype:float
The current flow at the extremity end of each powerline (expressed in A).
rho: :class:`numpy.ndarray`, dtype:float
The capacity of each powerline. It is defined at the observed current flow divided by the thermal limit of each
powerline (no unit)
connectivity_matrix_: :class:`numpy.ndarray`, dtype:float
The connectivityt matrix (if computed, or None) see definition of :func:`connectivity_matrix` for
more information
bus_connectivity_matrix_: :class:`numpy.ndarray`, dtype:float
The `bus_connectivity_matrix_` matrix (if computed, or None) see definition of
:func:`BaseObservation.bus_connectivity_matrix` for more information
vectorized: :class:`numpy.ndarray`, dtype:float
The vector representation of this BaseObservation (if computed, or None) see definition of
:func:`to_vect` for more information.
topo_vect: :class:`numpy.ndarray`, dtype:int
For each object (load, generator, ends of a powerline) it gives on which bus this object is connected
in its substation. See :func:`grid2op.Backend.Backend.get_topo_vect` for more information.
line_status: :class:`numpy.ndarray`, dtype:bool
Gives the status (connected / disconnected) for every powerline (``True`` at position `i` means the powerline
`i` is connected)
timestep_overflow: :class:`numpy.ndarray`, dtype:int
Gives the number of time steps since a powerline is in overflow.
time_before_cooldown_line: :class:`numpy.ndarray`, dtype:int
For each powerline, it gives the number of time step the powerline is unavailable due to "cooldown"
(see :attr:`grid2op.Parameters.Parameters.NB_TIMESTEP_LINE_STATUS_REMODIF` for more information). 0 means the
an action will be able to act on this same powerline, a number > 0 (eg 1) means that an action at this time step
cannot act on this powerline (in the example the agent have to wait 1 time step)
time_before_cooldown_sub: :class:`numpy.ndarray`, dtype:int
Same as :attr:`BaseObservation.time_before_cooldown_line` but for substations. For each substation, it gives the
number of timesteps to wait before acting on this substation (see
see :attr:`grid2op.Parameters.Parameters.NB_TIMESTEP_TOPOLOGY_REMODIF` for more information).
time_next_maintenance: :class:`numpy.ndarray`, dtype:int
For each powerline, it gives the time of the next planned maintenance. For example if there is:
- `1` at position `i` it means that the powerline `i` will be disconnected for maintenance operation at the next time step. A
- `0` at position `i` means that powerline `i` is disconnected from the powergrid for maintenance operation
at the current time step.
- `-1` at position `i` means that powerline `i` will not be disconnected for maintenance reason for this
episode.
duration_next_maintenance: :class:`numpy.ndarray`, dtype:int
For each powerline, it gives the number of time step that the maintenance will last (if any). This means that,
if at position `i` of this vector:
- there is a `0`: the powerline is not disconnected from the grid for maintenance
- there is a `1`, `2`, ... the powerline will be disconnected for at least `1`, `2`, ... timestep (**NB**
in all case, the powerline will stay disconnected until a :class:`grid2op.BaseAgent.BaseAgent` performs the
proper :class:`grid2op.BaseAction.BaseAction` to reconnect it).
target_dispatch: :class:`numpy.ndarray`, dtype:float
For **each** generators, it gives the target redispatching, asked by the agent. This is the sum of all
redispatching asked by the agent for during all the episode. It for each generator it is a number between:
- pmax and pmax. Note that there is information about all generators there, even the one that are not
dispatchable.
actual_dispatch: :class:`numpy.ndarray`, dtype:float
For **each** generators, it gives the redispatching currently implemented by the environment.
Indeed, the environment tries to implement at best the :attr:`BaseObservation.target_dispatch`, but sometimes,
due to physical limitation (pmin, pmax, ramp min and ramp max) it cannot. In this case, only the best possible
redispatching is implemented at the current time step, and this is what this vector stores. Note that there is
information about all generators there, even the one that are not
dispatchable.
"""
def __init__(self,
obs_env=None,
action_helper=None,
seed=None):
GridObjects.__init__(self)
self.action_helper = action_helper
# time stamp information
self.year = 1970
self.month = 0
self.day = 0
self.hour_of_day = 0
self.minute_of_hour = 0
self.day_of_week = 0
# for non deterministic observation that would not use default np.random module
self.seed = None
# handles the forecasts here
self._forecasted_grid_act = {}
self._forecasted_inj = []
self.timestep_overflow = np.zeros(shape=(self.n_line,), dtype=dt_int)
# 0. (line is disconnected) / 1. (line is connected)
self.line_status = np.ones(shape=self.n_line, dtype=dt_bool)
# topological vector
self.topo_vect = np.full(shape=self.dim_topo, dtype=dt_int, fill_value=0)
# generators information
self.prod_p = np.full(shape=self.n_gen, dtype=dt_float, fill_value=np.NaN)
self.prod_q = np.full(shape=self.n_gen, dtype=dt_float, fill_value=np.NaN)
self.prod_v = np.full(shape=self.n_gen, dtype=dt_float, fill_value=np.NaN)
# loads information
self.load_p = np.full(shape=self.n_load, dtype=dt_float, fill_value=np.NaN)
self.load_q = np.full(shape=self.n_load, dtype=dt_float, fill_value=np.NaN)
self.load_v = np.full(shape=self.n_load, dtype=dt_float, fill_value=np.NaN)
# lines origin information
self.p_or = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
self.q_or = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
self.v_or = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
self.a_or = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
# lines extremity information
self.p_ex = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
self.q_ex = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
self.v_ex = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
self.a_ex = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
# lines relative flows
self.rho = np.full(shape=self.n_line, dtype=dt_float, fill_value=np.NaN)
# cool down and reconnection time after hard overflow, soft overflow or cascading failure
self.time_before_cooldown_line = np.full(shape=self.n_line, dtype=dt_int, fill_value=-1)
self.time_before_cooldown_sub = np.full(shape=self.n_sub, dtype=dt_int, fill_value=-1)
self.time_next_maintenance = np.full(shape=self.n_line, dtype=dt_int, fill_value=-1)
self.duration_next_maintenance = np.full(shape=self.n_line, dtype=dt_int, fill_value=-1)
# calendar data
self.year = dt_int(1970)
self.month = dt_int(0)
self.day = dt_int(0)
self.hour_of_day = dt_int(0)
self.minute_of_hour = dt_int(0)
self.day_of_week = dt_int(0)
# forecasts
self._forecasted_inj = []
self._forecasted_grid = []
self._obs_env = obs_env
# redispatching
self.target_dispatch = np.full(shape=self.n_gen, dtype=dt_float, fill_value=np.NaN)
self.actual_dispatch = np.full(shape=self.n_gen, dtype=dt_float, fill_value=np.NaN)
# value to assess if two observations are equal
self._tol_equal = 5e-1
self.attr_list_vect = None
def state_of(self, _sentinel=None, load_id=None, gen_id=None, line_id=None, substation_id=None):
"""
Return the state of this action on a give unique load, generator unit, powerline of substation.
Only one of load, gen, line or substation should be filled.
The querry of these objects can only be done by id here (ie by giving the integer of the object in the backed).
The :class:`ActionSpace` has some utilities to access them by name too.
Parameters
----------
_sentinel: ``None``
Used to prevent positional parameters. Internal, do not use.
load_id: ``int``
ID of the load we want to inspect
gen_id: ``int``
ID of the generator we want to inspect
line_id: ``int``
ID of the powerline we want to inspect
substation_id: ``int``
ID of the powerline we want to inspect
Returns
-------
res: :class:`dict`
A dictionnary with keys and value depending on which object needs to be inspected:
- if a load is inspected, then the keys are:
- "p" the active value consumed by the load
- "q" the reactive value consumed by the load
- "v" the voltage magnitude of the bus to which the load is connected
- "bus" on which bus the load is connected in the substation
- "sub_id" the id of the substation to which the load is connected
- if a generator is inspected, then the keys are:
- "p" the active value produced by the generator
- "q" the reactive value consumed by the generator
- "v" the voltage magnitude of the bus to which the generator is connected
- "bus" on which bus the generator is connected in the substation
- "sub_id" the id of the substation to which the generator is connected
- "actual_dispatch" the actual dispatch implemented for this generator
- "target_dispatch" the target dispatch (cumulation of all previously asked dispatch by the agent)
for this generator
- if a powerline is inspected then the keys are "origin" and "extremity" each being dictionnary with keys:
- "p" the active flow on line end (extremity or origin)
- "q" the reactive flow on line end (extremity or origin)
- "v" the voltage magnitude of the bus to which the line end (extremity or origin) is connected
- "bus" on which bus the line end (extremity or origin) is connected in the substation
- "sub_id" the id of the substation to which the generator is connected
- "a" the current flow on the line end (extremity or origin)
In the case of a powerline, additional information are:
- "maintenance": information about the maintenance operation (time of the next maintenance and duration
of this next maintenance.
- "cooldown_time": for how many timestep i am not supposed to act on the powerline due to cooldown
(see :attr:`grid2op.Parameters.Parameters.NB_TIMESTEP_LINE_STATUS_REMODIF` for more information)
- "indisponibility": for how many timestep the powerline is unavailable (disconnected, and it's
impossible to reconnect it) due to hazards, maintenance or overflow (incl. cascading failure)
- if a substation is inspected, it returns the topology to this substation in a dictionary with keys:
- "topo_vect": the representation of which object is connected where
- "nb_bus": number of active buses in this substations
- "cooldown_time": for how many timestep i am not supposed to act on the substation due to cooldown
(see :attr:`grid2op.Parameters.Parameters.NB_TIMESTEP_TOPOLOGY_REMODIF` for more information)
Raises
------
Grid2OpException
If _sentinel is modified, or if None of the arguments are set or alternatively if 2 or more of the
parameters are being set.
"""
if _sentinel is not None:
raise Grid2OpException("action.effect_on should only be called with named argument.")
if load_id is None and gen_id is None and line_id is None and substation_id is None:
raise Grid2OpException("You ask the state of an object in a observation without specifying the object id. "
"Please provide \"load_id\", \"gen_id\", \"line_id\" or \"substation_id\"")
if load_id is not None:
if gen_id is not None or line_id is not None or substation_id is not None:
raise Grid2OpException("You can only the inspect the effect of an action on one single element")
if load_id >= len(self.load_p):
raise Grid2OpException("There are no load of id \"load_id={}\" in this grid.".format(load_id))
res = {"p": self.load_p[load_id],
"q": self.load_q[load_id],
"v": self.load_v[load_id],
"bus": self.topo_vect[self.load_pos_topo_vect[load_id]],
"sub_id": self.load_to_subid[load_id]
}
elif gen_id is not None:
if line_id is not None or substation_id is not None:
raise Grid2OpException("You can only the inspect the effect of an action on one single element")
if gen_id >= len(self.prod_p):
raise Grid2OpException("There are no generator of id \"gen_id={}\" in this grid.".format(gen_id))
res = {"p": self.prod_p[gen_id],
"q": self.prod_q[gen_id],
"v": self.prod_v[gen_id],
"bus": self.topo_vect[self.gen_pos_topo_vect[gen_id]],
"sub_id": self.gen_to_subid[gen_id],
"target_dispatch": self.target_dispatch[gen_id],
"actual_dispatch": self.target_dispatch[gen_id]
}
elif line_id is not None:
if substation_id is not None:
raise Grid2OpException("You can only the inspect the effect of an action on one single element")
if line_id >= len(self.p_or):
raise Grid2OpException("There are no powerline of id \"line_id={}\" in this grid.".format(line_id))
res = {}
# origin information
res["origin"] = {
"p": self.p_or[line_id],
"q": self.q_or[line_id],
"v": self.v_or[line_id],
"a": self.a_or[line_id],
"bus": self.topo_vect[self.line_or_pos_topo_vect[line_id]],
"sub_id": self.line_or_to_subid[line_id]
}
# extremity information
res["extremity"] = {
"p": self.p_ex[line_id],
"q": self.q_ex[line_id],
"v": self.v_ex[line_id],
"a": self.a_ex[line_id],
"bus": self.topo_vect[self.line_ex_pos_topo_vect[line_id]],
"sub_id": self.line_ex_to_subid[line_id]
}
# maintenance information
res["maintenance"] = {"next": self.time_next_maintenance[line_id],
"duration_next": self.duration_next_maintenance[line_id]}
# cooldown
res["cooldown_time"] = self.time_before_cooldown_line[line_id]
else:
if substation_id >= len(self.sub_info):
raise Grid2OpException("There are no substation of id \"substation_id={}\" in this grid.".format(substation_id))
beg_ = int(np.sum(self.sub_info[:substation_id]))
end_ = int(beg_ + self.sub_info[substation_id])
topo_sub = self.topo_vect[beg_:end_]
if np.any(topo_sub > 0):
nb_bus = np.max(topo_sub[topo_sub > 0]) - np.min(topo_sub[topo_sub > 0]) + 1
else:
nb_bus = 0
res = {
"topo_vect": topo_sub,
"nb_bus": nb_bus,
"cooldown_time": self.time_before_cooldown_sub[substation_id]
}
return res
def reset(self):
"""
Reset the :class:`BaseObservation` to a blank state, where everything is set to either ``None`` or to its default
value.
"""
# vecorized _grid
self.timestep_overflow[:] = 0
# 0. (line is disconnected) / 1. (line is connected)
self.line_status[:] = True
# topological vector
self.topo_vect[:] = 0
# generators information
self.prod_p[:] = np.NaN
self.prod_q[:] = np.NaN
self.prod_v[:] = np.NaN
# loads information
self.load_p[:] = np.NaN
self.load_q[:] = np.NaN
self.load_v[:] = np.NaN
# lines origin information
self.p_or[:] = np.NaN
self.q_or[:] = np.NaN
self.v_or[:] = np.NaN
self.a_or[:] = np.NaN
# lines extremity information
self.p_ex[:] = np.NaN
self.q_ex[:] = np.NaN
self.v_ex[:] = np.NaN
self.a_ex[:] = np.NaN
# lines relative flows
self.rho[:] = np.NaN
# cool down and reconnection time after hard overflow, soft overflow or cascading failure
self.time_before_cooldown_line[:] = -1
self.time_before_cooldown_sub[:] = -1
self.time_next_maintenance[:] = -1
self.duration_next_maintenance[:] = -1
# calendar data
self.year = dt_int(1970)
self.month = dt_int(0)
self.day = dt_int(0)
self.hour_of_day = dt_int(0)
self.minute_of_hour = dt_int(0)
self.day_of_week = dt_int(0)
# forecasts
self._forecasted_inj = []
self._forecasted_grid_act = {}
# redispatching
self.target_dispatch[:] = np.NaN
self.actual_dispatch[:] = np.NaN
def __compare_stats(self, other, name):
attr_me = getattr(self, name)
attr_other = getattr(other, name)
if attr_me is None and attr_other is not None:
return False
if attr_me is not None and attr_other is None:
return False
if attr_me is not None:
if attr_me.shape != attr_other.shape:
return False
if attr_me.dtype != attr_other.dtype:
return False
if np.issubdtype(attr_me.dtype, np.dtype(dt_float).type):
# special case of floating points, otherwise vector are never equal
if not np.all(np.abs(attr_me - attr_other) <= self._tol_equal):
return False
else:
if not np.all(attr_me == attr_other):
return False
return True
def __eq__(self, other):
"""
Test the equality of two actions.
2 actions are said to be identical if the have the same impact on the powergrid. This is unlrelated to their
respective class. For example, if an BaseAction is of class :class:`BaseAction` and doesn't act on the _injection, it
can be equal to a an BaseAction of derived class :class:`TopologyAction` (if the topological modification are the
same of course).
This implies that the attributes :attr:`BaseAction.authorized_keys` is not checked in this method.
Note that if 2 actions doesn't act on the same powergrid, or on the same backend (eg number of loads, or
generators is not the same in *self* and *other*, or they are not in the same order) then action will be
declared as different.
**Known issue** if two backend are different, but the description of the _grid are identical (ie all
n_gen, n_load, n_line, sub_info, dim_topo, all vectors \*_to_subid, and \*_pos_topo_vect are
identical) then this method will not detect the backend are different, and the action could be declared
as identical. For now, this is only a theoretical behaviour: if everything is the same, then probably, up to
the naming convention, then the powergrid are identical too.
Parameters
----------
other: :class:`BaseObservation`
An instance of class BaseAction to which "self" will be compared.
Returns
-------
"""
# TODO doc above
if self.year != other.year:
return False
if self.month != other.month:
return False
if self.day != other.day:
return False
if self.day_of_week != other.day_of_week:
return False
if self.hour_of_day != other.hour_of_day:
return False
if self.minute_of_hour != other.minute_of_hour:
return False
# check that the _grid is the same in both instances
same_grid = True
same_grid = same_grid and self.n_gen == other.n_gen
same_grid = same_grid and self.n_load == other.n_load
same_grid = same_grid and self.n_line == other.n_line
same_grid = same_grid and np.all(self.sub_info == other.sub_info)
same_grid = same_grid and self.dim_topo == other.dim_topo
# to which substation is connected each element
same_grid = same_grid and np.all(self.load_to_subid == other.load_to_subid)
same_grid = same_grid and np.all(self.gen_to_subid == other.gen_to_subid)
same_grid = same_grid and np.all(self.line_or_to_subid == other.line_or_to_subid)
same_grid = same_grid and np.all(self.line_ex_to_subid == other.line_ex_to_subid)
# which index has this element in the substation vector
same_grid = same_grid and np.all(self.load_to_sub_pos == other.load_to_sub_pos)
same_grid = same_grid and np.all(self.gen_to_sub_pos == other.gen_to_sub_pos)
same_grid = same_grid and np.all(self.line_or_to_sub_pos == other.line_or_to_sub_pos)
same_grid = same_grid and np.all(self.line_ex_to_sub_pos == other.line_ex_to_sub_pos)
# which index has this element in the topology vector
same_grid = same_grid and np.all(self.load_pos_topo_vect == other.load_pos_topo_vect)
same_grid = same_grid and np.all(self.gen_pos_topo_vect == other.gen_pos_topo_vect)
same_grid = same_grid and np.all(self.line_or_pos_topo_vect == other.line_or_pos_topo_vect)
same_grid = same_grid and np.all(self.line_ex_pos_topo_vect == other.line_ex_pos_topo_vect)
if not same_grid:
return False
for stat_nm in ["line_status", "topo_vect",
"timestep_overflow",
"prod_p", "prod_q", "prod_v",
"load_p", "load_q", "load_v",
"p_or", "q_or", "v_or", "a_or",
"p_ex", "q_ex", "v_ex", "a_ex",
"time_before_cooldown_line",
"time_before_cooldown_sub",
"time_next_maintenance",
"duration_next_maintenance",
"target_dispatch", "actual_dispatch"
]:
if not self.__compare_stats(other, stat_nm):
# one of the above stat is not equal in this and in other
return False
return True
@abstractmethod
def update(self, env):
"""
Update the actual instance of BaseObservation with the new received value from the environment.
An observation is a description of the powergrid perceived by an agent. The agent takes his decision based on
the current observation and the past rewards.
This method `update` receive complete detailed information about the powergrid, but that does not mean an
agent sees everything.
For example, it is possible to derive this class to implement some noise in the generator or load, or flows to
mimic sensor inaccuracy.
It is also possible to give fake information about the topology, the line status etc.
In the Grid2Op framework it's also through the observation that the agent has access to some forecast (the way
forecast are handled depends are implemented in this class). For example, forecast data (retrieved thanks to
`chronics_handler`) are processed, but can be processed differently. One can apply load / production forecast to
each _grid state, or to make forecast for one "reference" _grid state valid a whole day and update this one
only etc.
All these different mechanisms can be implemented in Grid2Op framework by overloading the `update` observation
method.
This class is really what a dispatcher observes from it environment.
It can also include some temperatures, nebulosity, wind etc. can also be included in this class.
"""
pass
def connectivity_matrix(self):
"""
Computes and return the "connectivity matrix" `con_mat`.
if "dim_topo = 2 * n_line + n_prod + n_conso"
It is a matrix of size dim_topo, dim_topo, with values 0 or 1.
For two objects (lines extremity, generator unit, load) i,j :
- if i and j are connected on the same substation:
- if `conn_mat[i,j] = 0` it means the objects id'ed i and j are not connected to the same bus.
- if `conn_mat[i,j] = 1` it means the objects id'ed i and j are connected to the same bus, are both end
of the same powerline
- if i and j are not connected on the same substation then`conn_mat[i,j] = 0` except if i and j are
the two extremities of the same power line, in this case `conn_mat[i,j] = 1`.
By definition, the diagonal is made of 0.
Returns
-------
res: ``numpy.ndarray``, shape:dim_topo,dim_topo, dtype:float
The connectivity matrix, as defined above
"""
raise NotImplementedError("This method is not implemented")
def bus_connectivity_matrix(self):
"""
If we denote by `nb_bus` the total number bus of the powergrid.
The `bus_connectivity_matrix` will have a size nb_bus, nb_bus and will be made of 0 and 1.
If `bus_connectivity_matrix[i,j] = 1` then at least a power line connects bus i and bus j.
Otherwise, nothing connects it.
Returns
-------
res: ``numpy.ndarray``, shape:nb_bus,nb_bus dtype:float
The bus connectivity matrix
"""
raise NotImplementedError("This method is not implemented. ")
def simulate(self, action, time_step=0):
"""
This method is used to simulate the effect of an action on a forecasted powergrid state. It has the same return
value as the :func:`grid2op.Environment.Environment.step` function.
Parameters
----------
action: :class:`grid2op.Action.Action`
The action to simulate
time_step: ``int``
The time step of the forecasted grid to perform the action on. If no forecast are available for this
time step, a :class:`grid2op.Exceptions.NoForecastAvailable` is thrown.
Raises
------
:class:`grid2op.Exceptions.NoForecastAvailable`
if no forecast are available for the time_step querried.
Returns
-------
observation: :class:`grid2op.Observation.Observation`
agent's observation of the current environment
reward: ``float``
amount of reward returned after previous action
done: ``bool``
whether the episode has ended, in which case further step() calls will return undefined results
info: ``dict``
contains auxiliary diagnostic information (helpful for debugging, and sometimes learning)
"""
if self.action_helper is None or self._obs_env is None:
raise NoForecastAvailable("No forecasts are available for this instance of BaseObservation (no action_space "
"and no simulated environment are set).")
if time_step >= len(self._forecasted_inj):
raise NoForecastAvailable("Forecast for {} timestep ahead is not possible with your chronics.".format(time_step))
if time_step not in self._forecasted_grid_act:
timestamp, inj_forecasted = self._forecasted_inj[time_step]
self._forecasted_grid_act[time_step] = {
"timestamp": timestamp,
"inj_action": self.action_helper(inj_forecasted)
}
timestamp = self._forecasted_grid_act[time_step]["timestamp"]
inj_action = self._forecasted_grid_act[time_step]["inj_action"]
self._obs_env.init(inj_action, time_stamp=timestamp,
timestep_overflow=self.timestep_overflow,
topo_vect=self.topo_vect)
sim_obs = self._obs_env.simulate(action)
return sim_obs
def copy(self):
"""
Make a (deep) copy of the observation.
Returns
-------
res: :class:`BaseObservation`
The deep copy of the observation
"""
obs_env = self._obs_env
self._obs_env = None
res = copy.deepcopy(self)
self._obs_env = obs_env
res._obs_env = obs_env.copy()
return res