2016-04-27 08:00:58 -07:00
|
|
|
import numpy as np
|
|
|
|
from nose2 import tools
|
2016-04-28 14:44:30 -07:00
|
|
|
import os
|
|
|
|
|
2016-05-11 14:35:51 +02:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
from gym import envs
|
|
|
|
|
|
|
|
# This runs a smoketest on each official registered env. We may want
|
|
|
|
# to try also running environments which are not officially registered
|
|
|
|
# envs.
|
2016-04-28 14:44:30 -07:00
|
|
|
specs = [spec for spec in envs.registry.all()]
|
2016-04-27 08:00:58 -07:00
|
|
|
@tools.params(*specs)
|
|
|
|
def test_env(spec):
|
2016-05-03 11:09:15 -04:00
|
|
|
# Skip for deprecated envs
|
|
|
|
if spec._entry_point is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Skip mujoco tests for pull request CI
|
2016-04-28 14:44:30 -07:00
|
|
|
skip_mujoco = os.environ.get('TRAVIS_PULL_REQUEST', 'false') != 'false'
|
2016-04-29 18:14:13 -07:00
|
|
|
if skip_mujoco and spec._entry_point.startswith('gym.envs.mujoco:'):
|
2016-04-28 14:44:30 -07:00
|
|
|
return
|
|
|
|
|
2016-05-11 14:35:51 +02:00
|
|
|
# TODO(jonas 2016-05-11): Re-enable these tests after fixing box2d-py
|
|
|
|
if spec._entry_point.startswith('gym.envs.box2d:'):
|
|
|
|
logger.warn("Skipping tests for box2d env {}".format(spec._entry_point))
|
|
|
|
return
|
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
env = spec.make()
|
|
|
|
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)
|
2016-05-07 22:58:18 -07:00
|
|
|
env.render(close=True)
|
2016-04-27 08:00:58 -07:00
|
|
|
|
|
|
|
# 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()
|
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)
|
|
|
|
if done: break
|