Skip to content

Commit dbb11e4

Browse files
[Docs] Usage (#88)
* amend * fig * codecov
1 parent 179a07e commit dbb11e4

9 files changed

Lines changed: 221 additions & 45 deletions

File tree

.github/workflows/python-app.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,7 @@ jobs:
5151
- if: matrix.python-version == '3.10'
5252
name: Upload coverage to Codecov
5353
uses: codecov/codecov-action@v3
54+
env:
55+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5456
with:
5557
fail_ci_if_error: false

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"torch": ("https://pytorch.org/docs/master", None),
4343
"torchrl": ("https://pytorch.org/rl", None),
4444
"tensordict": ("https://pytorch.org/tensordict", None),
45+
"benchmarl": ("https://benchmarl.readthedocs.io/en/latest/", None),
4546
}
4647
intersphinx_disabled_domains = ["std"]
4748

docs/source/index.rst

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
VMAS
33
====
44

5+
.. figure:: https://raw.githubusercontent.com/matteobettini/vmas-media/master/media/vmas_scenarios_more.gif
6+
:align: center
7+
8+
9+
:github:`null` `GitHub <https://github.com/proroklab/VectorizedMultiAgentSimulator>`__
10+
511
Docs are currently being written, please bear with us.
612

713
Forthcoming doc sections:
814

9-
- Usage
10-
- Notebooks
11-
- Installing
12-
- Running
13-
- Citing
1415

1516
- Concepts (explanation of all the lib features) (divide in basic, advanced and medium)
1617
- Input and output spaces
@@ -33,6 +34,19 @@ Forthcoming doc sections:
3334

3435
- Full package reference with docstrings for all public functions
3536

37+
.. toctree::
38+
:maxdepth: 1
39+
:caption: Using
40+
41+
usage/notebooks
42+
usage/installation
43+
usage/running
44+
usage/citing
45+
46+
.. toctree::
47+
:maxdepth: 1
48+
:caption: Concepts
49+
3650

3751
.. toctree::
3852
:maxdepth: 1

