2020-04-23 09:11:12 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
|
|
|
import ray.rllib.agents.ppo as ppo
|
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-04-23 09:11:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestAPPO(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ray.init()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ray.shutdown()
|
|
|
|
|
|
|
|
def test_appo_compilation(self):
|
|
|
|
"""Test whether an APPOTrainer can be built with both frameworks."""
|
|
|
|
config = ppo.appo.DEFAULT_CONFIG.copy()
|
|
|
|
config["num_workers"] = 1
|
|
|
|
num_iterations = 2
|
|
|
|
|
2020-09-02 14:03:01 +02:00
|
|
|
for _ in framework_iterator(config):
|
2020-10-27 10:00:24 +01:00
|
|
|
print("w/o v-trace")
|
2020-04-23 09:11:12 +02:00
|
|
|
_config = config.copy()
|
2020-11-28 01:25:47 +01:00
|
|
|
_config["vtrace"] = False
|
2020-04-23 09:11:12 +02:00
|
|
|
trainer = ppo.APPOTrainer(config=_config, env="CartPole-v0")
|
|
|
|
for i in range(num_iterations):
|
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-09-02 14:03:01 +02:00
|
|
|
trainer.stop()
|
2020-04-23 09:11:12 +02:00
|
|
|
|
2020-10-27 10:00:24 +01:00
|
|
|
print("w/ v-trace")
|
2020-04-23 09:11:12 +02:00
|
|
|
_config = config.copy()
|
|
|
|
_config["vtrace"] = True
|
|
|
|
trainer = ppo.APPOTrainer(config=_config, env="CartPole-v0")
|
|
|
|
for i in range(num_iterations):
|
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-09-02 14:03:01 +02:00
|
|
|
trainer.stop()
|
2020-04-23 09:11:12 +02:00
|
|
|
|
2021-09-21 22:00:14 +02:00
|
|
|
def test_appo_two_tf_optimizers(self):
|
|
|
|
config = ppo.appo.DEFAULT_CONFIG.copy()
|
|
|
|
config["num_workers"] = 1
|
|
|
|
|
|
|
|
# Not explicitly setting this should cause a warning, but not fail.
|
|
|
|
# config["_tf_policy_handles_more_than_one_loss"] = True
|
|
|
|
config["_separate_vf_optimizer"] = True
|
|
|
|
config["_lr_vf"] = 0.0002
|
|
|
|
|
|
|
|
# Make sure we have two completely separate models for policy and
|
|
|
|
# value function.
|
|
|
|
config["model"]["vf_share_layers"] = False
|
|
|
|
num_iterations = 2
|
|
|
|
|
|
|
|
# Only supported for tf so far.
|
2021-10-10 12:19:47 +02:00
|
|
|
for _ in framework_iterator(config, frameworks=("tf2", "tf")):
|
2021-09-21 22:00:14 +02:00
|
|
|
trainer = ppo.APPOTrainer(config=config, env="CartPole-v0")
|
|
|
|
for i in range(num_iterations):
|
2021-09-30 16:39:05 +02:00
|
|
|
results = trainer.train()
|
|
|
|
check_train_results(results)
|
|
|
|
print(results)
|
2021-09-21 22:00:14 +02:00
|
|
|
check_compute_single_action(trainer)
|
|
|
|
trainer.stop()
|
|
|
|
|
2020-04-23 09:11:12 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|