Tianshou/test/base/test_env.py

108 lines
3.5 KiB
Python
Raw Normal View History

2020-03-11 10:56:38 +08:00
import time
import numpy as np
from gym.spaces.discrete import Discrete
from tianshou.data import Batch
from tianshou.env import VectorEnv, SubprocVectorEnv, \
RayVectorEnv, AsyncVectorEnv
2020-03-11 10:56:38 +08:00
2020-03-21 10:58:01 +08:00
if __name__ == '__main__':
from env import MyTestEnv
else: # pytest
from test.base.env import MyTestEnv
2020-03-11 10:56:38 +08:00
2020-03-11 16:14:53 +08:00
def test_async_env(num=8, sleep=0.1):
# simplify the test case, just keep stepping
size = 10000
env_fns = [
lambda i=i: MyTestEnv(size=i, sleep=sleep, random_sleep=True)
for i in range(size, size + num)
]
v = AsyncVectorEnv(env_fns, wait_num=num // 2)
v.seed()
v.reset()
# for a random variable u ~ U[0, 1], let v = max{u1, u2, ..., un}
# P(v <= x) = x^n (0 <= x <= 1), pdf of v is nx^{n-1}
# expectation of v is n / (n + 1)
# for a synchronous environment, the following actions should take
# about 7 * sleep * num / (num + 1) seconds
# for AsyncVectorEnv, the analysis is complicated, but the time cost
# should be smaller
action_list = [1] * num + [0] * (num * 2) + [1] * (num * 4)
current_index_start = 0
action = action_list[:num]
env_ids = list(range(num))
o = []
spent_time = time.time()
while current_index_start < len(action_list):
A, B, C, D = v.step(action=action, id=env_ids)
b = Batch({'obs': A, 'rew': B, 'done': C, 'info': D})
env_ids = b.info.env_id
o.append(b)
current_index_start += len(action)
2020-07-28 20:06:01 +08:00
# len of action may be smaller than len(A) in the end
action = action_list[current_index_start: current_index_start + len(A)]
2020-07-28 20:06:01 +08:00
# truncate env_ids with the first terms
# typically len(env_ids) == len(A) == len(action), except for the
# last batch when actions are not enough
env_ids = env_ids[: len(action)]
spent_time = time.time() - spent_time
data = Batch.cat(o)
# assure 1/7 improvement
assert spent_time < 6.0 * sleep * num / (num + 1)
return spent_time, data
2020-03-16 11:11:29 +08:00
def test_vecenv(size=10, num=8, sleep=0.001):
verbose = __name__ == '__main__'
2020-03-25 14:08:28 +08:00
env_fns = [
lambda i=i: MyTestEnv(size=i, sleep=sleep)
for i in range(size, size + num)
2020-03-25 14:08:28 +08:00
]
2020-03-11 16:14:53 +08:00
venv = [
2020-03-25 14:08:28 +08:00
VectorEnv(env_fns),
SubprocVectorEnv(env_fns),
2020-03-11 16:14:53 +08:00
]
2020-03-16 11:11:29 +08:00
if verbose:
2020-03-25 14:08:28 +08:00
venv.append(RayVectorEnv(env_fns))
2020-03-11 16:14:53 +08:00
for v in venv:
v.seed()
2020-03-25 14:08:28 +08:00
action_list = [1] * 5 + [0] * 10 + [1] * 20
2020-03-11 16:14:53 +08:00
if not verbose:
o = [v.reset() for v in venv]
for i, a in enumerate(action_list):
2020-03-25 14:08:28 +08:00
o = []
for v in venv:
A, B, C, D = v.step([a] * num)
if sum(C):
A = v.reset(np.where(C)[0])
o.append([A, B, C, D])
2020-03-11 16:14:53 +08:00
for i in zip(*o):
2020-06-01 08:30:09 +08:00
for j in range(1, len(i) - 1):
2020-03-11 16:14:53 +08:00
assert (i[0] == i[j]).all()
else:
t = [0, 0, 0]
for i, e in enumerate(venv):
t[i] = time.time()
e.reset()
for a in action_list:
2020-03-25 14:08:28 +08:00
done = e.step([a] * num)[2]
if sum(done) > 0:
e.reset(np.where(done)[0])
2020-03-11 16:14:53 +08:00
t[i] = time.time() - t[i]
2020-03-13 17:49:22 +08:00
print(f'VectorEnv: {t[0]:.6f}s')
print(f'SubprocVectorEnv: {t[1]:.6f}s')
print(f'RayVectorEnv: {t[2]:.6f}s')
for v in venv:
assert v.size == list(range(size, size + num))
assert v.env_num == num
2020-06-11 09:07:45 +08:00
assert v.action_space == [Discrete(2)] * num
2020-03-11 17:28:51 +08:00
for v in venv:
v.close()
2020-03-11 16:14:53 +08:00
2020-03-11 10:56:38 +08:00
if __name__ == '__main__':
2020-03-16 11:11:29 +08:00
test_vecenv()
test_async_env()