2020-02-19 21:18:45 +01:00
|
|
|
import numpy as np
|
2020-02-11 00:22:07 +01:00
|
|
|
import unittest
|
|
|
|
|
2020-05-27 16:19:13 +02:00
|
|
|
import ray
|
2020-02-11 00:22:07 +01:00
|
|
|
import ray.rllib.agents.dqn as dqn
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.test_utils import (
|
|
|
|
check,
|
|
|
|
check_compute_single_action,
|
|
|
|
check_train_results,
|
|
|
|
framework_iterator,
|
|
|
|
)
|
2020-02-11 00:22:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestDQN(unittest.TestCase):
|
2020-05-27 16:19:13 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
|
|
|
ray.init()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls) -> None:
|
|
|
|
ray.shutdown()
|
|
|
|
|
2020-02-11 00:22:07 +01:00
|
|
|
def test_dqn_compilation(self):
|
2020-04-06 20:56:16 +02:00
|
|
|
"""Test whether a DQNTrainer can be built on all frameworks."""
|
2020-02-11 00:22:07 +01:00
|
|
|
config = dqn.DEFAULT_CONFIG.copy()
|
2020-05-27 16:19:13 +02:00
|
|
|
config["num_workers"] = 2
|
2021-08-31 12:21:49 +02:00
|
|
|
|
2020-05-27 16:19:13 +02:00
|
|
|
num_iterations = 1
|
2020-02-11 00:22:07 +01:00
|
|
|
|
2021-11-02 12:10:17 +01:00
|
|
|
for _ in framework_iterator(config, with_eager_tracing=True):
|
2020-05-30 22:48:34 +02:00
|
|
|
# Double-dueling DQN.
|
2020-07-25 09:29:24 +02:00
|
|
|
print("Double-dueling")
|
2020-04-06 20:56:16 +02:00
|
|
|
plain_config = config.copy()
|
|
|
|
trainer = dqn.DQNTrainer(config=plain_config, env="CartPole-v0")
|
|
|
|
for i in range(num_iterations):
|
|
|
|
results = trainer.train()
|
2021-09-30 16:39:05 +02:00
|
|
|
check_train_results(results)
|
2020-04-06 20:56:16 +02:00
|
|
|
print(results)
|
|
|
|
|
2020-06-13 17:51:50 +02:00
|
|
|
check_compute_single_action(trainer)
|
2020-07-11 22:06:35 +02:00
|
|
|
trainer.stop()
|
2020-05-08 16:31:31 +02:00
|
|
|
|
2020-04-03 21:24:25 +02:00
|
|
|
# Rainbow.
|
2020-07-25 09:29:24 +02:00
|
|
|
print("Rainbow")
|
2020-04-03 21:24:25 +02:00
|
|
|
rainbow_config = config.copy()
|
|
|
|
rainbow_config["num_atoms"] = 10
|
|
|
|
rainbow_config["noisy"] = True
|
|
|
|
rainbow_config["double_q"] = True
|
|
|
|
rainbow_config["dueling"] = True
|
|
|
|
rainbow_config["n_step"] = 5
|
|
|
|
trainer = dqn.DQNTrainer(config=rainbow_config, env="CartPole-v0")
|
|
|
|
for i in range(num_iterations):
|
|
|
|
results = trainer.train()
|
2021-09-30 16:39:05 +02:00
|
|
|
check_train_results(results)
|
2020-04-03 21:24:25 +02:00
|
|
|
print(results)
|
|
|
|
|
2020-06-13 17:51:50 +02:00
|
|
|
check_compute_single_action(trainer)
|
2021-08-31 12:21:49 +02:00
|
|
|
|
2020-07-11 22:06:35 +02:00
|
|
|
trainer.stop()
|
2020-05-08 16:31:31 +02:00
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
def test_dqn_exploration_and_soft_q_config(self):
|
|
|
|
"""Tests, whether a DQN Agent outputs exploration/softmaxed actions."""
|
|
|
|
config = dqn.DEFAULT_CONFIG.copy()
|
|
|
|
config["num_workers"] = 0 # Run locally.
|
|
|
|
config["env_config"] = {"is_slippery": False, "map_name": "4x4"}
|
|
|
|
obs = np.array(0)
|
|
|
|
|
|
|
|
# Test against all frameworks.
|
2020-04-06 20:56:16 +02:00
|
|
|
for _ in framework_iterator(config):
|
2020-02-19 21:18:45 +01:00
|
|
|
# Default EpsilonGreedy setup.
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
trainer = dqn.DQNTrainer(config=config, env="FrozenLake-v1")
|
2020-02-19 21:18:45 +01:00
|
|
|
# Setting explore=False should always return the same action.
|
2021-06-30 12:32:11 +02:00
|
|
|
a_ = trainer.compute_single_action(obs, explore=False)
|
2020-02-19 21:18:45 +01:00
|
|
|
for _ in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
a = trainer.compute_single_action(obs, explore=False)
|
2020-02-19 21:18:45 +01:00
|
|
|
check(a, a_)
|
|
|
|
# explore=None (default: explore) should return different actions.
|
|
|
|
actions = []
|
|
|
|
for _ in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
actions.append(trainer.compute_single_action(obs))
|
2020-02-19 21:18:45 +01:00
|
|
|
check(np.std(actions), 0.0, false=True)
|
2020-07-11 22:06:35 +02:00
|
|
|
trainer.stop()
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
# Low softmax temperature. Behaves like argmax
|
|
|
|
# (but no epsilon exploration).
|
2022-01-29 18:41:57 -08:00
|
|
|
config["exploration_config"] = {"type": "SoftQ", "temperature": 0.000001}
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
trainer = dqn.DQNTrainer(config=config, env="FrozenLake-v1")
|
2020-02-19 21:18:45 +01:00
|
|
|
# Due to the low temp, always expect the same action.
|
2021-06-30 12:32:11 +02:00
|
|
|
actions = [trainer.compute_single_action(obs)]
|
2020-02-19 21:18:45 +01:00
|
|
|
for _ in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
actions.append(trainer.compute_single_action(obs))
|
2020-04-06 20:56:16 +02:00
|
|
|
check(np.std(actions), 0.0, decimals=3)
|
2020-07-11 22:06:35 +02:00
|
|
|
trainer.stop()
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
# Higher softmax temperature.
|
|
|
|
config["exploration_config"]["temperature"] = 1.0
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
trainer = dqn.DQNTrainer(config=config, env="FrozenLake-v1")
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
# Even with the higher temperature, if we set explore=False, we
|
|
|
|
# should expect the same actions always.
|
2021-06-30 12:32:11 +02:00
|
|
|
a_ = trainer.compute_single_action(obs, explore=False)
|
2020-02-19 21:18:45 +01:00
|
|
|
for _ in range(50):
|
2021-06-30 12:32:11 +02:00
|
|
|
a = trainer.compute_single_action(obs, explore=False)
|
2020-02-19 21:18:45 +01:00
|
|
|
check(a, a_)
|
|
|
|
|
|
|
|
# Due to the higher temp, expect different actions avg'ing
|
|
|
|
# around 1.5.
|
|
|
|
actions = []
|
|
|
|
for _ in range(300):
|
2021-06-30 12:32:11 +02:00
|
|
|
actions.append(trainer.compute_single_action(obs))
|
2020-02-19 21:18:45 +01:00
|
|
|
check(np.std(actions), 0.0, false=True)
|
2020-07-11 22:06:35 +02:00
|
|
|
trainer.stop()
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
# With Random exploration.
|
|
|
|
config["exploration_config"] = {"type": "Random"}
|
|
|
|
config["explore"] = True
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
trainer = dqn.DQNTrainer(config=config, env="FrozenLake-v1")
|
2020-02-19 21:18:45 +01:00
|
|
|
actions = []
|
|
|
|
for _ in range(300):
|
2021-06-30 12:32:11 +02:00
|
|
|
actions.append(trainer.compute_single_action(obs))
|
2020-02-19 21:18:45 +01:00
|
|
|
check(np.std(actions), 0.0, false=True)
|
2020-07-11 22:06:35 +02:00
|
|
|
trainer.stop()
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-03-29 00:16:30 +01:00
|
|
|
import pytest
|
|
|
|
import sys
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2020-03-29 00:16:30 +01:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|