-
Notifications
You must be signed in to change notification settings - Fork 118
/
test_issue_418.py
55 lines (48 loc) · 2.22 KB
/
test_issue_418.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright (c) 2019-2023, 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 grid2op
from grid2op.gym_compat import GymEnv, GYMNASIUM_AVAILABLE
import unittest
import warnings
import numpy as np
from grid2op.gym_compat.utils import (check_gym_version, sample_seed,
_MAX_GYM_VERSION_RANDINT, GYM_VERSION)
class Issue418Tester(unittest.TestCase):
def setUp(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
# this needs to be tested with pandapower backend
self.env = grid2op.make("l2rpn_case14_sandbox", test=True, _add_to_name=type(self).__name__)
self.env.seed(0)
self.env.set_id(0)
def test_seed(self):
gymenv = GymEnv(self.env)
if GYM_VERSION <= _MAX_GYM_VERSION_RANDINT and not GYMNASIUM_AVAILABLE:
# legacy gym, with old gym gym version
gymenv.seed(42)
obs = gymenv.reset()
curt = np.array([1,1.,0.35566905,0.23095788,0.6338101,1])
year = 1249
day = 28
else:
# most recent gym API
obs = gymenv.reset(seed=42)
curt = np.array([1,1.,0.18852758,0.5537014,0.43770432,1])
curt = np.array([-1,-1.,0.18852758,0.5537014,0.43770432,-1])
year = 571
day = 9
# year = 1887
# day = 9
# test that the seeding worked also in action space and observation space
sampled_act = gymenv.action_space.sample()
assert np.allclose(sampled_act['curtail'], curt), f"{sampled_act['curtail']}"
sampled_obs = gymenv.observation_space.sample()
assert sampled_obs["year"] == year, f'{sampled_obs["year"]}'
assert sampled_obs["day"] == day, f'{sampled_obs["day"]}'
if __name__ == "__main__":
unittest.main()