2017-12-28 13:19:04 -08:00
|
|
|
import gym
|
|
|
|
import numpy as np
|
|
|
|
import unittest
|
2018-01-05 21:32:41 -08:00
|
|
|
from gym.spaces import Box, Discrete, Tuple
|
2017-12-28 13:19:04 -08:00
|
|
|
|
|
|
|
import ray
|
|
|
|
|
2019-08-06 18:13:16 +00:00
|
|
|
from ray.rllib.models import ModelCatalog, MODEL_DEFAULTS
|
2017-12-28 13:19:04 -08:00
|
|
|
from ray.rllib.models.model import Model
|
2019-08-06 18:13:16 +00:00
|
|
|
from ray.rllib.models.tf.tf_action_dist import TFActionDistribution
|
2018-07-19 15:30:36 -07:00
|
|
|
from ray.rllib.models.preprocessors import (NoPreprocessor, OneHotPreprocessor,
|
|
|
|
Preprocessor)
|
2019-07-27 02:08:16 -07:00
|
|
|
from ray.rllib.models.tf.fcnet_v1 import FullyConnectedNetwork
|
|
|
|
from ray.rllib.models.tf.visionnet_v1 import VisionNetwork
|
2019-05-16 22:12:07 -07:00
|
|
|
from ray.rllib.utils import try_import_tf
|
|
|
|
|
|
|
|
tf = try_import_tf()
|
2017-12-28 13:19:04 -08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomPreprocessor(Preprocessor):
|
2018-10-20 15:21:22 -07:00
|
|
|
def _init_shape(self, obs_space, options):
|
2019-03-25 19:00:33 -04:00
|
|
|
return [1]
|
2017-12-28 13:19:04 -08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomPreprocessor2(Preprocessor):
|
2018-10-20 15:21:22 -07:00
|
|
|
def _init_shape(self, obs_space, options):
|
2019-03-25 19:00:33 -04:00
|
|
|
return [1]
|
2017-12-28 13:19:04 -08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomModel(Model):
|
2018-06-27 22:51:04 -07:00
|
|
|
def _build_layers(self, *args):
|
2018-10-20 15:21:22 -07:00
|
|
|
return tf.constant([[0] * 5]), None
|
2017-12-28 13:19:04 -08:00
|
|
|
|
|
|
|
|
2019-08-06 18:13:16 +00:00
|
|
|
class CustomActionDistribution(TFActionDistribution):
|
|
|
|
@staticmethod
|
|
|
|
def required_model_output_shape(action_space, model_config=None):
|
|
|
|
custom_options = model_config["custom_options"] or {}
|
|
|
|
if custom_options is not None and custom_options.get("output_dim"):
|
|
|
|
return custom_options.get("output_dim")
|
|
|
|
return action_space.shape
|
|
|
|
|
|
|
|
def _build_sample_op(self):
|
|
|
|
custom_options = self.model_config["custom_options"]
|
|
|
|
if "output_dim" in custom_options:
|
|
|
|
output_shape = tf.concat(
|
|
|
|
[tf.shape(self.inputs)[:1], custom_options["output_dim"]],
|
|
|
|
axis=0)
|
|
|
|
else:
|
|
|
|
output_shape = tf.shape(self.inputs)
|
|
|
|
return tf.random_uniform(output_shape)
|
|
|
|
|
|
|
|
|
2017-12-28 13:19:04 -08:00
|
|
|
class ModelCatalogTest(unittest.TestCase):
|
|
|
|
def tearDown(self):
|
2018-07-12 14:00:00 -05:00
|
|
|
ray.shutdown()
|
2017-12-28 13:19:04 -08:00
|
|
|
|
|
|
|
def testGymPreprocessors(self):
|
2018-06-19 22:47:00 -07:00
|
|
|
p1 = ModelCatalog.get_preprocessor(gym.make("CartPole-v0"))
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(type(p1), NoPreprocessor)
|
2017-12-28 13:19:04 -08:00
|
|
|
|
2018-06-19 22:47:00 -07:00
|
|
|
p2 = ModelCatalog.get_preprocessor(gym.make("FrozenLake-v0"))
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(type(p2), OneHotPreprocessor)
|
|
|
|
|
|
|
|
def testTuplePreprocessor(self):
|
|
|
|
ray.init()
|
|
|
|
|
|
|
|
class TupleEnv(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.observation_space = Tuple(
|
2018-07-19 15:30:36 -07:00
|
|
|
[Discrete(5),
|
2019-04-07 16:11:50 -07:00
|
|
|
Box(0, 5, shape=(3, ), dtype=np.float32)])
|
2018-07-19 15:30:36 -07:00
|
|
|
|
2018-06-19 22:47:00 -07:00
|
|
|
p1 = ModelCatalog.get_preprocessor(TupleEnv())
|
2018-07-19 15:30:36 -07:00
|
|
|
self.assertEqual(p1.shape, (8, ))
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(
|
2019-04-07 16:11:50 -07:00
|
|
|
list(p1.transform((0, np.array([1, 2, 3])))),
|
2018-01-05 21:32:41 -08:00
|
|
|
[float(x) for x in [1, 0, 0, 0, 0, 1, 2, 3]])
|
2017-12-28 13:19:04 -08:00
|
|
|
|
|
|
|
def testCustomPreprocessor(self):
|
|
|
|
ray.init()
|
|
|
|
ModelCatalog.register_custom_preprocessor("foo", CustomPreprocessor)
|
|
|
|
ModelCatalog.register_custom_preprocessor("bar", CustomPreprocessor2)
|
|
|
|
env = gym.make("CartPole-v0")
|
2018-06-19 22:47:00 -07:00
|
|
|
p1 = ModelCatalog.get_preprocessor(env, {"custom_preprocessor": "foo"})
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(str(type(p1)), str(CustomPreprocessor))
|
2018-06-19 22:47:00 -07:00
|
|
|
p2 = ModelCatalog.get_preprocessor(env, {"custom_preprocessor": "bar"})
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(str(type(p2)), str(CustomPreprocessor2))
|
2018-06-19 22:47:00 -07:00
|
|
|
p3 = ModelCatalog.get_preprocessor(env)
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(type(p3), NoPreprocessor)
|
2017-09-16 15:53:19 -07:00
|
|
|
|
2017-12-28 13:19:04 -08:00
|
|
|
def testDefaultModels(self):
|
|
|
|
ray.init()
|
2017-09-16 15:53:19 -07:00
|
|
|
|
2017-12-28 13:19:04 -08:00
|
|
|
with tf.variable_scope("test1"):
|
2018-10-20 15:21:22 -07:00
|
|
|
p1 = ModelCatalog.get_model({
|
2018-12-03 01:24:36 -08:00
|
|
|
"obs": tf.zeros((10, 3), dtype=tf.float32)
|
2019-03-10 04:23:12 +01:00
|
|
|
}, Box(0, 1, shape=(3, ), dtype=np.float32), Discrete(5), 5, {})
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(type(p1), FullyConnectedNetwork)
|
2017-09-16 15:53:19 -07:00
|
|
|
|
2017-12-28 13:19:04 -08:00
|
|
|
with tf.variable_scope("test2"):
|
2018-10-20 15:21:22 -07:00
|
|
|
p2 = ModelCatalog.get_model({
|
2018-12-03 01:24:36 -08:00
|
|
|
"obs": tf.zeros((10, 84, 84, 3), dtype=tf.float32)
|
2019-03-10 04:23:12 +01:00
|
|
|
}, Box(0, 1, shape=(84, 84, 3), dtype=np.float32), Discrete(5), 5,
|
|
|
|
{})
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(type(p2), VisionNetwork)
|
2017-09-16 15:53:19 -07:00
|
|
|
|
2017-12-28 13:19:04 -08:00
|
|
|
def testCustomModel(self):
|
|
|
|
ray.init()
|
|
|
|
ModelCatalog.register_custom_model("foo", CustomModel)
|
2018-10-20 15:21:22 -07:00
|
|
|
p1 = ModelCatalog.get_model({
|
|
|
|
"obs": tf.constant([1, 2, 3])
|
2019-03-10 04:23:12 +01:00
|
|
|
}, Box(0, 1, shape=(3, ), dtype=np.float32), Discrete(5), 5,
|
2018-10-20 15:21:22 -07:00
|
|
|
{"custom_model": "foo"})
|
2018-01-05 21:32:41 -08:00
|
|
|
self.assertEqual(str(type(p1)), str(CustomModel))
|
2017-10-23 23:16:52 -07:00
|
|
|
|
2019-08-06 18:13:16 +00:00
|
|
|
def testCustomActionDistribution(self):
|
|
|
|
ray.init()
|
|
|
|
# registration
|
|
|
|
ModelCatalog.register_custom_action_dist("test",
|
|
|
|
CustomActionDistribution)
|
|
|
|
action_space = Box(0, 1, shape=(5, 3), dtype=np.float32)
|
|
|
|
|
|
|
|
# test retrieving it
|
|
|
|
model_config = MODEL_DEFAULTS.copy()
|
|
|
|
model_config["custom_action_dist"] = "test"
|
|
|
|
dist_cls, param_shape = ModelCatalog.get_action_dist(
|
|
|
|
action_space, model_config)
|
|
|
|
self.assertEqual(str(dist_cls), str(CustomActionDistribution))
|
|
|
|
self.assertEqual(param_shape, action_space.shape)
|
|
|
|
|
|
|
|
# test the class works as a distribution
|
|
|
|
dist_input = tf.placeholder(tf.float32, (None, ) + param_shape)
|
|
|
|
dist = dist_cls(dist_input, model_config=model_config)
|
|
|
|
self.assertEqual(dist.sample().shape[1:], dist_input.shape[1:])
|
|
|
|
self.assertIsInstance(dist.sample(), tf.Tensor)
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
dist.entropy()
|
|
|
|
|
|
|
|
# test passing the options to it
|
|
|
|
model_config["custom_options"].update({"output_dim": (3, )})
|
|
|
|
dist_cls, param_shape = ModelCatalog.get_action_dist(
|
|
|
|
action_space, model_config)
|
|
|
|
self.assertEqual(param_shape, (3, ))
|
|
|
|
dist_input = tf.placeholder(tf.float32, (None, ) + param_shape)
|
|
|
|
dist = dist_cls(dist_input, model_config=model_config)
|
|
|
|
self.assertEqual(dist.sample().shape[1:], dist_input.shape[1:])
|
|
|
|
self.assertIsInstance(dist.sample(), tf.Tensor)
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
dist.entropy()
|
|
|
|
|
2017-10-23 23:16:52 -07:00
|
|
|
|
2017-12-28 13:19:04 -08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main(verbosity=2)
|