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
|
|
|
|
2022-09-16 23:41:27 +01:00
|
|
|
import gymnasium as gym
|
2022-09-08 10:10:07 +01:00
|
|
|
from gymnasium.wrappers import RecordEpisodeStatistics, VectorListInfo
|
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):
|
2022-09-16 23:41:27 +01:00
|
|
|
env = gym.make(env_id, disable_env_checker=True)
|
2019-11-01 22:27:39 +01:00
|
|
|
env = RecordEpisodeStatistics(env, deque_size)
|
|
|
|
|
|
|
|
for n in range(5):
|
|
|
|
env.reset()
|
2022-07-04 18:19:25 +01:00
|
|
|
assert env.episode_returns is not None and env.episode_lengths is not None
|
2021-08-05 17:06:49 -04:00
|
|
|
assert env.episode_returns[0] == 0.0
|
|
|
|
assert env.episode_lengths[0] == 0
|
2022-11-12 10:21:24 +00:00
|
|
|
assert env.spec is not None
|
2019-11-01 22:27:39 +01:00
|
|
|
for t in range(env.spec.max_episode_steps):
|
2022-08-30 19:41:59 +05:30
|
|
|
_, _, terminated, truncated, info = env.step(env.action_space.sample())
|
|
|
|
if terminated or truncated:
|
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():
|
2022-09-16 23:41:27 +01:00
|
|
|
env = gym.make("CartPole-v1", disable_env_checker=True)
|
2022-02-06 17:28:27 -06:00
|
|
|
env = RecordEpisodeStatistics(env)
|
|
|
|
ob_space = env.observation_space
|
2022-08-23 11:09:54 -04:00
|
|
|
obs, info = env.reset()
|
2022-02-06 17:28:27 -06:00
|
|
|
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-09-16 23:41:27 +01:00
|
|
|
envs = gym.vector.make(
|
2022-06-16 14:29:13 +01:00
|
|
|
"CartPole-v1",
|
|
|
|
render_mode=None,
|
|
|
|
num_envs=num_envs,
|
|
|
|
asynchronous=asynchronous,
|
|
|
|
disable_env_checker=True,
|
2022-06-08 00:20:56 +02:00
|
|
|
)
|
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):
|
2022-08-30 19:41:59 +05:30
|
|
|
_, _, terminateds, truncateds, infos = envs.step(envs.action_space.sample())
|
|
|
|
if any(terminateds) or any(truncateds):
|
2022-05-24 16:36:35 +02:00
|
|
|
assert "episode" in infos
|
|
|
|
assert "_episode" in infos
|
2022-08-30 19:41:59 +05:30
|
|
|
assert all(infos["_episode"] == np.bitwise_or(terminateds, truncateds))
|
2022-05-24 16:36:35 +02:00
|
|
|
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():
|
2022-09-16 23:41:27 +01:00
|
|
|
envs = gym.vector.make("CartPole-v1", num_envs=3, disable_env_checker=True)
|
2022-05-24 16:36:35 +02:00
|
|
|
wrapped_env = RecordEpisodeStatistics(VectorListInfo(envs))
|
|
|
|
wrapped_env.reset()
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
wrapped_env.step(wrapped_env.action_space.sample())
|