2018-11-29 02:27:27 +01:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
import pytest
|
2018-11-29 02:27:27 +01:00
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
from gym import envs
|
2021-08-22 00:11:19 +02:00
|
|
|
from gym.spaces import Box
|
2021-08-12 12:35:09 -05:00
|
|
|
from gym.utils.env_checker import check_env
|
2022-03-31 12:50:38 -07:00
|
|
|
from tests.envs.spec_list import spec_list
|
2016-05-31 00:57:31 -07:00
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2016-05-31 00:57:31 -07:00
|
|
|
# This runs a smoketest on each official registered env. We may want
|
|
|
|
# to try also running environments which are not officially registered
|
|
|
|
# envs.
|
2022-03-14 14:27:03 +00:00
|
|
|
@pytest.mark.filterwarnings(
|
|
|
|
"ignore:.*We recommend you to use a symmetric and normalized Box action space.*"
|
|
|
|
)
|
2022-04-10 18:36:23 +01:00
|
|
|
@pytest.mark.parametrize("spec", spec_list, ids=[spec.id for spec in spec_list])
|
2016-05-31 00:57:31 -07:00
|
|
|
def test_env(spec):
|
2018-11-29 02:27:27 +01:00
|
|
|
# Capture warnings
|
|
|
|
with pytest.warns(None) as warnings:
|
|
|
|
env = spec.make()
|
|
|
|
|
2021-08-12 12:35:09 -05:00
|
|
|
# Test if env adheres to Gym API
|
|
|
|
check_env(env, warn=True, skip_render_check=True)
|
|
|
|
|
2018-11-29 02:27:27 +01:00
|
|
|
# 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)
|
2018-11-29 02:27:27 +01:00
|
|
|
|
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"
|
2021-08-22 00:11:19 +02:00
|
|
|
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}"
|
2021-08-22 00:11:19 +02:00
|
|
|
|
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"
|
2021-08-22 00:11:19 +02:00
|
|
|
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
|
|
|
|
2022-02-28 15:54:03 -05:00
|
|
|
for mode in env.metadata.get("render_modes", []):
|
2016-05-15 17:22:38 -07:00
|
|
|
env.render(mode=mode)
|
|
|
|
|
|
|
|
# Make sure we can render the environment after close.
|
2022-02-28 15:54:03 -05:00
|
|
|
for mode in env.metadata.get("render_modes", []):
|
2016-04-27 08:00:58 -07:00
|
|
|
env.render(mode=mode)
|
|
|
|
|
2016-05-27 12:16:35 -07:00
|
|
|
env.close()
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2022-04-10 18:36:23 +01:00
|
|
|
@pytest.mark.parametrize("spec", spec_list, ids=[spec.id for spec in spec_list])
|
2022-02-06 17:28:27 -06:00
|
|
|
def test_reset_info(spec):
|
|
|
|
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
with pytest.warns(None):
|
2022-02-06 17:28:27 -06:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
def test_env_render_result_is_immutable():
|
|
|
|
environs = [
|
2021-07-29 02:26:34 +02:00
|
|
|
envs.make("Taxi-v3"),
|
2021-08-13 00:18:42 -04:00
|
|
|
envs.make("FrozenLake-v1"),
|
2019-02-09 02:58:51 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
for env in environs:
|
|
|
|
env.reset()
|
2021-07-29 02:26:34 +02:00
|
|
|
output = env.render(mode="ansi")
|
2020-04-10 17:10:34 -05:00
|
|
|
assert isinstance(output, str)
|
2019-02-09 02:58:51 +02:00
|
|
|
env.close()
|