forked from IRT-SystemX/InteractiveAI
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassistant_manager.py
382 lines (339 loc) · 13.7 KB
/
assistant_manager.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
import os
from enum import Enum
import importlib.util
import sys
import numpy as np
import toml
from lightsim2grid import LightSimBackend
import grid2op
from grid2op.Chronics import FromHandlers
from grid2op.Chronics.handlers import PerfectForecastHandler, CSVHandler
from grid2op.Agent import BaseAgent
BkClass = LightSimBackend
class AgentType(Enum):
"""Recomendations' agent type
Args:
Enum (): Type "onto" or "IA"
"""
onto = 1
IA = 2
def lazy_import_package(package_name, package_path):
"""Import a package dynamically from a given path
Args:
package_name (string): Name of the package to be imported
package_path (string): Path to the package directory
Returns:
module: Imported package
"""
spec = importlib.util.spec_from_file_location(package_name,
os.path.join(package_path, '__init__.py'))
if spec and spec.loader:
package = importlib.util.module_from_spec(spec)
sys.modules[package_name] = package
spec.loader.exec_module(package)
return package
else:
raise ImportError(f"Cannot import package {package_name} from {package_path}")
class AgentManager:
"""PowerGrid IA agent object based on Grid2Op and XD_silly
"""
def __init__(self):
# Load PowerGrid simulator configuration
script_dir = os.path.dirname(os.path.abspath(__file__))
# Build the path to the config file
config_path = os.path.join(script_dir, "CONFIG_POWERGRID.toml")
config_assistant = toml.load(config_path)
# grid2op Environment and observation definition and loading
env_name = os.path.join(script_dir, config_assistant["env_name"])
forecasts_horizons = [5, 10, 15, 20, 25, 30]
self.env = grid2op.make(
env_name,
backend=BkClass(),
data_feeding_kwargs={
"gridvalueClass": FromHandlers,
"gen_p_handler": CSVHandler("prod_p"),
"load_p_handler": CSVHandler("load_p"),
"gen_v_handler": CSVHandler("prod_v"),
"load_q_handler": CSVHandler("load_q"),
"h_forecast": forecasts_horizons,
"gen_p_for_handler": PerfectForecastHandler(
"prod_p_forecasted"
),
"load_p_for_handler": PerfectForecastHandler(
"load_p_forecasted"
),
"load_q_for_handler": PerfectForecastHandler(
"load_q_forecasted"
),
},
)
self.env.seed(config_assistant["env_seed"])
# Search scenario with provided name
for sc_id, sp in enumerate(self.env.chronics_handler.real_data.subpaths):
sp_end = os.path.basename(sp)
if sp_end == config_assistant["scenario_name"]:
self.id_scenario = sc_id
self.env.set_id(self.id_scenario) # Scenario choice
self.obs = self.env.reset()
if self.obs.current_step is None:
self.previous_step = "1"
else:
self.previous_step = self.obs.current_step
# assistant definition and loading
assistant_path = os.path.join(
script_dir, config_assistant["assistant_name"])
assistant_seed = config_assistant["assistant_seed"]
submission_path = os.path.join(assistant_path, "submission")
submission = lazy_import_package("submission", submission_path)
self.assistant = submission.make_agent(
self.env.copy(),
submission_path,
)
if not isinstance(self.assistant, BaseAgent):
msg_ = "Your assistant must be a grid2op.Agent.BaseAgent"
raise RuntimeError(msg_)
self.assistant.seed(int(assistant_seed))
self.nb_timestep = 0 # Initialize timestep count
# Action "do nothing"
self.action_do_nothing = self.env.action_space({})
self.recommendations = []
def reset_obs_if_needed(self, obs_dict):
"""Reset obs
Args:
obs_dict (dict): Observation dictionary
"""
if self.nb_timestep < 0:
self.env.set_id(self.id_scenario)
self.obs = self.env.reset()
self.previous_step = "1"
self.get_nb_of_timestep_since_last_obs(obs_dict)
def get_nb_of_timestep_since_last_obs(self, obs_dict):
"""Count number of simulation timestep bewteen 2 consecutive
call to this function.
Args:
obs_dict (dict): Observation dictionary
Returns:
int : Number of timesteps
"""
self.nb_timestep = int(obs_dict.get("current_step")[0]) - int(
self.previous_step
)
return self.nb_timestep
def create_recommendation(self, obs_dict, n_actions=3):
"""Create PowerGrid IA agent recomendations
Args:
obs_dict (dict): Observation dictionary
n_actions (int, optional): Number of recomendations
that should be generated.Defaults to 3.
Returns:
[act_dict]: Recomendations objects compliant with PowerGrid simulator
"""
self.get_nb_of_timestep_since_last_obs(obs_dict)
self.reset_obs_if_needed(obs_dict)
if (
self.nb_timestep > 1
): # no sense to fast-forward only for next time step ?
self.env.fast_forward_chronics(self.nb_timestep)
self.previous_step = obs_dict.get("current_step")[0]
elif self.nb_timestep == 1:
self.env.step(self.action_do_nothing)
self.previous_step = obs_dict.get("current_step")[0]
self.obs = self.env.get_obs()
self.obs.from_json(
obs_dict
) # il faut aussi modifier les _env_internal_params
self.obs._env_internal_params["_line_status_env"] = (
self.obs.line_status.astype(int)
)
self.recommendations = self.assistant.make_recommandations(
self.obs, n_actions
)
return self.recommendations
def get_parade_info(self, act):
"""Compile unitary recomendation in json format for InteractiveAI's frontend compliance
Args:
act (): Unitary action object
Returns:
dict: Recomendations data in json format
"""
kpis = {}
title = []
description = []
impact = act.impact_on_objects()
# redispatch
if act._modif_redispatch:
kpis["type_of_the_reco"] = (
"Redispatch" # pour renvoyer le kpi type_of_the_reco
)
title.append(
"Parade injection: redispatch de source de production"
)
cpt = 0
for gen_idx in range(act.n_gen):
if act._redispatch[gen_idx] != 0.0:
gen_name = act.name_gen[gen_idx]
r_amount = act._redispatch[gen_idx]
if cpt > 0:
description.append(", ")
cpt = 1
description.append(
f'"{gen_name}" de {r_amount:.2f} MW'
)
# storage
if act._modif_storage:
kpis["type_of_the_reco"] = (
"Stockage" # pour renvoyer le kpi type_of_the_reco
)
title.append("Parade stockage")
cpt = 0
for stor_idx in range(act.n_storage):
amount_ = act._storage_power[stor_idx]
if np.isfinite(amount_) and amount_ != 0.0:
name_ = act.name_storage[stor_idx]
if cpt > 0:
description.append(", ")
cpt = 1
description.append(
f'Demande à l\'unité "{name_}" de '
f'{"charger" if amount_ > 0.0 else "decharger"} '
f'{abs(amount_):.2f} MW (setpoint: {amount_:.2f} MW)'
)
# curtailment
if act._modif_curtailment:
kpis["type_of_the_reco"] = (
"Injection" # pour renvoyer le kpi type_of_the_reco
)
title.append("Parade injection")
cpt = 0
for gen_idx in range(act.n_gen):
amount_ = act._curtail[gen_idx]
if np.isfinite(amount_) and amount_ != -1.0:
name_ = act.name_gen[gen_idx]
if cpt > 0:
description.append(", ")
cpt = 1
description.append(
f'Limiter l\'unité "{name_}" à '
f'{100.0 * amount_:.1f}% de sa capacité max '
f'(setpoint: {amount_:.3f})'
)
# force line status
force_line_impact = impact["force_line"]
if force_line_impact["changed"]:
kpis["type_of_the_reco"] = (
"Topologique" # pour renvoyer le kpi type_of_the_reco
)
title.append(
"Parade topologique: connection/deconnection de ligne"
)
reconnections = force_line_impact["reconnections"]
if reconnections["count"] > 0:
description.append(
f"Reconnection de {reconnections['count']} lignes "
f"({reconnections['powerlines']})"
)
disconnections = force_line_impact["disconnections"]
if disconnections["count"] > 0:
description.append(
f"Déconnection de {disconnections['count']} lignes "
f"({disconnections['powerlines']})"
)
# swtich line status
swith_line_impact = impact["switch_line"]
if swith_line_impact["changed"]:
kpis["type_of_the_reco"] = (
"Topologique" # pour renvoyer le kpi type_of_the_reco
)
title.append("Parade topologique: changer l'état d'une ligne")
description.append(
f"Changer le statut de {swith_line_impact['count']} lignes "
f"({swith_line_impact['powerlines']})"
)
# topology
bus_switch_impact = impact["topology"]["bus_switch"]
if len(bus_switch_impact) > 0:
kpis["type_of_the_reco"] = (
"Topologique" # pour renvoyer le kpi type_of_the_reco
)
title.append(
"Parade topologique: prise de schéma au poste "
+ str(bus_switch_impact["substation"])
)
description.append("Changement de bus:")
for switch in bus_switch_impact:
description.append(
f"\t \t - Switch bus de {switch['object_type']} id "
f"{switch['object_id']} [au poste {switch['substation']}]"
)
assigned_bus_impact = impact["topology"]["assigned_bus"]
disconnect_bus_impact = impact["topology"]["disconnect_bus"]
if len(assigned_bus_impact) > 0 or len(disconnect_bus_impact) > 0:
kpis["type_of_the_reco"] = (
"Topologique" # pour renvoyer le kpi type_of_the_reco
)
title.append(
"Parade topologique: prise de schéma au poste "
+ str(assigned_bus_impact[0]["substation"])
)
if assigned_bus_impact:
description.append("")
cpt = 0
for assigned in assigned_bus_impact:
if cpt > 0:
description.append(", ")
cpt = 1
description.append(
f" Assigner le bus {assigned['bus']} à "
f"{assigned['object_type']} id {assigned['object_id']}"
)
if disconnect_bus_impact:
description.append("")
cpt = 0
for disconnected in disconnect_bus_impact:
if cpt > 0:
description.append(", ")
cpt = 1
description.append(
f"Déconnecter {disconnected['object_type']} avec l'id "
f"{disconnected['object_id']} [au niveau du poste "
f"{disconnected['substation']}]"
)
# Any of the above cases,
# then the recommendation is most likely "Do nothing"
if not title and act == self.action_do_nothing:
kpis["type_of_the_reco"] = (
"Ne rien faire" # pour renvoyer le kpi type_of_the_reco
)
title.append("Poursuivre")
description.append(
"Poursuite du scénario sans intervention extérieur"
)
title = "".join(title)
description = "".join(description)
if title:
obs_simulate, _, _, _ = (
self.obs.simulate(act, time_step=1)
)
kpis["efficiency_of_the_reco"] = float(
np.float32(obs_simulate.rho.max())
) # pour renvoyer le kpi efficiency_of_the_reco
return {
"title": title,
"description": description,
"use_case": "PowerGrid",
"agent_type": AgentType.IA.name,
"actions": [act.to_json()],
"kpis": kpis,
}
def get_list_of_parade_info(self):
"""Compile PowerGrid IA agent recomendations in a single list
for InteractiveAI's frontend compliance
Returns:
[act_dict]: List of action dictionary
"""
list_of_act_dict = []
for rec in self.recommendations:
act, _ = rec
act_dict = self.get_parade_info(act)
list_of_act_dict.append(act_dict)
return list_of_act_dict