import unittest

import ray
import ray.rllib.agents.ppo as ppo
from ray.rllib.utils.test_utils import check_compute_single_action, \
    framework_iterator


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

        for _ in framework_iterator(config, frameworks=("torch", "tf")):
            _config = config.copy()
            trainer = ppo.APPOTrainer(config=_config, env="CartPole-v0")
            for i in range(num_iterations):
                print(trainer.train())
            check_compute_single_action(trainer)

            _config = config.copy()
            _config["vtrace"] = True
            trainer = ppo.APPOTrainer(config=_config, env="CartPole-v0")
            for i in range(num_iterations):
                print(trainer.train())
            check_compute_single_action(trainer)


if __name__ == "__main__":
    import pytest
    import sys
    sys.exit(pytest.main(["-v", __file__]))

[((12, 8, 12, 18), 'ray.init', 'ray.init', ({}, {}), '()', False, 'import ray\n'), ((16, 8, 16, 22), 'ray.shutdown', 'ray.shutdown', ({}, {}), '()', False, 'import ray\n'), ((20, 17, 20, 47), 'ray.rllib.agents.ppo.appo.DEFAULT_CONFIG.copy', 'ppo.appo.DEFAULT_CONFIG.copy', ({}, {}), '()', True, 'import ray.rllib.agents.ppo as ppo\n'), ((24, 17, 24, 71), 'ray.rllib.utils.test_utils.framework_iterator', 'framework_iterator', (), '', False, 'from ray.rllib.utils.test_utils import check_compute_single_action, framework_iterator\n'), ((42, 13, 42, 42), 'pytest.main', 'pytest.main', ({(42, 25, 42, 41): "['-v', __file__]"}, {}), "(['-v', __file__])", False, 'import pytest\n'), ((26, 22, 26, 72), 'ray.rllib.agents.ppo.APPOTrainer', 'ppo.APPOTrainer', (), '', True, 'import ray.rllib.agents.ppo as ppo\n'), ((29, 12, 29, 48), 'ray.rllib.utils.test_utils.check_compute_single_action', 'check_compute_single_action', ({(29, 40, 29, 47): 'trainer'}, {}), '(trainer)', False, 'from ray.rllib.utils.test_utils import check_compute_single_action, framework_iterator\n'), ((33, 22, 33, 72), 'ray.rllib.agents.ppo.APPOTrainer', 'ppo.APPOTrainer', (), '', True, 'import ray.rllib.agents.ppo as ppo\n'), ((36, 12, 36, 48), 'ray.rllib.utils.test_utils.check_compute_single_action', 'check_compute_single_action', ({(36, 40, 36, 47): 'trainer'}, {}), '(trainer)', False, 'from ray.rllib.utils.test_utils import check_compute_single_action, framework_iterator\n')]