2018-11-27 23:35:19 -08:00
|
|
|
"""Example of handling variable length and/or parametric action spaces.
|
|
|
|
|
|
|
|
This is a toy example of the action-embedding based approach for handling large
|
2019-02-24 15:36:13 -08:00
|
|
|
discrete action spaces (potentially infinite in size), similar to this:
|
2018-11-27 23:35:19 -08:00
|
|
|
|
|
|
|
https://neuro.cs.ut.ee/the-use-of-embeddings-in-openai-five/
|
|
|
|
|
|
|
|
This currently works with RLlib's policy gradient style algorithms
|
|
|
|
(e.g., PG, PPO, IMPALA, A2C) and also DQN.
|
|
|
|
|
|
|
|
Note that since the model outputs now include "-inf" tf.float32.min
|
|
|
|
values, not all algorithm options are supported at the moment. For example,
|
|
|
|
algorithms might crash if they don't properly ignore the -inf action scores.
|
|
|
|
Working configurations are given below.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
2020-10-02 23:07:44 +02:00
|
|
|
import os
|
2018-11-27 23:35:19 -08:00
|
|
|
|
|
|
|
import ray
|
2019-03-30 14:07:50 -07:00
|
|
|
from ray import tune
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.examples.env.parametric_actions_cartpole import ParametricActionsCartPole
|
|
|
|
from ray.rllib.examples.models.parametric_actions_model import (
|
|
|
|
ParametricActionsModel,
|
|
|
|
TorchParametricActionsModel,
|
|
|
|
)
|
2019-07-27 02:08:16 -07:00
|
|
|
from ray.rllib.models import ModelCatalog
|
2020-05-12 08:23:10 +02:00
|
|
|
from ray.rllib.utils.test_utils import check_learning_achieved
|
2020-05-18 17:26:40 +02:00
|
|
|
from ray.tune.registry import register_env
|
2018-11-27 23:35:19 -08:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--run", type=str, default="PPO", help="The RLlib-registered algorithm to use."
|
|
|
|
)
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--framework",
|
|
|
|
choices=["tf", "tf2", "tfe", "torch"],
|
|
|
|
default="tf",
|
2022-01-29 18:41:57 -08:00
|
|
|
help="The DL framework specifier.",
|
|
|
|
)
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--as-test",
|
|
|
|
action="store_true",
|
|
|
|
help="Whether this script should be run as a test: --stop-reward must "
|
2022-01-29 18:41:57 -08:00
|
|
|
"be achieved within --stop-timesteps AND --stop-iters.",
|
|
|
|
)
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--stop-iters", type=int, default=200, help="Number of iterations to train."
|
|
|
|
)
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--stop-timesteps", type=int, default=100000, help="Number of timesteps to train."
|
|
|
|
)
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--stop-reward", type=float, default=150.0, help="Reward at which we stop training."
|
|
|
|
)
|
2018-11-27 23:35:19 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
|
|
|
ray.init()
|
|
|
|
|
2020-05-01 22:59:34 +02:00
|
|
|
register_env("pa_cartpole", lambda _: ParametricActionsCartPole(10))
|
2020-05-12 08:23:10 +02:00
|
|
|
ModelCatalog.register_custom_model(
|
2022-01-29 18:41:57 -08:00
|
|
|
"pa_model",
|
|
|
|
TorchParametricActionsModel
|
|
|
|
if args.framework == "torch"
|
|
|
|
else ParametricActionsModel,
|
|
|
|
)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
2019-07-27 02:08:16 -07:00
|
|
|
if args.run == "DQN":
|
2018-11-27 23:35:19 -08:00
|
|
|
cfg = {
|
2019-07-27 02:08:16 -07:00
|
|
|
# TODO(ekl) we need to set these to prevent the masked values
|
|
|
|
# from being further processed in DistributionalQModel, which
|
|
|
|
# would mess up the masking. It is possible to support these if we
|
2020-07-10 12:43:03 +02:00
|
|
|
# defined a custom DistributionalQModel that is aware of masking.
|
2019-07-27 02:08:16 -07:00
|
|
|
"hiddens": [],
|
2019-07-03 15:59:47 -07:00
|
|
|
"dueling": False,
|
2018-11-27 23:35:19 -08:00
|
|
|
}
|
|
|
|
else:
|
2019-07-27 02:08:16 -07:00
|
|
|
cfg = {}
|
2020-05-12 08:23:10 +02:00
|
|
|
|
2020-10-02 23:07:44 +02:00
|
|
|
config = dict(
|
|
|
|
{
|
|
|
|
"env": "pa_cartpole",
|
|
|
|
"model": {
|
|
|
|
"custom_model": "pa_model",
|
|
|
|
},
|
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
|
|
|
"num_workers": 0,
|
2021-05-18 13:18:12 +02:00
|
|
|
"framework": args.framework,
|
2019-03-30 14:07:50 -07:00
|
|
|
},
|
2022-01-29 18:41:57 -08:00
|
|
|
**cfg
|
|
|
|
)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
stop = {
|
|
|
|
"training_iteration": args.stop_iters,
|
|
|
|
"timesteps_total": args.stop_timesteps,
|
|
|
|
"episode_reward_mean": args.stop_reward,
|
|
|
|
}
|
|
|
|
|
2020-06-27 20:50:01 +02:00
|
|
|
results = tune.run(args.run, stop=stop, config=config, verbose=1)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
if args.as_test:
|
|
|
|
check_learning_achieved(results, args.stop_reward)
|
|
|
|
|
|
|
|
ray.shutdown()
|