2023-11-07 13:27:25 +00:00
|
|
|
"""Test vector environment implementations."""
|
|
|
|
|
2022-10-05 17:53:45 +01:00
|
|
|
from functools import partial
|
|
|
|
|
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
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
from gymnasium.spaces import Discrete
|
2023-12-03 19:50:18 +01:00
|
|
|
from gymnasium.utils.env_checker import data_equivalence
|
2023-11-07 13:27:25 +00:00
|
|
|
from gymnasium.vector import AsyncVectorEnv, SyncVectorEnv
|
2022-10-05 17:53:45 +01:00
|
|
|
from tests.testing_env import GenericTestEnv
|
2023-11-07 13:27:25 +00:00
|
|
|
from tests.vector.testing_utils import make_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_vector_env_equal(shared_memory):
|
2023-11-07 13:27:25 +00:00
|
|
|
"""Test that vector environment are equal for both async and sync variants."""
|
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
|
|
|
num_steps = 100
|
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
async_env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
|
|
|
|
sync_env = SyncVectorEnv(env_fns)
|
|
|
|
|
|
|
|
assert async_env.num_envs == sync_env.num_envs
|
|
|
|
assert async_env.observation_space == sync_env.observation_space
|
|
|
|
assert async_env.single_observation_space == sync_env.single_observation_space
|
|
|
|
assert async_env.action_space == sync_env.action_space
|
|
|
|
assert async_env.single_action_space == sync_env.single_action_space
|
|
|
|
|
2022-08-23 11:09:54 -04:00
|
|
|
async_observations, async_infos = async_env.reset(seed=0)
|
|
|
|
sync_observations, sync_infos = sync_env.reset(seed=0)
|
2022-07-04 18:19:25 +01:00
|
|
|
assert np.all(async_observations == sync_observations)
|
2023-12-03 19:50:18 +01:00
|
|
|
assert data_equivalence(async_infos, sync_infos)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
for _ in range(num_steps):
|
|
|
|
actions = async_env.action_space.sample()
|
|
|
|
assert actions in sync_env.action_space
|
2021-11-14 08:57:44 -05:00
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
(
|
|
|
|
async_observations,
|
|
|
|
async_rewards,
|
|
|
|
async_terminations,
|
|
|
|
async_truncations,
|
|
|
|
async_infos,
|
|
|
|
) = async_env.step(actions)
|
|
|
|
(
|
|
|
|
sync_observations,
|
|
|
|
sync_rewards,
|
|
|
|
sync_terminations,
|
|
|
|
sync_truncations,
|
|
|
|
sync_infos,
|
|
|
|
) = sync_env.step(actions)
|
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
assert np.all(async_observations == sync_observations)
|
|
|
|
assert np.all(async_rewards == sync_rewards)
|
2023-11-07 13:27:25 +00:00
|
|
|
assert np.all(async_terminations == sync_terminations)
|
|
|
|
assert np.all(async_truncations == sync_truncations)
|
2023-12-03 19:50:18 +01:00
|
|
|
assert data_equivalence(async_infos, sync_infos)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
async_env.close()
|
|
|
|
sync_env.close()
|
2020-09-21 22:38:51 +02:00
|
|
|
|
|
|
|
|
2022-10-05 17:53:45 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"vectoriser",
|
|
|
|
(
|
|
|
|
SyncVectorEnv,
|
|
|
|
partial(AsyncVectorEnv, shared_memory=True),
|
|
|
|
partial(AsyncVectorEnv, shared_memory=False),
|
|
|
|
),
|
|
|
|
ids=["Sync", "Async with shared memory", "Async without shared memory"],
|
|
|
|
)
|
|
|
|
def test_final_obs_info(vectoriser):
|
|
|
|
"""Tests that the vector environments correctly return the final observation and info."""
|
|
|
|
|
|
|
|
def reset_fn(self, seed=None, options=None):
|
|
|
|
return 0, {"reset": True}
|
|
|
|
|
|
|
|
def thunk():
|
|
|
|
return GenericTestEnv(
|
|
|
|
action_space=Discrete(4),
|
|
|
|
observation_space=Discrete(4),
|
2022-12-05 19:14:56 +00:00
|
|
|
reset_func=reset_fn,
|
|
|
|
step_func=lambda self, action: (
|
2022-10-05 17:53:45 +01:00
|
|
|
action if action < 3 else 0,
|
|
|
|
0,
|
|
|
|
action >= 3,
|
|
|
|
False,
|
|
|
|
{"action": action},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
env = vectoriser([thunk])
|
|
|
|
obs, info = env.reset()
|
|
|
|
assert obs == np.array([0]) and info == {
|
|
|
|
"reset": np.array([True]),
|
|
|
|
"_reset": np.array([True]),
|
|
|
|
}
|
|
|
|
|
|
|
|
obs, _, termination, _, info = env.step([1])
|
|
|
|
assert (
|
|
|
|
obs == np.array([1])
|
|
|
|
and termination == np.array([False])
|
|
|
|
and info == {"action": np.array([1]), "_action": np.array([True])}
|
|
|
|
)
|
|
|
|
|
|
|
|
obs, _, termination, _, info = env.step([2])
|
|
|
|
assert (
|
|
|
|
obs == np.array([2])
|
|
|
|
and termination == np.array([False])
|
|
|
|
and info == {"action": np.array([2]), "_action": np.array([True])}
|
|
|
|
)
|
|
|
|
|
|
|
|
obs, _, termination, _, info = env.step([3])
|
2023-12-03 19:50:18 +01:00
|
|
|
assert obs == np.array([0]) and info == {"action": 3, "_action": np.array([True])}
|
|
|
|
|
|
|
|
obs, _, terminated, _, info = env.step([4])
|
2022-10-05 17:53:45 +01:00
|
|
|
assert (
|
|
|
|
obs == np.array([0])
|
|
|
|
and termination == np.array([True])
|
|
|
|
and info["reset"] == np.array([True])
|
|
|
|
)
|
2023-11-07 13:27:25 +00:00
|
|
|
|
|
|
|
env.close()
|