2022-05-12 15:33:48 +02:00
|
|
|
"""Test environment determinism by performing a rollout."""
|
|
|
|
|
2017-02-11 22:17:02 -08:00
|
|
|
import pytest
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2022-06-06 16:21:45 +01:00
|
|
|
from gym.utils.env_checker import data_equivalence
|
2021-09-29 01:53:30 +02:00
|
|
|
from tests.envs.spec_list import spec_list
|
2016-05-29 09:07:09 -07:00
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2022-04-10 18:36:23 +01:00
|
|
|
@pytest.mark.parametrize("spec", spec_list, ids=[spec.id for spec in spec_list])
|
2016-05-29 09:07:09 -07:00
|
|
|
def test_env(spec):
|
2022-05-12 15:33:48 +02:00
|
|
|
"""Run a rollout with two environments and assert equality.
|
|
|
|
|
|
|
|
This test run a rollout of NUM_STEPS steps with two environments
|
|
|
|
initialized with the same seed and assert that:
|
|
|
|
|
|
|
|
- observation after first reset are the same
|
|
|
|
- same actions are sampled by the two envs
|
|
|
|
- observations are contained in the observation space
|
|
|
|
- obs, rew, done and info are equals between the two envs
|
|
|
|
|
|
|
|
Args:
|
|
|
|
spec (EnvSpec): Environment specification
|
|
|
|
|
|
|
|
"""
|
2016-05-30 18:07:59 -07:00
|
|
|
# Note that this precludes running this test in multiple
|
|
|
|
# threads. However, we probably already can't do multithreading
|
|
|
|
# due to some environments.
|
2022-05-12 15:33:48 +02:00
|
|
|
SEED = 0
|
|
|
|
NUM_STEPS = 50
|
2016-05-29 09:07:09 -07:00
|
|
|
|
2022-05-12 15:33:48 +02:00
|
|
|
env1, env2 = spec.make(), spec.make()
|
|
|
|
|
|
|
|
initial_observation1 = env1.reset(seed=SEED)
|
|
|
|
initial_observation2 = env2.reset(seed=SEED)
|
|
|
|
|
|
|
|
env1.action_space.seed(SEED)
|
|
|
|
env2.action_space.seed(SEED)
|
|
|
|
|
2022-06-06 16:21:45 +01:00
|
|
|
assert data_equivalence(
|
|
|
|
initial_observation1, initial_observation2
|
|
|
|
), f"Initial Observations 1 and 2 are not equivalent. initial obs 1={initial_observation1}, initial obs 2={initial_observation2}"
|
2022-05-12 15:33:48 +02:00
|
|
|
|
|
|
|
for i in range(NUM_STEPS):
|
|
|
|
action1 = env1.action_space.sample()
|
|
|
|
action2 = env2.action_space.sample()
|
2016-05-29 09:07:09 -07:00
|
|
|
|
2017-03-28 11:25:21 -07:00
|
|
|
try:
|
2022-06-06 16:21:45 +01:00
|
|
|
assert data_equivalence(
|
|
|
|
action1, action2
|
|
|
|
), f"Action 1 and 2 are not equivalent. action 1={action1}, action 2={action2}"
|
2017-03-28 11:25:21 -07:00
|
|
|
except AssertionError:
|
2022-06-06 16:21:45 +01:00
|
|
|
print(f"env 1 action space={env1.action_space}")
|
|
|
|
print(f"env 2 action space={env2.action_space}")
|
|
|
|
print(f"[{i}] action sample 1={action1}, action sample 2={action2}")
|
2017-03-28 11:25:21 -07:00
|
|
|
raise
|
2016-05-29 09:07:09 -07:00
|
|
|
|
2022-06-06 16:21:45 +01:00
|
|
|
# Don't check rollout equality if it's a nondeterministic
|
2022-05-12 15:33:48 +02:00
|
|
|
# environment.
|
|
|
|
if spec.nondeterministic:
|
|
|
|
return
|
2016-05-29 09:07:09 -07:00
|
|
|
|
2022-05-12 15:33:48 +02:00
|
|
|
obs1, rew1, done1, info1 = env1.step(action1)
|
|
|
|
obs2, rew2, done2, info2 = env2.step(action2)
|
|
|
|
|
2022-06-06 16:21:45 +01:00
|
|
|
assert data_equivalence(
|
|
|
|
obs1, obs2
|
|
|
|
), f"Observation 1 and 2 are not equivalent. obs 1={obs1}, obs 2={obs2}"
|
2022-05-12 15:33:48 +02:00
|
|
|
|
|
|
|
assert env1.observation_space.contains(obs1)
|
|
|
|
assert env2.observation_space.contains(obs2)
|
2016-05-29 09:07:09 -07:00
|
|
|
|
2022-06-06 16:21:45 +01:00
|
|
|
assert rew1 == rew2, f"[{i}] reward1: {rew1}, reward2: {rew2}"
|
|
|
|
assert done1 == done2, f"[{i}] done1: {done1}, done2: {done2}"
|
|
|
|
assert data_equivalence(
|
|
|
|
info1, info2
|
|
|
|
), f"Info 1 and 2 are not equivalent. info 1={info1}, info 2={info2}"
|
2016-05-29 09:07:09 -07:00
|
|
|
|
2022-05-12 15:33:48 +02:00
|
|
|
if done1: # done2 verified in previous assertion
|
|
|
|
env1.reset(seed=SEED)
|
|
|
|
env2.reset(seed=SEED)
|
|
|
|
|
|
|
|
env1.close()
|
|
|
|
env2.close()
|