2020-05-27 16:19:13 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
|
|
|
import ray.rllib.agents.a3c as a3c
|
2021-10-25 10:39:35 +03:00
|
|
|
from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
|
|
|
|
from ray.rllib.utils.metrics.learner_info import LEARNER_INFO, \
|
|
|
|
LEARNER_STATS_KEY
|
2020-06-13 17:51:50 +02:00
|
|
|
from ray.rllib.utils.test_utils import check_compute_single_action, \
|
2021-09-30 16:39:05 +02:00
|
|
|
check_train_results, framework_iterator
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestA3C(unittest.TestCase):
|
|
|
|
"""Sanity tests for A2C exec impl."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
ray.init(num_cpus=4)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ray.shutdown()
|
|
|
|
|
|
|
|
def test_a3c_compilation(self):
|
|
|
|
"""Test whether an A3CTrainer can be built with both frameworks."""
|
|
|
|
config = a3c.DEFAULT_CONFIG.copy()
|
|
|
|
config["num_workers"] = 2
|
|
|
|
config["num_envs_per_worker"] = 2
|
|
|
|
|
|
|
|
num_iterations = 1
|
|
|
|
|
|
|
|
# Test against all frameworks.
|
2021-01-18 19:29:03 +01:00
|
|
|
for _ in framework_iterator(config):
|
[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
|
|
|
for env in ["CartPole-v1", "Pendulum-v1", "PongDeterministic-v0"]:
|
2020-09-05 13:14:24 +02:00
|
|
|
print("env={}".format(env))
|
2021-08-16 22:01:01 +02:00
|
|
|
config["model"]["use_lstm"] = env == "CartPole-v1"
|
2020-05-27 16:19:13 +02:00
|
|
|
trainer = a3c.A3CTrainer(config=config, env=env)
|
|
|
|
for i in range(num_iterations):
|
|
|
|
results = trainer.train()
|
2021-09-30 16:39:05 +02:00
|
|
|
check_train_results(results)
|
2020-05-27 16:19:13 +02:00
|
|
|
print(results)
|
2021-08-16 22:01:01 +02:00
|
|
|
check_compute_single_action(
|
|
|
|
trainer, include_state=config["model"]["use_lstm"])
|
2020-07-08 16:12:20 +02:00
|
|
|
trainer.stop()
|
2020-05-27 16:19:13 +02:00
|
|
|
|
2021-10-25 10:39:35 +03:00
|
|
|
def test_a3c_entropy_coeff_schedule(self):
|
|
|
|
"""Test A3CTrainer entropy coeff schedule support."""
|
|
|
|
config = a3c.DEFAULT_CONFIG.copy()
|
|
|
|
config["num_workers"] = 1
|
|
|
|
config["num_envs_per_worker"] = 1
|
|
|
|
config["train_batch_size"] = 20
|
|
|
|
config["batch_mode"] = "truncate_episodes"
|
|
|
|
config["rollout_fragment_length"] = 10
|
|
|
|
config["timesteps_per_iteration"] = 20
|
|
|
|
# 0 metrics reporting delay, this makes sure timestep,
|
|
|
|
# which entropy coeff depends on, is updated after each worker rollout.
|
|
|
|
config["min_iter_time_s"] = 0
|
|
|
|
# Initial lr, doesn't really matter because of the schedule below.
|
|
|
|
config["entropy_coeff"] = 0.01
|
|
|
|
schedule = [
|
|
|
|
[0, 0.01],
|
|
|
|
[120, 0.0001],
|
|
|
|
]
|
|
|
|
config["entropy_coeff_schedule"] = schedule
|
|
|
|
|
|
|
|
def _step_n_times(trainer, n: int):
|
|
|
|
"""Step trainer n times.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
learning rate at the end of the execution.
|
|
|
|
"""
|
|
|
|
for _ in range(n):
|
|
|
|
results = trainer.train()
|
|
|
|
return results["info"][LEARNER_INFO][DEFAULT_POLICY_ID][
|
|
|
|
LEARNER_STATS_KEY]["entropy_coeff"]
|
|
|
|
|
|
|
|
# Test against all frameworks.
|
|
|
|
for _ in framework_iterator(config):
|
|
|
|
trainer = a3c.A3CTrainer(config=config, env="CartPole-v1")
|
|
|
|
|
2021-11-02 08:52:56 -07:00
|
|
|
coeff = _step_n_times(trainer, 1) # 20 timesteps
|
|
|
|
# Should be close to the starting coeff of 0.01
|
|
|
|
self.assertGreaterEqual(coeff, 0.005)
|
2021-10-25 10:39:35 +03:00
|
|
|
|
2021-11-02 08:52:56 -07:00
|
|
|
coeff = _step_n_times(trainer, 10) # 200 timesteps
|
|
|
|
# Should have annealed to the final coeff of 0.0001.
|
|
|
|
self.assertLessEqual(coeff, 0.00011)
|
2021-10-25 10:39:35 +03:00
|
|
|
|
|
|
|
trainer.stop()
|
|
|
|
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|