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
|
|
|
|
|
|
|
|
import ray
|
2019-03-30 14:07:50 -07:00
|
|
|
from ray import tune
|
2020-05-01 22:59:34 +02:00
|
|
|
from ray.rllib.examples.env.parametric_actions_cartpole import \
|
|
|
|
ParametricActionsCartPole
|
2020-05-12 08:23:10 +02:00
|
|
|
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()
|
|
|
|
parser.add_argument("--run", type=str, default="PPO")
|
2020-05-12 08:23:10 +02:00
|
|
|
parser.add_argument("--torch", action="store_true")
|
|
|
|
parser.add_argument("--as-test", action="store_true")
|
|
|
|
parser.add_argument("--stop-iters", type=int, default=200)
|
|
|
|
parser.add_argument("--stop-reward", type=float, default=150.0)
|
|
|
|
parser.add_argument("--stop-timesteps", type=int, default=100000)
|
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(
|
|
|
|
"pa_model", TorchParametricActionsModel
|
|
|
|
if args.torch else ParametricActionsModel)
|
|
|
|
|
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
|
|
|
|
# defined a a custom DistributionalQModel that is aware of masking.
|
|
|
|
"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
|
|
|
|
|
|
|
config = dict({
|
|
|
|
"env": "pa_cartpole",
|
|
|
|
"model": {
|
|
|
|
"custom_model": "pa_model",
|
2019-03-30 14:07:50 -07:00
|
|
|
},
|
2020-05-12 08:23:10 +02:00
|
|
|
"num_workers": 0,
|
2020-05-27 16:19:13 +02:00
|
|
|
"framework": "torch" if args.torch else "tf",
|
2020-05-12 08:23:10 +02:00
|
|
|
}, **cfg)
|
|
|
|
|
|
|
|
stop = {
|
|
|
|
"training_iteration": args.stop_iters,
|
|
|
|
"timesteps_total": args.stop_timesteps,
|
|
|
|
"episode_reward_mean": args.stop_reward,
|
|
|
|
}
|
|
|
|
|
|
|
|
results = tune.run(args.run, stop=stop, config=config)
|
|
|
|
|
|
|
|
if args.as_test:
|
|
|
|
check_learning_achieved(results, args.stop_reward)
|
|
|
|
|
|
|
|
ray.shutdown()
|