forked from ugr-sail/sinergym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_env.py
145 lines (118 loc) · 4.55 KB
/
test_env.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
from random import randint, sample
import gymnasium as gym
import pytest
from sinergym.utils.constants import *
from sinergym.utils.env_checker import check_env
@pytest.mark.parametrize('env_name',
[('env_demo'),
('env_demo_continuous'),
('env_demo_continuous_stochastic')
])
def test_reset(env_name, request):
env = request.getfixturevalue(env_name)
obs, info = env.reset()
# obs check
assert len(obs) == len(DEFAULT_5ZONE_OBSERVATION_VARIABLES) + \
4 # year, month, day and hour
assert env.simulator._episode_existed
# info check
assert isinstance(info, dict)
assert len(info) > 0
# default_options check
if 'stochastic' not in env_name:
assert not env.default_options.get('weather_variability')
else:
assert isinstance(env.default_options['weather_variability'], tuple)
def test_reset_custom_options(env_demo_continuous_stochastic):
assert env_demo_continuous_stochastic.default_options['weather_variability'] == (
1.0, 0.0, 0.001)
custom_options = {'weather_variability': (1.1, 0.1, 0.002)}
obs, info = env_demo_continuous_stochastic.reset(options=custom_options)
# Check if epw with new variation is overwriting default options
assert os.path.isfile(
info['eplus_working_dir'] +
'/USA_PA_Pittsburgh-Allegheny.County.AP.725205_TMY3_Random_1.1_0.1_0.002.epw')
@pytest.mark.parametrize('env_name',
[('env_demo'),
('env_demo_continuous'),
])
def test_step(env_name, request):
env = request.getfixturevalue(env_name)
env.reset()
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
assert len(obs) == len(DEFAULT_5ZONE_OBSERVATION_VARIABLES) + \
4 # year, month, day and hour
assert not isinstance(reward, type(None))
assert not terminated
assert not truncated
assert info['timestep'] == 1
assert info['time_elapsed'] == env.simulator._eplus_run_stepsize * \
info['timestep']
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
assert len(obs) == 20
assert not isinstance(reward, type(None))
assert not terminated
assert not truncated
assert info['timestep'] == 2
assert info['time_elapsed'] == env.simulator._eplus_run_stepsize * \
info['timestep']
def test_close(env_demo):
env_demo.reset()
assert env_demo.simulator._episode_existed
env_demo.close()
assert not env_demo.simulator._episode_existed
assert env_demo.simulator._conn is None
def test_render(env_demo):
env_demo.render()
def test_get_schedulers(env_demo):
# Check if generate a dictionary
assert isinstance(env_demo.get_schedulers(), dict)
# Check if specifying a path, generate an excel
env_demo.get_schedulers(path='./TESTschedulers.xlsx')
assert os.path.isfile('./TESTschedulers.xlsx')
def test_get_zones(env_demo):
zones = env_demo.get_zones()
assert isinstance(zones, list)
assert len(zones) > 0
def test_get_action(env_demo):
# Here is checked special cases
# int
action = randint(0, 9)
_action = env_demo._get_action(action)
assert isinstance(_action, list)
assert len(_action) == 2
# [int]
action = [randint(0, 9)]
_action = env_demo._get_action(action)
assert isinstance(_action, list)
assert len(_action) == 2
# custom discrete action (without mapping)
action = (22.0, 20.0)
_action = env_demo._get_action(action)
assert isinstance(_action, list)
assert _action == [22.0, 20.0]
# np.ndarray
action = np.array([randint(0, 9)])
_action = env_demo._get_action(action)
assert isinstance(_action, list)
assert len(_action) == 2
# Not supported action
action = 'fbsufb'
with pytest.raises(RuntimeError):
env_demo._get_action(action)
def test_all_environments():
envs_id = [env_id for env_id in gym.envs.registration.registry.keys(
) if env_id.startswith('Eplus')]
# Select 10 environments randomly (test would be too large)
samples_id = sample(envs_id, 5)
for env_id in samples_id:
# Create env with TEST name
env = gym.make(env_id)
check_env(env)
# Rename directory with name TEST for future remove
os.rename(env.simulator._env_working_dir_parent, 'Eplus-env-TEST' +
env.simulator._env_working_dir_parent.split('/')[-1])
env.close()