Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev possibility to turn off generators, loads and storage units #675

Merged
merged 78 commits into from
Dec 16, 2024
Merged
Changes from 11 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
96e1ca5
Add: AllowShedding attribute to GridObjects
DEUCE1957 Nov 4, 2024
75cf5a7
Debug: GridObject AllowShedding conversion
DEUCE1957 Nov 4, 2024
55e8c41
Add: Exception if shedding is not supported by Backend
DEUCE1957 Nov 4, 2024
51eb97e
Refact: Remove bool return from set_shedding() method
DEUCE1957 Nov 4, 2024
623d04b
Test: Add Entry for Allow_Shedding
DEUCE1957 Nov 4, 2024
ccbd1db
Refact: Remove Allow_Shedding from dict repr of GridObject
DEUCE1957 Nov 4, 2024
9c0078e
Add: Unit Tests
DEUCE1957 Nov 5, 2024
90c96d8
Refact: Use DEFAULT constants for n_busbar and allow_shedding
DEUCE1957 Nov 6, 2024
50b7c0f
Add: Can/Cannot Handle Interface for Load/Gen detachment
DEUCE1957 Nov 6, 2024
54ef719
Refact: Remove Set_Shedding
DEUCE1957 Nov 6, 2024
d9da4bf
Add: Backend-Agnostic Detachment Check
DEUCE1957 Nov 6, 2024
e66fd03
Add: ClassAttribute integration for AllowDetachment
DEUCE1957 Nov 6, 2024
6aa67c0
Debug: Use _get_topo_vect inside Backend check for Detachment
DEUCE1957 Nov 8, 2024
aebfaa1
Test: Update Backend Tests to expect _run_pf_with_diverging to catch …
DEUCE1957 Nov 8, 2024
486cffa
Debug: Try to fix Test_Issue_125
DEUCE1957 Nov 8, 2024
f16ba3c
Merge remote-tracking branch 'deuce1957/dev_shedding' into dev_shedding
BDonnot Nov 19, 2024
938709a
Add: 'detachment_is_allowed' in GridObj and Environment
DEUCE1957 Nov 23, 2024
4112dfe
Debug: assert 'detachment_is_allowed' in GridObject
DEUCE1957 Nov 23, 2024
7b6dabc
Test: Update strings for detachment
DEUCE1957 Nov 24, 2024
0666b39
Debug: Fix nested f-string in GridObjects
DEUCE1957 Nov 24, 2024
7cdf6c2
Debug: Add post-powerflow check for load / gen disconnect
DEUCE1957 Nov 24, 2024
bb23193
Debug: Add convergence check to post-powerflow check
DEUCE1957 Nov 24, 2024
c4d2c5c
Merge remote-tracking branch 'deuce1957/dev_shedding' into dev_shedding
BDonnot Nov 25, 2024
62f5428
fixing conflicts
BDonnot Nov 25, 2024
e921057
fix some minor things, before handling actions [skip ci]
BDonnot Nov 25, 2024
d9b3e11
some addition of the feature on the action, not finished yet
BDonnot Nov 25, 2024
6e08212
forbid args in env creation, fix bug on CI, refacto the 'make' call
BDonnot Nov 25, 2024
4cf73c6
fixes to make tests pass
BDonnot Nov 25, 2024
dbba375
fix issue 125
BDonnot Nov 25, 2024
09d1a65
still working on making tests pass with new more concise implementation
BDonnot Nov 25, 2024
d6bf9a8
should fix all issues, need to make the AAA test suite better now
BDonnot Nov 26, 2024
8b70407
fixing broken tests (hopefully) and improve AAA backend test suite fo…
BDonnot Nov 26, 2024
2e81300
remove a pdb...
BDonnot Nov 26, 2024
3e25e95
Debug: Fix check in PandaPower Backend that prevents large-scale load…
DEUCE1957 Nov 27, 2024
d2d71ba
Debug: Use load dataframe instead of res_load for in_service check
DEUCE1957 Nov 27, 2024
809dfe4
Debug: Temporarily Rollback HotFix to PandaPowerBackend
DEUCE1957 Nov 27, 2024
5350712
Merge pull request #1 from BDonnot/dev_shedding
DEUCE1957 Nov 27, 2024
7d34daa
Merge remote-tracking branch 'upstream/dev_1.11.0' into dev_dev_shedding
BDonnot Nov 28, 2024
b970632
fixing conflicts when synching with dev_shedding
BDonnot Nov 28, 2024
a97643c
add possibility to detach generators and storage units in the action
BDonnot Nov 28, 2024
d2117e9
Merge branch 'dev_1.11.0' of https://github.com/Grid2op/grid2op into …
DEUCE1957 Dec 2, 2024
edcc81c
Refact: Improve Code Smell
DEUCE1957 Dec 2, 2024
5ee9aee
Add: Bring back fix to PandaPowerBackend for isolated loads
DEUCE1957 Dec 2, 2024
9adc3a4
Refact: Simplify _check_kirchhoff
DEUCE1957 Dec 2, 2024
42535de
Debug: Fix missing param in kirchhoff static methods
DEUCE1957 Dec 2, 2024
9a6b696
Refact: Emulate obs._check_kirchhoff in Backend._check_kirchhoff
DEUCE1957 Dec 2, 2024
0df6f5d
Merge branch 'dev_shedding' into dev_dev_shedding
BDonnot Dec 3, 2024
d07041b
fixing conflicts by merging with 'upstream' and bd_dev
BDonnot Dec 3, 2024
3bf8934
Merge pull request #671 from Grid2op/dev_1.11.0
BDonnot Dec 3, 2024
5851026
refactoring check_kirchhoff methods of Observation and Backend, shoul…
BDonnot Dec 4, 2024
2b37759
Merge branch 'dev_shedding' into dev_dev_shedding
BDonnot Dec 4, 2024
3529f10
fix a bug in loading back actions from / to json and to / from vect …
BDonnot Dec 4, 2024
38cd8c3
fix issue with copying action, add action topological_impact caching
BDonnot Dec 4, 2024
70d9292
Merge: dev_1.11.0 into dev_shedding
DEUCE1957 Dec 5, 2024
7dcbfa4
Merge pull request #2 from BDonnot/dev_shedding
DEUCE1957 Dec 5, 2024
62a1471
fix a bug when deserializing actions
BDonnot Dec 5, 2024
777d778
fix some other bugs in the tests
BDonnot Dec 5, 2024
24c6cf6
try to fix ci bug that I cannot reproduce
BDonnot Dec 5, 2024
17afbf4
fix some issue when resetting the class attributes, hopefully
BDonnot Dec 5, 2024
1c0220f
trying to fix bug on CI
BDonnot Dec 6, 2024
eb49910
trying to fix bug on CI
BDonnot Dec 6, 2024
8dcf4aa
adding support for detachment in backend action
BDonnot Dec 12, 2024
d3415f9
adding first working -for basic functionality- for shedding, end to end
BDonnot Dec 13, 2024
812a2e8
Merge remote-tracking branch 'upstream/dev_1.11.0' into dev_shedding
BDonnot Dec 13, 2024
c29b619
fix some last issue before merging on dev_turnoff_gen_load
BDonnot Dec 13, 2024
fb28f9b
fix a typo and synch with base branch
BDonnot Dec 13, 2024
7241e13
fix another typo
BDonnot Dec 13, 2024
93ec180
fixing broken tests for the Observation
BDonnot Dec 13, 2024
d67e08d
fix some broken tests again
BDonnot Dec 13, 2024
953d273
fix some other broken tests
BDonnot Dec 13, 2024
0a40daf
yet some other fixes
BDonnot Dec 13, 2024
73d14dd
Merge branch 'BDonnot-dev_shedding' into dev_shedding
DEUCE1957 Dec 13, 2024
d0e8385
Merge pull request #655 from DEUCE1957/dev_shedding
BDonnot Dec 13, 2024
7d8af65
upgrade to version 1.11.0.dev3
BDonnot Dec 13, 2024
cb7a9f4
fixing some issues spotted by sonarcube
BDonnot Dec 16, 2024
55f08cf
two minor fixes
BDonnot Dec 16, 2024
c6299c9
some fixes due to matplotlib deprecation function
BDonnot Dec 16, 2024
1b717c8
Merge pull request #674 from BDonnot/dev_dev_shedding
BDonnot Dec 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -107,6 +107,8 @@ Native multi agents support:
name is added. This behaviour can be turned off by passing `_add_cls_nm_bk=False`
when calling `grid2op.make(...)`. If you develop a new Backend, you can also
customize the added name by overloading the `get_class_added_name` class method.
- [BREAKING] it is now forbidden to create environment with arguments.
Only key-word arguments are allowed.
- [FIXED] issue https://github.com/Grid2op/grid2op/issues/657
- [FIXED] missing an import on the `MaskedEnvironment` class
- [FIXED] a bug when trying to set the load_p, load_q, gen_p, gen_v by names.
@@ -138,7 +140,9 @@ Native multi agents support:
does not have shunt information but there are not shunts on the grid.
- [IMPROVED] consistency of `MultiMixEnv` in case of automatic_classes (only one
class is generated for all mixes)

