2022-05-24 16:36:35 +02:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
import pytest
|
2022-02-06 17:28:27 -06:00
|
|
|
|
2019-11-01 22:27:39 +01:00
|
|
|
import gym
|
2022-05-24 16:36:35 +02:00
|
|
|
from gym.wrappers import RecordEpisodeStatistics, VectorListInfo
|
|
|
|
from gym.wrappers.record_episode_statistics import add_vector_episode_statistics
|
2019-11-01 22:27:39 +01:00
|
|
|
|
|
|
|
|
2022-03-14 14:27:03 +00:00
|
|
|
@pytest.mark.parametrize("env_id", ["CartPole-v1", "Pendulum-v1"])
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("deque_size", [2, 5])
|
2019-11-01 22:27:39 +01:00
|
|
|
def test_record_episode_statistics(env_id, deque_size):
|
|
|
|
env = gym.make(env_id)
|
|
|
|
env = RecordEpisodeStatistics(env, deque_size)
|
|
|
|
|
|
|
|
for n in range(5):
|
|
|
|
env.reset()
|
2021-08-05 17:06:49 -04:00
|
|
|
assert env.episode_returns[0] == 0.0
|
|
|
|
assert env.episode_lengths[0] == 0
|
2019-11-01 22:27:39 +01:00
|
|
|
for t in range(env.spec.max_episode_steps):
|
|
|
|
_, _, done, info = env.step(env.action_space.sample())
|
|
|
|
if done:
|
2021-07-29 02:26:34 +02:00
|
|
|
assert "episode" in info
|
|
|
|
assert all([item in info["episode"] for item in ["r", "l", "t"]])
|
2019-11-01 22:27:39 +01:00
|
|
|
break
|
|
|
|
assert len(env.return_queue) == deque_size
|
|
|
|
assert len(env.length_queue) == deque_size
|
2021-08-05 17:06:49 -04:00
|
|
|
|
|
|
|
|
2022-02-06 17:28:27 -06:00
|
|
|
def test_record_episode_statistics_reset_info():
|
|
|
|
env = gym.make("CartPole-v1")
|
|
|
|
env = RecordEpisodeStatistics(env)
|
|
|
|
ob_space = env.observation_space
|
|
|
|
obs = env.reset()
|
|
|
|
assert ob_space.contains(obs)
|
|
|
|
del obs
|
|
|
|
obs, info = env.reset(return_info=True)
|
|
|
|
assert ob_space.contains(obs)
|
|
|
|
assert isinstance(info, dict)
|
|
|
|
|
|
|
|
|
2022-01-30 02:44:31 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("num_envs", "asynchronous"), [(1, False), (1, True), (4, False), (4, True)]
|
|
|
|
)
|
|
|
|
def test_record_episode_statistics_with_vectorenv(num_envs, asynchronous):
|
2022-03-14 14:27:03 +00:00
|
|
|
envs = gym.vector.make("CartPole-v1", num_envs=num_envs, asynchronous=asynchronous)
|
2021-08-05 17:06:49 -04:00
|
|
|
envs = RecordEpisodeStatistics(envs)
|
2022-01-30 02:44:31 +01:00
|
|
|
max_episode_step = (
|
|
|
|
envs.env_fns[0]().spec.max_episode_steps
|
|
|
|
if asynchronous
|
|
|
|
else envs.env.envs[0].spec.max_episode_steps
|
|
|
|
)
|
2021-08-05 17:06:49 -04:00
|
|
|
envs.reset()
|
2022-01-30 02:44:31 +01:00
|
|
|
for _ in range(max_episode_step + 1):
|
2021-08-05 17:06:49 -04:00
|
|
|
_, _, dones, infos = envs.step(envs.action_space.sample())
|
2022-05-24 16:36:35 +02:00
|
|
|
if any(dones):
|
|
|
|
assert "episode" in infos
|
|
|
|
assert "_episode" in infos
|
|
|
|
assert all(infos["_episode"] == dones)
|
|
|
|
assert all([item in infos["episode"] for item in ["r", "l", "t"]])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
assert "episode" not in infos
|
|
|
|
assert "_episode" not in infos
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrong_wrapping_order():
|
|
|
|
envs = gym.vector.make("CartPole-v1", num_envs=3)
|
|
|
|
wrapped_env = RecordEpisodeStatistics(VectorListInfo(envs))
|
|
|
|
wrapped_env.reset()
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
wrapped_env.step(wrapped_env.action_space.sample())
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_vector_episode_statistics():
|
|
|
|
NUM_ENVS = 5
|
|
|
|
|
|
|
|
info = {}
|
|
|
|
for i in range(NUM_ENVS):
|
|
|
|
episode_info = {
|
|
|
|
"episode": {
|
|
|
|
"r": i,
|
|
|
|
"l": i,
|
|
|
|
"t": i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info = add_vector_episode_statistics(info, episode_info["episode"], NUM_ENVS, i)
|
|
|
|
assert np.alltrue(info["_episode"][: i + 1])
|
|
|
|
|
|
|
|
for j in range(NUM_ENVS):
|
|
|
|
if j <= i:
|
|
|
|
assert info["episode"]["r"][j] == j
|
|
|
|
assert info["episode"]["l"][j] == j
|
|
|
|
assert info["episode"]["t"][j] == j
|
|
|
|
else:
|
|
|
|
assert info["episode"]["r"][j] == 0
|
|
|
|
assert info["episode"]["l"][j] == 0
|
|
|
|
assert info["episode"]["t"][j] == 0
|