2020-03-01 20:53:35 +01:00
|
|
|
from gym.spaces import Discrete, MultiDiscrete, Tuple
|
2020-04-15 13:25:16 +02:00
|
|
|
import numpy as np
|
2020-04-28 14:59:16 +02:00
|
|
|
import tree
|
2020-03-04 13:00:37 -08:00
|
|
|
from typing import Union
|
2020-02-19 21:18:45 +01:00
|
|
|
|
2020-04-01 09:43:21 +02:00
|
|
|
from ray.rllib.models.action_dist import ActionDistribution
|
2020-02-19 21:18:45 +01:00
|
|
|
from ray.rllib.utils.annotations import override
|
|
|
|
from ray.rllib.utils.exploration.exploration import Exploration
|
2020-04-28 14:59:16 +02:00
|
|
|
from ray.rllib.utils import force_tuple
|
2020-02-19 21:18:45 +01:00
|
|
|
from ray.rllib.utils.framework import try_import_tf, try_import_torch, \
|
2020-03-12 19:02:51 +01:00
|
|
|
TensorType
|
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-04-01 09:43:21 +02:00
|
|
|
def __init__(self, action_space, *, model, framework, **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__(
|
2020-04-01 09:43:21 +02:00
|
|
|
action_space=action_space,
|
|
|
|
model=model,
|
2020-04-03 19:44:25 +02:00
|
|
|
framework=framework,
|
2020-04-01 09:43:21 +02:00
|
|
|
**kwargs)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
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-04-01 09:43:21 +02:00
|
|
|
*,
|
|
|
|
action_distribution: ActionDistribution,
|
2020-03-04 13:00:37 -08:00
|
|
|
timestep: Union[int, TensorType],
|
|
|
|
explore: bool = True):
|
2020-02-19 21:18:45 +01:00
|
|
|
# Instantiate the distribution object.
|
|
|
|
if self.framework == "tf":
|
2020-04-01 09:43:21 +02:00
|
|
|
return self.get_tf_exploration_action_op(action_distribution,
|
|
|
|
explore)
|
2020-02-19 21:18:45 +01:00
|
|
|
else:
|
2020-04-01 09:43:21 +02:00
|
|
|
return self.get_torch_exploration_action(action_distribution,
|
|
|
|
explore)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
2020-03-01 20:53:35 +01:00
|
|
|
def get_tf_exploration_action_op(self, action_dist, explore):
|
2020-03-12 19:02:51 +01:00
|
|
|
def true_fn():
|
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-12 19:02:51 +01:00
|
|
|
return tf.expand_dims(tf.cast(action, dtype=self.dtype), 0)
|
|
|
|
|
|
|
|
def false_fn():
|
|
|
|
return tf.cast(
|
2020-03-01 20:53:35 +01:00
|
|
|
action_dist.deterministic_sample(), dtype=self.dtype)
|
|
|
|
|
2020-03-12 19:02:51 +01:00
|
|
|
action = tf.cond(
|
|
|
|
pred=tf.constant(explore, dtype=tf.bool)
|
|
|
|
if isinstance(explore, bool) else explore,
|
|
|
|
true_fn=true_fn,
|
|
|
|
false_fn=false_fn)
|
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
# TODO(sven): Move into (deterministic_)sample(logp=True|False)
|
2020-04-28 14:59:16 +02:00
|
|
|
batch_size = tf.shape(tree.flatten(action)[0])[0]
|
2020-02-19 21:18:45 +01:00
|
|
|
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):
|
2020-02-19 21:18:45 +01:00
|
|
|
if explore:
|
|
|
|
# Unsqueeze will be unnecessary, once we support batch/time-aware
|
|
|
|
# Spaces.
|
2020-04-03 19:44:25 +02:00
|
|
|
a = self.action_space.sample()
|
2020-04-15 13:25:16 +02:00
|
|
|
req = force_tuple(
|
|
|
|
action_dist.required_model_output_shape(
|
|
|
|
self.action_space, self.model.model_config))
|
|
|
|
# Add a batch dimension.
|
|
|
|
if len(action_dist.inputs.shape) == len(req) + 1:
|
|
|
|
a = np.expand_dims(a, 0)
|
|
|
|
action = torch.from_numpy(a).to(self.device)
|
2020-02-19 21:18:45 +01:00
|
|
|
else:
|
2020-04-15 13:25:16 +02:00
|
|
|
action = action_dist.deterministic_sample()
|
|
|
|
logp = torch.zeros(
|
|
|
|
(action.size()[0], ), dtype=torch.float32, device=self.device)
|
2020-02-19 21:18:45 +01:00
|
|
|
return action, logp
|