-
Notifications
You must be signed in to change notification settings - Fork 111
/
env_cache.py
292 lines (253 loc) · 9.55 KB
/
env_cache.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
# 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 collections.abc import Iterable
import numpy as np
from grid2op.MakeEnv import make
try:
from lightsim2grid import LightSimBackend
bkclass = LightSimBackend
# raise ImportError()
except ImportError as excq_:
from grid2op.Backend import PandaPowerBackend
bkclass = PandaPowerBackend
pass
class EnvCache(object):
"""
TODO this is not implemented yet (at least the "cache" part)
We should have a flag that when an environment is computing, it returns an "error" to indicate that on the person
who makes the call (for now i'm pretty sure the current implementation will not work in asynch mode).
"""
ENV_NOT_FOUND = 0
ENV_ID_NOT_FOUND = 1
INVALID_ACTION = 2
INVALID_STEP = 3
ERROR_ENV_SEED = 4
ERROR_ENV_RESET = 5
ERROR_ENV_SET_ID = 6
ERROR_ENV_THERMAL_LIMIT = 7
ERROR_CLOSE = 8
ERROR_ENV_FAST_FORWARD = 9
ERROR_ENV_PATH = 10
def __init__(self, ujson_as_json):
self.all_env = {}
self.ujson_as_json = (
ujson_as_json # do i use the faster "ujson" library to parse json
)
self._convert_json = not self.ujson_as_json
def insert_env(self, env_name):
"""
TODO
"""
try:
env = make(env_name, backend=bkclass()) # TODO look at the RemoteEnv here
env.deactivate_forecast()
# call deactivate_forecast
except Exception as exc_:
return None, None, exc_
if env_name not in self.all_env:
# create an environment with that name
self.all_env[env_name] = [env]
else:
self.all_env[env_name].append(env)
id_ = len(self.all_env[env_name]) - 1
obs = env.reset()
return id_, obs.to_json(convert=self._convert_json), None
def step(self, env_name, env_id, action_as_json):
"""
TODO
"""
res_env = (None, None, None, None)
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return res_env, (error_id, error_msg)
try:
act = env.action_space()
act.from_json(action_as_json)
except Exception as exc_:
msg_ = (
f"impossible to convert the provided action to a valid action on this environment with error:\n"
f"{exc_}"
)
return res_env, (self.INVALID_ACTION, msg_)
try:
obs, reward, done, info = env.step(act)
except Exception as exc_:
msg_ = (
f"impossible to make a step on the give environment with error\n{exc_}"
)
return res_env, (self.INVALID_STEP, msg_)
return (
obs.to_json(convert=self._convert_json),
float(reward),
bool(done),
self._aux_info_to_json(info),
), (None, None)
def seed(self, env_name, env_id, seed):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return None, (error_id, error_msg)
try:
seeds = env.seed(seed)
except Exception as exc_:
msg_ = f"Impossible to seed the environment with error:\n{exc_}"
return None, (self.ERROR_ENV_SEED, msg_)
return self._aux_array_to_json(seeds), (None, None)
def reset(self, env_name, env_id):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return None, (error_id, error_msg)
try:
obs = env.reset()
except Exception as exc_:
msg_ = f"Impossible to reset the environment with error {exc_}"
return None, (self.ERROR_ENV_RESET, msg_)
return obs.to_json(convert=self._convert_json), (None, None)
def set_id(self, env_name, env_id, chron_id):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return error_id, error_msg
try:
env.set_id(chron_id)
except Exception as exc_:
msg_ = f"Impossible to set the chronics id of the environment with error:\n {exc_}"
return self.ERROR_ENV_SET_ID, msg_
return None, None
def set_thermal_limit(self, env_name, env_id, thermal_limit):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return error_id, error_msg
try:
env.set_thermal_limit(thermal_limit)
except Exception as exc_:
msg_ = f"Impossible to set the thermal limits of the environment with error:\n {exc_}"
return self.ERROR_ENV_THERMAL_LIMIT, msg_
return None, None
def get_thermal_limit(self, env_name, env_id):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return None, (error_id, error_msg)
try:
res = env.get_thermal_limit()
except Exception as exc_:
msg_ = f"Impossible to get the thermal limits of the environment with error:\n {exc_}"
return None, (self.ERROR_ENV_THERMAL_LIMIT, msg_)
res = res.tolist()
return res, (None, None)
def close(self, env_name, env_id):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return error_id, error_msg
try:
env.close()
except Exception as exc_:
msg_ = f"Impossible to close the environment with error:\n {exc_}"
return self.ERROR_CLOSE, msg_
return None, None
def fast_forward_chronics(self, env_name, env_id, nb_step):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return error_id, error_msg
try:
env.fast_forward_chronics(nb_step)
except Exception as exc_:
msg_ = f"Impossible to fast forward the environment with error:\n {exc_}"
return self.ERROR_ENV_FAST_FORWARD, msg_
return None, None
def get_path_env(self, env_name, env_id):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return None, (error_id, error_msg)
try:
res = env.get_path_env()
except Exception as exc_:
msg_ = f"Impossible to fast forward the environment with error:\n {exc_}"
return None, (self.ERROR_ENV_PATH, msg_)
return res, (None, None)
def train_val_split(self, env_name, env_id, id_chron_val):
"""
TODO
"""
env, (error_id, error_msg) = self._aux_get_env(env_name, env_id)
if error_id is not None:
return (None, None), (error_id, error_msg)
try:
res = env.train_val_split(
val_scen_id=id_chron_val, add_for_train="train", add_for_val="val"
)
except Exception as exc_:
msg_ = f"Impossible to split the environment with error:\n {exc_}"
return (None, None), (self.ERROR_ENV_PATH, msg_)
return res, (None, None)
def _aux_array_to_json(self, array):
if isinstance(array, Iterable):
res = None
if isinstance(array, np.ndarray):
if array.shape == ():
res = []
if res is None:
res = [self._aux_array_to_json(el) for el in array]
return res
else:
return float(array)
def _aux_info_to_json(self, info):
# TODO
res = {}
res["disc_lines"] = [int(el) for el in info["disc_lines"]]
res["is_illegal"] = bool(info["is_illegal"])
res["is_ambiguous"] = bool(info["is_ambiguous"])
res["is_dispatching_illegal"] = bool(info["is_dispatching_illegal"])
res["is_illegal_reco"] = bool(info["is_illegal_reco"])
if info["opponent_attack_line"] is not None:
res["opponent_attack_line"] = [
bool(el) for el in info["opponent_attack_line"]
]
else:
res["opponent_attack_line"] = None
res["exception"] = [f"{exc_}" for exc_ in info["exception"]]
return res
def _aux_get_env(self, env_name, env_id):
if env_name not in self.all_env:
return None, (
self.ENV_NOT_FOUND,
f'environment "{env_name}" does not exists',
)
li_env = self.all_env[env_name]
env_id = int(env_id)
nb_env = len(li_env)
if env_id >= nb_env:
msg_ = (
f"you asked to run the environment {env_id} of {env_name}. But there are only {nb_env} "
f"such environments"
)
return None, (self.ENV_ID_NOT_FOUND, msg_)
env = li_env[env_id]
return env, (None, None)