Files
Gymnasium/tests/envs/test_envs.py

104 lines
3.2 KiB
Python
Raw Normal View History

import numpy as np
import pytest
2016-04-27 08:00:58 -07:00
from gym import envs
from gym.spaces import Box
from gym.utils.env_checker import check_env
from tests.envs.spec_list import spec_list
2021-07-29 02:26:34 +02:00
# This runs a smoketest on each official registered env. We may want
# to try also running environments which are not officially registered
# envs.
Reduces warnings produced by pytest from ~1500 to 13 (#2660) * Updated cartpole-v0 to v1 to prevent warning and added pytest.mark.filterwarnings for tests where warnings are unavoidable * Change np.bool to bool as numpy raises a warning and bool is the suggested solution * Seeding randint is deprecated in the future, integers is new solution * Fixed errors thrown when the video recorder is deleted but not closed * spaces.Box expects a floating array, updated all cases where this was not true and modified float32 to float64 as float array default to float64. Otherwise space.Box raises warning that dtype precision (float32) is lower than array precision (float64). * Added pytest.mark.filterwarnings to preventing the raising of an intended warning * Added comment to explain why a warning is raised that can't be prevented without version update to the environment * Added comment to explain why warning is raised * Changed values to float as expected by the box which default to float64 * Removed --forked from pytest as the pytest-forked project is no being maintained and was not raising warnings as expected * When AsyncVectorEnv has shared_memory=True then a ValueError is raised before _state is initialised. Therefore, on the destruction on the env an error is thrown in .close_extra as _state does not exist * Possible fix that was causing an error in test_call_async_vector_env by ensuring that pygame resources are released * Pygame throws an error with ALSA when closed, using a fix from PettingZoo (https://github.com/Farama-Foundation/PettingZoo/blob/master/pettingzoo/__init__.py). We use the dsp audiodriver to prevent this issue * Modification due to running pre-commit locally * Updated cartpole-v0 to v1 to prevent warning and added pytest.mark.filterwarnings for tests where warnings are unavoidable * Change np.bool to bool as numpy raises a warning and bool is the suggested solution * Seeding randint is deprecated in the future, integers is new solution * Fixed errors thrown when the video recorder is deleted but not closed * spaces.Box expects a floating array, updated all cases where this was not true and modified float32 to float64 as float array default to float64. Otherwise space.Box raises warning that dtype precision (float32) is lower than array precision (float64). * Added pytest.mark.filterwarnings to preventing the raising of an intended warning * Added comment to explain why a warning is raised that can't be prevented without version update to the environment * Added comment to explain why warning is raised * Changed values to float as expected by the box which default to float64 * Removed --forked from pytest as the pytest-forked project is no being maintained and was not raising warnings as expected * When AsyncVectorEnv has shared_memory=True then a ValueError is raised before _state is initialised. Therefore, on the destruction on the env an error is thrown in .close_extra as _state does not exist * Possible fix that was causing an error in test_call_async_vector_env by ensuring that pygame resources are released * Pygame throws an error with ALSA when closed, using a fix from PettingZoo (https://github.com/Farama-Foundation/PettingZoo/blob/master/pettingzoo/__init__.py). We use the dsp audiodriver to prevent this issue * Modification due to running pre-commit locally
2022-03-14 14:27:03 +00:00
@pytest.mark.filterwarnings(
"ignore:.*We recommend you to use a symmetric and normalized Box action space.*"
)
@pytest.mark.parametrize("spec", spec_list)
def test_env(spec):
# Capture warnings
with pytest.warns(None) as warnings:
env = spec.make()
# Test if env adheres to Gym API
check_env(env, warn=True, skip_render_check=True)
# Check that dtype is explicitly declared for gym.Box spaces
for warning_msg in warnings:
2021-07-29 02:26:34 +02:00
assert "autodetected dtype" not in str(warning_msg.message)
2016-04-27 08:00:58 -07:00
ob_space = env.observation_space
act_space = env.action_space
ob = env.reset()
2022-01-11 18:12:05 +01:00
assert ob_space.contains(ob), f"Reset observation: {ob!r} not in space"
if isinstance(ob_space, Box):
# Only checking dtypes for Box spaces to avoid iterating through tuple entries
assert (
ob.dtype == ob_space.dtype
2022-01-11 18:12:05 +01:00
), f"Reset observation dtype: {ob.dtype}, expected: {ob_space.dtype}"
2016-04-27 08:00:58 -07:00
a = act_space.sample()
observation, reward, done, _info = env.step(a)
2022-01-11 18:12:05 +01:00
assert ob_space.contains(
2021-07-29 15:39:42 -04:00
observation
2022-01-11 18:12:05 +01:00
), f"Step observation: {observation!r} not in space"
assert np.isscalar(reward), f"{reward} is not a scalar for {env}"
assert isinstance(done, bool), f"Expected {done} to be a boolean"
if isinstance(ob_space, Box):
assert (
observation.dtype == ob_space.dtype
2022-01-11 18:12:05 +01:00
), f"Step observation dtype: {ob.dtype}, expected: {ob_space.dtype}"
2016-04-27 08:00:58 -07:00
for mode in env.metadata.get("render_modes", []):
env.render(mode=mode)
# Make sure we can render the environment after close.
for mode in env.metadata.get("render_modes", []):
2016-04-27 08:00:58 -07:00
env.render(mode=mode)
env.close()
2021-07-29 02:26:34 +02:00
@pytest.mark.parametrize("spec", spec_list)
def test_reset_info(spec):
with pytest.warns(None) as warnings:
env = spec.make()
ob_space = env.observation_space
obs = env.reset()
assert ob_space.contains(obs)
obs = env.reset(return_info=False)
assert ob_space.contains(obs)
obs, info = env.reset(return_info=True)
assert ob_space.contains(obs)
assert isinstance(info, dict)
env.close()
2016-04-27 08:00:58 -07:00
# Run a longer rollout on some environments
def test_random_rollout():
Reduces warnings produced by pytest from ~1500 to 13 (#2660) * Updated cartpole-v0 to v1 to prevent warning and added pytest.mark.filterwarnings for tests where warnings are unavoidable * Change np.bool to bool as numpy raises a warning and bool is the suggested solution * Seeding randint is deprecated in the future, integers is new solution * Fixed errors thrown when the video recorder is deleted but not closed * spaces.Box expects a floating array, updated all cases where this was not true and modified float32 to float64 as float array default to float64. Otherwise space.Box raises warning that dtype precision (float32) is lower than array precision (float64). * Added pytest.mark.filterwarnings to preventing the raising of an intended warning * Added comment to explain why a warning is raised that can't be prevented without version update to the environment * Added comment to explain why warning is raised * Changed values to float as expected by the box which default to float64 * Removed --forked from pytest as the pytest-forked project is no being maintained and was not raising warnings as expected * When AsyncVectorEnv has shared_memory=True then a ValueError is raised before _state is initialised. Therefore, on the destruction on the env an error is thrown in .close_extra as _state does not exist * Possible fix that was causing an error in test_call_async_vector_env by ensuring that pygame resources are released * Pygame throws an error with ALSA when closed, using a fix from PettingZoo (https://github.com/Farama-Foundation/PettingZoo/blob/master/pettingzoo/__init__.py). We use the dsp audiodriver to prevent this issue * Modification due to running pre-commit locally * Updated cartpole-v0 to v1 to prevent warning and added pytest.mark.filterwarnings for tests where warnings are unavoidable * Change np.bool to bool as numpy raises a warning and bool is the suggested solution * Seeding randint is deprecated in the future, integers is new solution * Fixed errors thrown when the video recorder is deleted but not closed * spaces.Box expects a floating array, updated all cases where this was not true and modified float32 to float64 as float array default to float64. Otherwise space.Box raises warning that dtype precision (float32) is lower than array precision (float64). * Added pytest.mark.filterwarnings to preventing the raising of an intended warning * Added comment to explain why a warning is raised that can't be prevented without version update to the environment * Added comment to explain why warning is raised * Changed values to float as expected by the box which default to float64 * Removed --forked from pytest as the pytest-forked project is no being maintained and was not raising warnings as expected * When AsyncVectorEnv has shared_memory=True then a ValueError is raised before _state is initialised. Therefore, on the destruction on the env an error is thrown in .close_extra as _state does not exist * Possible fix that was causing an error in test_call_async_vector_env by ensuring that pygame resources are released * Pygame throws an error with ALSA when closed, using a fix from PettingZoo (https://github.com/Farama-Foundation/PettingZoo/blob/master/pettingzoo/__init__.py). We use the dsp audiodriver to prevent this issue * Modification due to running pre-commit locally
2022-03-14 14:27:03 +00:00
for env in [envs.make("CartPole-v1"), envs.make("FrozenLake-v1")]:
2016-04-27 08:00:58 -07:00
agent = lambda ob: env.action_space.sample()
ob = env.reset()
for _ in range(10):
2016-04-27 08:00:58 -07:00
assert env.observation_space.contains(ob)
a = agent(ob)
assert env.action_space.contains(a)
(ob, _reward, done, _info) = env.step(a)
2021-07-29 02:26:34 +02:00
if done:
break
env.close()
def test_env_render_result_is_immutable():
environs = [
2021-07-29 02:26:34 +02:00
envs.make("Taxi-v3"),
envs.make("FrozenLake-v1"),
]
for env in environs:
env.reset()
2021-07-29 02:26:34 +02:00
output = env.render(mode="ansi")
assert isinstance(output, str)
env.close()