2020-12-26 20:14:36 -05:00
|
|
|
import gym
|
|
|
|
from gym.spaces import Box, Dict, Discrete, MultiDiscrete, Tuple
|
|
|
|
import numpy as np
|
|
|
|
import unittest
|
|
|
|
|
2021-09-23 12:56:45 +02:00
|
|
|
import ray
|
|
|
|
import ray.rllib.agents.ppo as ppo
|
2020-12-26 20:14:36 -05:00
|
|
|
from ray.rllib.models.catalog import ModelCatalog
|
|
|
|
from ray.rllib.models.preprocessors import DictFlatteningPreprocessor, \
|
|
|
|
get_preprocessor, NoPreprocessor, TupleFlatteningPreprocessor, \
|
|
|
|
OneHotPreprocessor, AtariRamPreprocessor, GenericPixelPreprocessor
|
2021-09-23 12:56:45 +02:00
|
|
|
from ray.rllib.utils.test_utils import check, check_compute_single_action, \
|
2021-09-30 16:39:05 +02:00
|
|
|
check_train_results, framework_iterator
|
2020-12-26 20:14:36 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TestPreprocessors(unittest.TestCase):
|
2021-09-23 12:56:45 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
|
|
|
ray.init()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls) -> None:
|
|
|
|
ray.shutdown()
|
|
|
|
|
|
|
|
def test_preprocessing_disabled(self):
|
|
|
|
config = ppo.DEFAULT_CONFIG.copy()
|
|
|
|
|
|
|
|
config["env"] = "ray.rllib.examples.env.random_env.RandomEnv"
|
|
|
|
config["env_config"] = {
|
|
|
|
"config": {
|
|
|
|
"observation_space": Dict({
|
|
|
|
"a": Discrete(5),
|
|
|
|
"b": Dict({
|
|
|
|
"ba": Discrete(4),
|
|
|
|
"bb": Box(-1.0, 1.0, (2, 3), dtype=np.float32)
|
|
|
|
}),
|
|
|
|
"c": Tuple((MultiDiscrete([2, 3]), Discrete(1))),
|
|
|
|
"d": Box(-1.0, 1.0, (1, ), dtype=np.int32),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
# Set this to True to enforce no preprocessors being used.
|
|
|
|
# Complex observations now arrive directly in the model as
|
|
|
|
# structures of batches, e.g. {"a": tensor, "b": [tensor, tensor]}
|
|
|
|
# for obs-space=Dict(a=..., b=Tuple(..., ...)).
|
|
|
|
config["_disable_preprocessor_api"] = True
|
|
|
|
|
|
|
|
num_iterations = 1
|
|
|
|
# Only supported for tf so far.
|
|
|
|
for _ in framework_iterator(config):
|
|
|
|
trainer = ppo.PPOTrainer(config=config)
|
|
|
|
for i in range(num_iterations):
|
2021-09-30 16:39:05 +02:00
|
|
|
results = trainer.train()
|
|
|
|
check_train_results(results)
|
|
|
|
print(results)
|
2021-09-23 12:56:45 +02:00
|
|
|
check_compute_single_action(trainer)
|
|
|
|
trainer.stop()
|
|
|
|
|
2020-12-26 20:14:36 -05:00
|
|
|
def test_gym_preprocessors(self):
|
|
|
|
p1 = ModelCatalog.get_preprocessor(gym.make("CartPole-v0"))
|
|
|
|
self.assertEqual(type(p1), NoPreprocessor)
|
|
|
|
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
p2 = ModelCatalog.get_preprocessor(gym.make("FrozenLake-v1"))
|
2020-12-26 20:14:36 -05:00
|
|
|
self.assertEqual(type(p2), OneHotPreprocessor)
|
|
|
|
|
|
|
|
p3 = ModelCatalog.get_preprocessor(gym.make("MsPacman-ram-v0"))
|
|
|
|
self.assertEqual(type(p3), AtariRamPreprocessor)
|
|
|
|
|
|
|
|
p4 = ModelCatalog.get_preprocessor(gym.make("MsPacmanNoFrameskip-v4"))
|
|
|
|
self.assertEqual(type(p4), GenericPixelPreprocessor)
|
|
|
|
|
|
|
|
def test_tuple_preprocessor(self):
|
|
|
|
class TupleEnv:
|
|
|
|
def __init__(self):
|
|
|
|
self.observation_space = Tuple(
|
|
|
|
[Discrete(5),
|
|
|
|
Box(0, 5, shape=(3, ), dtype=np.float32)])
|
|
|
|
|
|
|
|
pp = ModelCatalog.get_preprocessor(TupleEnv())
|
|
|
|
self.assertTrue(isinstance(pp, TupleFlatteningPreprocessor))
|
|
|
|
self.assertEqual(pp.shape, (8, ))
|
|
|
|
self.assertEqual(
|
2021-11-02 12:10:17 +01:00
|
|
|
list(pp.transform((0, np.array([1, 2, 3], np.float32)))),
|
2020-12-26 20:14:36 -05:00
|
|
|
[float(x) for x in [1, 0, 0, 0, 0, 1, 2, 3]])
|
|
|
|
|
|
|
|
def test_dict_flattening_preprocessor(self):
|
|
|
|
space = Dict({
|
|
|
|
"a": Discrete(2),
|
|
|
|
"b": Tuple([Discrete(3), Box(-1.0, 1.0, (4, ))]),
|
|
|
|
})
|
|
|
|
pp = get_preprocessor(space)(space)
|
|
|
|
self.assertTrue(isinstance(pp, DictFlatteningPreprocessor))
|
|
|
|
self.assertEqual(pp.shape, (9, ))
|
|
|
|
check(
|
|
|
|
pp.transform({
|
|
|
|
"a": 1,
|
2021-11-02 12:10:17 +01:00
|
|
|
"b": (1, np.array([0.0, -0.5, 0.1, 0.6], np.float32))
|
2020-12-26 20:14:36 -05:00
|
|
|
}), [0.0, 1.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.1, 0.6])
|
|
|
|
|
|
|
|
def test_one_hot_preprocessor(self):
|
|
|
|
space = Discrete(5)
|
|
|
|
pp = get_preprocessor(space)(space)
|
|
|
|
self.assertTrue(isinstance(pp, OneHotPreprocessor))
|
|
|
|
self.assertTrue(pp.shape == (5, ))
|
|
|
|
check(pp.transform(3), [0.0, 0.0, 0.0, 1.0, 0.0])
|
|
|
|
check(pp.transform(0), [1.0, 0.0, 0.0, 0.0, 0.0])
|
|
|
|
|
|
|
|
space = MultiDiscrete([2, 3, 4])
|
|
|
|
pp = get_preprocessor(space)(space)
|
|
|
|
self.assertTrue(isinstance(pp, OneHotPreprocessor))
|
|
|
|
self.assertTrue(pp.shape == (9, ))
|
|
|
|
check(
|
|
|
|
pp.transform(np.array([1, 2, 0])),
|
|
|
|
[0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0])
|
|
|
|
check(
|
|
|
|
pp.transform(np.array([0, 1, 3])),
|
|
|
|
[1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
|
|
|
|
|
2021-01-21 15:36:11 +00:00
|
|
|
def test_nested_multidiscrete_one_hot_preprocessor(self):
|
|
|
|
space = Tuple((MultiDiscrete([2, 3, 4]), ))
|
|
|
|
pp = get_preprocessor(space)(space)
|
|
|
|
self.assertTrue(pp.shape == (9, ))
|
|
|
|
check(
|
|
|
|
pp.transform((np.array([1, 2, 0]), )),
|
|
|
|
[0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0])
|
|
|
|
check(
|
|
|
|
pp.transform((np.array([0, 1, 3]), )),
|
|
|
|
[1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
|
|
|
|
|
2020-12-26 20:14:36 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|