mirror of
https://github.com/wassname/ray.git
synced 2026-07-14 11:17:54 +08:00
[RLlib] Unity3D integration (n Unity3D clients vs learning server). (#8590)
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 264 KiB |
+27
-14
@@ -36,7 +36,7 @@ You can pass either a string name or a Python class to specify an environment. B
|
||||
while True:
|
||||
print(trainer.train())
|
||||
|
||||
You can also register a custom env creator function with a string name. This function must take a single ``env_config`` parameter and return an env instance:
|
||||
You can also register a custom env creator function with a string name. This function must take a single ``env_config`` (dict) parameter and return an env instance:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -113,19 +113,20 @@ When using remote envs, you can control the batching level for inference with ``
|
||||
Multi-Agent and Hierarchical
|
||||
----------------------------
|
||||
|
||||
A multi-agent environment is one which has multiple acting entities per step, e.g., in a traffic simulation, there may be multiple "car" and "traffic light" agents in the environment. The model for multi-agent in RLlib as follows: (1) as a user you define the number of policies available up front, and (2) a function that maps agent ids to policy ids. This is summarized by the below figure:
|
||||
A multi-agent environment is one which has multiple acting entities per step, e.g., in a traffic simulation, there may be multiple "car"- and "traffic light" agents in the environment. The model for multi-agent in RLlib is as follows: (1) as a user, you define the number of policies available up front, and (2) a function that maps agent ids to policy ids. This is summarized by the below figure:
|
||||
|
||||
.. image:: multi-agent.svg
|
||||
|
||||
The environment itself must subclass the `MultiAgentEnv <https://github.com/ray-project/ray/blob/master/rllib/env/multi_agent_env.py>`__ interface, which can returns observations and rewards from multiple ready agents per step:
|
||||
The environment itself must subclass the `MultiAgentEnv <https://github.com/ray-project/ray/blob/master/rllib/env/multi_agent_env.py>`__ interface, which can return observations and rewards from multiple ready agents per step:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Example: using a multi-agent env
|
||||
> env = MultiAgentTrafficEnv(num_cars=20, num_traffic_lights=5)
|
||||
|
||||
# Observations are a dict mapping agent names to their obs. Not all agents
|
||||
# may be present in the dict in each time step.
|
||||
# Observations are a dict mapping agent names to their obs. Only those
|
||||
# agents' names that require actions in the next call to `step()` will
|
||||
# be present in the returned observation dict.
|
||||
> print(env.reset())
|
||||
{
|
||||
"car_1": [[...]],
|
||||
@@ -133,14 +134,15 @@ The environment itself must subclass the `MultiAgentEnv <https://github.com/ray-
|
||||
"traffic_light_1": [[...]],
|
||||
}
|
||||
|
||||
# Actions should be provided for each agent that returned an observation.
|
||||
> new_obs, rewards, dones, infos = env.step(actions={"car_1": ..., "car_2": ...})
|
||||
# In the following call to `step`, actions should be provided for each
|
||||
# agent that returned an observation before:
|
||||
> new_obs, rewards, dones, infos = env.step(actions={"car_1": ..., "car_2": ..., "traffic_light_1": ...})
|
||||
|
||||
# Similarly, new_obs, rewards, dones, etc. also become dicts
|
||||
> print(rewards)
|
||||
{"car_1": 3, "car_2": -1, "traffic_light_1": 0}
|
||||
|
||||
# Individual agents can early exit; env is done when "__all__" = True
|
||||
# Individual agents can early exit; The entire episode is done when "__all__" = True
|
||||
> print(dones)
|
||||
{"car_2": True, "__all__": False}
|
||||
|
||||
@@ -305,9 +307,14 @@ See this file for a runnable example: `hierarchical_training.py <https://github.
|
||||
External Agents and Applications
|
||||
--------------------------------
|
||||
|
||||
In many situations, it does not make sense for an environment to be "stepped" by RLlib. For example, if a policy is to be used in a web serving system, then it is more natural for an agent to query a service that serves policy decisions, and for that service to learn from experience over time. This case also naturally arises with **external simulators** that run independently outside the control of RLlib, but may still want to leverage RLlib for training.
|
||||
In many situations, it does not make sense for an environment to be "stepped" by RLlib. For example, if a policy is to be used in a web serving system, then it is more natural for an agent to query a service that serves policy decisions, and for that service to learn from experience over time. This case also naturally arises with **external simulators** (e.g. Unity3D, other game engines, or the Gazebo robotics simulator) that run independently outside the control of RLlib, but may still want to leverage RLlib for training.
|
||||
|
||||
RLlib provides the `ExternalEnv <https://github.com/ray-project/ray/blob/master/rllib/env/external_env.py>`__ class for this purpose. Unlike other envs, ExternalEnv has its own thread of control. At any point, agents on that thread can query the current policy for decisions via ``self.get_action()`` and reports rewards via ``self.log_returns()``. This can be done for multiple concurrent episodes as well.
|
||||
.. figure:: images/rllib-training-inside-a-unity3d-env.png
|
||||
:scale: 75 %
|
||||
|
||||
A Unity3D soccer game being learnt by RLlib via the ExternalEnv API.
|
||||
|
||||
RLlib provides the `ExternalEnv <https://github.com/ray-project/ray/blob/master/rllib/env/external_env.py>`__ class for this purpose. Unlike other envs, ExternalEnv has its own thread of control. At any point, agents on that thread can query the current policy for decisions via ``self.get_action()`` and reports rewards, done-dicts, and infos via ``self.log_returns()``. This can be done for multiple concurrent episodes as well.
|
||||
|
||||
Logging off-policy actions
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
@@ -330,8 +337,8 @@ You can configure any Trainer to launch a policy server with the following confi
|
||||
trainer_config = {
|
||||
# An environment class is still required, but it doesn't need to be runnable.
|
||||
# You only need to define its action and observation space attributes.
|
||||
# See examples/serving/unity3d_server.py for an example using a RandomMultiAgentEnv stub.
|
||||
"env": YOUR_ENV_STUB,
|
||||
|
||||
# Use the policy server to generate experiences.
|
||||
"input": (
|
||||
lambda ioctx: PolicyServerInput(ioctx, SERVER_ADDRESS, SERVER_PORT)
|
||||
@@ -360,7 +367,13 @@ To understand the difference between standard envs, external envs, and connectin
|
||||
.. https://docs.google.com/drawings/d/1hJvT9bVGHVrGTbnCZK29BYQIcYNRbZ4Dr6FOPMJDjUs/edit
|
||||
.. image:: rllib-external.svg
|
||||
|
||||
Try it yourself by launching a `cartpole_server.py <https://github.com/ray-project/ray/blob/master/rllib/examples/serving/cartpole_server.py>`__, and connecting to it with any number of clients (`cartpole_client.py <https://github.com/ray-project/ray/blob/master/rllib/examples/serving/cartpole_client.py>`__):
|
||||
Try it yourself by launching either a
|
||||
`simple CartPole server <https://github.com/ray-project/ray/blob/master/rllib/examples/serving/cartpole_server.py>`__ (see below), and connecting it to any number of clients
|
||||
(`cartpole_client.py <https://github.com/ray-project/ray/blob/master/rllib/examples/serving/cartpole_client.py>`__) or
|
||||
run a `Unity3D learning sever <https://github.com/ray-project/ray/blob/master/rllib/examples/serving/unity3d_server.py>`__
|
||||
against distributed Unity game engines in the cloud.
|
||||
|
||||
CartPole Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
@@ -391,9 +404,9 @@ Try it yourself by launching a `cartpole_server.py <https://github.com/ray-proje
|
||||
Total reward: 200.0
|
||||
...
|
||||
|
||||
For the best performance, when possible we recommend using ``inference_mode="local"`` when possible.
|
||||
For the best performance, we recommend using ``inference_mode="local"`` when possible.
|
||||
|
||||
Advanced Integrations
|
||||
---------------------
|
||||
|
||||
For more complex / high-performance environment integrations, you can instead extend the low-level `BaseEnv <https://github.com/ray-project/ray/blob/master/rllib/env/base_env.py>`__ class. This low-level API models multiple agents executing asynchronously in multiple environments. A call to ``BaseEnv:poll()`` returns observations from ready agents keyed by their environment and agent ids, and actions for those agents are sent back via ``BaseEnv:send_actions()``. BaseEnv is used to implement all the other env types in RLlib, so it offers a superset of their functionality. For example, ``BaseEnv`` is used to implement dynamic batching of observations for inference over `multiple simulator actors <https://github.com/ray-project/ray/blob/master/rllib/env/remote_vector_env.py>`__.
|
||||
For more complex / high-performance environment integrations, you can instead extend the low-level `BaseEnv <https://github.com/ray-project/ray/blob/master/rllib/env/base_env.py>`__ class. This low-level API models multiple agents executing asynchronously in multiple environments. A call to ``BaseEnv:poll()`` returns observations from ready agents keyed by 1) their environment, then 2) agent ids. Actions for those agents are sent back via ``BaseEnv:send_actions()``. BaseEnv is used to implement all the other env types in RLlib, so it offers a superset of their functionality. For example, ``BaseEnv`` is used to implement dynamic batching of observations for inference over `multiple simulator actors <https://github.com/ray-project/ray/blob/master/rllib/env/remote_vector_env.py>`__.
|
||||
|
||||
@@ -36,12 +36,18 @@ Training Workflows
|
||||
Custom Envs and Models
|
||||
----------------------
|
||||
|
||||
- `Local Unity3D multi-agent environment example <https://github.com/ray-project/ray/tree/master/rllib/examples/unity3d_env_local.py>`__:
|
||||
Example of how to setup an RLlib Trainer against a locally running Unity3D editor instance to
|
||||
learn any Unity3D game (including support for multi-agent).
|
||||
Use this example to try things out and watch the game and the learning progress live in the editor.
|
||||
Providing a compiled game, this example could also run in distributed fashion with `num_workers > 0`.
|
||||
For a more heavy-weight, distributed, cloud-based example, see `Unity3D client/server`_ below.
|
||||
- `Registering a custom env and model <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_env.py>`__:
|
||||
Example of defining and registering a gym env and model for use with RLlib.
|
||||
- `Custom Keras model <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_keras_model.py>`__:
|
||||
Example of using a custom Keras model.
|
||||
- `Custom Keras RNN model <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_keras_rnn_model.py>`__:
|
||||
Example of using a custom Keras RNN model.
|
||||
- `Custom Keras RNN model <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_rnn_model.py>`__:
|
||||
Example of using a custom Keras- or PyTorch RNN model.
|
||||
- `Registering a custom model with supervised loss <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_loss.py>`__:
|
||||
Example of defining and registering a custom model with a supervised loss.
|
||||
- `Subprocess environment <https://github.com/ray-project/ray/blob/master/rllib/tests/test_env_with_subprocess.py>`__:
|
||||
@@ -55,7 +61,16 @@ Custom Envs and Models
|
||||
|
||||
Serving and Offline
|
||||
-------------------
|
||||
- `CartPole server <https://github.com/ray-project/ray/tree/master/rllib/examples/serving>`__:
|
||||
|
||||
.. _Unity3D client/server:
|
||||
|
||||
- `Unity3D client/server <https://github.com/ray-project/ray/tree/master/rllib/examples/serving/unity3d_server.py>`__:
|
||||
Example of how to setup n distributed Unity3D (compiled) games in the cloud that function as data collecting
|
||||
clients against a central RLlib Policy server learning how to play the game.
|
||||
The n distributed clients could themselves be servers for external/human players and allow for control
|
||||
being fully in the hands of the Unity entities instead of RLlib.
|
||||
Note: Uses Unity's MLAgents SDK (>=1.0) and supports all provided MLAgents example games and multi-agent setups.
|
||||
- `CartPole client/server <https://github.com/ray-project/ray/tree/master/rllib/examples/serving/cartpole_server.py>`__:
|
||||
Example of online serving of predictions for a simple CartPole policy.
|
||||
- `Saving experiences <https://github.com/ray-project/ray/blob/master/rllib/examples/saving_experiences.py>`__:
|
||||
Example of how to externally generate experience batches in RLlib-compatible format.
|
||||
|
||||
Reference in New Issue
Block a user