Skip to content

Commit 00c114b

Browse files
committed
still fixing some bugs
Signed-off-by: DONNOT Benjamin <[email protected]>
1 parent 4d303cd commit 00c114b

File tree

3 files changed

+118
-52
lines changed

3 files changed

+118
-52
lines changed

grid2op/Environment/environment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ def _init_backend(
491491
_no_overflow_disconnection = self._no_overflow_disconnection
492492
self._no_overflow_disconnection = True
493493
self._last_obs = None
494+
self._called_from_reset = True
494495
*_, fail_to_start, info = self.step(do_nothing)
495496
self._no_overflow_disconnection = _no_overflow_disconnection
496497

@@ -499,7 +500,8 @@ def _init_backend(
499500
"Impossible to initialize the powergrid, the powerflow diverge at iteration 0. "
500501
"Available information are: {}".format(info)
501502
) from info["exception"][0]
502-
503+
self._called_from_reset = False
504+
503505
# test the backend returns object of the proper size
504506
if need_process_backend:
505507

grid2op/tests/test_Observation.py

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,10 +2522,12 @@ def test_change_line_status(self):
25222522
# Create change action
25232523
change_act = self.env.action_space({"change_line_status": change_status})
25242524
# Simulate & Step
2525-
self.sim_obs, reward_sim, done_sim, _ = self.obs.simulate(change_act)
2526-
self.step_obs, reward_real, done_real, _ = self.env.step(change_act)
2527-
assert not done_sim
2528-
assert not done_real
2525+
self.sim_obs, reward_sim, done_simulate, info_simu = self.obs.simulate(change_act)
2526+
self.step_obs, reward_real, done_step, info_step = self.env.step(change_act)
2527+
assert not done_simulate, f"simulate is done, this should not be"
2528+
assert not done_step, f"step is done, this should not be"
2529+
assert not info_simu["exception"]
2530+
assert not info_step["exception"]
25292531
assert abs(reward_sim - reward_real) <= 1e-7
25302532
# Test observations are the same
25312533
if self.sim_obs != self.step_obs:
@@ -2540,8 +2542,12 @@ def test_set_line_status(self):
25402542
# Create set action
25412543
set_act = self.env.action_space({"set_line_status": set_status})
25422544
# Simulate & Step
2543-
self.sim_obs, _, _, _ = self.obs.simulate(set_act)
2544-
self.step_obs, _, _, _ = self.env.step(set_act)
2545+
self.sim_obs, _, done_simulate, info_simu = self.obs.simulate(set_act)
2546+
self.step_obs, _, done_step, info_step = self.env.step(set_act)
2547+
assert not done_simulate, f"simulate is done, this should not be"
2548+
assert not done_step, f"step is done, this should not be"
2549+
assert not info_simu["exception"]
2550+
assert not info_step["exception"]
25452551
# Test observations are the same
25462552
if self.sim_obs != self.step_obs:
25472553
diff_, attr_diff = self.sim_obs.where_different(self.step_obs)
@@ -2554,21 +2560,26 @@ def test_change_bus(self):
25542560
"change_bus": {
25552561
"loads_id": [0],
25562562
"generators_ids": [0],
2557-
"lines_or_id": [0],
2563+
# "lines_or_id": [0],
25582564
"lines_ex_id": [0],
25592565
}
25602566
}
25612567
)
25622568
# Simulate & Step
2563-
self.sim_obs, _, _, _ = self.obs.simulate(change_act)
2564-
self.step_obs, _, _, _ = self.env.step(change_act)
2569+
self.sim_obs, _, done_simulate, info_simu = self.obs.simulate(change_act)
2570+
self.step_obs, _, done_step, info_step = self.env.step(change_act)
2571+
assert not done_simulate, f"simulate is done, this should not be"
2572+
assert not done_step, f"step is done, this should not be"
2573+
assert not info_simu["exception"]
2574+
assert not info_step["exception"]
2575+
25652576
assert isinstance(
25662577
self.sim_obs, type(self.step_obs)
25672578
), "sim_obs is not the same type as the step"
25682579
assert isinstance(
25692580
self.step_obs, type(self.sim_obs)
25702581
), "step is not the same type as the simulation"
2571-
2582+
25722583
# Test observations are the same
25732584
if self.sim_obs != self.step_obs:
25742585
diff_, attr_diff = self.sim_obs.where_different(self.step_obs)
@@ -2587,14 +2598,18 @@ def test_set_bus(self):
25872598
"set_bus": {
25882599
"loads_id": [(0, new_load_bus)],
25892600
"generators_ids": [(0, new_gen_bus)],
2590-
"lines_or_id": [(0, new_lor_bus)],
2601+
# "lines_or_id": [(0, new_lor_bus)],
25912602
"lines_ex_id": [(0, new_lex_bus)],
25922603
}
25932604
}
25942605
)
25952606
# Simulate & Step
2596-
self.sim_obs, _, _, _ = self.obs.simulate(set_act)
2597-
self.step_obs, _, _, _ = self.env.step(set_act)
2607+
self.sim_obs, reward_sim, done_simulate, info_simu = self.obs.simulate(set_act)
2608+
self.step_obs, reward_real, done_step, info_step = self.env.step(set_act)
2609+
assert not done_simulate, f"simulate is done, this should not be"
2610+
assert not done_step, f"step is done, this should not be"
2611+
assert not info_simu["exception"]
2612+
assert not info_step["exception"]
25982613
# Test observations are the same
25992614
if self.sim_obs != self.step_obs:
26002615
diff_, attr_diff = self.sim_obs.where_different(self.step_obs)
@@ -2610,8 +2625,13 @@ def test_redispatch(self):
26102625
# Create redispatch action
26112626
redisp_act = self.env.action_space({"redispatch": [(gen_id, redisp_val)]})
26122627
# Simulate & Step
2613-
self.sim_obs, _, _, _ = self.obs.simulate(redisp_act)
2614-
self.step_obs, _, _, _ = self.env.step(redisp_act)
2628+
self.sim_obs, reward_sim, done_simulate, info_simu = self.obs.simulate(redisp_act)
2629+
self.step_obs, reward_real, done_step, info_step = self.env.step(redisp_act)
2630+
assert not done_simulate, f"simulate is done, this should not be"
2631+
assert not done_step, f"step is done, this should not be"
2632+
assert not info_simu["exception"]
2633+
assert not info_step["exception"]
2634+
assert abs(reward_sim - reward_real) <= 1e-7
26152635
# Test observations are the same
26162636
if self.sim_obs != self.step_obs:
26172637
diff_, attr_diff = self.sim_obs.where_different(self.step_obs)
@@ -2817,20 +2837,25 @@ def test_multi_simulate_last_change_bus(self):
28172837
change_bus_act = self.env.action_space(
28182838
{
28192839
"change_bus": {
2820-
"loads_id": [1],
2821-
"generators_ids": [1],
2822-
"lines_or_id": [1],
2840+
# "loads_id": [1],
2841+
# "generators_ids": [1],
2842+
# "lines_or_id": [1],
28232843
"lines_ex_id": [1],
28242844
}
28252845
}
28262846
)
28272847
actions.append(change_bus_act)
2828-
28292848
# Simulate all actions
28302849
for act in actions:
2831-
self.sim_obs, _, _, _ = self.obs.simulate(act)
2850+
self.sim_obs, sim_r, sim_d, sim_i = self.obs.simulate(act)
28322851
# Step with last action
2833-
self.step_obs, _, _, _ = self.env.step(actions[-1])
2852+
self.step_obs, step_r, step_d, step_i = self.env.step(actions[-1])
2853+
assert not sim_d, f"simulate is done, this should not be"
2854+
assert not step_d, f"step is done, this should not be"
2855+
assert not sim_i["exception"]
2856+
assert not step_i["exception"]
2857+
assert abs(sim_r - step_r) <= 1e-7
2858+
28342859
# Test observations are the same
28352860
if self.sim_obs != self.step_obs:
28362861
diff_, attr_diff = self.sim_obs.where_different(self.step_obs)
@@ -2850,9 +2875,9 @@ def test_multi_simulate_last_set_bus(self):
28502875
set_bus_act = self.env.action_space(
28512876
{
28522877
"set_bus": {
2853-
"loads_id": [(1, new_load_bus)],
2854-
"generators_ids": [(1, new_gen_bus)],
2855-
"lines_or_id": [(1, new_lor_bus)],
2878+
# "loads_id": [(1, new_load_bus)],
2879+
# "generators_ids": [(1, new_gen_bus)],
2880+
# "lines_or_id": [(1, new_lor_bus)],
28562881
"lines_ex_id": [(1, new_lex_bus)],
28572882
}
28582883
}
@@ -2861,9 +2886,14 @@ def test_multi_simulate_last_set_bus(self):
28612886

28622887
# Simulate all actions
28632888
for act in actions:
2864-
self.sim_obs, _, _, _ = self.obs.simulate(act)
2889+
self.sim_obs, sim_r, sim_d, sim_i = self.obs.simulate(act)
28652890
# Step with last action
2866-
self.step_obs, _, _, _ = self.env.step(actions[-1])
2891+
self.step_obs, step_r, step_d, step_i = self.env.step(actions[-1])
2892+
assert not sim_d, f"simulate is done, this should not be"
2893+
assert not step_d, f"step is done, this should not be"
2894+
assert not sim_i["exception"]
2895+
assert not step_i["exception"]
2896+
assert abs(sim_r - step_r) <= 1e-7
28672897
# Test observations are the same
28682898
if self.sim_obs != self.step_obs:
28692899
diff_, attr_diff = self.sim_obs.where_different(self.step_obs)

grid2op/tests/test_Rules.py

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import warnings
1111
import unittest
1212

13+
from grid2op.Observation.baseObservation import BaseObservation
1314
from grid2op.tests.helper_path_test import *
1415

1516
import grid2op
@@ -684,105 +685,138 @@ def setUp(self):
684685
warnings.filterwarnings("ignore")
685686
self.params = Parameters()
686687
self.env = grid2op.make("rte_case5_example", test=True, param=self.params, _add_to_name=type(self).__name__)
687-
688+
self.env_or_obs = self.env
689+
688690
def tearDown(self):
689691
self.env.close()
690692

693+
def _aux_do_act(self, act):
694+
if isinstance(self.env_or_obs, Environment):
695+
_, _, _, info = self.env_or_obs.step(act)
696+
elif isinstance(self.env_or_obs, BaseObservation):
697+
_, _, _, info = self.env_or_obs.simulate(act)
698+
else:
699+
raise RuntimeError(f"Test not implemented for {type(self.env_or_obs)}")
700+
return info
701+
702+
def _aux_change_par(self, max_sub_changed):
703+
if isinstance(self.env_or_obs, Environment):
704+
self.env_or_obs._parameters.MAX_SUB_CHANGED = max_sub_changed
705+
elif isinstance(self.env_or_obs, BaseObservation):
706+
self.env_or_obs._obs_env._parameters.MAX_SUB_CHANGED = max_sub_changed
707+
else:
708+
raise RuntimeError(f"Test not implemented for {type(self.env_or_obs)}")
709+
691710
def test_setbus_line_no_sub_allowed_is_illegal(self):
692711
# Set 0 allowed substation changes
693-
self.env._parameters.MAX_SUB_CHANGED = 0
712+
self._aux_change_par(0)
694713
# Make a setbus
695714
LINE_ID = 4
696715
bus_action = self.env.action_space({"set_bus": {"lines_ex_id": [(LINE_ID, 2)]}})
697716
# Make sure its illegal
698-
_, _, _, i = self.env.step(bus_action)
699-
assert i["is_illegal"] == True
717+
info = self._aux_do_act(bus_action)
718+
assert info["is_illegal"] == True
700719

701720
def test_two_setbus_line_one_sub_allowed_is_illegal(self):
702721
# Set 1 allowed substation changes
703-
self.env._parameters.MAX_SUB_CHANGED = 1
722+
self._aux_change_par(1)
704723
# Make a double setbus
705724
LINE1_ID = 4
706725
LINE2_ID = 5
707726
bus_action = self.env.action_space(
708727
{"set_bus": {"lines_ex_id": [(LINE1_ID, 2), (LINE2_ID, 2)]}}
709728
)
710729
# Make sure its illegal
711-
_, _, _, i = self.env.step(bus_action)
712-
assert i["is_illegal"] == True
730+
info = self._aux_do_act(bus_action)
731+
assert info["is_illegal"] == True
713732

714733
def test_one_setbus_line_one_sub_allowed_is_legal(self):
715734
# Set 1 allowed substation changes
716-
self.env._parameters.MAX_SUB_CHANGED = 1
735+
self._aux_change_par(1)
717736
# Make a setbus
718737
LINE1_ID = 4
719738
bus_action = self.env.action_space(
720739
{"set_bus": {"lines_ex_id": [(LINE1_ID, 2)]}}
721740
)
722741
# Make sure its legal
723-
_, _, _, i = self.env.step(bus_action)
724-
assert i["is_illegal"] == False
742+
info = self._aux_do_act(bus_action)
743+
assert info["is_illegal"] == False
725744

726745
def test_two_setbus_line_two_sub_allowed_is_legal(self):
727746
# Set 2 allowed substation changes
728-
self.env._parameters.MAX_SUB_CHANGED = 2
747+
self._aux_change_par(2)
729748
# Make a double setbus
730749
LINE1_ID = 4
731750
LINE2_ID = 5
732751
bus_action = self.env.action_space(
733752
{"set_bus": {"lines_ex_id": [(LINE1_ID, 2), (LINE2_ID, 2)]}}
734753
)
735754
# Make sure its legal
736-
_, _, _, i = self.env.step(bus_action)
737-
assert i["is_illegal"] == False
755+
info = self._aux_do_act(bus_action)
756+
assert info["is_illegal"] == False
738757

739758
def test_changebus_line_no_sub_allowed_is_illegal(self):
740759
# Set 0 allowed substation changes
741-
self.env._parameters.MAX_SUB_CHANGED = 0
760+
self._aux_change_par(0)
742761
# Make a changebus
743762
LINE_ID = 4
744763
bus_action = self.env.action_space({"change_bus": {"lines_ex_id": [LINE_ID]}})
745764
# Make sure its illegal
746-
_, _, _, i = self.env.step(bus_action)
747-
assert i["is_illegal"] == True
765+
info = self._aux_do_act(bus_action)
766+
assert info["is_illegal"] == True
748767

749768
def test_changebus_line_one_sub_allowed_is_legal(self):
750769
# Set 1 allowed substation changes
751-
self.env._parameters.MAX_SUB_CHANGED = 1
770+
self._aux_change_par(1)
752771
# Make a changebus
753772
LINE_ID = 4
754773
bus_action = self.env.action_space({"change_bus": {"lines_ex_id": [LINE_ID]}})
755774
# Make sure its legal
756-
_, _, _, i = self.env.step(bus_action)
757-
assert i["is_illegal"] == False
775+
info = self._aux_do_act(bus_action)
776+
assert info["is_illegal"] == False
758777

759778
def test_changebus_two_line_one_sub_allowed_is_illegal(self):
760779
# Set 1 allowed substation changes
761-
self.env._parameters.MAX_SUB_CHANGED = 1
780+
self._aux_change_par(1)
762781
# Make a changebus
763782
LINE1_ID = 4
764783
LINE2_ID = 5
765784
bus_action = self.env.action_space(
766785
{"change_bus": {"lines_ex_id": [LINE1_ID, LINE2_ID]}}
767786
)
768787
# Make sure its illegal
769-
_, _, _, i = self.env.step(bus_action)
770-
assert i["is_illegal"] == True
788+
info = self._aux_do_act(bus_action)
789+
assert info["is_illegal"] == True
771790

772791
def test_changebus_two_line_two_sub_allowed_is_legal(self):
773792
# Set 2 allowed substation changes
774-
self.env._parameters.MAX_SUB_CHANGED = 2
793+
self._aux_change_par(2)
775794
# Make a changebus
776795
LINE1_ID = 4
777796
LINE2_ID = 5
778797
bus_action = self.env.action_space(
779798
{"change_bus": {"lines_ex_id": [LINE1_ID, LINE2_ID]}}
780799
)
781800
# Make sure its legal
782-
_, _, _, i = self.env.step(bus_action)
783-
assert i["is_illegal"] == False
801+
info = self._aux_do_act(bus_action)
802+
assert info["is_illegal"] == False
784803

785804

805+
class TestSubstationImpactLegalitySimulate(TestSubstationImpactLegality):
806+
def setUp(self):
807+
super().setUp()
808+
self.env_or_obs = self.env.reset()
809+
810+
811+
class TestSubstationImpactLegalityForEnv(TestSubstationImpactLegality):
812+
def setUp(self):
813+
super().setUp()
814+
obs = self.env.reset()
815+
self.env_or_obs = obs.get_forecast_env()
816+
self.env_or_obs.reset()
817+
# TODO TestSubstationImpactLegalitySimulate with chain simulate
818+
819+
786820
class TestLoadingFromInstance(unittest.TestCase):
787821
def test_correct(self):
788822
rules = AlwaysLegal()

0 commit comments

Comments
 (0)