2020-03-01 20:53:35 +01:00
|
|
|
from gym.spaces import Discrete, MultiDiscrete, Tuple
|
2020-03-04 13:00:37 -08:00
|
|
|
from typing import Union
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
from ray.rllib.utils.annotations import override
|
|
|
|
from ray.rllib.utils.exploration.exploration import Exploration
|
|
|
|
from ray.rllib.utils.framework import try_import_tf, try_import_torch, \
|
2020-03-04 13:00:37 -08:00
|
|
|
tf_function, TensorType
|
2020-02-19 21:18:45 +01:00
|
|
|
from ray.rllib.utils.tuple_actions import TupleActions
|
2020-03-04 13:00:37 -08:00
|
|
|
from ray.rllib.models.modelv2 import ModelV2
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
tf = try_import_tf()
|
|
|
|
torch, _ = try_import_torch()
|
|
|
|
|
|
|
|
|
|
|
|
class Random(Exploration):
|
|
|
|
"""A random action selector (deterministic/greedy for explore=False).
|
|
|
|
|
|
|
|
If explore=True, returns actions randomly from `self.action_space` (via
|
|
|
|
Space.sample()).
|
|
|
|
If explore=False, returns the greedy/max-likelihood action.
|
|
|
|
"""
|
|
|
|
|
2020-03-01 20:53:35 +01:00
|
|
|
def __init__(self, action_space, *, framework="tf", **kwargs):
|
2020-02-19 21:18:45 +01:00
|
|
|
"""Initialize a Random Exploration object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
action_space (Space): The gym action space used by the environment.
|
|
|
|
framework (Optional[str]): One of None, "tf", "torch".
|
|
|
|
"""
|
|
|
|
super().__init__(
|
|
|
|
action_space=action_space, framework=framework, **kwargs)
|
|
|
|
|
2020-03-01 20:53:35 +01:00
|
|
|
# Determine py_func types, depending on our action-space.
|
|
|
|
if isinstance(self.action_space, (Discrete, MultiDiscrete)) or \
|
|
|
|
(isinstance(self.action_space, Tuple) and
|
|
|
|
isinstance(self.action_space[0], (Discrete, MultiDiscrete))):
|
|
|
|
self.dtype_sample, self.dtype = (tf.int64, tf.int32)
|
|
|
|
else:
|
|
|
|
self.dtype_sample, self.dtype = (tf.float64, tf.float32)
|
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
@override(Exploration)
|
|
|
|
def get_exploration_action(self,
|
2020-03-04 13:00:37 -08:00
|
|
|
distribution_inputs: TensorType,
|
|
|
|
action_dist_class: type,
|
|
|
|
model: ModelV2,
|
|
|
|
timestep: Union[int, TensorType],
|
|
|
|
explore: bool = True):
|
2020-02-19 21:18:45 +01:00
|
|
|
# Instantiate the distribution object.
|
2020-02-22 23:19:49 +01:00
|
|
|
action_dist = action_dist_class(distribution_inputs, model)
|
2020-02-19 21:18:45 +01:00
|
|
|
if self.framework == "tf":
|
2020-03-01 20:53:35 +01:00
|
|
|
return self.get_tf_exploration_action_op(action_dist, explore)
|
2020-02-19 21:18:45 +01:00
|
|
|
else:
|
2020-03-01 20:53:35 +01:00
|
|
|
return self.get_torch_exploration_action(action_dist, explore)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
@tf_function(tf)
|
2020-03-01 20:53:35 +01:00
|
|
|
def get_tf_exploration_action_op(self, action_dist, explore):
|
2020-02-19 21:18:45 +01:00
|
|
|
if explore:
|
2020-03-01 20:53:35 +01:00
|
|
|
action = tf.py_function(self.action_space.sample, [],
|
|
|
|
self.dtype_sample)
|
2020-02-19 21:18:45 +01:00
|
|
|
# Will be unnecessary, once we support batch/time-aware Spaces.
|
2020-03-01 20:53:35 +01:00
|
|
|
action = tf.expand_dims(tf.cast(action, dtype=self.dtype), 0)
|
2020-02-19 21:18:45 +01:00
|
|
|
else:
|
|
|
|
action = tf.cast(
|
2020-03-01 20:53:35 +01:00
|
|
|
action_dist.deterministic_sample(), dtype=self.dtype)
|
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
# TODO(sven): Move into (deterministic_)sample(logp=True|False)
|
|
|
|
if isinstance(action, TupleActions):
|
|
|
|
batch_size = tf.shape(action[0][0])[0]
|
|
|
|
else:
|
|
|
|
batch_size = tf.shape(action)[0]
|
|
|
|
logp = tf.zeros(shape=(batch_size, ), dtype=tf.float32)
|
|
|
|
return action, logp
|
|
|
|
|
2020-03-01 20:53:35 +01:00
|
|
|
def get_torch_exploration_action(self, action_dist, explore):
|
|
|
|
tensor_fn = torch.LongTensor if \
|
|
|
|
type(self.action_space) in [Discrete, MultiDiscrete] else \
|
|
|
|
torch.FloatTensor
|
2020-02-19 21:18:45 +01:00
|
|
|
if explore:
|
|
|
|
# Unsqueeze will be unnecessary, once we support batch/time-aware
|
|
|
|
# Spaces.
|
2020-03-01 20:53:35 +01:00
|
|
|
action = tensor_fn(self.action_space.sample()).unsqueeze(0)
|
2020-02-19 21:18:45 +01:00
|
|
|
else:
|
2020-03-01 20:53:35 +01:00
|
|
|
action = tensor_fn(action_dist.deterministic_sample())
|
2020-02-19 21:18:45 +01:00
|
|
|
logp = torch.zeros((action.size()[0], ), dtype=torch.float32)
|
|
|
|
return action, logp
|