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__]))

[((243, 253), 'ray.init', 'ray.init', ([], {}), '()\n', (251, 253), False, 'import ray\n'), ((308, 322), 'ray.shutdown', 'ray.shutdown', ([], {}), '()\n', (320, 322), False, 'import ray\n'), ((455, 485), 'ray.rllib.agents.ppo.appo.DEFAULT_CONFIG.copy', 'ppo.appo.DEFAULT_CONFIG.copy', ([], {}), '()\n', (483, 485), True, 'import ray.rllib.agents.ppo as ppo\n'), ((565, 619), 'ray.rllib.utils.test_utils.framework_iterator', 'framework_iterator', (['config'], {'frameworks': "('torch', 'tf')"}), "(config, frameworks=('torch', 'tf'))\n", (583, 619), False, 'from ray.rllib.utils.test_utils import check_compute_single_action, framework_iterator\n'), ((1216, 1245), 'pytest.main', 'pytest.main', (["['-v', __file__]"], {}), "(['-v', __file__])\n", (1227, 1245), False, 'import pytest\n'), ((679, 729), 'ray.rllib.agents.ppo.APPOTrainer', 'ppo.APPOTrainer', ([], {'config': '_config', 'env': '"""CartPole-v0"""'}), "(config=_config, env='CartPole-v0')\n", (694, 729), True, 'import ray.rllib.agents.ppo as ppo\n'), ((825, 861), 'ray.rllib.utils.test_utils.check_compute_single_action', 'check_compute_single_action', (['trainer'], {}), '(trainer)\n', (852, 861), False, 'from ray.rllib.utils.test_utils import check_compute_single_action, framework_iterator\n'), ((958, 1008), 'ray.rllib.agents.ppo.APPOTrainer', 'ppo.APPOTrainer', ([], {'config': '_config', 'env': '"""CartPole-v0"""'}), "(config=_config, env='CartPole-v0')\n", (973, 1008), True, 'import ray.rllib.agents.ppo as ppo\n'), ((1104, 1140), 'ray.rllib.utils.test_utils.check_compute_single_action', 'check_compute_single_action', (['trainer'], {}), '(trainer)\n', (1131, 1140), False, 'from ray.rllib.utils.test_utils import check_compute_single_action, framework_iterator\n')]