env.rst 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. .. _env-reference-docs:
  2. Environments
  3. ============
  4. Any environment type provided by you to RLlib (e.g. a user-defined `gym.Env <https://github.com/openai/gym>`_ class),
  5. is converted internally into the :py:class:`~ray.rllib.env.base_env.BaseEnv` API, whose main methods are ``poll()`` and ``send_actions()``:
  6. .. https://docs.google.com/drawings/d/1NtbVk-Mo89liTRx-sHu_7fqi3Kn7Hjdf3i6jIMbxGlY/edit
  7. .. image:: ../images/env_classes_overview.svg
  8. The :py:class:`~ray.rllib.env.base_env.BaseEnv` API allows RLlib to support:
  9. 1) Vectorization of sub-environments (i.e. individual `gym.Env <https://github.com/openai/gym>`_ instances, stacked to form a vector of envs) in order to batch the action computing model forward passes.
  10. 2) External simulators requiring async execution (e.g. envs that run on separate machines and independently request actions from a policy server).
  11. 3) Stepping through the individual sub-environments in parallel via pre-converting them into separate `@ray.remote` actors.
  12. 4) Multi-agent RL via dicts mapping agent IDs to observations/rewards/etc..
  13. For example, if you provide a custom `gym.Env <https://github.com/openai/gym>`_ class to RLlib, auto-conversion to :py:class:`~ray.rllib.env.base_env.BaseEnv` goes as follows:
  14. - User provides a `gym.Env <https://github.com/openai/gym>`_ -> :py:class:`~ray.rllib.env.vector_env._VectorizedGymEnv` (is-a :py:class:`~ray.rllib.env.vector_env.VectorEnv`) -> :py:class:`~ray.rllib.env.base_env.BaseEnv`
  15. Here is a simple example:
  16. .. literalinclude:: ../../../../rllib/examples/documentation/custom_gym_env.py
  17. :language: python
  18. .. start-after: __sphinx_doc_model_construct_1_begin__
  19. .. end-before: __sphinx_doc_model_construct_1_end__
  20. However, you may also conveniently sub-class any of the other supported RLlib-specific
  21. environment types. The automated paths from those env types (or callables returning instances of those types) to
  22. an RLlib :py:class:`~ray.rllib.env.base_env.BaseEnv` is as follows:
  23. - User provides a custom :py:class:`~ray.rllib.env.multi_agent_env.MultiAgentEnv` (is-a `gym.Env <https://github.com/openai/gym>`_) -> :py:class:`~ray.rllib.env.vector_env.VectorEnv` -> :py:class:`~ray.rllib.env.base_env.BaseEnv`
  24. - User uses a policy client (via an external simulator) -> :py:class:`~ray.rllib.env.external_env.ExternalEnv` | :py:class:`~ray.rllib.env.external_multi_agent_env.ExternalMultiAgentEnv` -> :py:class:`~ray.rllib.env.base_env.BaseEnv`
  25. - User provides a custom :py:class:`~ray.rllib.env.vector_env.VectorEnv` -> :py:class:`~ray.rllib.env.base_env.BaseEnv`
  26. - User provides a custom :py:class:`~ray.rllib.env.base_env.BaseEnv` -> do nothing
  27. Environment API Reference
  28. -------------------------
  29. .. toctree::
  30. :maxdepth: 1
  31. env/base_env.rst
  32. env/multi_agent_env.rst
  33. env/vector_env.rst
  34. env/external_env.rst