2020-10-07 21:59:14 +02:00
|
|
|
from gym.spaces import Space
|
|
|
|
from typing import Optional
|
|
|
|
|
2020-02-11 00:22:07 +01:00
|
|
|
from ray.rllib.utils.exploration.epsilon_greedy import EpsilonGreedy
|
|
|
|
from ray.rllib.utils.schedules import ConstantSchedule
|
|
|
|
|
|
|
|
|
|
|
|
class PerWorkerEpsilonGreedy(EpsilonGreedy):
|
|
|
|
"""A per-worker epsilon-greedy class for distributed algorithms.
|
|
|
|
|
|
|
|
Sets the epsilon schedules of individual workers to a constant:
|
|
|
|
0.4 ^ (1 + [worker-index] / float([num-workers] - 1) * 7)
|
|
|
|
See Ape-X paper.
|
|
|
|
"""
|
|
|
|
|
2020-10-07 21:59:14 +02:00
|
|
|
def __init__(self, action_space: Space, *, framework: str,
|
|
|
|
num_workers: Optional[int], worker_index: Optional[int],
|
2020-03-01 20:53:35 +01:00
|
|
|
**kwargs):
|
2020-03-04 13:00:37 -08:00
|
|
|
"""Create a PerWorkerEpsilonGreedy exploration class.
|
|
|
|
|
2020-02-11 00:22:07 +01:00
|
|
|
Args:
|
|
|
|
action_space (Space): The gym action space used by the environment.
|
|
|
|
num_workers (Optional[int]): The overall number of workers used.
|
|
|
|
worker_index (Optional[int]): The index of the Worker using this
|
|
|
|
Exploration.
|
|
|
|
framework (Optional[str]): One of None, "tf", "torch".
|
|
|
|
"""
|
|
|
|
epsilon_schedule = None
|
|
|
|
# Use a fixed, different epsilon per worker. See: Ape-X paper.
|
2020-03-10 11:14:14 -07:00
|
|
|
assert worker_index <= num_workers, (worker_index, num_workers)
|
2020-02-11 00:22:07 +01:00
|
|
|
if num_workers > 0:
|
2020-04-23 12:39:19 -07:00
|
|
|
if worker_index > 0:
|
|
|
|
# From page 5 of https://arxiv.org/pdf/1803.00933.pdf
|
|
|
|
alpha, eps, i = 7, 0.4, worker_index - 1
|
2020-07-16 14:55:50 +02:00
|
|
|
num_workers_minus_1 = float(num_workers - 1) \
|
|
|
|
if num_workers > 1 else 1.0
|
2020-03-10 11:14:14 -07:00
|
|
|
epsilon_schedule = ConstantSchedule(
|
2020-07-16 14:55:50 +02:00
|
|
|
eps**(1 + (i / num_workers_minus_1) * alpha),
|
2020-04-23 12:39:19 -07:00
|
|
|
framework=framework)
|
2020-02-11 00:22:07 +01:00
|
|
|
# Local worker should have zero exploration so that eval
|
|
|
|
# rollouts run properly.
|
|
|
|
else:
|
2020-03-10 11:14:14 -07:00
|
|
|
epsilon_schedule = ConstantSchedule(0.0, framework=framework)
|
2020-02-11 00:22:07 +01:00
|
|
|
|
|
|
|
super().__init__(
|
2020-03-01 20:53:35 +01:00
|
|
|
action_space,
|
|
|
|
epsilon_schedule=epsilon_schedule,
|
2020-02-11 00:22:07 +01:00
|
|
|
framework=framework,
|
2020-03-10 11:14:14 -07:00
|
|
|
num_workers=num_workers,
|
|
|
|
worker_index=worker_index,
|
2020-03-01 20:53:35 +01:00
|
|
|
**kwargs)
|