2018-01-24 11:03:43 -08:00
|
|
|
import gym
|
2019-05-29 20:41:02 -07:00
|
|
|
from gym.spaces import Box, Discrete, Tuple, Dict, MultiDiscrete
|
2018-01-24 11:03:43 -08:00
|
|
|
from gym.envs.registration import EnvSpec
|
2018-03-06 08:31:02 +00:00
|
|
|
import numpy as np
|
2018-07-07 13:29:20 -07:00
|
|
|
import sys
|
2020-02-19 21:18:45 +01:00
|
|
|
import unittest
|
|
|
|
import traceback
|
2018-01-24 11:03:43 -08:00
|
|
|
|
|
|
|
import ray
|
2020-02-22 23:19:49 +01:00
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
2018-12-21 03:44:34 +09:00
|
|
|
from ray.rllib.agents.registry import get_agent_class
|
2019-11-05 11:36:29 -08:00
|
|
|
from ray.rllib.models.tf.fcnet_v2 import FullyConnectedNetwork as FCNetV2
|
|
|
|
from ray.rllib.models.tf.visionnet_v2 import VisionNetwork as VisionNetV2
|
2020-03-02 19:53:19 +01:00
|
|
|
from ray.rllib.models.torch.visionnet import VisionNetwork as TorchVisionNetV2
|
|
|
|
from ray.rllib.models.torch.fcnet import FullyConnectedNetwork as TorchFCNetV2
|
2020-02-19 21:18:45 +01:00
|
|
|
from ray.rllib.tests.test_multi_agent_env import MultiCartpole, \
|
|
|
|
MultiMountainCar
|
2018-01-24 11:03:43 -08:00
|
|
|
from ray.rllib.utils.error import UnsupportedSpaceException
|
|
|
|
from ray.tune.registry import register_env
|
2020-02-22 23:19:49 +01:00
|
|
|
tf = try_import_tf()
|
2018-01-24 11:03:43 -08:00
|
|
|
|
|
|
|
ACTION_SPACES_TO_TEST = {
|
|
|
|
"discrete": Discrete(5),
|
2018-10-20 15:21:22 -07:00
|
|
|
"vector": Box(-1.0, 1.0, (5, ), dtype=np.float32),
|
2019-07-07 15:06:41 -07:00
|
|
|
"vector2": Box(-1.0, 1.0, (
|
|
|
|
5,
|
|
|
|
5,
|
|
|
|
), dtype=np.float32),
|
2019-05-29 20:41:02 -07:00
|
|
|
"multidiscrete": MultiDiscrete([1, 2, 3, 4]),
|
2018-10-20 15:21:22 -07:00
|
|
|
"tuple": Tuple(
|
2018-08-15 10:19:41 -07:00
|
|
|
[Discrete(2),
|
|
|
|
Discrete(3),
|
2018-10-20 15:21:22 -07:00
|
|
|
Box(-1.0, 1.0, (5, ), dtype=np.float32)]),
|
2018-01-24 11:03:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OBSERVATION_SPACES_TO_TEST = {
|
|
|
|
"discrete": Discrete(5),
|
2018-10-20 15:21:22 -07:00
|
|
|
"vector": Box(-1.0, 1.0, (5, ), dtype=np.float32),
|
2019-09-19 12:10:31 -07:00
|
|
|
"vector2": Box(-1.0, 1.0, (5, 5), dtype=np.float32),
|
2018-10-20 15:21:22 -07:00
|
|
|
"image": Box(-1.0, 1.0, (84, 84, 1), dtype=np.float32),
|
|
|
|
"atari": Box(-1.0, 1.0, (210, 160, 3), dtype=np.float32),
|
|
|
|
"tuple": Tuple([Discrete(10),
|
|
|
|
Box(-1.0, 1.0, (5, ), dtype=np.float32)]),
|
|
|
|
"dict": Dict({
|
|
|
|
"task": Discrete(10),
|
|
|
|
"position": Box(-1.0, 1.0, (5, ), dtype=np.float32),
|
|
|
|
}),
|
2018-01-24 11:03:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-20 15:21:22 -07:00
|
|
|
def make_stub_env(action_space, obs_space, check_action_bounds):
|
2018-01-24 11:03:43 -08:00
|
|
|
class StubEnv(gym.Env):
|
|
|
|
def __init__(self):
|
|
|
|
self.action_space = action_space
|
|
|
|
self.observation_space = obs_space
|
2018-03-06 08:31:02 +00:00
|
|
|
self.spec = EnvSpec("StubEnv-v0")
|
2018-01-24 11:03:43 -08:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
sample = self.observation_space.sample()
|
|
|
|
return sample
|
|
|
|
|
|
|
|
def step(self, action):
|
2018-10-20 15:21:22 -07:00
|
|
|
if check_action_bounds and not self.action_space.contains(action):
|
|
|
|
raise ValueError("Illegal action for {}: {}".format(
|
|
|
|
self.action_space, action))
|
|
|
|
if (isinstance(self.action_space, Tuple)
|
|
|
|
and len(action) != len(self.action_space.spaces)):
|
|
|
|
raise ValueError("Illegal action for {}: {}".format(
|
|
|
|
self.action_space, action))
|
2018-01-24 11:03:43 -08:00
|
|
|
return self.observation_space.sample(), 1, True, {}
|
|
|
|
|
|
|
|
return StubEnv
|
|
|
|
|
|
|
|
|
2019-05-29 20:41:02 -07:00
|
|
|
def check_support(alg, config, stats, check_bounds=False, name=None):
|
2019-07-07 15:06:41 -07:00
|
|
|
covered_a = set()
|
|
|
|
covered_o = set()
|
2019-08-23 02:21:11 -04:00
|
|
|
config["log_level"] = "ERROR"
|
2020-02-19 21:18:45 +01:00
|
|
|
first_error = None
|
2020-03-02 19:53:19 +01:00
|
|
|
torch = config.get("use_pytorch", False)
|
2018-01-24 11:03:43 -08:00
|
|
|
for a_name, action_space in ACTION_SPACES_TO_TEST.items():
|
|
|
|
for o_name, obs_space in OBSERVATION_SPACES_TO_TEST.items():
|
2020-03-02 19:53:19 +01:00
|
|
|
print("=== Testing {} (torch={}) A={} S={} ===".format(
|
|
|
|
alg, torch, action_space, obs_space))
|
2018-10-20 15:21:22 -07:00
|
|
|
stub_env = make_stub_env(action_space, obs_space, check_bounds)
|
2018-06-19 22:47:00 -07:00
|
|
|
register_env("stub_env", lambda c: stub_env())
|
2018-01-24 11:03:43 -08:00
|
|
|
stat = "ok"
|
|
|
|
a = None
|
|
|
|
try:
|
2019-07-07 15:06:41 -07:00
|
|
|
if a_name in covered_a and o_name in covered_o:
|
|
|
|
stat = "skip" # speed up tests by avoiding full grid
|
2020-03-02 19:53:19 +01:00
|
|
|
# TODO(sven): Add necessary torch distributions.
|
|
|
|
elif torch and a_name in ["tuple", "multidiscrete"]:
|
|
|
|
stat = "unsupported"
|
2019-07-07 15:06:41 -07:00
|
|
|
else:
|
|
|
|
a = get_agent_class(alg)(config=config, env="stub_env")
|
2020-02-19 21:18:45 +01:00
|
|
|
if alg not in ["DDPG", "ES", "ARS", "SAC"]:
|
2019-11-05 11:36:29 -08:00
|
|
|
if o_name in ["atari", "image"]:
|
2020-03-02 19:53:19 +01:00
|
|
|
if torch:
|
|
|
|
assert isinstance(
|
|
|
|
a.get_policy().model, TorchVisionNetV2)
|
|
|
|
else:
|
|
|
|
assert isinstance(
|
|
|
|
a.get_policy().model, VisionNetV2)
|
2019-11-05 11:36:29 -08:00
|
|
|
elif o_name in ["vector", "vector2"]:
|
2020-03-02 19:53:19 +01:00
|
|
|
if torch:
|
|
|
|
assert isinstance(
|
|
|
|
a.get_policy().model, TorchFCNetV2)
|
|
|
|
else:
|
|
|
|
assert isinstance(
|
|
|
|
a.get_policy().model, FCNetV2)
|
2019-07-07 15:06:41 -07:00
|
|
|
a.train()
|
|
|
|
covered_a.add(a_name)
|
|
|
|
covered_o.add(o_name)
|
2018-10-24 16:30:00 -07:00
|
|
|
except UnsupportedSpaceException:
|
2018-01-24 11:03:43 -08:00
|
|
|
stat = "unsupported"
|
|
|
|
except Exception as e:
|
|
|
|
stat = "ERROR"
|
|
|
|
print(e)
|
|
|
|
print(traceback.format_exc())
|
2020-02-19 21:18:45 +01:00
|
|
|
first_error = first_error if first_error is not None else e
|
2018-01-24 11:03:43 -08:00
|
|
|
finally:
|
|
|
|
if a:
|
|
|
|
try:
|
|
|
|
a.stop()
|
|
|
|
except Exception as e:
|
|
|
|
print("Ignoring error stopping agent", e)
|
|
|
|
pass
|
|
|
|
print(stat)
|
|
|
|
print()
|
2019-05-29 20:41:02 -07:00
|
|
|
stats[name or alg, a_name, o_name] = stat
|
2018-01-24 11:03:43 -08:00
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
# If anything happened, raise error.
|
|
|
|
if first_error is not None:
|
|
|
|
raise first_error
|
|
|
|
|
2018-01-24 11:03:43 -08:00
|
|
|
|
2018-11-14 14:14:07 -08:00
|
|
|
def check_support_multiagent(alg, config):
|
|
|
|
register_env("multi_mountaincar", lambda _: MultiMountainCar(2))
|
|
|
|
register_env("multi_cartpole", lambda _: MultiCartpole(2))
|
2019-09-19 12:10:31 -07:00
|
|
|
config["log_level"] = "ERROR"
|
2019-01-06 19:37:35 -08:00
|
|
|
if "DDPG" in alg:
|
2018-11-14 14:14:07 -08:00
|
|
|
a = get_agent_class(alg)(config=config, env="multi_mountaincar")
|
|
|
|
else:
|
|
|
|
a = get_agent_class(alg)(config=config, env="multi_cartpole")
|
|
|
|
try:
|
|
|
|
a.train()
|
|
|
|
finally:
|
|
|
|
a.stop()
|
|
|
|
|
|
|
|
|
2018-01-24 11:03:43 -08:00
|
|
|
class ModelSupportedSpaces(unittest.TestCase):
|
2020-02-19 21:18:45 +01:00
|
|
|
stats = {}
|
|
|
|
|
2018-11-14 14:14:07 -08:00
|
|
|
def setUp(self):
|
2020-02-19 21:18:45 +01:00
|
|
|
ray.init(num_cpus=4, ignore_reinit_error=True)
|
2018-11-14 14:14:07 -08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ray.shutdown()
|
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
def test_a3c(self):
|
2020-03-02 19:53:19 +01:00
|
|
|
config = {
|
|
|
|
"num_workers": 1,
|
|
|
|
"optimizer": {
|
|
|
|
"grads_per_step": 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check_support("A3C", config, self.stats, check_bounds=True)
|
|
|
|
config["use_pytorch"] = True
|
|
|
|
check_support("A3C", config, self.stats, check_bounds=True)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
def test_appo(self):
|
|
|
|
check_support("APPO", {"num_gpus": 0, "vtrace": False}, self.stats)
|
2019-05-29 20:41:02 -07:00
|
|
|
check_support(
|
|
|
|
"APPO", {
|
|
|
|
"num_gpus": 0,
|
|
|
|
"vtrace": True
|
2020-02-19 21:18:45 +01:00
|
|
|
},
|
|
|
|
self.stats,
|
|
|
|
name="APPO-vt")
|
|
|
|
|
|
|
|
def test_ars(self):
|
|
|
|
check_support(
|
|
|
|
"ARS", {
|
|
|
|
"num_workers": 1,
|
|
|
|
"noise_size": 10000000,
|
|
|
|
"num_rollouts": 1,
|
|
|
|
"rollouts_used": 1
|
|
|
|
}, self.stats)
|
|
|
|
|
|
|
|
def test_ddpg(self):
|
2018-11-24 00:56:50 -08:00
|
|
|
check_support(
|
|
|
|
"DDPG", {
|
2020-03-01 20:53:35 +01:00
|
|
|
"exploration_config": {
|
|
|
|
"ou_base_scale": 100.0
|
|
|
|
},
|
2019-04-26 17:49:53 -07:00
|
|
|
"timesteps_per_iteration": 1,
|
|
|
|
"use_state_preprocessor": True,
|
2018-11-24 00:56:50 -08:00
|
|
|
},
|
2020-02-19 21:18:45 +01:00
|
|
|
self.stats,
|
2018-11-24 00:56:50 -08:00
|
|
|
check_bounds=True)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
def test_dqn(self):
|
|
|
|
check_support("DQN", {"timesteps_per_iteration": 1}, self.stats)
|
|
|
|
|
|
|
|
def test_es(self):
|
2018-12-03 19:55:25 -08:00
|
|
|
check_support(
|
2020-02-19 21:18:45 +01:00
|
|
|
"ES", {
|
2018-12-03 19:55:25 -08:00
|
|
|
"num_workers": 1,
|
2020-02-19 21:18:45 +01:00
|
|
|
"noise_size": 10000000,
|
|
|
|
"episodes_per_batch": 1,
|
|
|
|
"train_batch_size": 1
|
|
|
|
}, self.stats)
|
|
|
|
|
|
|
|
def test_impala(self):
|
|
|
|
check_support("IMPALA", {"num_gpus": 0}, self.stats)
|
|
|
|
|
|
|
|
def test_ppo(self):
|
2020-03-02 19:53:19 +01:00
|
|
|
config = {
|
|
|
|
"num_workers": 1,
|
|
|
|
"num_sgd_iter": 1,
|
|
|
|
"train_batch_size": 10,
|
|
|
|
"sample_batch_size": 10,
|
|
|
|
"sgd_minibatch_size": 1,
|
|
|
|
}
|
|
|
|
check_support("PPO", config, self.stats, check_bounds=True)
|
|
|
|
config["use_pytorch"] = True
|
|
|
|
check_support("PPO", config, self.stats, check_bounds=True)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
def test_pg(self):
|
2020-03-02 19:53:19 +01:00
|
|
|
config = {
|
|
|
|
"num_workers": 1,
|
|
|
|
"optimizer": {}
|
|
|
|
}
|
|
|
|
check_support("PG", config, self.stats, check_bounds=True)
|
|
|
|
config["use_pytorch"] = True
|
|
|
|
check_support("PG", config, self.stats, check_bounds=True)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
def test_sac(self):
|
|
|
|
check_support("SAC", {}, self.stats, check_bounds=True)
|
|
|
|
|
|
|
|
def test_a3c_multiagent(self):
|
|
|
|
check_support_multiagent("A3C", {
|
|
|
|
"num_workers": 1,
|
|
|
|
"optimizer": {
|
|
|
|
"grads_per_step": 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
def test_apex_multiagent(self):
|
2019-01-06 19:37:35 -08:00
|
|
|
check_support_multiagent(
|
|
|
|
"APEX", {
|
|
|
|
"num_workers": 2,
|
|
|
|
"timesteps_per_iteration": 1000,
|
|
|
|
"num_gpus": 0,
|
|
|
|
"min_iter_time_s": 1,
|
|
|
|
"learning_starts": 1000,
|
|
|
|
"target_network_update_freq": 100,
|
|
|
|
})
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
def test_apex_ddpg_multiagent(self):
|
2019-01-06 19:37:35 -08:00
|
|
|
check_support_multiagent(
|
|
|
|
"APEX_DDPG", {
|
|
|
|
"num_workers": 2,
|
|
|
|
"timesteps_per_iteration": 1000,
|
|
|
|
"num_gpus": 0,
|
|
|
|
"min_iter_time_s": 1,
|
|
|
|
"learning_starts": 1000,
|
|
|
|
"target_network_update_freq": 100,
|
2019-04-26 17:49:53 -07:00
|
|
|
"use_state_preprocessor": True,
|
2019-01-06 19:37:35 -08:00
|
|
|
})
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
def test_ddpg_multiagent(self):
|
|
|
|
check_support_multiagent("DDPG", {
|
|
|
|
"timesteps_per_iteration": 1,
|
|
|
|
"use_state_preprocessor": True,
|
2018-11-14 14:14:07 -08:00
|
|
|
})
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
def test_dqn_multiagent(self):
|
|
|
|
check_support_multiagent("DQN", {"timesteps_per_iteration": 1})
|
|
|
|
|
|
|
|
def test_impala_multiagent(self):
|
|
|
|
check_support_multiagent("IMPALA", {"num_gpus": 0})
|
|
|
|
|
|
|
|
def test_pg_multiagent(self):
|
|
|
|
check_support_multiagent("PG", {"num_workers": 1, "optimizer": {}})
|
|
|
|
|
|
|
|
def test_ppo_multiagent(self):
|
2018-11-14 14:14:07 -08:00
|
|
|
check_support_multiagent(
|
|
|
|
"PPO", {
|
|
|
|
"num_workers": 1,
|
|
|
|
"num_sgd_iter": 1,
|
|
|
|
"train_batch_size": 10,
|
|
|
|
"sample_batch_size": 10,
|
|
|
|
"sgd_minibatch_size": 1,
|
|
|
|
})
|
|
|
|
|
2018-01-24 11:03:43 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-07-07 13:29:20 -07:00
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == "--smoke":
|
|
|
|
ACTION_SPACES_TO_TEST = {
|
|
|
|
"discrete": Discrete(5),
|
|
|
|
}
|
|
|
|
OBSERVATION_SPACES_TO_TEST = {
|
2018-07-19 15:30:36 -07:00
|
|
|
"vector": Box(0.0, 1.0, (5, ), dtype=np.float32),
|
2018-07-07 13:29:20 -07:00
|
|
|
"atari": Box(0.0, 1.0, (210, 160, 3), dtype=np.float32),
|
|
|
|
}
|
2018-01-24 11:03:43 -08:00
|
|
|
unittest.main(verbosity=2)
|