- [IMRPOVED] handling of disconnected elements in the backend no more
raise error. The base `Backend` class does that.

[1.10.4] - 2024-10-15
-------------------------
- [FIXED] new pypi link (no change in code)
88 changes: 47 additions & 41 deletions grid2op/Backend/backend.py
Original file line number Diff line number Diff line change
@@ -121,6 +121,9 @@
_complete_action_class : "Optional[grid2op.Action.CompleteAction]"= None

ERR_INIT_POWERFLOW : str = "Power cannot be computed on the first time step, please check your data."
ERR_DETACHMENT : str = ("One or more {} were isolated from the grid "
"but this is not allowed or not supported (Game Over) (detachment_is_allowed is False), "
"check {} {}")
def __init__(self,
detailed_infos_for_cascading_failures:bool=False,
can_be_copied:bool=True,
@@ -180,9 +183,9 @@
#: You should not worry about the class attribute of the backend in :func:`Backend.apply_action`
self.n_busbar_per_sub: int = DEFAULT_N_BUSBAR_PER_SUB

# .. versionadded: 1.11.0
self._missing_detachment_support:bool = True
self.detachment_is_allowed:bool = DEFAULT_ALLOW_DETACHMENT
#: .. versionadded: 1.11.0
self._missing_detachment_support_info : bool = True
self.detachment_is_allowed : bool = DEFAULT_ALLOW_DETACHMENT

def can_handle_more_than_2_busbar(self):
"""
@@ -270,7 +273,7 @@
We highly recommend you do not try to override this function.
At least, at time of writing there is no good reason to do so.
"""
self._missing_detachment_support = False
self._missing_detachment_support_info = False
self.detachment_is_allowed = type(self).detachment_is_allowed

def cannot_handle_detachment(self):
@@ -297,7 +300,7 @@
We highly recommend you do not try to override this function.
At least, at time of writing there is no good reason to do so.
"""
self._missing_detachment_support = False
self._missing_detachment_support_info = False
if type(self).detachment_is_allowed != DEFAULT_ALLOW_DETACHMENT:
warnings.warn("You asked in 'make' function to allow shedding. This is"
f"not possible with a backend of type {type(self)}.")
@@ -724,10 +727,10 @@
:return: an array with the line status of each powerline
:rtype: np.array, dtype:bool
"""
cls = type(self)
topo_vect = self.get_topo_vect()
return (topo_vect[self.line_or_pos_topo_vect] >= 0) & (
topo_vect[self.line_ex_pos_topo_vect] >= 0
)
return ((topo_vect[cls.line_or_pos_topo_vect] >= 0) &
(topo_vect[cls.line_ex_pos_topo_vect] >= 0))

def get_line_flow(self) -> np.ndarray:
"""
@@ -1079,45 +1082,43 @@
"""
conv = False
exc_me = None

cls = type(self)
try:
# Check if loads/gens have been detached and if this is allowed, otherwise raise an error
# .. versionadded:: 1.11.0
if hasattr(self, "_get_topo_vect"):
topo_vect = self._get_topo_vect()
else:
topo_vect = self.get_topo_vect()

load_buses = topo_vect[self.load_pos_topo_vect]
conv, exc_me = self.runpf(is_dc=is_dc) # run powerflow

if not self.detachment_is_allowed and (load_buses == -1).any():
raise Grid2OpException(f"One or more loads were detached before powerflow in Backend {type(self).__name__}"
"but this is not allowed or not supported (Game Over)")

gen_buses = topo_vect[self.gen_pos_topo_vect]
if not conv:
if exc_me is not None:
raise exc_me
raise BackendError("Divergence of the powerflow without further information.")

if not self.detachment_is_allowed and (gen_buses == -1).any():
raise Grid2OpException(f"One or more generators were detached before powerflow in Backend {type(self).__name__}"
"but this is not allowed or not supported (Game Over)")
# Check if loads/gens have been detached and if this is allowed, otherwise raise an error
# .. versionadded:: 1.11.0
topo_vect = self.get_topo_vect()
load_buses = topo_vect[cls.load_pos_topo_vect]
if not cls.detachment_is_allowed and (load_buses == -1).any():
raise BackendError(cls.ERR_DETACHMENT.format("loads", "loads", (load_buses == -1).nonzero()[0]))

gen_buses = topo_vect[cls.gen_pos_topo_vect]
if not cls.detachment_is_allowed and (gen_buses == -1).any():
raise BackendError(cls.ERR_DETACHMENT.format("gens", "gens", (gen_buses == -1).nonzero()[0]))

conv, exc_me = self.runpf(is_dc=is_dc) # run powerflow
if cls.n_storage > 0:
storage_buses = topo_vect[cls.storage_pos_topo_vect]
storage_p, *_ = self.storages_info()

Check failure on line 1107 in grid2op/Backend/backend.py

Codacy Production / Codacy Static Code Analysis

grid2op/Backend/backend.py#L1107

Assigning result of a function call, where the function has no return
sto_maybe_error = (storage_buses == -1) & (np.abs(storage_p) >= 1e-6)
if not cls.detachment_is_allowed and sto_maybe_error.any():
raise BackendError((cls.ERR_DETACHMENT.format("storages", "storages", sto_maybe_error.nonzero()[0]) +
" NB storage units are allowed to be disconnected even if "
"`detachment_is_allowed` is False but only if the don't produce active power."))

except Grid2OpException as exc_:
exc_me = exc_

if not conv and exc_me is None:
exc_me = DivergingPowerflow(
exc_me = BackendError(
"GAME OVER: Powerflow has diverged during computation "
"or a load has been disconnected or a generator has been disconnected."
)

# Post-Powerflow Check
if not self.detachment_is_allowed and conv:
resulting_act = self.get_action_to_set()
load_buses_act_set = resulting_act._set_topo_vect[self.load_pos_topo_vect]
gen_buses_act_set = resulting_act._set_topo_vect[self.gen_pos_topo_vect]
if (load_buses_act_set == -1).any() or (gen_buses_act_set == -1).any():
exc_me = Grid2OpException(f"One or more generators or loads were detached in Backend {type(self).__name__}"
" as a result of a Grid2Op action, but this is not allowed or not supported (Game Over)")
return exc_me

def next_grid_state(self,
@@ -2047,6 +2048,11 @@
)
prod_p, _, prod_v = self.generators_info()
load_p, load_q, _ = self.loads_info()
if type(self)._complete_action_class is None:
# some bug in multiprocessing, this was not set
# sub processes
from grid2op.Action.completeAction import CompleteAction
type(self)._complete_action_class = CompleteAction.init_grid(type(self))
set_me = self._complete_action_class()
dict_ = {
"set_line_status": line_status,
@@ -2185,8 +2191,8 @@
"attribute. This is known issue in lightims2grid <= 0.7.5. Please "
"upgrade your backend. This will raise an error in the future.")

if hasattr(self, "_missing_detachment_support"):
if self._missing_detachment_support:
if hasattr(self, "_missing_detachment_support_info"):
if self._missing_detachment_support_info:
warnings.warn("The backend implementation you are using is probably too old to take advantage of the "
"new feature added in grid2op 1.11.0: the possibility "
"to detach loads or generators without leading to an immediate game over. "
@@ -2198,12 +2204,12 @@
"\nAnd of course, ideally, if the current implementation "
"of your backend cannot handle detachment then change it :-)\n"
"Your backend will behave as if it did not support it.")
self._missing_detachment_support = False
self._missing_detachment_support_info = False
self.detachment_is_allowed = DEFAULT_ALLOW_DETACHMENT
else:
self._missing_detachment_support = False
self._missing_detachment_support_info = False
self.detachment_is_allowed = DEFAULT_ALLOW_DETACHMENT
warnings.warn("Your backend is missing the `_missing_detachment_support` "
warnings.warn("Your backend is missing the `_missing_detachment_support_info` "
"attribute.")

orig_type = type(self)
Loading