Skip to content

Commit

Permalink
ready to merge with dev_1.10.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed May 3, 2024
1 parent 93e2042 commit 8c3448e
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ grid2op/tests/requirements.txt
grid2op/tests/venv_test_311/
issue_577/
junk.py
grid2op/tests/20240429_failed_tests.txt
grid2op/tests/20240429_failed_tests_small.txt
grid2op/tests/20240429_teq_test.txt
grid2op/tests/req_38_np121
test_make_2_envs.py

# profiling files
**.prof
11 changes: 8 additions & 3 deletions grid2op/Action/baseAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6435,7 +6435,6 @@ def decompose_as_unary_actions(self,
tmp += a
assert tmp == act
Parameters
----------
group_topo : bool, optional
Expand Down Expand Up @@ -6499,12 +6498,15 @@ def decompose_as_unary_actions(self,
self._aux_decompose_as_unary_actions_curtail(cls, group_curtail, res)
return res

def _add_act_and_remove_line_status_only_set(self, other: "BaseAction"):
def _add_act_and_remove_line_status_only_set(self, other: "BaseAction") -> "BaseAction":
"""INTERNAL
This is used by the environment when combining action in the "set state" in env.reset.
It supposes both self and other are only "set" actions
.. versionadded:: 1.10.2
"""
self += other
cls = type(self)
Expand All @@ -6529,13 +6531,16 @@ def _add_act_and_remove_line_status_only_set(self, other: "BaseAction"):
self._modif_set_status = True
if (self._set_topo_vect != 0).any():
self._modif_set_bus = True
return self

def remove_change(self) -> "BaseAction":
"""This function will modify 'self' and remove all "change" action type.
It is mainly used in the environment, when removing the "change" type for setting the original
state of the grid.
.. versionadded:: 1.10.2
"""
if self._change_bus_vect.any():
warnings.warn("This action modified the buses with `change_bus` ")
Expand All @@ -6544,4 +6549,4 @@ def remove_change(self) -> "BaseAction":
if self._switch_line_status.any():
self._switch_line_status[:] = False
self._modif_change_status = False
return self
return self
125 changes: 125 additions & 0 deletions grid2op/tests/test_RunnerFast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.

import warnings
import unittest

from grid2op.tests.helper_path_test import *

PATH_ADN_CHRONICS_FOLDER = os.path.abspath(
os.path.join(PATH_CHRONICS, "test_multi_chronics")
)
PATH_PREVIOUS_RUNNER = os.path.join(data_test_dir, "runner_data")

import grid2op
from grid2op.Runner import Runner
from grid2op.dtypes import dt_float

warnings.simplefilter("error")


class TestRunner(HelperTests, unittest.TestCase):
def setUp(self):
super().setUp()
self.init_grid_path = os.path.join(PATH_DATA_TEST_PP, "test_case14.json")
self.path_chron = PATH_ADN_CHRONICS_FOLDER
self.parameters_path = None
self.max_iter = 10
self.real_reward = dt_float(7748.425 / 12.)
self.real_reward_li = [self.real_reward, dt_float(7786.8955 / 12.)] # 7786.89599609375

self.all_real_rewards = [
dt_float(el / 12.)
for el in [
761.3295,
768.10144,
770.2673,
767.767,
768.69,
768.71246,
779.1029,
783.2737,
788.7833,
792.39764,
]
]
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make("l2rpn_case14_sandbox", test=True, _add_to_name=type(self).__name__)
self.runner = Runner(**self.env.get_params_for_runner())

def test_one_episode(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
_, cum_reward, timestep, max_ts = self.runner.run_one_episode(
max_iter=self.max_iter
)
assert int(timestep) == self.max_iter
assert np.abs(cum_reward - self.real_reward) <= self.tol_one, f"{cum_reward} != {self.real_reward}"

def test_one_episode_detailed(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
_, cum_reward, timestep, max_ts, episode_data = self.runner.run_one_episode(
max_iter=self.max_iter, detailed_output=True
)
assert int(timestep) == self.max_iter
assert np.abs(cum_reward - self.real_reward) <= self.tol_one
for j in range(len(self.all_real_rewards)):
assert (
np.abs(episode_data.rewards[j] - self.all_real_rewards[j])
<= self.tol_one
), f"{episode_data.rewards[j]} != {self.all_real_rewards[j]}"

def test_2episode(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
res = self.runner._run_sequential(nb_episode=2, max_iter=self.max_iter)
assert len(res) == 2
for i, (stuff, _, cum_reward, timestep, total_ts) in enumerate(res):
assert int(timestep) == self.max_iter
assert np.abs(cum_reward - self.real_reward_li[i]) <= self.tol_one, f"for iter {i}: {cum_reward} != {self.real_reward_li[i]}"

def test_init_from_env(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case14_test", test=True, _add_to_name=type(self).__name__) as env:
runner = Runner(**env.get_params_for_runner())
res = runner.run(nb_episode=1, max_iter=self.max_iter)
for i, _, cum_reward, timestep, total_ts in res:
assert int(timestep) == self.max_iter, f"{timestep} != {self.max_iter}"

def test_seed_seq(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case14_test", test=True, _add_to_name=type(self).__name__) as env:
runner = Runner(**env.get_params_for_runner())
res = runner.run(
nb_episode=1, max_iter=self.max_iter, env_seeds=[1], agent_seeds=[2]
)
for i, _, cum_reward, timestep, total_ts in res:
assert int(timestep) == self.max_iter, f"{timestep} != {self.max_iter}"

def test_seed_par(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make("rte_case14_test", test=True, _add_to_name=type(self).__name__) as env:
runner = Runner(**env.get_params_for_runner())
res = runner.run(
nb_episode=2,
nb_process=2,
max_iter=self.max_iter,
env_seeds=[1, 2],
agent_seeds=[3, 4],
)
for i, _, cum_reward, timestep, total_ts in res:
assert int(timestep) == self.max_iter


if __name__ == "__main__":
unittest.main()
2 changes: 0 additions & 2 deletions grid2op/tests/test_action_set_orig_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@


# TODO test "change" is deactivated
# TODO test grid2Op compat mode (storage units)
# TODO test with redispatching and curtailment actions
# TODO test with "names_orig_to_backend"


Expand Down

0 comments on commit 8c3448e

Please sign in to comment.