|
| 1 | +"""Tests the functionality of the BSuiteCompatibilityV0 on bsuite envs.""" |
| 2 | +import warnings |
| 3 | + |
| 4 | +import bsuite |
| 5 | +import gymnasium as gym |
| 6 | +import pytest |
| 7 | +from gymnasium.envs.registration import registry |
| 8 | +from gymnasium.error import Error |
| 9 | +from gymnasium.utils.env_checker import check_env, data_equivalence |
| 10 | + |
| 11 | +BSUITE_ENV_IDS = [ |
| 12 | + env_id |
| 13 | + for env_id in registry |
| 14 | + if env_id.startswith("bsuite") and env_id != "bsuite/compatibility-env-v0" |
| 15 | +] |
| 16 | + |
| 17 | + |
| 18 | +def test_bsuite_suite_envs(): |
| 19 | + """Tests that all BSUITE_ENVS are equal to the known bsuite tasks.""" |
| 20 | + env_ids = [env_id.split("/")[-1].split("-")[0] for env_id in BSUITE_ENV_IDS] |
| 21 | + assert list(bsuite._bsuite.EXPERIMENT_NAME_TO_ENVIRONMENT.keys()) == env_ids |
| 22 | + |
| 23 | + |
| 24 | +BSUITE_ENV_SETTINGS = dict() |
| 25 | +BSUITE_ENV_SETTINGS["bsuite/bandit-v0"] = dict() |
| 26 | +BSUITE_ENV_SETTINGS["bsuite/bandit_noise-v0"] = dict( |
| 27 | + noise_scale=1, seed=42, mapping_seed=42 |
| 28 | +) |
| 29 | +BSUITE_ENV_SETTINGS["bsuite/bandit_scale-v0"] = dict( |
| 30 | + reward_scale=1, seed=42, mapping_seed=42 |
| 31 | +) |
| 32 | +BSUITE_ENV_SETTINGS["bsuite/cartpole-v0"] = dict() |
| 33 | +BSUITE_ENV_SETTINGS["bsuite/cartpole_noise-v0"] = dict(noise_scale=1, seed=42) |
| 34 | +BSUITE_ENV_SETTINGS["bsuite/cartpole_scale-v0"] = dict(reward_scale=1, seed=42) |
| 35 | +BSUITE_ENV_SETTINGS["bsuite/cartpole_swingup-v0"] = dict() |
| 36 | +BSUITE_ENV_SETTINGS["bsuite/catch-v0"] = dict() |
| 37 | +BSUITE_ENV_SETTINGS["bsuite/catch_noise-v0"] = dict(noise_scale=1, seed=42) |
| 38 | +BSUITE_ENV_SETTINGS["bsuite/catch_scale-v0"] = dict(reward_scale=1, seed=42) |
| 39 | +BSUITE_ENV_SETTINGS["bsuite/deep_sea-v0"] = dict(size=42) |
| 40 | +BSUITE_ENV_SETTINGS["bsuite/deep_sea_stochastic-v0"] = dict(size=42) |
| 41 | +BSUITE_ENV_SETTINGS["bsuite/discounting_chain-v0"] = dict() |
| 42 | +BSUITE_ENV_SETTINGS["bsuite/memory_len-v0"] = dict(memory_length=8) |
| 43 | +BSUITE_ENV_SETTINGS["bsuite/memory_size-v0"] = dict(num_bits=8) |
| 44 | +BSUITE_ENV_SETTINGS["bsuite/mnist-v0"] = dict() |
| 45 | +BSUITE_ENV_SETTINGS["bsuite/mnist_noise-v0"] = dict(noise_scale=1, seed=42) |
| 46 | +BSUITE_ENV_SETTINGS["bsuite/mnist_scale-v0"] = dict(reward_scale=1, seed=42) |
| 47 | +BSUITE_ENV_SETTINGS["bsuite/mountain_car-v0"] = dict() |
| 48 | +BSUITE_ENV_SETTINGS["bsuite/mountain_car_noise-v0"] = dict(noise_scale=1, seed=42) |
| 49 | +BSUITE_ENV_SETTINGS["bsuite/mountain_car_scale-v0"] = dict(reward_scale=1, seed=42) |
| 50 | +BSUITE_ENV_SETTINGS["bsuite/umbrella_distract-v0"] = dict(n_distractor=3) |
| 51 | +BSUITE_ENV_SETTINGS["bsuite/umbrella_length-v0"] = dict(chain_length=3) |
| 52 | + |
| 53 | +# todo - gymnasium v27 should remove the need for some of these warnings |
| 54 | +CHECK_ENV_IGNORE_WARNINGS = [ |
| 55 | + f"\x1b[33mWARN: {message}\x1b[0m" |
| 56 | + for message in [ |
| 57 | + "A Box observation space minimum value is -infinity. This is probably too low.", |
| 58 | + "A Box observation space maximum value is -infinity. This is probably too high.", |
| 59 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (28, 28)", |
| 60 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (42, 42)", |
| 61 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (10, 5)", |
| 62 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (1, 1)", |
| 63 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (1, 2)", |
| 64 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (1, 3)", |
| 65 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (1, 6)", |
| 66 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (1, 8)", |
| 67 | + "A Box observation space has an unconventional shape (neither an image, nor a 1D vector). We recommend flattening the observation to have only a 1D vector or use a custom policy to properly process the data. Actual observation shape: (1, 10)", |
| 68 | + ] |
| 69 | +] |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("env_id", BSUITE_ENV_IDS) |
| 73 | +def test_check_env(env_id): |
| 74 | + """Check that environment pass the gymnasium check_env.""" |
| 75 | + env = gym.make(env_id, **BSUITE_ENV_SETTINGS[env_id]) |
| 76 | + |
| 77 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 78 | + check_env(env.unwrapped) |
| 79 | + |
| 80 | + for warning_message in caught_warnings: |
| 81 | + assert isinstance(warning_message.message, Warning) |
| 82 | + if warning_message.message.args[0] not in CHECK_ENV_IGNORE_WARNINGS: |
| 83 | + raise Error(f"Unexpected warning: {warning_message.message}") |
| 84 | + |
| 85 | + env.close() |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize("env_id", BSUITE_ENV_IDS) |
| 89 | +def test_seeding(env_id): |
| 90 | + """Test that dm-control seeding works.""" |
| 91 | + if gym.spec(env_id).nondeterministic: |
| 92 | + return |
| 93 | + |
| 94 | + env_1 = gym.make(env_id, **BSUITE_ENV_SETTINGS[env_id]) |
| 95 | + env_2 = gym.make(env_id, **BSUITE_ENV_SETTINGS[env_id]) |
| 96 | + |
| 97 | + obs_1, info_1 = env_1.reset(seed=42) |
| 98 | + obs_2, info_2 = env_2.reset(seed=42) |
| 99 | + assert data_equivalence(obs_1, obs_2) |
| 100 | + assert data_equivalence(info_1, info_2) |
| 101 | + for _ in range(100): |
| 102 | + actions = int(env_1.action_space.sample()) |
| 103 | + obs_1, reward_1, term_1, trunc_1, info_1 = env_1.step(actions) |
| 104 | + obs_2, reward_2, term_2, trunc_2, info_2 = env_2.step(actions) |
| 105 | + assert data_equivalence(obs_1, obs_2) |
| 106 | + assert reward_1 == reward_2 |
| 107 | + assert term_1 == term_2 and trunc_1 == trunc_2 |
| 108 | + assert data_equivalence(info_1, info_2) |
| 109 | + |
| 110 | + env_1.close() |
| 111 | + env_2.close() |
0 commit comments