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
|
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-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."""
|
2021-04-27 02:36:04 -04:00
|
|
|
config = a3c.a2c.A2C_DEFAULT_CONFIG.copy()
|
2020-05-27 16:19:13 +02:00
|
|
|
config["num_workers"] = 2
|
|
|
|
config["num_envs_per_worker"] = 2
|
|
|
|
|
|
|
|
num_iterations = 1
|
|
|
|
|
|
|
|
# Test against all frameworks.
|
2021-03-17 08:18:15 +01:00
|
|
|
for _ in framework_iterator(config):
|
2020-05-27 16:19:13 +02:00
|
|
|
for env in ["PongDeterministic-v0"]:
|
|
|
|
trainer = a3c.A2CTrainer(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)
|
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
|
|
|
|
2020-03-12 00:54:08 -07:00
|
|
|
def test_a2c_exec_impl(ray_start_regular):
|
2020-05-27 16:19:13 +02:00
|
|
|
config = {"min_iter_time_s": 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
|
|
|
|
2020-03-12 00:54:08 -07:00
|
|
|
def test_a2c_exec_impl_microbatch(ray_start_regular):
|
2020-05-27 16:19:13 +02:00
|
|
|
config = {
|
|
|
|
"min_iter_time_s": 0,
|
|
|
|
"microbatch_size": 10,
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|