Initialize observation spaces and pytest (#2929)

* Remove step initialization for mujoco obs spaces

	* remove step initialization for mujoco obs space

	* pre-commit

pytest obs space mujoco
This commit is contained in:
Rodrigo de Lazcano
2022-06-30 10:59:59 -04:00
committed by GitHub
parent 7f6effbc0d
commit 61a39f41bc
30 changed files with 364 additions and 37 deletions

View File

@@ -3,6 +3,7 @@ import pytest
import gym
from gym import envs
from gym.envs.registration import EnvSpec
from tests.envs.utils import mujoco_testing_env_specs
EPS = 1e-6
@@ -37,6 +38,65 @@ def verify_environments_match(
break
EXCLUDE_POS_FROM_OBS = [
"Ant",
"HalfCheetah",
"Hopper",
"Humanoid",
"Swimmer",
"Walker2d",
]
@pytest.mark.parametrize(
"env_spec",
mujoco_testing_env_specs,
ids=[env_spec.id for env_spec in mujoco_testing_env_specs],
)
def test_obs_space_mujoco_environments(env_spec: EnvSpec):
"""Check that the returned observations are contained in the observation space of the environment"""
env = env_spec.make(disable_env_checker=True)
reset_obs = env.reset()
assert env.observation_space.contains(
reset_obs
), f"Obseravtion returned by reset() of {env_spec.id} is not contained in the default observation space {env.observation_space}."
action = env.action_space.sample()
step_obs, _, _, _ = env.step(action)
assert env.observation_space.contains(
step_obs
), f"Obseravtion returned by step(action) of {env_spec.id} is not contained in the default observation space {env.observation_space}."
if env_spec.name in EXCLUDE_POS_FROM_OBS and (
env_spec.version == 4 or env_spec.version == 3
):
env = env_spec.make(
disable_env_checker=True, exclude_current_positions_from_observation=False
)
reset_obs = env.reset()
assert env.observation_space.contains(
reset_obs
), f"Obseravtion of {env_spec.id} is not contained in the default observation space {env.observation_space} when excluding current position from observation."
step_obs, _, _, _ = env.step(action)
assert env.observation_space.contains(
step_obs
), f"Obseravtion returned by step(action) of {env_spec.id} is not contained in the default observation space {env.observation_space} when excluding current position from observation."
# Ant-v4 has the option of including contact forces in the observation space with the use_contact_forces argument
if env_spec.name == "Ant" and env_spec.version == 4:
env = env_spec.make(disable_env_checker=True, use_contact_forces=True)
reset_obs = env.reset()
assert env.observation_space.contains(
reset_obs
), f"Obseravtion of {env_spec.id} is not contained in the default observation space {env.observation_space} when using contact forces."
step_obs, _, _, _ = env.step(action)
assert env.observation_space.contains(
step_obs
), f"Obseravtion returned by step(action) of {env_spec.id} is not contained in the default observation space {env.observation_space} when using contact forces."
MUJOCO_V2_V3_ENVS = [
spec.name
for spec in mujoco_testing_env_specs