2020-02-19 16:07:37 -08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
2020-05-27 16:19:13 +02:00
|
|
|
import ray.rllib.agents.a3c as a3c
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.test_utils import (
|
|
|
|
check_compute_single_action,
|
|
|
|
check_train_results,
|
|
|
|
framework_iterator,
|
|
|
|
)
|
2020-02-19 16:07:37 -08:00
|
|
|
|
|
|
|
|
|
|
|
class TestA2C(unittest.TestCase):
|
2020-03-12 00:54:08 -07:00
|
|
|
"""Sanity tests for A2C exec impl."""
|
2020-02-19 16:07:37 -08:00
|
|
|
|
|
|
|
def setUp(self):
|
2020-05-27 16:19:13 +02:00
|
|
|
ray.init(num_cpus=4)
|
2020-02-19 16:07:37 -08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ray.shutdown()
|
|
|
|
|
2020-05-27 16:19:13 +02:00
|
|
|
def test_a2c_compilation(self):
|
|
|
|
"""Test whether an A2CTrainer can be built with both frameworks."""
|
2022-04-30 09:51:09 +02:00
|
|
|
config = a3c.A2CConfig().rollouts(num_rollout_workers=2, num_envs_per_worker=2)
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
num_iterations = 1
|
|
|
|
|
|
|
|
# Test against all frameworks.
|
2021-11-05 16:10:00 +01:00
|
|
|
for _ in framework_iterator(config, with_eager_tracing=True):
|
|
|
|
for env in ["CartPole-v0", "Pendulum-v1", "PongDeterministic-v0"]:
|
2022-04-30 09:51:09 +02:00
|
|
|
trainer = config.build(env=env)
|
2020-05-27 16:19:13 +02:00
|
|
|
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)
|
2020-06-13 17:51:50 +02:00
|
|
|
check_compute_single_action(trainer)
|
2020-07-08 16:12:20 +02:00
|
|
|
trainer.stop()
|
2020-05-27 16:19:13 +02:00
|
|
|
|
2022-02-10 13:44:22 +01:00
|
|
|
def test_a2c_exec_impl(self):
|
2022-04-30 09:51:09 +02:00
|
|
|
config = a3c.A2CConfig().reporting(min_time_s_per_reporting=0)
|
2020-07-08 16:12:20 +02:00
|
|
|
for _ in framework_iterator(config):
|
2020-05-27 16:19:13 +02:00
|
|
|
trainer = a3c.A2CTrainer(env="CartPole-v0", config=config)
|
2021-09-30 16:39:05 +02:00
|
|
|
results = trainer.train()
|
|
|
|
check_train_results(results)
|
|
|
|
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-02-19 16:07:37 -08:00
|
|
|
|
2022-02-10 13:44:22 +01:00
|
|
|
def test_a2c_exec_impl_microbatch(self):
|
2022-04-30 09:51:09 +02:00
|
|
|
config = (
|
|
|
|
a3c.A2CConfig()
|
|
|
|
.reporting(min_time_s_per_reporting=0)
|
|
|
|
.training(microbatch_size=10)
|
|
|
|
)
|
|
|
|
|
2020-07-08 16:12:20 +02:00
|
|
|
for _ in framework_iterator(config):
|
2022-04-30 09:51:09 +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-06-13 17:51:50 +02:00
|
|
|
check_compute_single_action(trainer)
|
2020-07-08 16:12:20 +02:00
|
|
|
trainer.stop()
|
2020-02-19 16:07:37 -08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2020-02-19 16:07:37 -08:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|