mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-17 20:39:12 +00:00
* Copied over SB3 env checker * Added test from SB3 * Addition of MIT license attribution and black formatting * Removed SB3 dependence * Implemented rough, non-vectorized version of check_nan * Made some SB3 warnings a bit more "general" * Removed check for "unsupported" spaces, since Gym should support any space * Added action/observation checks from pettingzoo, referenced in file docstring * Removed copy of MIT license in file docstring * Re-added usage of the check_inf flag for helper functino * Changed test to be simple/classic example. Should add more tests * Added API compliance check to registered envs
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
import pytest
|
|
import numpy as np
|
|
|
|
from gym import envs
|
|
from gym.envs.tests.spec_list import spec_list
|
|
from gym.utils.env_checker import check_env
|
|
|
|
|
|
# This runs a smoketest on each official registered env. We may want
|
|
# to try also running environments which are not officially registered
|
|
# envs.
|
|
@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:
|
|
assert "autodetected dtype" not in str(warning_msg.message)
|
|
|
|
ob_space = env.observation_space
|
|
act_space = env.action_space
|
|
ob = env.reset()
|
|
assert ob_space.contains(ob), "Reset observation: {!r} not in space".format(ob)
|
|
a = act_space.sample()
|
|
observation, reward, done, _info = env.step(a)
|
|
assert ob_space.contains(observation), "Step observation: {!r} not in space".format(
|
|
observation
|
|
)
|
|
assert np.isscalar(reward), "{} is not a scalar for {}".format(reward, env)
|
|
assert isinstance(done, bool), "Expected {} to be a boolean".format(done)
|
|
|
|
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", []):
|
|
env.render(mode=mode)
|
|
|
|
env.close()
|
|
|
|
|
|
# Run a longer rollout on some environments
|
|
def test_random_rollout():
|
|
for env in [envs.make("CartPole-v0"), envs.make("FrozenLake-v0")]:
|
|
agent = lambda ob: env.action_space.sample()
|
|
ob = env.reset()
|
|
for _ in range(10):
|
|
assert env.observation_space.contains(ob)
|
|
a = agent(ob)
|
|
assert env.action_space.contains(a)
|
|
(ob, _reward, done, _info) = env.step(a)
|
|
if done:
|
|
break
|
|
env.close()
|
|
|
|
|
|
def test_env_render_result_is_immutable():
|
|
environs = [
|
|
envs.make("Taxi-v3"),
|
|
envs.make("FrozenLake-v0"),
|
|
envs.make("Reverse-v0"),
|
|
]
|
|
|
|
for env in environs:
|
|
env.reset()
|
|
output = env.render(mode="ansi")
|
|
assert isinstance(output, str)
|
|
env.close()
|