2020-03-01 20:53:35 +01:00
|
|
|
import numpy as np
|
|
|
|
import unittest
|
|
|
|
|
2021-03-17 08:18:15 +01:00
|
|
|
import ray
|
2020-03-01 20:53:35 +01:00
|
|
|
import ray.rllib.agents.ddpg.td3 as td3
|
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.test_utils import (
|
|
|
|
check,
|
|
|
|
check_compute_single_action,
|
|
|
|
check_train_results,
|
|
|
|
framework_iterator,
|
|
|
|
)
|
2020-03-01 20:53:35 +01:00
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
2020-03-01 20:53:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestTD3(unittest.TestCase):
|
2021-03-17 08:18:15 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
|
|
|
ray.init()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls) -> None:
|
|
|
|
ray.shutdown()
|
|
|
|
|
2020-03-01 20:53:35 +01:00
|
|
|
def test_td3_compilation(self):
|
|
|
|
"""Test whether a TD3Trainer can be built with both frameworks."""
|
|
|
|
config = td3.TD3_DEFAULT_CONFIG.copy()
|
|
|
|
config["num_workers"] = 0 # Run locally.
|
|
|
|
|
|
|
|
# Test against all frameworks.
|
2021-11-02 12:10:17 +01:00
|
|
|
for _ in framework_iterator(config, with_eager_tracing=True):
|
[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
|
|
|
trainer = td3.TD3Trainer(config=config, env="Pendulum-v1")
|
2020-07-08 16:12:20 +02:00
|
|
|
num_iterations = 1
|
2020-03-01 20:53:35 +01:00
|
|
|
for i in range(num_iterations):
|
|
|
|
results = trainer.train()
|
2021-09-30 16:39:05 +02:00
|
|
|
check_train_results(results)
|
2020-03-01 20:53:35 +01:00
|
|
|
print(results)
|
2020-06-13 17:51:50 +02:00
|
|
|
check_compute_single_action(trainer)
|
2020-07-08 16:12:20 +02:00
|
|
|
trainer.stop()
|
2020-03-01 20:53:35 +01:00
|
|
|
|
|
|
|
def test_td3_exploration_and_with_random_prerun(self):
|
|
|
|
"""Tests TD3's Exploration (w/ random actions for n timesteps)."""
|
|
|
|
config = td3.TD3_DEFAULT_CONFIG.copy()
|
|
|
|
config["num_workers"] = 0 # Run locally.
|
|
|
|
obs = np.array([0.0, 0.1, -0.1])
|
|
|
|
|
|
|
|
# Test against all frameworks.
|
2021-11-02 12:10:17 +01:00
|
|
|
for _ in framework_iterator(config, with_eager_tracing=True):
|
2020-06-30 10:13:20 +02:00
|
|
|
lcl_config = config.copy()
|
2020-03-01 20:53:35 +01:00
|
|
|
# Default GaussianNoise setup.
|
[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
|
|
|
trainer = td3.TD3Trainer(config=lcl_config, env="Pendulum-v1")
|
2020-03-01 20:53:35 +01:00
|
|
|
# Setting explore=False should always return the same action.
|
2021-06-30 12:32:11 +02:00
|
|
|
a_ = trainer.compute_single_action(obs, explore=False)
|
2020-10-06 20:28:16 +02:00
|
|
|
self.assertEqual(trainer.get_policy().global_timestep, 1)
|
|
|
|
for i in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
a = trainer.compute_single_action(obs, explore=False)
|
2020-10-06 20:28:16 +02:00
|
|
|
self.assertEqual(trainer.get_policy().global_timestep, i + 2)
|
2020-03-01 20:53:35 +01:00
|
|
|
check(a, a_)
|
|
|
|
# explore=None (default: explore) should return different actions.
|
|
|
|
actions = []
|
2020-10-06 20:28:16 +02:00
|
|
|
for i in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
actions.append(trainer.compute_single_action(obs))
|
2020-10-06 20:28:16 +02:00
|
|
|
self.assertEqual(trainer.get_policy().global_timestep, i + 52)
|
2020-03-01 20:53:35 +01:00
|
|
|
check(np.std(actions), 0.0, false=True)
|
2020-06-30 10:13:20 +02:00
|
|
|
trainer.stop()
|
2020-03-01 20:53:35 +01:00
|
|
|
|
|
|
|
# Check randomness at beginning.
|
2020-06-30 10:13:20 +02:00
|
|
|
lcl_config["exploration_config"] = {
|
2020-03-01 20:53:35 +01:00
|
|
|
# Act randomly at beginning ...
|
|
|
|
"random_timesteps": 30,
|
|
|
|
# Then act very closely to deterministic actions thereafter.
|
|
|
|
"stddev": 0.001,
|
|
|
|
"initial_scale": 0.001,
|
|
|
|
"final_scale": 0.001,
|
|
|
|
}
|
[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
|
|
|
trainer = td3.TD3Trainer(config=lcl_config, env="Pendulum-v1")
|
2020-10-06 20:28:16 +02:00
|
|
|
# ts=0 (get a deterministic action as per explore=False).
|
2022-01-29 18:41:57 -08:00
|
|
|
deterministic_action = trainer.compute_single_action(obs, explore=False)
|
2020-10-06 20:28:16 +02:00
|
|
|
self.assertEqual(trainer.get_policy().global_timestep, 1)
|
|
|
|
# ts=1-29 (in random window).
|
2020-03-01 20:53:35 +01:00
|
|
|
random_a = []
|
2020-10-06 20:28:16 +02:00
|
|
|
for i in range(1, 30):
|
2022-01-29 18:41:57 -08:00
|
|
|
random_a.append(trainer.compute_single_action(obs, explore=True))
|
2020-10-06 20:28:16 +02:00
|
|
|
self.assertEqual(trainer.get_policy().global_timestep, i + 1)
|
2020-03-01 20:53:35 +01:00
|
|
|
check(random_a[-1], deterministic_action, false=True)
|
2021-06-30 12:32:11 +02:00
|
|
|
self.assertTrue(np.std(random_a) > 0.3)
|
2020-03-01 20:53:35 +01:00
|
|
|
|
|
|
|
# ts > 30 (a=deterministic_action + scale * N[0,1])
|
2020-10-06 20:28:16 +02:00
|
|
|
for i in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
a = trainer.compute_single_action(obs, explore=True)
|
2020-10-06 20:28:16 +02:00
|
|
|
self.assertEqual(trainer.get_policy().global_timestep, i + 31)
|
2020-03-01 20:53:35 +01:00
|
|
|
check(a, deterministic_action, rtol=0.1)
|
|
|
|
|
|
|
|
# ts >> 30 (BUT: explore=False -> expect deterministic action).
|
2020-10-06 20:28:16 +02:00
|
|
|
for i in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
a = trainer.compute_single_action(obs, explore=False)
|
2020-10-06 20:28:16 +02:00
|
|
|
self.assertEqual(trainer.get_policy().global_timestep, i + 81)
|
2020-03-01 20:53:35 +01:00
|
|
|
check(a, deterministic_action)
|
2020-06-30 10:13:20 +02:00
|
|
|
trainer.stop()
|
2020-03-01 20:53:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-03-12 04:39:47 +01:00
|
|
|
import pytest
|
|
|
|
import sys
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|