2020-03-10 11:14:14 -07:00
|
|
|
import pytest
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
|
|
|
import ray.rllib.agents.dqn.apex as apex
|
2021-09-08 14:29:40 -07:00
|
|
|
from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.metrics.learner_info import LEARNER_INFO, LEARNER_STATS_KEY
|
|
|
|
from ray.rllib.utils.test_utils import (
|
|
|
|
check,
|
|
|
|
check_compute_single_action,
|
|
|
|
check_train_results,
|
|
|
|
framework_iterator,
|
|
|
|
)
|
2020-03-10 11:14:14 -07:00
|
|
|
|
|
|
|
|
2020-05-04 09:36:27 +02:00
|
|
|
class TestApexDQN(unittest.TestCase):
|
2020-03-10 11:14:14 -07:00
|
|
|
def setUp(self):
|
|
|
|
ray.init(num_cpus=4)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ray.shutdown()
|
|
|
|
|
2020-05-11 20:24:43 -07:00
|
|
|
def test_apex_zero_workers(self):
|
2022-05-23 12:15:45 +02:00
|
|
|
config = (
|
|
|
|
apex.ApexConfig()
|
|
|
|
.rollouts(num_rollout_workers=0)
|
|
|
|
.resources(num_gpus=0)
|
|
|
|
.training(
|
|
|
|
replay_buffer_config={
|
|
|
|
"learning_starts": 1000,
|
|
|
|
},
|
|
|
|
optimizer={
|
|
|
|
"num_replay_buffer_shards": 1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.reporting(
|
|
|
|
min_sample_timesteps_per_reporting=100,
|
|
|
|
min_time_s_per_reporting=1,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-07-11 22:06:35 +02:00
|
|
|
for _ in framework_iterator(config):
|
2022-05-23 12:15:45 +02:00
|
|
|
trainer = config.build(env="CartPole-v0")
|
2021-09-30 16:39:05 +02:00
|
|
|
results = trainer.train()
|
|
|
|
check_train_results(results)
|
|
|
|
print(results)
|
2020-05-27 16:19:13 +02:00
|
|
|
trainer.stop()
|
2020-05-11 20:24:43 -07:00
|
|
|
|
2020-05-04 09:36:27 +02:00
|
|
|
def test_apex_dqn_compilation_and_per_worker_epsilon_values(self):
|
2020-04-20 10:03:25 +02:00
|
|
|
"""Test whether an APEX-DQNTrainer can be built on all frameworks."""
|
2022-05-23 12:15:45 +02:00
|
|
|
config = (
|
|
|
|
apex.ApexConfig()
|
|
|
|
.rollouts(num_rollout_workers=3)
|
|
|
|
.resources(num_gpus=0)
|
|
|
|
.training(
|
|
|
|
replay_buffer_config={
|
|
|
|
"learning_starts": 1000,
|
|
|
|
},
|
|
|
|
optimizer={
|
|
|
|
"num_replay_buffer_shards": 1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.reporting(
|
|
|
|
min_sample_timesteps_per_reporting=100,
|
|
|
|
min_time_s_per_reporting=1,
|
|
|
|
)
|
|
|
|
)
|
2020-04-06 20:56:16 +02:00
|
|
|
|
2021-11-05 16:10:00 +01:00
|
|
|
for _ in framework_iterator(config, with_eager_tracing=True):
|
2022-05-23 12:15:45 +02:00
|
|
|
trainer = config.build(env="CartPole-v0")
|
2020-04-20 10:03:25 +02:00
|
|
|
|
|
|
|
# Test per-worker epsilon distribution.
|
2020-04-06 20:56:16 +02:00
|
|
|
infos = trainer.workers.foreach_policy(
|
2022-01-29 18:41:57 -08:00
|
|
|
lambda p, _: p.get_exploration_state()
|
|
|
|
)
|
2020-05-04 09:36:27 +02:00
|
|
|
expected = [0.4, 0.016190862, 0.00065536]
|
|
|
|
check([i["cur_epsilon"] for i in infos], [0.0] + expected)
|
2020-03-10 11:14:14 -07:00
|
|
|
|
2020-06-13 17:51:50 +02:00
|
|
|
check_compute_single_action(trainer)
|
2020-05-08 16:31:31 +02:00
|
|
|
|
2021-05-20 09:27:03 +02:00
|
|
|
for i in range(2):
|
2021-09-30 16:39:05 +02:00
|
|
|
results = trainer.train()
|
|
|
|
check_train_results(results)
|
|
|
|
print(results)
|
2020-04-23 12:39:19 -07:00
|
|
|
|
2020-05-04 09:36:27 +02:00
|
|
|
# Test again per-worker epsilon distribution
|
|
|
|
# (should not have changed).
|
|
|
|
infos = trainer.workers.foreach_policy(
|
2022-01-29 18:41:57 -08:00
|
|
|
lambda p, _: p.get_exploration_state()
|
|
|
|
)
|
2020-05-04 09:36:27 +02:00
|
|
|
check([i["cur_epsilon"] for i in infos], [0.0] + expected)
|
|
|
|
|
2020-04-23 12:39:19 -07:00
|
|
|
trainer.stop()
|
2020-04-20 10:03:25 +02:00
|
|
|
|
2021-09-08 14:29:40 -07:00
|
|
|
def test_apex_lr_schedule(self):
|
2022-05-23 12:15:45 +02:00
|
|
|
config = (
|
|
|
|
apex.ApexConfig()
|
|
|
|
.rollouts(
|
|
|
|
num_rollout_workers=1,
|
|
|
|
rollout_fragment_length=5,
|
|
|
|
)
|
|
|
|
.resources(num_gpus=0)
|
|
|
|
.training(
|
|
|
|
train_batch_size=10,
|
|
|
|
optimizer={
|
|
|
|
"num_replay_buffer_shards": 1,
|
|
|
|
# This makes sure learning schedule is checked every 10 timesteps.
|
|
|
|
"max_weight_sync_delay": 10,
|
|
|
|
},
|
|
|
|
replay_buffer_config={
|
|
|
|
"no_local_replay_buffer": True,
|
|
|
|
"type": "MultiAgentPrioritizedReplayBuffer",
|
|
|
|
"learning_starts": 10,
|
|
|
|
"capacity": 100,
|
|
|
|
"prioritized_replay_alpha": 0.6,
|
|
|
|
# Beta parameter for sampling from prioritized replay buffer.
|
|
|
|
"prioritized_replay_beta": 0.4,
|
|
|
|
# Epsilon to add to the TD errors when updating priorities.
|
|
|
|
"prioritized_replay_eps": 1e-6,
|
|
|
|
},
|
|
|
|
# Initial lr, doesn't really matter because of the schedule below.
|
|
|
|
lr=0.2,
|
|
|
|
lr_schedule=[[0, 0.2], [100, 0.001]],
|
|
|
|
)
|
|
|
|
.reporting(
|
|
|
|
min_sample_timesteps_per_reporting=10,
|
|
|
|
# 0 metrics reporting delay, this makes sure timestep,
|
|
|
|
# which lr depends on, is updated after each worker rollout.
|
|
|
|
min_time_s_per_reporting=0,
|
|
|
|
)
|
|
|
|
)
|
2021-09-08 14:29:40 -07:00
|
|
|
|
|
|
|
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()
|
2022-01-29 18:41:57 -08:00
|
|
|
return results["info"][LEARNER_INFO][DEFAULT_POLICY_ID][LEARNER_STATS_KEY][
|
|
|
|
"cur_lr"
|
|
|
|
]
|
2021-09-08 14:29:40 -07:00
|
|
|
|
|
|
|
for _ in framework_iterator(config):
|
2022-05-23 12:15:45 +02:00
|
|
|
trainer = config.build(env="CartPole-v0")
|
2021-09-08 14:29:40 -07:00
|
|
|
|
2022-05-17 10:31:07 +02:00
|
|
|
lr = _step_n_times(trainer, 5) # 50 timesteps
|
2021-11-02 08:52:56 -07:00
|
|
|
# Close to 0.2
|
|
|
|
self.assertGreaterEqual(lr, 0.1)
|
2021-09-08 14:29:40 -07:00
|
|
|
|
2021-11-02 08:52:56 -07:00
|
|
|
lr = _step_n_times(trainer, 20) # 200 timesteps
|
|
|
|
# LR Annealed to 0.001
|
|
|
|
self.assertLessEqual(lr, 0.0011)
|
2021-09-08 14:29:40 -07:00
|
|
|
|
|
|
|
trainer.stop()
|
|
|
|
|
2020-03-10 11:14:14 -07:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2020-03-10 11:14:14 -07:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|