| import gym |
| import numpy as np |
| import pytest |
| from gym import spaces |
|
|
| from stable_baselines3.common.bit_flipping_env import BitFlippingEnv |
| from stable_baselines3.common.env_checker import check_env |
| from stable_baselines3.common.identity_env import ( |
| FakeImageEnv, |
| IdentityEnv, |
| IdentityEnvBox, |
| IdentityEnvMultiBinary, |
| IdentityEnvMultiDiscrete, |
| ) |
|
|
| ENV_CLASSES = [BitFlippingEnv, IdentityEnv, IdentityEnvBox, IdentityEnvMultiBinary, IdentityEnvMultiDiscrete, FakeImageEnv] |
|
|
|
|
| @pytest.mark.parametrize("env_id", ["CartPole-v0", "Pendulum-v0"]) |
| def test_env(env_id): |
| """ |
| Check that environmnent integrated in Gym pass the test. |
| |
| :param env_id: (str) |
| """ |
| env = gym.make(env_id) |
| with pytest.warns(None) as record: |
| check_env(env) |
|
|
| |
| |
| if env_id == "Pendulum-v0": |
| assert len(record) == 1 |
| else: |
| |
| assert len(record) == 0 |
|
|
|
|
| @pytest.mark.parametrize("env_class", ENV_CLASSES) |
| def test_custom_envs(env_class): |
| env = env_class() |
| check_env(env) |
|
|
|
|
| def test_high_dimension_action_space(): |
| """ |
| Test for continuous action space |
| with more than one action. |
| """ |
| env = FakeImageEnv() |
| |
| env.action_space = spaces.Box(low=-1, high=1, shape=(20,), dtype=np.float32) |
|
|
| |
| def patched_step(_action): |
| return env.observation_space.sample(), 0.0, False, {} |
|
|
| env.step = patched_step |
| check_env(env) |
|
|
|
|
| @pytest.mark.parametrize( |
| "new_obs_space", |
| [ |
| |
| spaces.Box(low=0, high=255, shape=(32, 32, 3), dtype=np.uint8), |
| |
| spaces.Box(low=0, high=1, shape=(64, 64, 3), dtype=np.uint8), |
| |
| spaces.Box(low=0, high=255, shape=(64, 64, 3), dtype=np.float32), |
| |
| spaces.Box(low=-1, high=1, shape=(64, 3), dtype=np.float32), |
| |
| spaces.Tuple([spaces.Discrete(5), spaces.Discrete(10)]), |
| |
| spaces.Dict({"position": spaces.Discrete(5)}), |
| ], |
| ) |
| def test_non_default_spaces(new_obs_space): |
| env = FakeImageEnv() |
| env.observation_space = new_obs_space |
| |
| env.reset = new_obs_space.sample |
|
|
| def patched_step(_action): |
| return new_obs_space.sample(), 0.0, False, {} |
|
|
| env.step = patched_step |
| with pytest.warns(UserWarning): |
| check_env(env) |
|
|
|
|
| def check_reset_assert_error(env, new_reset_return): |
| """ |
| Helper to check that the error is caught. |
| :param env: (gym.Env) |
| :param new_reset_return: (Any) |
| """ |
|
|
| def wrong_reset(): |
| return new_reset_return |
|
|
| |
| env.reset = wrong_reset |
| with pytest.raises(AssertionError): |
| check_env(env) |
|
|
|
|
| def test_common_failures_reset(): |
| """ |
| Test that common failure cases of the `reset_method` are caught |
| """ |
| env = IdentityEnvBox() |
| |
| check_reset_assert_error(env, np.ones((3,))) |
| |
| check_reset_assert_error(env, 1) |
|
|
| |
| check_reset_assert_error(env, (env.observation_space.sample(), False)) |
|
|
|
|
| def check_step_assert_error(env, new_step_return=()): |
| """ |
| Helper to check that the error is caught. |
| :param env: (gym.Env) |
| :param new_step_return: (tuple) |
| """ |
|
|
| def wrong_step(_action): |
| return new_step_return |
|
|
| |
| env.step = wrong_step |
| with pytest.raises(AssertionError): |
| check_env(env) |
|
|
|
|
| def test_common_failures_step(): |
| """ |
| Test that common failure cases of the `step` method are caught |
| """ |
| env = IdentityEnvBox() |
|
|
| |
| check_step_assert_error(env, (np.ones((4,)), 1.0, False, {})) |
| |
| check_step_assert_error(env, (1, 1.0, False, {})) |
|
|
| |
| check_step_assert_error(env, (env.observation_space.sample(), np.ones(1), False, {})) |
|
|
| |
| check_step_assert_error(env, (env.observation_space.sample(), 0.0, False)) |
|
|
| |
| check_step_assert_error(env, (env.observation_space.sample(), 0.0, 3.0, {})) |
| check_step_assert_error(env, (env.observation_space.sample(), 0.0, 1, {})) |
|
|