mirror of
https://github.com/vale981/ray
synced 2025-03-08 19:41:38 -05:00

* Exploration API (+EpsilonGreedy sub-class). * Exploration API (+EpsilonGreedy sub-class). * Cleanup/LINT. * Add `deterministic` to generic Trainer config (NOTE: this is still ignored by most Agents). * Add `error` option to deprecation_warning(). * WIP. * Bug fix: Get exploration-info for tf framework. Bug fix: Properly deprecate some DQN config keys. * WIP. * LINT. * WIP. * Split PerWorkerEpsilonGreedy out of EpsilonGreedy. Docstrings. * Fix bug in sampler.py in case Policy has self.exploration = None * Update rllib/agents/dqn/dqn.py Co-Authored-By: Eric Liang <ekhliang@gmail.com> * WIP. * Update rllib/agents/trainer.py Co-Authored-By: Eric Liang <ekhliang@gmail.com> * WIP. * Change requests. * LINT * In tune/utils/util.py::deep_update() Only keep deep_updat'ing if both original and value are dicts. If value is not a dict, set * Completely obsolete syn_replay_optimizer.py's parameters schedule_max_timesteps AND beta_annealing_fraction (replaced with prioritized_replay_beta_annealing_timesteps). * Update rllib/evaluation/worker_set.py Co-Authored-By: Eric Liang <ekhliang@gmail.com> * Review fixes. * Fix default value for DQN's exploration spec. * LINT * Fix recursion bug (wrong parent c'tor). * Do not pass timestep to get_exploration_info. * Update tf_policy.py * Fix some remaining issues with test cases and remove more deprecated DQN/APEX exploration configs. * Bug fix tf-action-dist * DDPG incompatibility bug fix with new DQN exploration handling (which is imported by DDPG). * Switch off exploration when getting action probs from off-policy-estimator's policy. * LINT * Fix test_checkpoint_restore.py. * Deprecate all SAC exploration (unused) configs. * Properly use `model.last_output()` everywhere. Instead of `model._last_output`. * WIP. * Take out set_epsilon from multi-agent-env test (not needed, decays anyway). * WIP. * Trigger re-test (flaky checkpoint-restore test). * WIP. * WIP. * Add test case for deterministic action sampling in PPO. * bug fix. * Added deterministic test cases for different Agents. * Fix problem with TupleActions in dynamic-tf-policy. * Separate supported_spaces tests so they can be run separately for easier debugging. * LINT. * Fix autoregressive_action_dist.py test case. * Re-test. * Fix. * Remove duplicate py_test rule from bazel. * LINT. * WIP. * WIP. * SAC fix. * SAC fix. * WIP. * WIP. * WIP. * FIX 2 examples tests. * WIP. * WIP. * WIP. * WIP. * WIP. * Fix. * LINT. * Renamed test file. * WIP. * Add unittest.main. * Make action_dist_class mandatory. * fix * FIX. * WIP. * WIP. * Fix. * Fix. * Fix explorations test case (contextlib cannot find its own nullcontext??). * Force torch to be installed for QMIX. * LINT. * Fix determine_tests_to_run.py. * Fix determine_tests_to_run.py. * WIP * Add Random exploration component to tests (fixed issue with "static-graph randomness" via py_function). * Add Random exploration component to tests (fixed issue with "static-graph randomness" via py_function). * Rename some stuff. * Rename some stuff. * WIP. * WIP. * Fix SAC. * Fix SAC. * Fix strange tf-error in ray core tests. * Fix strange ray-core tf-error in test_memory_scheduling test case. * Fix test_io.py. * LINT. * Update SAC yaml files' config. Co-authored-by: Eric Liang <ekhliang@gmail.com>
162 lines
5.2 KiB
Python
162 lines
5.2 KiB
Python
import numpy as np
|
|
from tensorflow.python.eager.context import eager_mode
|
|
import unittest
|
|
|
|
import ray
|
|
import ray.rllib.agents.a3c as a3c
|
|
import ray.rllib.agents.dqn as dqn
|
|
import ray.rllib.agents.impala as impala
|
|
import ray.rllib.agents.pg as pg
|
|
import ray.rllib.agents.ppo as ppo
|
|
import ray.rllib.agents.sac as sac
|
|
from ray.rllib.utils import check
|
|
|
|
|
|
def test_explorations(run,
|
|
env,
|
|
config,
|
|
dummy_obs,
|
|
prev_a=None,
|
|
expected_mean_action=None):
|
|
"""Calls an Agent's `compute_actions` with different `explore` options."""
|
|
|
|
config = config.copy()
|
|
if run not in [a3c.A3CTrainer]:
|
|
config["num_workers"] = 0
|
|
|
|
# Test all frameworks.
|
|
for fw in ["torch", "eager", "tf"]:
|
|
if fw == "torch" and \
|
|
run in [dqn.DQNTrainer, dqn.SimpleQTrainer,
|
|
impala.ImpalaTrainer, sac.SACTrainer]:
|
|
continue
|
|
print("Testing {} in framework={}".format(run, fw))
|
|
config["eager"] = (fw == "eager")
|
|
config["use_pytorch"] = (fw == "torch")
|
|
|
|
# Test for both the default Agent's exploration AND the `Random`
|
|
# exploration class.
|
|
for exploration in [None, "Random"]:
|
|
if exploration == "Random":
|
|
# TODO(sven): Random doesn't work for cont. action spaces
|
|
# or IMPALA yet.
|
|
if env == "Pendulum-v0" or run is impala.ImpalaTrainer:
|
|
continue
|
|
config["exploration_config"] = {"type": "Random"}
|
|
print("exploration={}".format(exploration or "default"))
|
|
|
|
eager_mode_ctx = eager_mode()
|
|
if fw == "eager":
|
|
eager_mode_ctx.__enter__()
|
|
|
|
trainer = run(config=config, env=env)
|
|
|
|
# Make sure all actions drawn are the same, given same
|
|
# observations.
|
|
actions = []
|
|
for _ in range(100):
|
|
actions.append(
|
|
trainer.compute_action(
|
|
observation=dummy_obs,
|
|
explore=False,
|
|
prev_action=prev_a,
|
|
prev_reward=1.0 if prev_a is not None else None))
|
|
check(actions[-1], actions[0])
|
|
|
|
# Make sure actions drawn are different
|
|
# (around some mean value), given constant observations.
|
|
actions = []
|
|
for _ in range(100):
|
|
actions.append(
|
|
trainer.compute_action(
|
|
observation=dummy_obs,
|
|
explore=True,
|
|
prev_action=prev_a,
|
|
prev_reward=1.0 if prev_a is not None else None))
|
|
check(
|
|
np.mean(actions),
|
|
expected_mean_action
|
|
if expected_mean_action is not None else 0.5,
|
|
atol=0.3)
|
|
# Check that the stddev is not 0.0 (values differ).
|
|
check(np.std(actions), 0.0, false=True)
|
|
|
|
if fw == "eager":
|
|
eager_mode_ctx.__exit__(None, None, None)
|
|
|
|
|
|
class TestExplorations(unittest.TestCase):
|
|
"""
|
|
Tests all Exploration components and the deterministic flag for
|
|
compute_action calls.
|
|
"""
|
|
ray.init(ignore_reinit_error=True)
|
|
|
|
def test_a2c(self):
|
|
test_explorations(
|
|
a3c.A2CTrainer,
|
|
"CartPole-v0",
|
|
a3c.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0, 0.0]),
|
|
prev_a=np.array(1))
|
|
|
|
def test_a3c(self):
|
|
test_explorations(
|
|
a3c.A3CTrainer,
|
|
"CartPole-v0",
|
|
a3c.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0, 0.0]),
|
|
prev_a=np.array(1))
|
|
|
|
def test_simple_dqn(self):
|
|
test_explorations(dqn.SimpleQTrainer, "CartPole-v0",
|
|
dqn.DEFAULT_CONFIG, np.array([0.0, 0.1, 0.0, 0.0]))
|
|
|
|
def test_dqn(self):
|
|
test_explorations(dqn.DQNTrainer, "CartPole-v0", dqn.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0, 0.0]))
|
|
|
|
def test_impala(self):
|
|
test_explorations(
|
|
impala.ImpalaTrainer,
|
|
"CartPole-v0",
|
|
impala.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0, 0.0]),
|
|
prev_a=np.array(0))
|
|
|
|
def test_pg(self):
|
|
test_explorations(
|
|
pg.PGTrainer,
|
|
"CartPole-v0",
|
|
pg.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0, 0.0]),
|
|
prev_a=np.array(1))
|
|
|
|
def test_ppo_discr(self):
|
|
test_explorations(
|
|
ppo.PPOTrainer,
|
|
"CartPole-v0",
|
|
ppo.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0, 0.0]),
|
|
prev_a=np.array(0))
|
|
|
|
def test_ppo_cont(self):
|
|
test_explorations(
|
|
ppo.PPOTrainer,
|
|
"Pendulum-v0",
|
|
ppo.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0]),
|
|
prev_a=np.array([0.0]),
|
|
expected_mean_action=0.0)
|
|
|
|
def test_sac(self):
|
|
test_explorations(
|
|
sac.SACTrainer,
|
|
"Pendulum-v0",
|
|
sac.DEFAULT_CONFIG,
|
|
np.array([0.0, 0.1, 0.0]),
|
|
expected_mean_action=0.0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|