Skip to content

Commit

Permalink
fix issues spotted in the CI after the act.as_serializable_dict() fix…
Browse files Browse the repository at this point in the history
… and improvment

Signed-off-by: DONNOT Benjamin <[email protected]>
  • Loading branch information
BDonnot committed Nov 29, 2024
1 parent 85d2e3e commit e83face
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 55 deletions.
37 changes: 3 additions & 34 deletions grid2op/Action/baseAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down Expand Up @@ -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 "
Expand Down
32 changes: 16 additions & 16 deletions grid2op/tests/test_act_as_serializable_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,59 +398,59 @@ 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

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

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

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
Expand All @@ -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()
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions grid2op/tests/test_n_busbar_per_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e83face

Please sign in to comment.