diff --git a/grid2op/Action/baseAction.py b/grid2op/Action/baseAction.py index f3f0c68f..587ee00c 100644 --- a/grid2op/Action/baseAction.py +++ b/grid2op/Action/baseAction.py @@ -730,15 +730,15 @@ def as_serializable_dict(self) -> dict: res["shunt"] = {} if np.isfinite(self.shunt_p).any(): res["shunt"]["shunt_p"] = [ - (int(sh_id), float(val)) for sh_id, val in enumerate(self.shunt_p) if np.isfinite(val) + (str(cls.name_shunt[sh_id]), float(val)) for sh_id, val in enumerate(self.shunt_p) if np.isfinite(val) ] if np.isfinite(self.shunt_q).any(): res["shunt"]["shunt_q"] = [ - (int(sh_id), float(val)) for sh_id, val in enumerate(self.shunt_q) if np.isfinite(val) + (str(cls.name_shunt[sh_id]), float(val)) for sh_id, val in enumerate(self.shunt_q) if np.isfinite(val) ] if (self.shunt_bus != 0).any(): res["shunt"]["shunt_bus"] = [ - (int(sh_id), int(val)) + (str(cls.name_shunt[sh_id]), int(val)) for sh_id, val in enumerate(self.shunt_bus) if val != 0 ] @@ -1901,37 +1901,6 @@ def _digest_shunt(self, dict_): np.arange(cls.n_shunt), vect_self ) - - # if isinstance(tmp, np.ndarray): - # # complete shunt vector is provided - # vect_self[:] = tmp - # elif isinstance(tmp, list): - # # expected a list: (id shunt, new bus) - # for (sh_id, new_bus) in tmp: - # if sh_id < 0: - # raise AmbiguousAction( - # "Invalid shunt id {}. Shunt id should be positive".format( - # sh_id - # ) - # ) - # if sh_id >= cls.n_shunt: - # raise AmbiguousAction( - # "Invalid shunt id {}. Shunt id should be less than the number " - # "of shunt {}".format(sh_id, cls.n_shunt) - # ) - # if key_n == "shunt_bus" or key_n == "set_bus": - # if new_bus <= -2: - # raise IllegalAction( - # f"Cannot ask for a shunt bus <= -2, found {new_bus} for shunt id {sh_id}" - # ) - # elif new_bus > cls.n_busbar_per_sub: - # raise IllegalAction( - # f"Cannot ask for a shunt bus > {cls.n_busbar_per_sub} " - # f"the maximum number of busbar per substations" - # f", found {new_bus} for shunt id {sh_id}" - # ) - - # vect_self[sh_id] = new_bus else: raise AmbiguousAction( "Invalid way to modify {} for shunts. It should be a numpy array or a " diff --git a/grid2op/tests/test_act_as_serializable_dict.py b/grid2op/tests/test_act_as_serializable_dict.py index 3ac3df59..fc274191 100644 --- a/grid2op/tests/test_act_as_serializable_dict.py +++ b/grid2op/tests/test_act_as_serializable_dict.py @@ -398,14 +398,14 @@ def tearDown(self) -> None: def test_can_make_lineor(self): act : BaseAction = self.env1.action_space({"set_bus": {"lines_or_id": [(0, 2), (5, 1), (15, 2)]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'set_bus': {'lines_or_id': [(0, 2), (5, 1), (15, 2)]}} + assert dict_ == {'set_bus': {'lines_or_id': [('0_1_0', 2), ('2_3_5', 1), ('3_6_15', 2)]}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 act : BaseAction = self.env1.action_space({"change_bus": {"lines_or_id": [0, 5, 15]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'change_bus': {'lines_or_id': [0, 5, 15]}} + assert dict_ == {'change_bus': {'lines_or_id': ['0_1_0', '2_3_5', '3_6_15']}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 @@ -413,14 +413,14 @@ def test_can_make_lineor(self): def test_can_make_lineex(self): act : BaseAction = self.env1.action_space({"set_bus": {"lines_ex_id": [(0, 2), (5, 1), (15, 2)]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'set_bus': {'lines_ex_id': [(0, 2), (5, 1), (15, 2)]}} + assert dict_ == {'set_bus': {'lines_ex_id': [('0_1_0', 2), ('2_3_5', 1), ('3_6_15', 2)]}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 act : BaseAction = self.env1.action_space({"change_bus": {"lines_ex_id": [0, 5, 15]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'change_bus': {'lines_ex_id': [0, 5, 15]}} + assert dict_ == {'change_bus': {'lines_ex_id': ['0_1_0', '2_3_5', '3_6_15']}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 @@ -428,14 +428,14 @@ def test_can_make_lineex(self): def test_can_make_gen(self): act : BaseAction = self.env1.action_space({"set_bus": {"generators_id": [(0, 2), (5, 1)]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'set_bus': {'generators_id': [(0, 2), (5, 1)]}} + assert dict_ == {'set_bus': {'generators_id': [('gen_1_0', 2), ('gen_0_5', 1)]}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 act : BaseAction = self.env1.action_space({"change_bus": {"generators_id": [0, 5]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'change_bus': {'generators_id': [0, 5]}} + assert dict_ == {'change_bus': {'generators_id': ['gen_1_0', 'gen_0_5']}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 @@ -443,14 +443,14 @@ def test_can_make_gen(self): def test_can_make_load(self): act : BaseAction = self.env1.action_space({"set_bus": {"loads_id": [(0, 2), (5, 1)]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'set_bus': {'loads_id': [(0, 2), (5, 1)]}} + assert dict_ == {'set_bus': {'loads_id': [('load_1_0', 2), ('load_8_5', 1)]}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 act : BaseAction = self.env1.action_space({"change_bus": {"loads_id": [0, 5]}}) dict_ = act.as_serializable_dict() - assert dict_ == {'change_bus': {'loads_id': [0, 5]}} + assert dict_ == {'change_bus': {'loads_id': ['load_1_0', 'load_8_5']}} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() assert dict_ == dict_2 @@ -462,10 +462,10 @@ def test_with_gen_load_lineor_lineex(self): "lines_or_id": [(0, 2), (5, 1), (15, 2)] }}) dict_ = act.as_serializable_dict() - assert dict_ == {'set_bus': {'loads_id': [(0, 2), (5, 1)], - 'generators_id': [(0, 2), (5, 1)], - 'lines_ex_id': [(0, 2), (5, 1), (15, 2)], - 'lines_or_id': [(0, 2), (5, 1), (15, 2)] + assert dict_ == {'set_bus': {'loads_id': [('load_1_0', 2), ('load_8_5', 1)], + 'generators_id': [('gen_1_0', 2), ('gen_0_5', 1)], + 'lines_ex_id': [('0_1_0', 2), ('2_3_5', 1), ('3_6_15', 2)], + 'lines_or_id': [('0_1_0', 2), ('2_3_5', 1), ('3_6_15', 2)] }} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() @@ -477,10 +477,10 @@ def test_with_gen_load_lineor_lineex(self): 'lines_or_id': [0, 5, 15] }}) dict_ = act.as_serializable_dict() - assert dict_ == {'change_bus': {'loads_id': [0, 5], - 'generators_id': [0, 5], - 'lines_ex_id': [0, 5, 15], - 'lines_or_id': [0, 5, 15] + assert dict_ == {'change_bus': {'loads_id': ["load_1_0", "load_8_5"], + 'generators_id': ["gen_1_0", "gen_0_5"], + 'lines_ex_id': ["0_1_0", "2_3_5", "3_6_15"], + 'lines_or_id': ["0_1_0", "2_3_5", "3_6_15"] }} act2 = self.env2.action_space(dict_) dict_2 = act2.as_serializable_dict() diff --git a/grid2op/tests/test_n_busbar_per_sub.py b/grid2op/tests/test_n_busbar_per_sub.py index f1e59b0c..8a5f7f17 100644 --- a/grid2op/tests/test_n_busbar_per_sub.py +++ b/grid2op/tests/test_n_busbar_per_sub.py @@ -506,18 +506,18 @@ def _aux_test_act_consistent_as_dict(self, act_as_dict, name_xxx, el_id, bus_val tmp = act_as_dict["shunt"]["shunt_bus"] assert tmp[el_id] == bus_val - def _aux_test_act_consistent_as_serializable_dict(self, act_as_dict, el_nms, el_id, bus_val): + def _aux_test_act_consistent_as_serializable_dict(self, act_as_dict, el_nms, el_id, bus_val, nm_els): if el_nms is not None: # regular element assert "set_bus" in act_as_dict assert el_nms in act_as_dict["set_bus"] tmp = act_as_dict["set_bus"][el_nms] - assert tmp == [(el_id, bus_val)] + assert tmp == [(nm_els[el_id], bus_val)] else: # shunts of other things not in the topo vect assert "shunt" in act_as_dict tmp = act_as_dict["shunt"]["shunt_bus"] - assert tmp == [(el_id, bus_val)] + assert tmp == [(nm_els[el_id], bus_val)] def _aux_test_action(self, act : BaseAction, name_xxx, el_id, bus_val, el_nms): assert act.can_affect_something() @@ -526,7 +526,7 @@ def _aux_test_action(self, act : BaseAction, name_xxx, el_id, bus_val, el_nms): tmp = act.as_dict() # test I can convert to dict self._aux_test_act_consistent_as_dict(tmp, name_xxx, el_id, bus_val) tmp = act.as_serializable_dict() # test I can convert to another type of dict - self._aux_test_act_consistent_as_serializable_dict(tmp, el_nms, el_id, bus_val) + self._aux_test_act_consistent_as_serializable_dict(tmp, el_nms, el_id, bus_val, name_xxx) def _aux_test_set_bus_onebus(self, nm_prop, el_id, bus_val, name_xxx, el_nms): act = self.env.action_space() @@ -610,7 +610,7 @@ def _aux_test_action_shunt(self, act : BaseAction, el_id, bus_val): tmp = act.as_dict() # test I can convert to dict self._aux_test_act_consistent_as_dict(tmp, name_xxx, el_id, bus_val) tmp = act.as_serializable_dict() # test I can convert to another type of dict - self._aux_test_act_consistent_as_serializable_dict(tmp, el_nms, el_id, bus_val) + self._aux_test_act_consistent_as_serializable_dict(tmp, el_nms, el_id, bus_val, type(act).name_shunt) def test_shunt(self): el_id = 0