docs/source/usage/citing.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Citing
2+
======
3+
4+
If you use VMAS in your research please use the following BibTeX entry:
5+
6+
7+
.. code-block:: bibtex
8+
9+
@inproceedings{bettini2022vmas,
10+
title = {VMAS: A Vectorized Multi-Agent Simulator for Collective Robot Learning},
11+
author = {Bettini, Matteo and Kortvelesy, Ryan and Blumenkamp, Jan and Prorok, Amanda},
12+
year = {2022},
13+
booktitle = {Proceedings of the 16th International Symposium on Distributed Autonomous Robotic Systems},
14+
publisher = {Springer},
15+
series = {DARS '22}
16+
}

docs/source/usage/installation.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Installation
2+
============
3+
4+
5+
Install from PyPi
6+
-----------------
7+
8+
You can install `VMAS <https://github.com/proroklab/VectorizedMultiAgentSimulator>`__ from PyPi.
9+
10+
.. code-block:: console
11+
12+
pip install vmas
13+
14+
Install from source
15+
-------------------
16+
17+
If you want to install the current main version (more up to date than latest release), you can do:
18+
19+
.. code-block:: console
20+
21+
git clone https://github.com/proroklab/VectorizedMultiAgentSimulator.git
22+
cd VectorizedMultiAgentSimulator
23+
pip install -e .
24+
25+
26+
Install optional requirements
27+
-----------------------------
28+
29+
By default, vmas has only the core requirements.
30+
Here are some optional packages you may want to install.
31+
32+
Training
33+
^^^^^^^^
34+
35+
You may want to install one of the following training libraries
36+
37+
.. code-block:: console
38+
39+
pip install benchmarl
40+
pip install torchrl
41+
pip install "ray[rllib]"==2.1.0 # We support versions "ray[rllib]<=2.2,>=1.13"
42+
43+
Logging
44+
^^^^^^^
45+
46+
You may want to install the following rendering and logging tools
47+
48+
.. code-block:: console
49+
50+
pip install wandb
51+
pip install opencv-python moviepy

docs/source/usage/notebooks.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Notebooks
2+
=========
3+
4+
In the following you can find a list of :colab:`null` Google Colab notebooks to help you learn how to use VMAS:
5+
6+
- :colab:`null` `Using a VMAS environment <https://colab.research.google.com/github/proroklab/VectorizedMultiAgentSimulator/blob/main/notebooks/VMAS_Use_vmas_environment.ipynb>`_. Here is a simple notebook that you can run to create, step and render any scenario in VMAS. It reproduces the ``use_vmas_env.py`` script in the ``examples`` folder
7+
- :colab:`null` `Using VMAS in BenchMARL (suggested) <https://colab.research.google.com/github/facebookresearch/BenchMARL/blob/main/notebooks/run.ipynb>`_. In this notebook, we show how to use VMAS in BenchMARL, TorchRL's MARL training library
8+
- :colab:`null` `Using VMAS in TorchRL <https://colab.research.google.com/github/pytorch/rl/blob/gh-pages/_downloads/a977047786179278d12b52546e1c0da8/multiagent_ppo.ipynb>`_. In this notebook, `available in the TorchRL docs <https://pytorch.org/rl/tutorials/multiagent_ppo.html>`_, we show how to use any VMAS scenario in TorchRL. It will guide you through the full pipeline needed to train agents using MAPPO/IPPO.
9+
- :colab:`null` `Using VMAS in RLlib <https://colab.research.google.com/github/proroklab/VectorizedMultiAgentSimulator/blob/main/notebooks/VMAS_RLlib.ipynb>`_. In this notebook, we show how to use any VMAS scenario in RLlib. It reproduces the ``rllib.py`` script in the ``examples`` folder.

docs/source/usage/running.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Running
2+
=======
3+
4+
To use the simulator, simply create an environment by passing the name of the scenario
5+
you want (from the ``scenarios`` folder) to the :class:`vmas.make_env` function.
6+
The function arguments are explained in the documentation. The function returns an environment
7+
object which you can step and reset.
8+
9+
.. code-block:: python
10+
11+
import vmas
12+
13+
# Create the environment
14+
env = vmas.make_env(
15+
scenario="waterfall", # can be scenario name or BaseScenario class
16+
num_envs=32,
17+
device="cpu", # Or "cuda" for GPU
18+
continuous_actions=True,
19+
max_steps=None, # Defines the horizon. None is infinite horizon.
20+
seed=None, # Seed of the environment
21+
n_agents=3 # Additional arguments you want to pass to the scenario
22+
)
23+
# Reset it
24+
obs = env.reset()
25+
26+
# Step it with deterministic actions (all agents take their maximum range action)
27+
for _ in range(10):
28+
obs, rews, dones, info = env.step(env.get_random_actions())
29+
30+
Here is a python example on how you can execute vmas environments.
31+
32+
.. python_example_button::
33+
https://github.com/proroklab/VectorizedMultiAgentSimulator/blob/main/vmas/examples/use_vmas_env.py
34+
35+
The `Concepts` documentation contains a series of sections that
36+
can help you get familiar with further VMAS functionalities.

vmas/examples/use_vmas_env.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,6 @@
1111
from vmas.simulator.utils import save_video
1212

1313

14-
def _get_random_action(agent: Agent, continuous: bool, env):
15-
if continuous:
16-
actions = []
17-
for action_index in range(agent.action_size):
18-
actions.append(
19-
torch.zeros(
20-
agent.batch_dim,
21-
device=agent.device,
22-
dtype=torch.float32,
23-
).uniform_(
24-
-agent.action.u_range_tensor[action_index],
25-
agent.action.u_range_tensor[action_index],
26-
)
27-
)
28-
if env.world.dim_c != 0 and not agent.silent:
29-
# If the agent needs to communicate
30-
for _ in range(env.world.dim_c):
31-
actions.append(
32-
torch.zeros(
33-
agent.batch_dim,
34-
device=agent.device,
35-
dtype=torch.float32,
36-
).uniform_(
37-
0,
38-
1,
39-
)
40-
)
41-
action = torch.stack(actions, dim=-1)
42-
else:
43-
action = torch.randint(
44-
low=0,
45-
high=env.get_agent_action_space(agent).n,
46-
size=(agent.batch_dim,),
47-
device=agent.device,
48-
)
49-
return action
50-
51-
5214
def _get_deterministic_action(agent: Agent, continuous: bool, env):
5315
if continuous:
5416
action = -agent.action.u_range_tensor.expand(env.batch_dim, agent.action_size)
@@ -123,7 +85,7 @@ def use_vmas_env(
12385
if not random_action:
12486
action = _get_deterministic_action(agent, continuous_actions, env)
12587
else:
126-
action = _get_random_action(agent, continuous_actions, env)
88+
action = env.get_random_action(agent)
12789
if dict_actions:
12890
actions.update({agent.name: action})
12991
else:

vmas/simulator/environment/environment.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# All rights reserved.
44
import random
55
from ctypes import byref
6-
from typing import Callable, Dict, List, Optional, Tuple, Union
6+
from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union
77

88
import numpy as np
99
import torch
@@ -197,6 +197,21 @@ def step(self, actions: Union[List, Dict]):
197197
dones: Tensor of len 'self.num_envs' of which each element is a bool
198198
infos : List on len 'self.n_agents' of which each element is a dictionary for which each key is a metric
199199
and the value is a tensor of shape '(self.num_envs, metric_size_per_agent)'
200+
201+
Examples:
202+
>>> import vmas
203+
>>> env = vmas.make_env(
204+
... scenario="waterfall", # can be scenario name or BaseScenario class
205+
... num_envs=32,
206+
... device="cpu", # Or "cuda" for GPU
207+
... continuous_actions=True,
208+
... max_steps=None, # Defines the horizon. None is infinite horizon.
209+
... seed=None, # Seed of the environment
210+
... n_agents=3, # Additional arguments you want to pass to the scenario
211+
... )
212+
>>> obs = env.reset()
213+
>>> for _ in range(10):
214+
... obs, rews, dones, info = env.step(env.get_random_actions())
200215
"""
201216
if isinstance(actions, Dict):
202217
actions_dict = actions
@@ -352,6 +367,76 @@ def get_agent_observation_space(self, agent: Agent, obs: AGENT_OBS_TYPE):
352367
f"Invalid type of observation {obs} for agent {agent.name}"
353368
)
354369

370+
def get_random_action(self, agent: Agent) -> torch.Tensor:
371+
"""Returns a random action for the given agent.
372+
373+
Args:
374+
agent (Agent): The agent to get the action for
375+
376+
Returns:
377+
torch.tensor: the random actions tensor with shape ``(agent.batch_dim, agent.action_size)``
378+
379+
"""
380+
if self.continuous_actions:
381+
actions = []
382+
for action_index in range(agent.action_size):
383+
actions.append(
384+
torch.zeros(
385+
agent.batch_dim,
386+
device=agent.device,
387+
dtype=torch.float32,
388+
).uniform_(
389+
-agent.action.u_range_tensor[action_index],
390+
agent.action.u_range_tensor[action_index],
391+
)
392+
)
393+
if self.world.dim_c != 0 and not agent.silent:
394+
# If the agent needs to communicate
395+
for _ in range(self.world.dim_c):
396+
actions.append(
397+
torch.zeros(
398+
agent.batch_dim,
399+
device=agent.device,
400+
dtype=torch.float32,
401+
).uniform_(
402+
0,
403+
1,
404+
)
405+
)
406+
action = torch.stack(actions, dim=-1)
407+
else:
408+
action = torch.randint(
409+
low=0,
410+
high=self.get_agent_action_space(agent).n,
411+
size=(agent.batch_dim,),
412+
device=agent.device,
413+
)
414+
return action
415+
416+
def get_random_actions(self) -> Sequence[torch.Tensor]:
417+
"""Returns random actions for all agents that you can feed to :class:`step`
418+
419+
Returns:
420+
Sequence[torch.tensor]: the random actions for the agents
421+
422+
Examples:
423+
>>> import vmas
424+
>>> env = vmas.make_env(
425+
... scenario="waterfall", # can be scenario name or BaseScenario class
426+
... num_envs=32,
427+
... device="cpu", # Or "cuda" for GPU
428+
... continuous_actions=True,
429+
... max_steps=None, # Defines the horizon. None is infinite horizon.
430+
... seed=None, # Seed of the environment
431+
... n_agents=3, # Additional arguments you want to pass to the scenario
432+
... )
433+
>>> obs = env.reset()
434+
>>> for _ in range(10):
435+
... obs, rews, dones, info = env.step(env.get_random_actions())
436+
437+
"""
438+
return [self.get_random_action(agent) for agent in self.agents]
439+
355440
def _check_discrete_action(self, action: Tensor, low: int, high: int, type: str):
356441
assert torch.all(
357442
(action >= torch.tensor(low, device=self.device))

0 commit comments

Comments
 (0)