Ray RLlib: A Scalable Reinforcement Learning Library ==================================================== Ray RLlib is a reinforcement learning library that aims to provide both performance and composability: - Performance - High performance algorithm implementions - Pluggable distributed RL execution strategies - Composability - Integration with the `Ray.tune `__ hyperparam tuning tool - Support for multiple frameworks (TensorFlow, PyTorch) - Scalable primitives for developing new algorithms - Shared models between algorithms You can find the code for RLlib `here on GitHub `__, and the NIPS symposium paper `here `__. RLlib currently provides the following algorithms: - `Proximal Policy Optimization (PPO) `__ which is a proximal variant of `TRPO `__. - `The Asynchronous Advantage Actor-Critic (A3C) `__. - `Deep Q Networks (DQN) `__. - Evolution Strategies, as described in `this paper `__. Our implementation is adapted from `here `__. These algorithms can be run on any `OpenAI Gym MDP `__, including custom ones written and registered by the user. Installation ------------ RLlib has extra dependencies on top of **ray**: .. code-block:: bash pip install 'ray[rllib]' For usage of PyTorch models, visit the `PyTorch website `__ for instructions on installing PyTorch. Getting Started --------------- You can train a simple DQN agent with the following command :: python ray/python/ray/rllib/train.py --run DQN --env CartPole-v0 By default, the results will be logged to a subdirectory of ``~/ray_results``. This subdirectory will contain a file ``params.json`` which contains the hyperparameters, a file ``result.json`` which contains a training summary for each episode and a TensorBoard file that can be used to visualize training process with TensorBoard by running :: tensorboard --logdir=~/ray_results The ``train.py`` script has a number of options you can show by running :: python ray/python/ray/rllib/train.py --help The most important options are for choosing the environment with ``--env`` (any OpenAI gym environment including ones registered by the user can be used) and for choosing the algorithm with ``--run`` (available options are ``PPO``, ``A3C``, ``ES`` and ``DQN``). Specifying Parameters ~~~~~~~~~~~~~~~~~~~~~ Each algorithm has specific hyperparameters that can be set with ``--config`` - see the ``DEFAULT_CONFIG`` variable in `PPO `__, `A3C `__, `ES `__ and `DQN `__. In an example below, we train A3C by specifying 8 workers through the config flag. :: python ray/python/ray/rllib/train.py --env=PongDeterministic-v4 --run=A3C --config '{"num_workers": 8}' Evaluating Trained Agents ~~~~~~~~~~~~~~~~~~~~~~~~~ In order to save checkpoints from which to evaluate agents, set ``--checkpoint-freq`` (number of training iterations between checkpoints) when running ``train.py``. You can evaluate a simple DQN agent with the following command :: python ray/python/ray/rllib/eval.py \ /tmp/ray/default/DQN_CartPole-v0_0upjmdgr0/checkpoint-1 \ --run DQN --env CartPole-v0 By default, the script reconstructs a DQN agent from the checkpoint located at ``/tmp/ray/default/DQN_CartPole-v0_0upjmdgr0/checkpoint-1`` and renders its behavior in the environment specified by ``--env``. Checkpoints are be found within the experiment directory, specified by ``--local-dir`` and ``--experiment-name`` when running ``train.py``. Tuned Examples -------------- Some good hyperparameters and settings are available in `the repository `__ (some of them are tuned to run on GPUs). If you find better settings or tune an algorithm on a different domain, consider submitting a Pull Request! Python User API --------------- You will be using this part of the API if you run the existing algorithms on a new problem. Here is an example how to use it: :: import ray import ray.rllib.ppo as ppo ray.init() config = ppo.DEFAULT_CONFIG.copy() alg = ppo.PPOAgent(config=config, env="CartPole-v1") # Can optionally call alg.restore(path) to load a checkpoint. for i in range(10): # Perform one iteration of the algorithm. result = alg.train() print("result: {}".format(result)) print("checkpoint saved at path: {}".format(alg.save())) Custom Environments ~~~~~~~~~~~~~~~~~~~ To train against a custom environment, i.e. one not in the gym catalog, you can register a function that creates the env to refer to it by name. For example: :: import ray from ray.tune.registry import register_env from ray.rllib import ppo env_creator = lambda env_config: create_my_env() env_creator_name = "custom_env" register_env(env_creator_name, env_creator) ray.init() alg = ppo.PPOAgent(env=env_creator_name) Custom Models and Preprocessors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RLlib includes default neural network models and preprocessors for common gym environments, but you can also specify your own as follows. The interfaces for custom model and preprocessor classes are documented in the `RLlib Developer Guide `__. :: import ray from ray.rllib.models import ModelCatalog ModelCatalog.register_custom_preprocessor("my_prep", MyPreprocessorClass) ModelCatalog.register_custom_model("my_model", MyModelClass) ray.init() alg = ppo.PPOAgent(env="CartPole-v0", config={ "custom_preprocessor": "my_prep", "custom_model": "my_model", "custom_options": {}, # extra options to pass to your classes }) Using RLlib with Ray.tune ------------------------- All Agents implemented in RLlib support the `tune Trainable `__ interface. Here is an example of using the command-line interface with RLlib: :: python ray/python/ray/rllib/train.py -f tuned_examples/cartpole-grid-search-example.yaml Here is an example using the Python API. The same config passed to ``Agents`` may be placed in the ``config`` section of the experiments. :: from ray.tune.tune import run_experiments from ray.tune.variant_generator import grid_search experiment = { 'cartpole-ppo': { 'run': 'PPO', 'env': 'CartPole-v0', 'resources': { 'cpu': 2, 'driver_cpu_limit': 1}, 'stop': { 'episode_reward_mean': 200, 'time_total_s': 180 }, 'config': { 'num_sgd_iter': grid_search([1, 4]), 'num_workers': 2, 'sgd_batchsize': grid_search([128, 256, 512]) } }, # put additional experiments to run concurrently here } run_experiments(experiment) .. _`managing a cluster with parallel ssh`: using-ray-on-a-large-cluster.html Contributing to RLlib --------------------- See the `RLlib Developer Guide `__.