2017-02-11 22:17:02 -08:00
|
|
|
import pytest
|
2018-11-29 02:27:27 +01:00
|
|
|
import numpy as np
|
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
from gym import envs
|
2017-02-11 22:17:02 -08:00
|
|
|
from gym.envs.tests.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.
|
2017-02-11 22:17:02 -08:00
|
|
|
@pytest.mark.parametrize("spec", 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()
|
|
|
|
|
|
|
|
# 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()
|
2021-07-29 02:26:34 +02:00
|
|
|
assert ob_space.contains(ob), "Reset observation: {!r} not in space".format(ob)
|
2016-04-27 08:00:58 -07:00
|
|
|
a = act_space.sample()
|
|
|
|
observation, reward, done, _info = env.step(a)
|
2021-07-29 12:42:48 -04:00
|
|
|
assert ob_space.contains(observation), "Step observation: {!r} not in space".format(observation)
|
2016-04-27 08:00:58 -07:00
|
|
|
assert np.isscalar(reward), "{} is not a scalar for {}".format(reward, env)
|
|
|
|
assert isinstance(done, bool), "Expected {} to be a boolean".format(done)
|
|
|
|
|
2021-07-29 02:26:34 +02: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.
|
2021-07-29 02:26:34 +02: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
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
# Run a longer rollout on some environments
|
|
|
|
def test_random_rollout():
|
2021-07-29 02:26:34 +02:00
|
|
|
for env in [envs.make("CartPole-v0"), envs.make("FrozenLake-v0")]:
|
2016-04-27 08:00:58 -07:00
|
|
|
agent = lambda ob: env.action_space.sample()
|
|
|
|
ob = env.reset()
|
2016-04-27 18:03:29 -07:00
|
|
|
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
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
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"),
|
|
|
|
envs.make("FrozenLake-v0"),
|
|
|
|
envs.make("Reverse-v0"),
|
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()
|