1010import numpy as np
1111import warnings
1212from typing import Tuple , Dict , Literal , Any , List
13+
14+
1315try :
1416 from typing import Self
1517except ImportError :
1618 from typing_extensions import Self
1719
1820from packaging import version
1921
22+ import grid2op
23+ from grid2op .typing_variables import DICT_ACT_TYPING
2024from grid2op .dtypes import dt_int , dt_bool , dt_float
2125from grid2op .Exceptions import *
2226from grid2op .Space import GridObjects
@@ -624,6 +628,7 @@ def as_serializable_dict(self) -> dict:
624628
625629 """
626630 res = {}
631+ cls = type (self )
627632 # bool elements
628633 if self ._modif_alert :
629634 res ["raise_alert" ] = [
@@ -645,7 +650,7 @@ def as_serializable_dict(self) -> dict:
645650 self ._aux_serialize_add_key_change ("gen_change_bus" , "generators_id" , res ["change_bus" ])
646651 self ._aux_serialize_add_key_change ("line_or_change_bus" , "lines_or_id" , res ["change_bus" ])
647652 self ._aux_serialize_add_key_change ("line_ex_change_bus" , "lines_ex_id" , res ["change_bus" ])
648- if hasattr (type ( self ) , "n_storage" ) and type ( self ) .n_storage :
653+ if hasattr (cls , "n_storage" ) and cls .n_storage :
649654 self ._aux_serialize_add_key_change ("storage_change_bus" , "storages_id" , res ["change_bus" ])
650655 if not res ["change_bus" ]:
651656 del res ["change_bus" ]
@@ -664,7 +669,7 @@ def as_serializable_dict(self) -> dict:
664669 self ._aux_serialize_add_key_set ("gen_set_bus" , "generators_id" , res ["set_bus" ])
665670 self ._aux_serialize_add_key_set ("line_or_set_bus" , "lines_or_id" , res ["set_bus" ])
666671 self ._aux_serialize_add_key_set ("line_ex_set_bus" , "lines_ex_id" , res ["set_bus" ])
667- if hasattr (type ( self ) , "n_storage" ) and type ( self ) .n_storage :
672+ if hasattr (cls , "n_storage" ) and cls .n_storage :
668673 self ._aux_serialize_add_key_set ("storage_set_bus" , "storages_id" , res ["set_bus" ])
669674 if not res ["set_bus" ]:
670675 del res ["set_bus" ]
@@ -715,7 +720,7 @@ def as_serializable_dict(self) -> dict:
715720 if not res ["injection" ]:
716721 del res ["injection" ]
717722
718- if type ( self ) .shunts_data_available :
723+ if cls .shunts_data_available :
719724 res ["shunt" ] = {}
720725 if np .isfinite (self .shunt_p ).any ():
721726 res ["shunt" ]["shunt_p" ] = [
@@ -2094,11 +2099,31 @@ def _digest_redispatching(self, dict_):
20942099
20952100 def _digest_storage (self , dict_ ):
20962101 if "set_storage" in dict_ :
2097- self .storage_p = dict_ ["set_storage" ]
2098-
2102+ try :
2103+ self .storage_p = dict_ ["set_storage" ]
2104+ except IllegalAction as exc_ :
2105+ cls = type (self )
2106+ # only raise the error if I am not in compat mode
2107+ if cls .glop_version == grid2op .__version__ :
2108+ raise exc_
2109+ else :
2110+ # TODO be more specific on the version
2111+ warnings .warn (f"Ignored error on storage units, because "
2112+ f"you are in a backward compatibility mode." )
2113+
20992114 def _digest_curtailment (self , dict_ ):
21002115 if "curtail" in dict_ :
2101- self .curtail = dict_ ["curtail" ]
2116+ try :
2117+ self .curtail = dict_ ["curtail" ]
2118+ except IllegalAction as exc_ :
2119+ cls = type (self )
2120+ # only raise the error if I am not in compat mode
2121+ if cls .glop_version == grid2op .__version__ :
2122+ raise exc_
2123+ else :
2124+ # TODO be more specific on the version
2125+ warnings .warn (f"Ignored error on curtailment, because "
2126+ f"you are in a backward compatibility mode." )
21022127
21032128 def _digest_alarm (self , dict_ ):
21042129 """
@@ -2125,7 +2150,7 @@ def _reset_vect(self):
21252150 self ._subs_impacted = None
21262151 self ._lines_impacted = None
21272152
2128- def update (self , dict_ ):
2153+ def update (self , dict_ : DICT_ACT_TYPING ):
21292154 """
21302155 Update the action with a comprehensible format specified by a dictionary.
21312156
@@ -6410,7 +6435,6 @@ def decompose_as_unary_actions(self,
64106435 tmp += a
64116436 assert tmp == act
64126437
6413-
64146438 Parameters
64156439 ----------
64166440 group_topo : bool, optional
@@ -6473,3 +6497,56 @@ def decompose_as_unary_actions(self,
64736497 if self ._modif_curtailment :
64746498 self ._aux_decompose_as_unary_actions_curtail (cls , group_curtail , res )
64756499 return res
6500+
6501+ def _add_act_and_remove_line_status_only_set (self , other : "BaseAction" ) -> "BaseAction" :
6502+ """INTERNAL
6503+
6504+ This is used by the environment when combining action in the "set state" in env.reset.
6505+
6506+ It supposes both self and other are only "set" actions
6507+
6508+ .. versionadded:: 1.10.2
6509+
6510+ """
6511+ self += other
6512+ cls = type (self )
6513+ # switch off in self the element disconnected in other
6514+ switched_off = other ._set_line_status == - 1
6515+ switched_off |= other ._set_topo_vect [cls .line_or_pos_topo_vect ] == - 1
6516+ switched_off |= other ._set_topo_vect [cls .line_ex_pos_topo_vect ] == - 1
6517+ self ._set_topo_vect [cls .line_or_pos_topo_vect [switched_off ]] = - 1
6518+ self ._set_topo_vect [cls .line_ex_pos_topo_vect [switched_off ]] = - 1
6519+ self ._set_line_status [switched_off ] = - 1
6520+
6521+ # switch on in self the element reconnected in other
6522+ switched_on = other ._set_line_status == 1
6523+ switched_on |= other ._set_topo_vect [cls .line_or_pos_topo_vect ] > 0
6524+ switched_on |= other ._set_topo_vect [cls .line_ex_pos_topo_vect ] > 0
6525+ self ._set_line_status [switched_on ] = 1
6526+ # "reconnect" object through topo vect
6527+ topo_vect = other ._set_topo_vect > 0
6528+ self ._set_topo_vect [topo_vect ] = other ._set_topo_vect [topo_vect ]
6529+
6530+ if (self ._set_line_status != 0 ).any ():
6531+ self ._modif_set_status = True
6532+ if (self ._set_topo_vect != 0 ).any ():
6533+ self ._modif_set_bus = True
6534+ return self
6535+
6536+ def remove_change (self ) -> "BaseAction" :
6537+ """This function will modify 'self' and remove all "change" action type.
6538+
6539+ It is mainly used in the environment, when removing the "change" type for setting the original
6540+ state of the grid.
6541+
6542+ .. versionadded:: 1.10.2
6543+
6544+ """
6545+ if self ._change_bus_vect .any ():
6546+ warnings .warn ("This action modified the buses with `change_bus` " )
6547+ self ._change_bus_vect [:] = False
6548+ self ._modif_change_bus = False
6549+ if self ._switch_line_status .any ():
6550+ self ._switch_line_status [:] = False
6551+ self ._modif_change_status = False
6552+ return self
0 commit comments