2023-11-07 13:27:25 +00:00
|
|
|
"""Test the `SyncVectorEnv` implementation."""
|
|
|
|
|
2022-08-30 19:47:26 +01:00
|
|
|
import re
|
2024-07-15 15:53:11 +01:00
|
|
|
import warnings
|
2022-03-31 12:50:38 -07:00
|
|
|
from multiprocessing import TimeoutError
|
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
import pytest
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-09-08 10:11:31 +01:00
|
|
|
from gymnasium.error import (
|
|
|
|
AlreadyPendingCallError,
|
|
|
|
ClosedEnvironmentError,
|
|
|
|
NoAsyncCallError,
|
|
|
|
)
|
2022-09-08 10:10:07 +01:00
|
|
|
from gymnasium.spaces import Box, Discrete, MultiDiscrete, Tuple
|
2023-11-07 13:27:25 +00:00
|
|
|
from gymnasium.vector import AsyncVectorEnv
|
2024-07-15 15:53:11 +01:00
|
|
|
from tests.testing_env import GenericTestEnv
|
2023-11-07 13:27:25 +00:00
|
|
|
from tests.vector.testing_utils import (
|
2021-07-29 02:26:34 +02:00
|
|
|
CustomSpace,
|
2022-03-31 12:50:38 -07:00
|
|
|
make_custom_space_env,
|
2021-07-29 02:26:34 +02:00
|
|
|
make_env,
|
|
|
|
make_slow_env,
|
|
|
|
)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_create_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test creating an async vector environment with or without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2019-06-21 17:29:44 -04:00
|
|
|
assert env.num_envs == 8
|
2022-07-04 18:19:25 +01:00
|
|
|
env.close()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_reset_async_vector_env(shared_memory):
|
2023-12-03 19:50:18 +01:00
|
|
|
"""Test the reset of async vector environment with or without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2022-08-23 11:09:54 -04:00
|
|
|
observations, infos = env.reset()
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env.close()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
assert isinstance(env.observation_space, Box)
|
|
|
|
assert isinstance(observations, np.ndarray)
|
|
|
|
assert observations.dtype == env.observation_space.dtype
|
|
|
|
assert observations.shape == (8,) + env.single_observation_space.shape
|
|
|
|
assert observations.shape == env.observation_space.shape
|
|
|
|
|
2022-02-06 17:28:27 -06:00
|
|
|
try:
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2022-08-23 11:09:54 -04:00
|
|
|
observations, infos = env.reset()
|
2022-02-06 17:28:27 -06:00
|
|
|
finally:
|
|
|
|
env.close()
|
|
|
|
|
|
|
|
assert isinstance(env.observation_space, Box)
|
|
|
|
assert isinstance(observations, np.ndarray)
|
|
|
|
assert observations.dtype == env.observation_space.dtype
|
|
|
|
assert observations.shape == (8,) + env.single_observation_space.shape
|
|
|
|
assert observations.shape == env.observation_space.shape
|
2022-05-24 16:36:35 +02:00
|
|
|
assert isinstance(infos, dict)
|
2022-02-06 17:28:27 -06:00
|
|
|
assert all([isinstance(info, dict) for info in infos])
|
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
|
|
|
@pytest.mark.parametrize("use_single_action_space", [True, False])
|
2019-06-28 17:46:45 -04:00
|
|
|
def test_step_async_vector_env(shared_memory, use_single_action_space):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test the step async vector environment with and without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
|
2021-12-08 21:31:41 -05:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2023-11-07 13:27:25 +00:00
|
|
|
env.reset()
|
2021-12-08 21:31:41 -05:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
assert isinstance(env.single_action_space, Discrete)
|
|
|
|
assert isinstance(env.action_space, MultiDiscrete)
|
|
|
|
|
|
|
|
if use_single_action_space:
|
|
|
|
actions = [env.single_action_space.sample() for _ in range(8)]
|
|
|
|
else:
|
|
|
|
actions = env.action_space.sample()
|
2023-11-07 13:27:25 +00:00
|
|
|
observations, rewards, terminations, truncations, _ = env.step(actions)
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env.close()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
assert isinstance(env.observation_space, Box)
|
|
|
|
assert isinstance(observations, np.ndarray)
|
|
|
|
assert observations.dtype == env.observation_space.dtype
|
|
|
|
assert observations.shape == (8,) + env.single_observation_space.shape
|
|
|
|
assert observations.shape == env.observation_space.shape
|
|
|
|
|
|
|
|
assert isinstance(rewards, np.ndarray)
|
|
|
|
assert isinstance(rewards[0], (float, np.floating))
|
|
|
|
assert rewards.ndim == 1
|
|
|
|
assert rewards.size == 8
|
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
assert isinstance(terminations, np.ndarray)
|
|
|
|
assert terminations.dtype == np.bool_
|
|
|
|
assert terminations.ndim == 1
|
|
|
|
assert terminations.size == 8
|
2022-08-30 19:41:59 +05:30
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
assert isinstance(truncations, np.ndarray)
|
|
|
|
assert truncations.dtype == np.bool_
|
|
|
|
assert truncations.ndim == 1
|
|
|
|
assert truncations.size == 8
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2022-01-29 12:32:35 -05:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
|
|
|
def test_call_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test call with async vector environment."""
|
2022-09-01 14:06:42 +01:00
|
|
|
env_fns = [
|
|
|
|
make_env("CartPole-v1", i, render_mode="rgb_array_list") for i in range(4)
|
|
|
|
]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2023-11-07 13:27:25 +00:00
|
|
|
env.reset()
|
2022-07-04 18:19:25 +01:00
|
|
|
images = env.call("render")
|
|
|
|
gravity = env.call("gravity")
|
|
|
|
|
|
|
|
env.close()
|
2022-01-29 12:32:35 -05:00
|
|
|
|
|
|
|
assert isinstance(images, tuple)
|
|
|
|
assert len(images) == 4
|
|
|
|
for i in range(4):
|
2022-06-08 00:20:56 +02:00
|
|
|
assert len(images[i]) == 1
|
|
|
|
assert isinstance(images[i][0], np.ndarray)
|
2022-01-29 12:32:35 -05:00
|
|
|
|
|
|
|
assert isinstance(gravity, tuple)
|
|
|
|
assert len(gravity) == 4
|
|
|
|
for i in range(4):
|
|
|
|
assert isinstance(gravity[i], float)
|
|
|
|
assert gravity[i] == 9.8
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
|
|
|
def test_set_attr_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test `set_attr_` for async vector environment with or without shared memory."""
|
2022-01-29 12:32:35 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
|
|
|
env.set_attr("gravity", [9.81, 3.72, 8.87, 1.62])
|
|
|
|
gravity = env.get_attr("gravity")
|
|
|
|
assert gravity == (9.81, 3.72, 8.87, 1.62)
|
|
|
|
|
|
|
|
env.close()
|
2022-01-29 12:32:35 -05:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_copy_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test observations are a copy of the true observation with and without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
# TODO, these tests do nothing, understand the purpose of the tests and fix them
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory, copy=True)
|
2022-08-23 11:09:54 -04:00
|
|
|
observations, infos = env.reset()
|
2022-07-04 18:19:25 +01:00
|
|
|
observations[0] = 0
|
|
|
|
|
|
|
|
env.close()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_no_copy_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test observation are not a copy of the true observation with and without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(8)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
# TODO, these tests do nothing, understand the purpose of the tests and fix them
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory, copy=False)
|
2022-08-23 11:09:54 -04:00
|
|
|
observations, infos = env.reset()
|
2022-07-04 18:19:25 +01:00
|
|
|
observations[0] = 0
|
|
|
|
|
|
|
|
env.close()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_reset_timeout_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test timeout error on reset with and without shared memory."""
|
2019-06-21 17:29:44 -04:00
|
|
|
env_fns = [make_slow_env(0.3, i) for i in range(4)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2019-06-21 17:29:44 -04:00
|
|
|
with pytest.raises(TimeoutError):
|
2022-07-04 18:19:25 +01:00
|
|
|
env.reset_async()
|
|
|
|
env.reset_wait(timeout=0.1)
|
|
|
|
|
|
|
|
env.close(terminate=True)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_step_timeout_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test timeout error on step with and without shared memory."""
|
2021-07-29 02:26:34 +02:00
|
|
|
env_fns = [make_slow_env(0.0, i) for i in range(4)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2019-06-21 17:29:44 -04:00
|
|
|
with pytest.raises(TimeoutError):
|
2022-07-04 18:19:25 +01:00
|
|
|
env.reset()
|
|
|
|
env.step_async(np.array([0.1, 0.1, 0.3, 0.1]))
|
2023-11-07 13:27:25 +00:00
|
|
|
observations, rewards, terminations, truncations, _ = env.step_wait(timeout=0.1)
|
2022-07-04 18:19:25 +01:00
|
|
|
env.close(terminate=True)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_reset_out_of_order_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test reset being called out of order with and without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2022-08-30 19:47:26 +01:00
|
|
|
with pytest.raises(
|
|
|
|
NoAsyncCallError,
|
|
|
|
match=re.escape(
|
|
|
|
"Calling `reset_wait` without any prior call to `reset_async`."
|
|
|
|
),
|
|
|
|
):
|
|
|
|
env.reset_wait()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
env.close(terminate=True)
|
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2022-08-30 19:47:26 +01:00
|
|
|
with pytest.raises(
|
|
|
|
AlreadyPendingCallError,
|
|
|
|
match=re.escape(
|
|
|
|
"Calling `reset_async` while waiting for a pending call to `step` to complete"
|
|
|
|
),
|
|
|
|
):
|
|
|
|
actions = env.action_space.sample()
|
|
|
|
env.reset()
|
|
|
|
env.step_async(actions)
|
|
|
|
env.reset_async()
|
2022-07-04 18:19:25 +01:00
|
|
|
|
2022-08-30 19:47:26 +01:00
|
|
|
with pytest.warns(
|
|
|
|
UserWarning,
|
|
|
|
match=re.escape(
|
|
|
|
"Calling `close` while waiting for a pending call to `step` to complete."
|
|
|
|
),
|
|
|
|
):
|
|
|
|
env.close(terminate=True)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_step_out_of_order_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test step out of order with and without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2022-08-30 19:47:26 +01:00
|
|
|
with pytest.raises(
|
|
|
|
NoAsyncCallError,
|
|
|
|
match=re.escape("Calling `step_wait` without any prior call to `step_async`."),
|
|
|
|
):
|
|
|
|
env.action_space.sample()
|
|
|
|
env.reset()
|
|
|
|
env.step_wait()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
env.close(terminate=True)
|
|
|
|
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
2022-08-30 19:47:26 +01:00
|
|
|
with pytest.raises(
|
|
|
|
AlreadyPendingCallError,
|
|
|
|
match=re.escape(
|
|
|
|
"Calling `step_async` while waiting for a pending call to `reset` to complete"
|
|
|
|
),
|
|
|
|
):
|
|
|
|
actions = env.action_space.sample()
|
|
|
|
env.reset_async()
|
|
|
|
env.step_async(actions)
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
UserWarning,
|
|
|
|
match=re.escape(
|
|
|
|
"Calling `close` while waiting for a pending call to `reset` to complete."
|
|
|
|
),
|
|
|
|
):
|
|
|
|
env.close(terminate=True)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_already_closed_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test the error if a function is called if environment is already closed."""
|
2022-01-10 23:42:26 -05:00
|
|
|
env_fns = [make_env("CartPole-v1", i) for i in range(4)]
|
2019-06-21 17:29:44 -04:00
|
|
|
with pytest.raises(ClosedEnvironmentError):
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
|
|
|
env.close()
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
env.reset()
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("shared_memory", [True, False])
|
2021-12-08 21:31:41 -05:00
|
|
|
def test_check_spaces_async_vector_env(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test check spaces for async vector environment with and without shared memory."""
|
2022-01-10 23:42:26 -05:00
|
|
|
# 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)
|
|
|
|
env_fns[1] = make_env("FrozenLake-v1", 1)
|
2019-06-21 17:29:44 -04:00
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
|
|
|
env.close(terminate=True)
|
2020-09-21 22:38:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_custom_space_async_vector_env():
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test custom spaces with async vector environment."""
|
2020-09-21 22:38:51 +02:00
|
|
|
env_fns = [make_custom_space_env(i) for i in range(4)]
|
2021-12-08 21:31:41 -05:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=False)
|
2022-08-23 11:09:54 -04:00
|
|
|
reset_observations, reset_infos = env.reset()
|
2021-12-08 21:31:41 -05:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
assert isinstance(env.single_action_space, CustomSpace)
|
|
|
|
assert isinstance(env.action_space, Tuple)
|
|
|
|
|
|
|
|
actions = ("action-2", "action-3", "action-5", "action-7")
|
2023-11-07 13:27:25 +00:00
|
|
|
step_observations, rewards, terminations, truncations, _ = env.step(actions)
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
env.close()
|
2020-09-21 22:38:51 +02:00
|
|
|
|
|
|
|
assert isinstance(env.single_observation_space, CustomSpace)
|
|
|
|
assert isinstance(env.observation_space, Tuple)
|
|
|
|
|
|
|
|
assert isinstance(reset_observations, tuple)
|
2021-07-29 02:26:34 +02:00
|
|
|
assert reset_observations == ("reset", "reset", "reset", "reset")
|
2020-09-21 22:38:51 +02:00
|
|
|
|
|
|
|
assert isinstance(step_observations, tuple)
|
2021-07-29 02:26:34 +02:00
|
|
|
assert step_observations == (
|
|
|
|
"step(action-2)",
|
|
|
|
"step(action-3)",
|
|
|
|
"step(action-5)",
|
|
|
|
"step(action-7)",
|
|
|
|
)
|
2020-09-21 22:38:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_custom_space_async_vector_env_shared_memory():
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test custom space with shared memory."""
|
2020-09-21 22:38:51 +02:00
|
|
|
env_fns = [make_custom_space_env(i) for i in range(4)]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
env = AsyncVectorEnv(env_fns, shared_memory=True)
|
|
|
|
env.close(terminate=True)
|
2024-07-15 15:53:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
def raise_error_reset(self, seed, options):
|
|
|
|
super(GenericTestEnv, self).reset(seed=seed, options=options)
|
|
|
|
if seed == 1:
|
|
|
|
raise ValueError("Error in reset")
|
|
|
|
return self.observation_space.sample(), {}
|
|
|
|
|
|
|
|
|
|
|
|
def raise_error_step(self, action):
|
|
|
|
if action >= 1:
|
|
|
|
raise ValueError(f"Error in step with {action}")
|
|
|
|
|
|
|
|
return self.observation_space.sample(), 0, False, False, {}
|
|
|
|
|
|
|
|
|
|
|
|
def test_async_vector_subenv_error():
|
|
|
|
envs = AsyncVectorEnv(
|
|
|
|
[
|
|
|
|
lambda: GenericTestEnv(
|
|
|
|
reset_func=raise_error_reset, step_func=raise_error_step
|
|
|
|
)
|
|
|
|
]
|
|
|
|
* 2
|
|
|
|
)
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as caught_warnings:
|
|
|
|
envs.reset(seed=[0, 0])
|
|
|
|
assert len(caught_warnings) == 0
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as caught_warnings:
|
|
|
|
with pytest.raises(ValueError, match="Error in reset"):
|
|
|
|
envs.reset(seed=[1, 0])
|
|
|
|
|
|
|
|
envs.close()
|
|
|
|
|
|
|
|
assert len(caught_warnings) == 3
|
|
|
|
assert (
|
|
|
|
"Received the following error from Worker-0 - Shutting it down"
|
|
|
|
in caught_warnings[0].message.args[0]
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
'in raise_error_reset\n raise ValueError("Error in reset")\nValueError: Error in reset'
|
|
|
|
in caught_warnings[1].message.args[0]
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
caught_warnings[2].message.args[0]
|
|
|
|
== "\x1b[31mERROR: Raising the last exception back to the main process.\x1b[0m"
|
|
|
|
)
|
|
|
|
|
|
|
|
envs = AsyncVectorEnv(
|
|
|
|
[
|
|
|
|
lambda: GenericTestEnv(
|
|
|
|
reset_func=raise_error_reset, step_func=raise_error_step
|
|
|
|
)
|
|
|
|
]
|
|
|
|
* 3
|
|
|
|
)
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as caught_warnings:
|
|
|
|
with pytest.raises(ValueError, match="Error in step"):
|
|
|
|
envs.step([0, 1, 2])
|
|
|
|
|
|
|
|
envs.close()
|
|
|
|
|
|
|
|
assert len(caught_warnings) == 5
|
|
|
|
# due to variance in the step time, the order of warnings is random
|
|
|
|
assert re.match(
|
|
|
|
r"\x1b\[31mERROR: Received the following error from Worker-[12] - Shutting it down\x1b\[0m",
|
|
|
|
caught_warnings[0].message.args[0],
|
|
|
|
)
|
|
|
|
assert re.match(
|
|
|
|
r"\x1b\[31mERROR: Traceback \(most recent call last\):(?s:.)*in raise_error_step(?s:.)*ValueError: Error in step with [12]\n\x1b\[0m",
|
|
|
|
caught_warnings[1].message.args[0],
|
|
|
|
)
|
|
|
|
assert re.match(
|
|
|
|
r"\x1b\[31mERROR: Received the following error from Worker-[12] - Shutting it down\x1b\[0m",
|
|
|
|
caught_warnings[2].message.args[0],
|
|
|
|
)
|
|
|
|
assert re.match(
|
|
|
|
r"\x1b\[31mERROR: Traceback \(most recent call last\):(?s:.)*in raise_error_step(?s:.)*ValueError: Error in step with [12]\n\x1b\[0m",
|
|
|
|
caught_warnings[3].message.args[0],
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
caught_warnings[4].message.args[0]
|
|
|
|
== "\x1b[31mERROR: Raising the last exception back to the main process.\x1b[0m"
|
|
|
|
)
|