Skip to content

Commit

Permalink
Test: Update Backend Tests to expect _run_pf_with_diverging to catch …
Browse files Browse the repository at this point in the history
…detachments

Signed-off-by: Xavier Weiss <[email protected]>
  • Loading branch information
DEUCE1957 committed Nov 8, 2024
1 parent 8669fd6 commit 2f8f954
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 44 deletions.
74 changes: 30 additions & 44 deletions grid2op/tests/aaa_test_backend_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ def test_16_isolated_load_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=False)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated loads in AC."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated loads in AC."
assert res[1] is not None, "When your backend diverges, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
Expand All @@ -829,7 +829,7 @@ def test_16_isolated_load_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=True)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated loads in DC."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated loads in DC."
assert res[1] is not None, "When your backend diverges, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
Expand Down Expand Up @@ -862,7 +862,7 @@ def test_17_isolated_gen_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=False)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated gen."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated gen."
assert res[1] is not None, "When your backend diverges, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
Expand All @@ -877,7 +877,7 @@ def test_17_isolated_gen_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=True)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated gen."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated gen."
assert res[1] is not None, "When your backend diverges, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
Expand Down Expand Up @@ -916,7 +916,7 @@ def test_18_isolated_shunt_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=False)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated shunt."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated shunt."
assert res[1] is not None, "When your backend diverges, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
Expand All @@ -931,7 +931,7 @@ def test_18_isolated_shunt_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=True)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated shunt in DC."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated shunt in DC."
assert res[1] is not None, "When your backend stops, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend returns `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
Expand Down Expand Up @@ -967,7 +967,7 @@ def test_19_isolated_storage_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=False)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated storage units in AC."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated storage units in AC."
assert res[1] is not None, "When your backend stops, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
Expand All @@ -981,15 +981,16 @@ def test_19_isolated_storage_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=True)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated storage unit."
# assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of isolated storage unit."
assert res[1] is not None, "When your backend stops, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
if not isinstance(error, BackendError):
warnings.warn("The error returned by your backend when it stopped (due to isolated storage units) should preferably inherit from BackendError")

def test_20_disconnected_load_stops_computation(self):
"""Tests that a disconnected load unit will be spotted by the `run_pf` method and forwarded to grid2op by returining `False, an_exception` (in AC and DC)
"""
Tests that a disconnected load unit will be caught by the `_runpf_with_diverging_exception` method.
This test supposes that :
Expand All @@ -1003,7 +1004,7 @@ def test_20_disconnected_load_stops_computation(self):
.. note::
Currently this stops the computation of the environment and lead to a game over.
This behaviour might change in the future.
Behaviour changed in version 1.11.0 (no longer caught by runpf() itelf)
"""
self.skip_if_needed()
backend = self.aux_make_backend()
Expand All @@ -1014,13 +1015,9 @@ def test_20_disconnected_load_stops_computation(self):
bk_act = type(backend).my_bk_act_class()
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=False)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of disconnected load in AC."
assert res[1] is not None, "When your backend stops, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
if not isinstance(error, BackendError):
warnings.warn("The error returned by your backend when it stopped (due to disconnected load) should preferably inherit from BackendError")
error = backend._runpf_with_diverging_exception(is_dc=False)
assert error is not None
assert isinstance(error, Grid2OpException)

backend.reset(self.get_path(), self.get_casefile())
# a load alone on a bus
Expand All @@ -1029,16 +1026,13 @@ def test_20_disconnected_load_stops_computation(self):
bk_act = type(backend).my_bk_act_class()
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=True)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of disconnected load in DC."
assert res[1] is not None, "When your backend stops, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
if not isinstance(error, BackendError):
warnings.warn("The error returned by your backend when it stopped (due to disconnected load) should preferably inherit from BackendError")

error = backend._runpf_with_diverging_exception(is_dc=True)
assert error is not None
assert isinstance(error, Grid2OpException)

def test_21_disconnected_gen_stops_computation(self):
"""Tests that a disconnected generator will be spotted by the `run_pf` method and forwarded to grid2op by returining `False, an_exception` (in AC and DC)
"""
Tests that a disconnected generator will be caught by the `_runpf_with_diverging_exception` method
This test supposes that :
Expand All @@ -1052,7 +1046,7 @@ def test_21_disconnected_gen_stops_computation(self):
.. note::
Currently this stops the computation of the environment and lead to a game over.
This behaviour might change in the future.
Behaviour changed in version 1.11.0 (no longer caught by runpf() itelf)
"""
self.skip_if_needed()
backend = self.aux_make_backend()
Expand All @@ -1063,13 +1057,9 @@ def test_21_disconnected_gen_stops_computation(self):
bk_act = type(backend).my_bk_act_class()
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=False)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of disconnected gen in AC."
assert res[1] is not None, "When your backend stops, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
if not isinstance(error, BackendError):
warnings.warn("The error returned by your backend when it stopped (due to disconnected gen) should preferably inherit from BackendError")
error = backend._runpf_with_diverging_exception(is_dc=False)
assert error is not None
assert isinstance(error, Grid2OpException)

backend.reset(self.get_path(), self.get_casefile())
# a disconnected generator
Expand All @@ -1078,14 +1068,10 @@ def test_21_disconnected_gen_stops_computation(self):
bk_act = type(backend).my_bk_act_class()
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=True)
assert not res[0], "It is expected (at time of writing) that your backend returns `False` in case of disconnected gen in DC."
assert res[1] is not None, "When your backend stops, we expect it throws an exception (second return value)"
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
if not isinstance(error, BackendError):
warnings.warn("The error returned by your backend when it stopped (due to disconnected gen) should preferably inherit from BackendError")

res = backend._runpf_with_diverging_exception(is_dc=True)
assert error is not None
assert isinstance(error, Grid2OpException)

def test_22_islanded_grid_stops_computation(self):
"""Tests that when the grid is split in two different "sub_grid" is spotted by the `run_pf` method and forwarded to grid2op by returining `False, an_exception` (in AC and DC)
Expand Down Expand Up @@ -1117,7 +1103,7 @@ def test_22_islanded_grid_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=False)
assert not res[0], f"It is expected that your backend return `(False, _)` in case of non connected grid in AC."
# assert not res[0], f"It is expected that your backend return `(False, _)` in case of non connected grid in AC."
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
if not isinstance(error, BackendError):
Expand All @@ -1133,7 +1119,7 @@ def test_22_islanded_grid_stops_computation(self):
bk_act += action
backend.apply_action(bk_act) # mix of bus 1 and 2 on substation 1
res = backend.runpf(is_dc=True)
assert not res[0], f"It is expected that your backend return `(False, _)` in case of non connected grid in DC."
# assert not res[0], f"It is expected that your backend return `(False, _)` in case of non connected grid in DC."
error = res[1]
assert isinstance(error, Grid2OpException), f"When your backend return `False`, we expect it throws an exception inheriting from Grid2OpException (second return value), backend returned {type(error)}"
if not isinstance(error, BackendError):
Expand Down
1 change: 1 addition & 0 deletions grid2op/tests/test_alert_gym_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def test_print_alert(self):
act.raise_alert = [2]
act_gym = env_gym.action_space.to_gym(act)
act_str = act_gym.__str__()

assert act_str == ("OrderedDict([('change_bus', array([False, False, False, False, False, False, False, False, False,"
"\n False, False, False, False, False, False, False, False, False,\n "
"False, False, False, False, False, False, False, False, False,\n False, False, False, "
Expand Down

0 comments on commit 2f8f954

Please sign in to comment.