Skip to content

Commit e7713f5

Browse files
authored
Merge pull request #686 from BDonnot/bd_dev
Some fixes for the detachment feature
2 parents 0ed82aa + 59ac5c4 commit e7713f5

File tree

13 files changed

+294
-35
lines changed

13 files changed

+294
-35
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
ls ./dist/*.whl
8787

8888
- name: Upload wheel
89-
uses: actions/upload-artifact@v3
89+
uses: actions/upload-artifact@v4
9090
with:
9191
name: grid2op-wheel-${{ matrix.config.name }}-${{ matrix.python.name }}
9292
path: dist/*.whl

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
author = 'Benjamin Donnot'
2323

2424
# The full version, including alpha/beta/rc tags
25-
release = '1.11.0.dev3'
25+
release = '1.11.0.dev4'
2626
version = '1.11'
2727

2828

getting_started/grid2op

Lines changed: 0 additions & 1 deletion
This file was deleted.

grid2op/Action/baseAction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from grid2op.typing_variables import DICT_ACT_TYPING
2424
from grid2op.dtypes import dt_int, dt_bool, dt_float
2525
from grid2op.Exceptions import *
26-
from grid2op.Space import GridObjects
26+
from grid2op.Space import GridObjects, GRID2OP_CURRENT_VERSION_STR
2727

2828
# TODO time delay somewhere (eg action is implemented after xxx timestep, and not at the time where it's proposed)
2929

@@ -2425,7 +2425,7 @@ def _digest_storage(self, dict_):
24252425
except IllegalAction as exc_:
24262426
cls = type(self)
24272427
# only raise the error if I am not in compat mode
2428-
if cls.glop_version == grid2op.__version__:
2428+
if cls.glop_version == GRID2OP_CURRENT_VERSION_STR:
24292429
raise exc_
24302430
else:
24312431
# TODO be more specific on the version
@@ -2439,7 +2439,7 @@ def _digest_curtailment(self, dict_):
24392439
except IllegalAction as exc_:
24402440
cls = type(self)
24412441
# only raise the error if I am not in compat mode
2442-
if cls.glop_version == grid2op.__version__:
2442+
if cls.glop_version == GRID2OP_CURRENT_VERSION_STR:
24432443
raise exc_
24442444
else:
24452445
# TODO be more specific on the version

grid2op/Environment/baseEnv.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,6 +3170,7 @@ def _update_alert_properties(self, action, lines_attacked, subs_attacked):
31703170
# self._is_alert_illegal
31713171

31723172
def _aux_register_env_converged(self, disc_lines, action, init_line_status, new_p) -> Optional[Grid2OpException]:
3173+
cls = type(self)
31733174
beg_res = time.perf_counter()
31743175
# update the thermal limit, for DLR for example
31753176
self.backend.update_thermal_limit(self)
@@ -3224,7 +3225,6 @@ def _aux_register_env_converged(self, disc_lines, action, init_line_status, new_
32243225
self._gen_activeprod_t[:] = tmp_gen_p
32253226
else:
32263227
# I need to check whether all generators meet the constraints
3227-
cls = type(self)
32283228
if tmp_gen_p[cls.gen_redispatchable] > cls.gen_pmax[cls.gen_redispatchable] + self._tol_poly:
32293229
gen_ko = (tmp_gen_p[cls.gen_redispatchable] > cls.gen_pmax[cls.gen_redispatchable]).nonzero()[0]
32303230
gen_ko_nms = cls.name_gen[cls.gen_redispatchable][gen_ko]
@@ -3245,6 +3245,16 @@ def _aux_register_env_converged(self, disc_lines, action, init_line_status, new_
32453245
return SomeGeneratorBelowRampmin(f"Especially generators {gen_ko_nms}")
32463246

32473247
self._gen_activeprod_t[:] = tmp_gen_p
3248+
3249+
# set the status of the other elements (if the backend
3250+
# disconnect them)
3251+
if cls.detachment_is_allowed:
3252+
gen_detached_user = self._backend_action.get_gen_detached()
3253+
topo_ = self.backend.get_topo_vect()
3254+
self._backend_action.current_topo.values[:] = topo_
3255+
self._aux_update_detachment_info()
3256+
else:
3257+
gen_detached_user = np.zeros(cls.n_gen, dtype=dt_bool)
32483258

32493259
# problem with the gen_activeprod_t above, is that the slack bus absorbs alone all the losses
32503260
# of the system. So basically, when it's too high (higher than the ramp) it can
@@ -3253,11 +3263,11 @@ def _aux_register_env_converged(self, disc_lines, action, init_line_status, new_
32533263

32543264
# set the line status
32553265
self._line_status[:] = self.backend.get_line_status()
3256-
3266+
32573267
# for detachment remember previous loads and generation
32583268
self._prev_load_p[:], self._prev_load_q[:], *_ = self.backend.loads_info()
32593269
self._delta_gen_p[:] = self._gen_activeprod_t - self._gen_activeprod_t_redisp
3260-
self._delta_gen_p[self._gens_detached] = 0.
3270+
self._delta_gen_p[gen_detached_user] = 0. # when the backend disconnect it it should not be set to 0.
32613271
self._prev_gen_p[:] = self._gen_activeprod_t
32623272

32633273
# finally, build the observation (it's a different one at each step, we cannot reuse the same one)
@@ -3272,11 +3282,7 @@ def _backend_next_grid_state(self):
32723282
"""overlaoded in MaskedEnv"""
32733283
return self.backend.next_grid_state(env=self, is_dc=self._env_dc)
32743284

3275-
def _aux_run_pf_after_state_properly_set(
3276-
self, action, init_line_status, new_p, except_
3277-
):
3278-
has_error = True
3279-
detailed_info = None
3285+
def _aux_update_detachment_info(self):
32803286
cls = type(self)
32813287
if cls.detachment_is_allowed:
32823288
self._loads_detached[:] = self._backend_action.get_load_detached()
@@ -3297,6 +3303,12 @@ def _aux_run_pf_after_state_properly_set(
32973303
self._storage_p_detached[mask_chgt] = self._backend_action.storage_power.values[mask_chgt]
32983304
self._storage_p_detached[~self._storages_detached] = 0.
32993305

3306+
def _aux_run_pf_after_state_properly_set(
3307+
self, action, init_line_status, new_p, except_
3308+
):
3309+
has_error = True
3310+
detailed_info = None
3311+
33003312
try:
33013313
# compute the next _grid state
33023314
beg_pf = time.perf_counter()

grid2op/Environment/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from grid2op.Environment.baseEnv import BaseEnv
3333
from grid2op.Opponent import BaseOpponent, NeverAttackBudget
3434
from grid2op.operator_attention import LinearAttentionBudget
35-
from grid2op.Space import DEFAULT_N_BUSBAR_PER_SUB, DEFAULT_ALLOW_DETACHMENT
35+
from grid2op.Space import DEFAULT_N_BUSBAR_PER_SUB, DEFAULT_ALLOW_DETACHMENT, GRID2OP_CURRENT_VERSION_STR
3636
from grid2op.typing_variables import RESET_OPTIONS_TYPING, N_BUSBAR_PER_SUB_TYPING
3737
from grid2op.MakeEnv.PathUtils import USE_CLASS_IN_FILE
3838

@@ -647,7 +647,7 @@ def _helper_action_player(self):
647647
def _handle_compat_glop_version(self, need_process_backend):
648648
if (
649649
self._compat_glop_version is not None
650-
and self._compat_glop_version != grid2op.__version__
650+
and self._compat_glop_version != GRID2OP_CURRENT_VERSION_STR
651651
):
652652
warnings.warn(
653653
'You are using a grid2op "compatibility" environment. This means that some '

grid2op/Episode/EpisodeData.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
IncorrectNumberOfElements,
2020
NonFiniteElement,
2121
)
22+
from grid2op.Space import GRID2OP_CURRENT_VERSION_STR
2223
from grid2op.Action import ActionSpace
2324
from grid2op.Observation import ObservationSpace
2425

@@ -535,7 +536,7 @@ def from_disk(cls,
535536
helper_action_env = ActionSpace.from_dict(
536537
os.path.join(agent_path, EpisodeData.ENV_MODIF_SPACE)
537538
)
538-
if observation_space.glop_version != grid2op.__version__:
539+
if observation_space.glop_version != GRID2OP_CURRENT_VERSION_STR:
539540
warnings.warn(
540541
'You are using a "grid2op compatibility" feature (the data you saved '
541542
"have been saved with a previous grid2op version). When we loaded your data, we attempted "
@@ -833,7 +834,7 @@ def to_disk(self):
833834
"w",
834835
encoding="utf-8",
835836
) as f:
836-
dict_ = {"version": f"{grid2op.__version__}"}
837+
dict_ = {"version": f"{GRID2OP_CURRENT_VERSION_STR}"}
837838
json.dump(obj=dict_, fp=f, indent=4, sort_keys=True)
838839

839840
def _aux_make_obs_space_serializable(self):

grid2op/Observation/baseObservation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ def process_grid2op_compat(cls) -> None:
13091309
cls._aux_process_grid2op_compat_191()
13101310

13111311
if glop_ver < cls.MIN_VERSION_DETACH:
1312-
# alert attributes have been added in 1.9.1
1312+
# detachment has been added in grid2op 1.11
13131313
cls._aux_process_grid2op_compat_1_11_0()
13141314

13151315
cls.attr_list_set = copy.deepcopy(cls.attr_list_set)

grid2op/Space/GridObjects.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
1+
# Copyright (c) 2019-2025, RTE (https://www.rte-france.com)
22
# See AUTHORS.txt
33
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
44
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
@@ -22,6 +22,8 @@
2222
import os
2323
import numpy as np
2424
import sys
25+
26+
import importlib.metadata
2527
from packaging import version
2628
from typing import Dict, Type, Union, Literal, Any, List, Optional, ClassVar, Tuple
2729

@@ -30,11 +32,13 @@
3032
from grid2op.typing_variables import CLS_AS_DICT_TYPING, N_BUSBAR_PER_SUB_TYPING
3133
from grid2op.Exceptions import *
3234
from grid2op.Space.space_utils import extract_from_dict, save_to_dict, ElTypeInfo
35+
from grid2op.Space.default_var import (DEFAULT_ALLOW_DETACHMENT,
36+
DEFAULT_N_BUSBAR_PER_SUB,
37+
GRID2OP_CLASSES_ENV_FOLDER,
38+
GRID2OP_CURRENT_VERSION_STR,
39+
GRID2OP_CURRENT_VERSION)
3340

3441
# TODO tests of these methods and this class in general
35-
DEFAULT_N_BUSBAR_PER_SUB = 2
36-
DEFAULT_ALLOW_DETACHMENT = False
37-
GRID2OP_CLASSES_ENV_FOLDER = "_grid2op_classes"
3842

3943

4044
class GridObjects:
@@ -480,7 +484,7 @@ class GridObjects:
480484

481485
BEFORE_COMPAT_VERSION : ClassVar[str] = "neurips_2020_compat"
482486
MIN_VERSION_DETACH : ClassVar[str] = version.parse("1.11.0.dev2")
483-
glop_version : ClassVar[str] = grid2op.__version__
487+
glop_version : ClassVar[str] = GRID2OP_CURRENT_VERSION_STR
484488

485489
_INIT_GRID_CLS = None # do not modify that, this is handled by grid2op automatically
486490
_PATH_GRID_CLASSES : ClassVar[Optional[str]] = None # especially do not modify that
@@ -746,7 +750,7 @@ def _clear_grid_dependant_class_attributes(cls) -> None:
746750
cls.n_busbar_per_sub = DEFAULT_N_BUSBAR_PER_SUB
747751
cls.detachment_is_allowed = DEFAULT_ALLOW_DETACHMENT
748752

749-
cls.glop_version = grid2op.__version__
753+
cls.glop_version = GRID2OP_CURRENT_VERSION_STR
750754

751755
cls.attr_list_vect = None
752756
cls.attr_list_set = {}
@@ -2967,7 +2971,7 @@ def init_grid(cls, gridobj, force=False, extra_name=None, force_module=None, _lo
29672971
"""
29682972
# nothing to do now that the value are class member
29692973
name_res = "{}_{}".format(cls.__name__, gridobj.env_name)
2970-
if gridobj.glop_version != grid2op.__version__:
2974+
if gridobj.glop_version != GRID2OP_CURRENT_VERSION_STR:
29712975
name_res += f"_{gridobj.glop_version}"
29722976

29732977
if gridobj._PATH_GRID_CLASSES is not None:
@@ -4409,7 +4413,7 @@ class res(GridObjects):
44094413

44104414
cls.process_shunt_static_data()
44114415

4412-
if cls.glop_version != grid2op.__version__:
4416+
if cls.glop_version != GRID2OP_CURRENT_VERSION_STR:
44134417
# change name of the environment, this is done in Environment.py for regular environment
44144418
# see `self.backend.set_env_name(f"{self.name}_{self._compat_glop_version}")`
44154419
# cls.set_env_name(f"{cls.env_name}_{cls.glop_version}")
@@ -4597,7 +4601,7 @@ def init_grid_from_dict_for_pickle(name_res, orig_cls, cls_attr):
45974601
# if hasattr(res_cls, "n_sub") and res_cls.n_sub > 0:
45984602
# that's a grid2op class iniailized with an environment, I need to initialize it too
45994603
res_cls._compute_pos_big_topo_cls()
4600-
if res_cls.glop_version != grid2op.__version__:
4604+
if res_cls.glop_version != GRID2OP_CURRENT_VERSION_STR:
46014605
res_cls.process_grid2op_compat()
46024606
res_cls.process_shunt_static_data()
46034607
res_cls.process_detachment()
@@ -4981,7 +4985,7 @@ def format_el(values):
49814985
49824986
class {cls.__name__}({cls._INIT_GRID_CLS.__name__}):
49834987
BEFORE_COMPAT_VERSION = \"{cls.BEFORE_COMPAT_VERSION}\"
4984-
glop_version = grid2op.__version__ # tells it's the installed grid2op version
4988+
glop_version = \"{GRID2OP_CURRENT_VERSION_STR}\" # tells it's the installed grid2op version
49854989
_PATH_GRID_CLASSES = {_PATH_ENV_str} # especially do not modify that
49864990
_INIT_GRID_CLS = {cls._INIT_GRID_CLS.__name__}
49874991
_CLS_DICT = None # init once to avoid yet another serialization of the class as dict (in make_cls_dict)
@@ -5387,7 +5391,7 @@ def _aux_check_kirchhoff(cls,
53875391
shunt_info : Optional[ElTypeInfo] = None,
53885392
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
53895393
"""
5390-
Analogous to "backend.check_kirchhoff" but from the observation
5394+
Analogous to "backend.check_kirchhoff" but can be used for both the observation and the backend
53915395
53925396
.. versionadded:: 1.11.0
53935397

grid2op/Space/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
"ElTypeInfo",
55
"DEFAULT_N_BUSBAR_PER_SUB",
66
"GRID2OP_CLASSES_ENV_FOLDER",
7-
"DEFAULT_ALLOW_DETACHMENT"]
7+
"DEFAULT_ALLOW_DETACHMENT",
8+
"GRID2OP_CURRENT_VERSION",
9+
"GRID2OP_CURRENT_VERSION_STR"]
10+
811

912
from grid2op.Space.RandomObject import RandomObject
13+
1014
from grid2op.Space.SerializableSpace import SerializableSpace
15+
1116
from grid2op.Space.GridObjects import (GridObjects,
12-
ElTypeInfo,
13-
DEFAULT_N_BUSBAR_PER_SUB,
17+
ElTypeInfo)
18+
19+
from grid2op.Space.default_var import (DEFAULT_N_BUSBAR_PER_SUB,
1420
GRID2OP_CLASSES_ENV_FOLDER,
15-
DEFAULT_ALLOW_DETACHMENT)
21+
DEFAULT_ALLOW_DETACHMENT,
22+
GRID2OP_CURRENT_VERSION,
23+
GRID2OP_CURRENT_VERSION_STR)
24+

0 commit comments

Comments
 (0)