-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #448 from BDonnot/master
Add some security when using MultiFolderWithCache
- Loading branch information
Showing
5 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import grid2op | ||
from grid2op.Runner import Runner | ||
from grid2op.Reward import LinesCapacityReward # or any other rewards | ||
from lightsim2grid import LightSimBackend # highly recommended ! | ||
from grid2op.Chronics import MultifolderWithCache # highly recommended for training | ||
import warnings | ||
import unittest | ||
from grid2op.Exceptions import ChronicsError | ||
from grid2op.Runner import Runner | ||
|
||
|
||
class TestPreventWrongBehaviour(unittest.TestCase): | ||
def setUp(self) -> None: | ||
env_name = "l2rpn_case14_sandbox" | ||
with warnings.catch_warnings(): | ||
warnings.filterwarnings("ignore") | ||
self.env = grid2op.make(env_name, | ||
reward_class=LinesCapacityReward, | ||
backend=LightSimBackend(), | ||
chronics_class=MultifolderWithCache, | ||
test=True) | ||
def tearDown(self) -> None: | ||
self.env.close() | ||
|
||
def test_can_make(self): | ||
pass | ||
|
||
def test_cannot_step(self): | ||
with self.assertRaises(ChronicsError): | ||
self.env.step(self.env.action_space()) | ||
|
||
def test_cannot_reset(self): | ||
with self.assertRaises(ChronicsError): | ||
obs = self.env.reset() | ||
|
||
def test_can_reset(self): | ||
self.env.chronics_handler.reset() | ||
obs = self.env.reset() | ||
self.env.step(self.env.action_space()) | ||
|
||
def test_can_reset(self): | ||
self.env.chronics_handler.reset() | ||
obs = self.env.reset() | ||
|
||
def test_when_change_filter(self): | ||
self.env.chronics_handler.set_filter(lambda x: True) | ||
with self.assertRaises(ChronicsError): | ||
obs = self.env.reset() | ||
self.env.chronics_handler.reset() | ||
obs = self.env.reset() | ||
self.env.step(self.env.action_space()) | ||
|
||
def test_runner(self): | ||
with self.assertRaises(ChronicsError): | ||
runner = Runner(**self.env.get_params_for_runner()) | ||
res = runner.run(nb_episode=1, | ||
nb_process=1, | ||
max_iter=5 | ||
) | ||
|
||
self.env.chronics_handler.real_data.reset() | ||
runner = Runner(**self.env.get_params_for_runner()) | ||
res = runner.run(nb_episode=1, | ||
nb_process=1, | ||
max_iter=5 | ||
) | ||
|
||
def test_copy(self): | ||
# when the copied env is not init | ||
env_cpy = self.env.copy() | ||
with self.assertRaises(ChronicsError): | ||
env_cpy.step(self.env.action_space()) | ||
with self.assertRaises(ChronicsError): | ||
env_cpy.reset() | ||
env_cpy.chronics_handler.reset() | ||
obs = env_cpy.reset() | ||
env_cpy.step(env_cpy.action_space()) | ||
env_cpy.close() | ||
|
||
# if the copied env is properly init | ||
self.env.chronics_handler.reset() | ||
env_cpy2 = self.env.copy() | ||
env_cpy2.chronics_handler.reset() | ||
obs = env_cpy2.reset() | ||
env_cpy2.step(env_cpy2.action_space()) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |