2019-07-03 15:59:47 -07:00
|
|
|
"""Basic example of a DQN policy without any optimizations."""
|
|
|
|
|
|
|
|
from gym.spaces import Discrete
|
2019-07-07 19:51:26 -07:00
|
|
|
import logging
|
2019-07-03 15:59:47 -07:00
|
|
|
|
|
|
|
import ray
|
|
|
|
from ray.rllib.agents.dqn.simple_q_model import SimpleQModel
|
|
|
|
from ray.rllib.policy.sample_batch import SampleBatch
|
|
|
|
from ray.rllib.models import ModelCatalog
|
2020-02-19 21:18:45 +01:00
|
|
|
from ray.rllib.models.tf.tf_action_dist import Categorical
|
2019-07-03 15:59:47 -07:00
|
|
|
from ray.rllib.utils.annotations import override
|
|
|
|
from ray.rllib.utils.error import UnsupportedSpaceException
|
|
|
|
from ray.rllib.policy.tf_policy import TFPolicy
|
|
|
|
from ray.rllib.policy.tf_policy_template import build_tf_policy
|
|
|
|
from ray.rllib.utils import try_import_tf
|
2019-08-23 02:21:11 -04:00
|
|
|
from ray.rllib.utils.tf_ops import huber_loss, make_tf_callable
|
2019-07-03 15:59:47 -07:00
|
|
|
|
|
|
|
tf = try_import_tf()
|
2019-07-07 19:51:26 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-07-03 15:59:47 -07:00
|
|
|
|
|
|
|
Q_SCOPE = "q_func"
|
|
|
|
Q_TARGET_SCOPE = "target_q_func"
|
|
|
|
|
|
|
|
|
2020-01-02 17:42:13 -08:00
|
|
|
class TargetNetworkMixin:
|
2019-07-03 15:59:47 -07:00
|
|
|
def __init__(self, obs_space, action_space, config):
|
2019-08-23 02:21:11 -04:00
|
|
|
@make_tf_callable(self.get_session())
|
|
|
|
def do_update():
|
|
|
|
# update_target_fn will be called periodically to copy Q network to
|
|
|
|
# target Q network
|
|
|
|
update_target_expr = []
|
|
|
|
assert len(self.q_func_vars) == len(self.target_q_func_vars), \
|
|
|
|
(self.q_func_vars, self.target_q_func_vars)
|
|
|
|
for var, var_target in zip(self.q_func_vars,
|
|
|
|
self.target_q_func_vars):
|
|
|
|
update_target_expr.append(var_target.assign(var))
|
|
|
|
logger.debug("Update target op {}".format(var_target))
|
|
|
|
return tf.group(*update_target_expr)
|
|
|
|
|
|
|
|
self.update_target = do_update
|
2019-07-03 15:59:47 -07:00
|
|
|
|
2019-10-31 15:16:02 -07:00
|
|
|
@override(TFPolicy)
|
|
|
|
def variables(self):
|
|
|
|
return self.q_func_vars + self.target_q_func_vars
|
|
|
|
|
2019-07-03 15:59:47 -07:00
|
|
|
|
|
|
|
def build_q_models(policy, obs_space, action_space, config):
|
|
|
|
|
|
|
|
if not isinstance(action_space, Discrete):
|
|
|
|
raise UnsupportedSpaceException(
|
|
|
|
"Action space {} is not supported for DQN.".format(action_space))
|
|
|
|
|
|
|
|
if config["hiddens"]:
|
|
|
|
num_outputs = 256
|
|
|
|
config["model"]["no_final_linear"] = True
|
|
|
|
else:
|
|
|
|
num_outputs = action_space.n
|
|
|
|
|
|
|
|
policy.q_model = ModelCatalog.get_model_v2(
|
|
|
|
obs_space,
|
|
|
|
action_space,
|
|
|
|
num_outputs,
|
|
|
|
config["model"],
|
|
|
|
framework="tf",
|
|
|
|
name=Q_SCOPE,
|
|
|
|
model_interface=SimpleQModel,
|
|
|
|
q_hiddens=config["hiddens"])
|
|
|
|
|
|
|
|
policy.target_q_model = ModelCatalog.get_model_v2(
|
|
|
|
obs_space,
|
|
|
|
action_space,
|
|
|
|
num_outputs,
|
|
|
|
config["model"],
|
|
|
|
framework="tf",
|
|
|
|
name=Q_TARGET_SCOPE,
|
|
|
|
model_interface=SimpleQModel,
|
|
|
|
q_hiddens=config["hiddens"])
|
|
|
|
|
|
|
|
return policy.q_model
|
|
|
|
|
|
|
|
|
2020-04-01 09:43:21 +02:00
|
|
|
def get_distribution_inputs_and_class(policy,
|
|
|
|
q_model,
|
|
|
|
obs_batch,
|
|
|
|
*,
|
|
|
|
explore=True,
|
|
|
|
**kwargs):
|
|
|
|
q_vals = compute_q_values(policy, q_model, obs_batch, explore)
|
2020-02-22 23:19:49 +01:00
|
|
|
q_vals = q_vals[0] if isinstance(q_vals, tuple) else q_vals
|
2020-02-19 21:18:45 +01:00
|
|
|
|
2020-04-01 09:43:21 +02:00
|
|
|
policy.q_values = q_vals
|
|
|
|
policy.q_func_vars = q_model.variables()
|
|
|
|
return policy.q_values, Categorical, [] # state-outs
|
2019-07-03 15:59:47 -07:00
|
|
|
|
|
|
|
|
2019-08-23 02:21:11 -04:00
|
|
|
def build_q_losses(policy, model, dist_class, train_batch):
|
2019-07-03 15:59:47 -07:00
|
|
|
# q network evaluation
|
2020-04-01 09:43:21 +02:00
|
|
|
q_t = compute_q_values(
|
|
|
|
policy,
|
|
|
|
policy.q_model,
|
|
|
|
train_batch[SampleBatch.CUR_OBS],
|
|
|
|
explore=False)
|
2019-07-03 15:59:47 -07:00
|
|
|
|
|
|
|
# target q network evalution
|
2020-04-01 09:43:21 +02:00
|
|
|
q_tp1 = compute_q_values(
|
|
|
|
policy,
|
|
|
|
policy.target_q_model,
|
|
|
|
train_batch[SampleBatch.NEXT_OBS],
|
|
|
|
explore=False)
|
2019-07-03 15:59:47 -07:00
|
|
|
policy.target_q_func_vars = policy.target_q_model.variables()
|
|
|
|
|
|
|
|
# q scores for actions which we know were selected in the given state.
|
|
|
|
one_hot_selection = tf.one_hot(
|
2019-08-23 02:21:11 -04:00
|
|
|
tf.cast(train_batch[SampleBatch.ACTIONS], tf.int32),
|
2019-07-03 15:59:47 -07:00
|
|
|
policy.action_space.n)
|
|
|
|
q_t_selected = tf.reduce_sum(q_t * one_hot_selection, 1)
|
|
|
|
|
|
|
|
# compute estimate of best possible value starting from state at t + 1
|
2019-08-23 02:21:11 -04:00
|
|
|
dones = tf.cast(train_batch[SampleBatch.DONES], tf.float32)
|
2019-07-03 15:59:47 -07:00
|
|
|
q_tp1_best_one_hot_selection = tf.one_hot(
|
|
|
|
tf.argmax(q_tp1, 1), policy.action_space.n)
|
|
|
|
q_tp1_best = tf.reduce_sum(q_tp1 * q_tp1_best_one_hot_selection, 1)
|
|
|
|
q_tp1_best_masked = (1.0 - dones) * q_tp1_best
|
|
|
|
|
|
|
|
# compute RHS of bellman equation
|
2019-08-23 02:21:11 -04:00
|
|
|
q_t_selected_target = (train_batch[SampleBatch.REWARDS] +
|
2019-07-03 15:59:47 -07:00
|
|
|
policy.config["gamma"] * q_tp1_best_masked)
|
|
|
|
|
|
|
|
# compute the error (potentially clipped)
|
|
|
|
td_error = q_t_selected - tf.stop_gradient(q_t_selected_target)
|
|
|
|
loss = tf.reduce_mean(huber_loss(td_error))
|
|
|
|
|
|
|
|
# save TD error as an attribute for outside access
|
|
|
|
policy.td_error = td_error
|
|
|
|
|
|
|
|
return loss
|
|
|
|
|
|
|
|
|
2020-04-01 09:43:21 +02:00
|
|
|
def compute_q_values(policy, model, obs, explore):
|
|
|
|
model_out, _ = model({
|
|
|
|
SampleBatch.CUR_OBS: obs,
|
2019-07-03 15:59:47 -07:00
|
|
|
"is_training": policy._get_is_training_placeholder(),
|
2020-04-01 09:43:21 +02:00
|
|
|
}, [], None)
|
|
|
|
|
2019-07-03 15:59:47 -07:00
|
|
|
return model.get_q_values(model_out)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_late_mixins(policy, obs_space, action_space, config):
|
|
|
|
TargetNetworkMixin.__init__(policy, obs_space, action_space, config)
|
|
|
|
|
|
|
|
|
|
|
|
SimpleQPolicy = build_tf_policy(
|
|
|
|
name="SimpleQPolicy",
|
|
|
|
get_default_config=lambda: ray.rllib.agents.dqn.dqn.DEFAULT_CONFIG,
|
|
|
|
make_model=build_q_models,
|
2020-04-01 09:43:21 +02:00
|
|
|
action_distribution_fn=get_distribution_inputs_and_class,
|
2019-07-03 15:59:47 -07:00
|
|
|
loss_fn=build_q_losses,
|
|
|
|
extra_action_fetches_fn=lambda policy: {"q_values": policy.q_values},
|
|
|
|
extra_learn_fetches_fn=lambda policy: {"td_error": policy.td_error},
|
|
|
|
after_init=setup_late_mixins,
|
|
|
|
obs_include_prev_action_reward=False,
|
2020-04-03 19:44:25 +02:00
|
|
|
mixins=[TargetNetworkMixin])
|