/
Converters.py
159 lines (122 loc) · 5.67 KB
/
Converters.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
# 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.
from grid2op.Action import ActionSpace
class Converter(ActionSpace):
"""
This Base class should be use to implement any converter. If for some reasons
"""
def __init__(self, action_space):
ActionSpace.__init__(self, action_space, action_space.legal_action, action_space.subtype)
self.space_prng = action_space.space_prng
self.seed_used = action_space.seed_used
def init_converter(self, **kwargs):
pass
def convert_obs(self, obs):
"""
This function is used to convert an observation into something that is easier to manipulate.
Parameters
----------
obs: :class:`grid2op.Observation.Observation`
The input observation.
Returns
-------
transformed_obs: ``object``
An different representation of the input observation, typically represented as a 1d vector that can be
processed by a neural networks.
"""
transformed_obs = obs
return transformed_obs
def convert_act(self, encoded_act):
"""
This function will transform the action, encoded somehow (for example identified by an id, represented by
an integer) to a valid actions that can be processed by the environment.
Parameters
----------
encoded_act: ``object``
Representation of an action, as a vector or an integer etc.
Returns
-------
regular_act: :class:`grid2op.Action.Action`
The action corresponding to the `encoded_action` above converted into a format that can be processed
by the environment.
"""
regular_act = encoded_act
return regular_act
def get_gym_dict(self):
"""
To convert this space into a open ai gym space. This function returns a dictionnary used
to initialize such a converter.
It should not be used directly. Prefer to use the :class:`grid2op.Converter.GymConverter`
Returns
-------
res: ``dict``
The dictionary of gym spaces representing this converter.
"""
raise NotImplementedError("Impossible to convert the converter \"{}\" automatically "
"into a gym space (or gym is not installed on your machine)."
"".format(self))
def convert_action_from_gym(self, gymlike_action):
"""
Convert the action (represented as a gym object, in fact an ordered dict) as an action
compatible with this converter.
This is not compatible with all converters and you need to install gym for it to work.
Parameters
----------
gymlike_action:
the action to be converted to an action compatible with the action space representation
Returns
-------
res:
The action converted to be understandable by this converter.
Examples
---------
Here is an example on how to use this feature with the :class:`grid2op.Converter.IdToAct`
converter (imports are not shown here).
.. code-block:: python
# create the environment
env = grid2op.make()
# create the converter
converter = IdToAct(env.action_space)
# create the gym action space
gym_action_space = GymObservationSpace(action_space=converter)
gym_action = gym_action_space.sample()
converter_action = converter.convert_action_from_gym(gym_action) # this represents the same action
grid2op_action = converter.convert_act(converter_action)
"""
raise NotImplementedError("Impossible to convert the gym-like action automatically "
"into the converter representation for \"{}\" "
"".format(self))
def convert_action_to_gym(self, action):
"""
Convert the action (compatible with this converter) into a "gym action" (ie an OrderedDict)
This is not compatible with all converters and you need to install gym for it to work.
Parameters
----------
action:
the action to be converted to an action compatible with the action space representation
Returns
-------
res:
The action converted to a "gym" model (can be used by a machine learning model)
Examples
---------
Here is an example on how to use this feature with the :class:`grid2op.Converter.IdToAct`
converter (imports are not shown here).
.. code-block:: python
# create the environment
env = grid2op.make()
# create the converter
converter = IdToAct(env.action_space)
# create the gym action space
gym_action_space = GymObservationSpace(action_space=converter)
converter_action = converter.sample()
gym_action = converter.to_gym(converter_action) # this represents the same action
"""
raise NotImplementedError("Impossible to convert the gym-like action automatically "
"into the converter representation for \"{}\" "
"".format(self))