Merge v1.0.0 (#682)

Co-authored-by: Kallinteris Andreas <30759571+Kallinteris-Andreas@users.noreply.github.com>
Co-authored-by: Jet <38184875+jjshoots@users.noreply.github.com>
Co-authored-by: Omar Younis <42100908+younik@users.noreply.github.com>
This commit is contained in:
Mark Towers
2023-11-07 13:27:25 +00:00
committed by GitHub
parent cf5f588433
commit 27f8e85051
256 changed files with 7051 additions and 13421 deletions

View File

@@ -1,3 +1,5 @@
"""Test the `SyncVectorEnv` implementation."""
import re
from multiprocessing import TimeoutError
@@ -10,8 +12,8 @@ from gymnasium.error import (
NoAsyncCallError,
)
from gymnasium.spaces import Box, Discrete, MultiDiscrete, Tuple
from gymnasium.vector.async_vector_env import AsyncVectorEnv
from tests.vector.utils import (
from gymnasium.vector import AsyncVectorEnv
from tests.vector.testing_utils import (
CustomSpace,
make_custom_space_env,
make_env,
@@ -21,6 +23,7 @@ from tests.vector.utils import (
@pytest.mark.parametrize("shared_memory", [True, False])
def test_create_async_vector_env(shared_memory):
"""Test creating an async vector environment with or without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
@@ -30,6 +33,7 @@ def test_create_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_reset_async_vector_env(shared_memory):
"""Test the reset of an sync vector environment with or without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
@@ -39,7 +43,6 @@ def test_reset_async_vector_env(shared_memory):
assert isinstance(env.observation_space, Box)
assert isinstance(observations, np.ndarray)
assert isinstance(infos, dict)
assert observations.dtype == env.observation_space.dtype
assert observations.shape == (8,) + env.single_observation_space.shape
assert observations.shape == env.observation_space.shape
@@ -59,13 +62,32 @@ def test_reset_async_vector_env(shared_memory):
assert all([isinstance(info, dict) for info in infos])
def test_render_async_vector():
envs = AsyncVectorEnv(
[make_env("CartPole-v1", i, render_mode="rgb_array") for i in range(3)]
)
assert envs.render_mode == "rgb_array"
envs.reset()
rendered_frames = envs.render()
assert isinstance(rendered_frames, tuple)
assert len(rendered_frames) == envs.num_envs
assert all(isinstance(frame, np.ndarray) for frame in rendered_frames)
envs.close()
envs = AsyncVectorEnv([make_env("CartPole-v1", i) for i in range(3)])
assert envs.render_mode is None
envs.close()
@pytest.mark.parametrize("shared_memory", [True, False])
@pytest.mark.parametrize("use_single_action_space", [True, False])
def test_step_async_vector_env(shared_memory, use_single_action_space):
"""Test the step async vector environment with and without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
observations = env.reset()
env.reset()
assert isinstance(env.single_action_space, Discrete)
assert isinstance(env.action_space, MultiDiscrete)
@@ -74,7 +96,7 @@ def test_step_async_vector_env(shared_memory, use_single_action_space):
actions = [env.single_action_space.sample() for _ in range(8)]
else:
actions = env.action_space.sample()
observations, rewards, terminateds, truncateds, _ = env.step(actions)
observations, rewards, terminations, truncations, _ = env.step(actions)
env.close()
@@ -89,25 +111,26 @@ def test_step_async_vector_env(shared_memory, use_single_action_space):
assert rewards.ndim == 1
assert rewards.size == 8
assert isinstance(terminateds, np.ndarray)
assert terminateds.dtype == np.bool_
assert terminateds.ndim == 1
assert terminateds.size == 8
assert isinstance(terminations, np.ndarray)
assert terminations.dtype == np.bool_
assert terminations.ndim == 1
assert terminations.size == 8
assert isinstance(truncateds, np.ndarray)
assert truncateds.dtype == np.bool_
assert truncateds.ndim == 1
assert truncateds.size == 8
assert isinstance(truncations, np.ndarray)
assert truncations.dtype == np.bool_
assert truncations.ndim == 1
assert truncations.size == 8
@pytest.mark.parametrize("shared_memory", [True, False])
def test_call_async_vector_env(shared_memory):
"""Test call with async vector environment."""
env_fns = [
make_env("CartPole-v1", i, render_mode="rgb_array_list") for i in range(4)
]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
_ = env.reset()
env.reset()
images = env.call("render")
gravity = env.call("gravity")
@@ -128,6 +151,7 @@ def test_call_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_set_attr_async_vector_env(shared_memory):
"""Test `set_attr_` for async vector environment with or without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
@@ -140,6 +164,7 @@ def test_set_attr_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_copy_async_vector_env(shared_memory):
"""Test observations are a copy of the true observation with and without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
# TODO, these tests do nothing, understand the purpose of the tests and fix them
@@ -152,6 +177,7 @@ def test_copy_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_no_copy_async_vector_env(shared_memory):
"""Test observation are not a copy of the true observation with and without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
# TODO, these tests do nothing, understand the purpose of the tests and fix them
@@ -164,6 +190,7 @@ def test_no_copy_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_reset_timeout_async_vector_env(shared_memory):
"""Test timeout error on reset with and without shared memory."""
env_fns = [make_slow_env(0.3, i) for i in range(4)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
@@ -176,18 +203,20 @@ def test_reset_timeout_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_step_timeout_async_vector_env(shared_memory):
"""Test timeout error on step with and without shared memory."""
env_fns = [make_slow_env(0.0, i) for i in range(4)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
with pytest.raises(TimeoutError):
env.reset()
env.step_async(np.array([0.1, 0.1, 0.3, 0.1]))
observations, rewards, terminateds, truncateds, _ = env.step_wait(timeout=0.1)
observations, rewards, terminations, truncations, _ = env.step_wait(timeout=0.1)
env.close(terminate=True)
@pytest.mark.parametrize("shared_memory", [True, False])
def test_reset_out_of_order_async_vector_env(shared_memory):
"""Test reset being called out of order with and without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
@@ -224,6 +253,7 @@ def test_reset_out_of_order_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_step_out_of_order_async_vector_env(shared_memory):
"""Test step out of order with and without shared memory."""
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
@@ -259,6 +289,7 @@ def test_step_out_of_order_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_already_closed_async_vector_env(shared_memory):
"""Test the error if a function is called if environment is already closed."""
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
with pytest.raises(ClosedEnvironmentError):
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
@@ -268,6 +299,7 @@ def test_already_closed_async_vector_env(shared_memory):
@pytest.mark.parametrize("shared_memory", [True, False])
def test_check_spaces_async_vector_env(shared_memory):
"""Test check spaces for async vector environment with and without shared memory."""
# CartPole-v1 - observation_space: Box(4,), action_space: Discrete(2)
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
# FrozenLake-v1 - Discrete(16), action_space: Discrete(4)
@@ -278,6 +310,7 @@ def test_check_spaces_async_vector_env(shared_memory):
def test_custom_space_async_vector_env():
"""Test custom spaces with async vector environment."""
env_fns = [make_custom_space_env(i) for i in range(4)]
env = AsyncVectorEnv(env_fns, shared_memory=False)
@@ -287,7 +320,7 @@ def test_custom_space_async_vector_env():
assert isinstance(env.action_space, Tuple)
actions = ("action-2", "action-3", "action-5", "action-7")
step_observations, rewards, terminateds, truncateds, _ = env.step(actions)
step_observations, rewards, terminations, truncations, _ = env.step(actions)
env.close()
@@ -307,6 +340,7 @@ def test_custom_space_async_vector_env():
def test_custom_space_async_vector_env_shared_memory():
"""Test custom space with shared memory."""
env_fns = [make_custom_space_env(i) for i in range(4)]
with pytest.raises(ValueError):
env = AsyncVectorEnv(env_fns, shared_memory=True)