2022-05-24 16:36:35 +02:00
|
|
|
import numpy as np
|
|
|
|
import pytest
|
|
|
|
|
2022-09-08 10:10:07 +01:00
|
|
|
import gymnasium
|
|
|
|
from gymnasium.vector.sync_vector_env import SyncVectorEnv
|
2022-05-24 16:36:35 +02:00
|
|
|
from tests.vector.utils import make_env
|
|
|
|
|
|
|
|
ENV_ID = "CartPole-v1"
|
|
|
|
NUM_ENVS = 3
|
|
|
|
ENV_STEPS = 50
|
|
|
|
SEED = 42
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("asynchronous", [True, False])
|
|
|
|
def test_vector_env_info(asynchronous):
|
2022-09-08 10:10:07 +01:00
|
|
|
env = gymnasium.vector.make(
|
2022-06-16 14:29:13 +01:00
|
|
|
ENV_ID, num_envs=NUM_ENVS, asynchronous=asynchronous, disable_env_checker=True
|
|
|
|
)
|
2022-05-24 16:36:35 +02:00
|
|
|
env.reset(seed=SEED)
|
|
|
|
for _ in range(ENV_STEPS):
|
|
|
|
env.action_space.seed(SEED)
|
|
|
|
action = env.action_space.sample()
|
2022-08-30 19:41:59 +05:30
|
|
|
_, _, terminateds, truncateds, infos = env.step(action)
|
|
|
|
if any(terminateds) or any(truncateds):
|
2022-07-10 02:18:06 +05:30
|
|
|
assert len(infos["final_observation"]) == NUM_ENVS
|
|
|
|
assert len(infos["_final_observation"]) == NUM_ENVS
|
2022-05-24 16:36:35 +02:00
|
|
|
|
2022-07-10 02:18:06 +05:30
|
|
|
assert isinstance(infos["final_observation"], np.ndarray)
|
|
|
|
assert isinstance(infos["_final_observation"], np.ndarray)
|
2022-05-24 16:36:35 +02:00
|
|
|
|
2022-08-30 19:41:59 +05:30
|
|
|
for i, (terminated, truncated) in enumerate(zip(terminateds, truncateds)):
|
|
|
|
if terminated or truncated:
|
2022-07-10 02:18:06 +05:30
|
|
|
assert infos["_final_observation"][i]
|
2022-05-24 16:36:35 +02:00
|
|
|
else:
|
2022-07-10 02:18:06 +05:30
|
|
|
assert not infos["_final_observation"][i]
|
|
|
|
assert infos["final_observation"][i] is None
|
2022-05-24 16:36:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("concurrent_ends", [1, 2, 3])
|
|
|
|
def test_vector_env_info_concurrent_termination(concurrent_ends):
|
|
|
|
# envs that need to terminate together will have the same action
|
|
|
|
actions = [0] * concurrent_ends + [1] * (NUM_ENVS - concurrent_ends)
|
|
|
|
envs = [make_env(ENV_ID, SEED) for _ in range(NUM_ENVS)]
|
|
|
|
envs = SyncVectorEnv(envs)
|
|
|
|
|
|
|
|
for _ in range(ENV_STEPS):
|
2022-08-30 19:41:59 +05:30
|
|
|
_, _, terminateds, truncateds, infos = envs.step(actions)
|
|
|
|
if any(terminateds) or any(truncateds):
|
|
|
|
for i, (terminated, truncated) in enumerate(zip(terminateds, truncateds)):
|
2022-05-24 16:36:35 +02:00
|
|
|
if i < concurrent_ends:
|
2022-08-30 19:41:59 +05:30
|
|
|
assert terminated or truncated
|
2022-07-10 02:18:06 +05:30
|
|
|
assert infos["_final_observation"][i]
|
2022-05-24 16:36:35 +02:00
|
|
|
else:
|
2022-07-10 02:18:06 +05:30
|
|
|
assert not infos["_final_observation"][i]
|
|
|
|
assert infos["final_observation"][i] is None
|
2022-05-24 16:36:35 +02:00
|
|
|
return
|