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:10:07 +01:00
|
|
|
from gymnasium.spaces import Tuple
|
|
|
|
from gymnasium.vector.async_vector_env import AsyncVectorEnv
|
|
|
|
from gymnasium.vector.sync_vector_env import SyncVectorEnv
|
|
|
|
from gymnasium.vector.vector_env import VectorEnv
|
2022-03-31 12:50:38 -07:00
|
|
|
from tests.vector.utils import CustomSpace, 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):
|
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)
|
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
|
|
|
|
2022-07-04 18:19:25 +01:00
|
|
|
# fmt: off
|
2022-08-30 19:41:59 +05:30
|
|
|
async_observations, async_rewards, async_terminateds, async_truncateds, async_infos = async_env.step(actions)
|
|
|
|
sync_observations, sync_rewards, sync_terminateds, sync_truncateds, sync_infos = sync_env.step(actions)
|
2022-07-04 18:19:25 +01:00
|
|
|
# fmt: on
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-08-30 19:41:59 +05:30
|
|
|
if any(sync_terminateds) or any(sync_truncateds):
|
2022-07-10 02:18:06 +05:30
|
|
|
assert "final_observation" in async_infos
|
|
|
|
assert "_final_observation" in async_infos
|
|
|
|
assert "final_observation" in sync_infos
|
|
|
|
assert "_final_observation" in sync_infos
|
2022-07-04 18:19:25 +01:00
|
|
|
|
|
|
|
assert np.all(async_observations == sync_observations)
|
|
|
|
assert np.all(async_rewards == sync_rewards)
|
2022-08-30 19:41:59 +05:30
|
|
|
assert np.all(async_terminateds == sync_terminateds)
|
|
|
|
assert np.all(async_truncateds == sync_truncateds)
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_custom_space_vector_env():
|
|
|
|
env = VectorEnv(4, CustomSpace(), CustomSpace())
|
|
|
|
|
|
|
|
assert isinstance(env.single_observation_space, CustomSpace)
|
|
|
|
assert isinstance(env.observation_space, Tuple)
|
|
|
|
|
|
|
|
assert isinstance(env.single_action_space, CustomSpace)
|
|
|
|
assert isinstance(env.action_space, Tuple)
|