From 0b56e3268ec89238e8a24cb05d397ff75e19d59d Mon Sep 17 00:00:00 2001 From: Matteo Bettini Date: Sat, 7 Oct 2023 18:05:13 +0100 Subject: [PATCH] [Doc] Add notebook Signed-off-by: Matteo Bettini --- README.md | 3 +- examples/running/run_benchmark.py | 2 +- notebooks/run.ipynb | 260 ++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 notebooks/run.ipynb diff --git a/README.md b/README.md index 84c01bda..6f7e0c15 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ python benchmarl/run.py algorithm=mappo task=vmas/balance ``` -[![Examples](https://img.shields.io/badge/Examples-blue.svg)](examples) +[![Examples](https://img.shields.io/badge/Examples-blue.svg)](examples) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/facebookresearch/BenchMARL/blob/main/notebooks/run.ipynb) @@ -44,6 +44,7 @@ statistically strong evaluations. ## How to use ### Notebooks +- [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/facebookresearch/BenchMARL/blob/main/notebooks/run.ipynb)   **Running BenchMARL experiments**. ### Install diff --git a/examples/running/run_benchmark.py b/examples/running/run_benchmark.py index 39434dec..12bd9cb7 100644 --- a/examples/running/run_benchmark.py +++ b/examples/running/run_benchmark.py @@ -9,7 +9,7 @@ # Loads from "benchmarl/conf/experiment/base_experiment.yaml" experiment_config = ExperimentConfig.get_from_yaml() - # Loads from "benchmarl/conf/task" + # Loads from "benchmarl/conf/task/vmas" tasks = [VmasTask.BALANCE.get_from_yaml(), VmasTask.SAMPLING.get_from_yaml()] # Loads from "benchmarl/conf/algorithm" diff --git a/notebooks/run.ipynb b/notebooks/run.ipynb new file mode 100644 index 00000000..c533a977 --- /dev/null +++ b/notebooks/run.ipynb @@ -0,0 +1,260 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "20326118", + "metadata": {}, + "source": [ + "# Install" + ] + }, + { + "cell_type": "markdown", + "id": "2422f32f", + "metadata": {}, + "source": [ + "## Install BenchMARL" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b10fd32", + "metadata": {}, + "outputs": [], + "source": [ + "#@title\n", + "!git clone https://github.com/facebookresearch/BenchMARL" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cd7b69b", + "metadata": {}, + "outputs": [], + "source": [ + "#@title\n", + "%cd /content/BenchMARL" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f32b88e", + "metadata": {}, + "outputs": [], + "source": [ + "#@title\n", + "!pip install -e ." + ] + }, + { + "cell_type": "markdown", + "id": "585d3a35", + "metadata": {}, + "source": [ + "## Install VMAS" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d2e551b1", + "metadata": {}, + "outputs": [], + "source": [ + "#@title\n", + "!pip install vmas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33d72783", + "metadata": {}, + "outputs": [], + "source": [ + "#@title\n", + "!apt-get update\n", + "!apt-get install -y x11-utils \n", + "!apt-get install -y xvfb\n", + "!apt-get install -y imagemagick\n", + "!pip install pyvirtualdisplay\n", + "import pyvirtualdisplay\n", + "display = pyvirtualdisplay.Display(visible=False, size=(1400, 900))\n", + "display.start()" + ] + }, + { + "cell_type": "markdown", + "id": "caa7225f", + "metadata": {}, + "source": [ + "# Launch from command line" + ] + }, + { + "cell_type": "markdown", + "id": "30075032", + "metadata": {}, + "source": [ + "To launch an experiment from the command line you can do" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5369898f", + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "!python benchmarl/run.py algorithm=mappo task=vmas/balance experiment.max_n_iters=2 \"experiment.loggers=[]\"" + ] + }, + { + "cell_type": "markdown", + "id": "23f9338f", + "metadata": {}, + "source": [ + "You can run benchmarks as multi-runs like" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "90a135ea", + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "!python benchmarl/run.py -m algorithm=mappo,qmix,masac task=vmas/balance,vmas/sampling seed=0,1 experiment.max_n_iters=2 \"experiment.loggers=[]\"" + ] + }, + { + "cell_type": "markdown", + "id": "0b01091a", + "metadata": {}, + "source": [ + "# Launch from a python script" + ] + }, + { + "cell_type": "markdown", + "id": "67c6dc69", + "metadata": {}, + "source": [ + "You can also load and launch your experiments from within a script" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c5b5fcd", + "metadata": {}, + "outputs": [], + "source": [ + "from benchmarl.algorithms import MappoConfig\n", + "from benchmarl.environments import VmasTask\n", + "from benchmarl.experiment import Experiment, ExperimentConfig\n", + "from benchmarl.models.mlp import MlpConfig\n", + "\n", + "# Loads from \"benchmarl/conf/experiment/base_experiment.yaml\"\n", + "experiment_config = ExperimentConfig.get_from_yaml()\n", + "# Loads from \"benchmarl/conf/task/vmas/balance.yaml\"\n", + "task = VmasTask.BALANCE.get_from_yaml()\n", + "# Loads from \"benchmarl/conf/algorithm/mappo.yaml\"\n", + "algorithm_config = MappoConfig.get_from_yaml()\n", + "# Loads from \"benchmarl/conf/model/layers/mlp.yaml\"\n", + "model_config = MlpConfig.get_from_yaml()\n", + "critic_model_config = MlpConfig.get_from_yaml()\n", + "\n", + "experiment_config.max_n_iters = 2\n", + "experiment_config.loggers = []\n", + "\n", + "experiment = Experiment(\n", + " task=task,\n", + " algorithm_config=algorithm_config,\n", + " model_config=model_config,\n", + " critic_model_config=critic_model_config,\n", + " seed=0,\n", + " config=experiment_config,\n", + ")\n", + "experiment.run()" + ] + }, + { + "cell_type": "markdown", + "id": "a20864e3", + "metadata": {}, + "source": [ + "You can also run multiple experiments in a Benchmark." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ec3bd4b", + "metadata": {}, + "outputs": [], + "source": [ + "from benchmarl.algorithms import MappoConfig, MasacConfig, QmixConfig\n", + "from benchmarl.benchmark import Benchmark\n", + "from benchmarl.environments import VmasTask\n", + "from benchmarl.experiment import ExperimentConfig\n", + "from benchmarl.models.mlp import MlpConfig\n", + "\n", + "# Loads from \"benchmarl/conf/experiment/base_experiment.yaml\"\n", + "experiment_config = ExperimentConfig.get_from_yaml()\n", + "# Loads from \"benchmarl/conf/task/vmas\"\n", + "tasks = [VmasTask.BALANCE.get_from_yaml(), VmasTask.SAMPLING.get_from_yaml()]\n", + "# Loads from \"benchmarl/conf/algorithm\"\n", + "algorithm_configs = [\n", + " MappoConfig.get_from_yaml(),\n", + " QmixConfig.get_from_yaml(),\n", + " MasacConfig.get_from_yaml(),\n", + "]\n", + "# Loads from \"benchmarl/conf/model/layers\"\n", + "model_config = MlpConfig.get_from_yaml()\n", + "critic_model_config = MlpConfig.get_from_yaml()\n", + "\n", + "experiment_config.max_n_iters = 2\n", + "experiment_config.loggers = []\n", + "\n", + "benchmark = Benchmark(\n", + " algorithm_configs=algorithm_configs,\n", + " tasks=tasks,\n", + " seeds={0, 1},\n", + " experiment_config=experiment_config,\n", + " model_config=model_config,\n", + " critic_model_config=critic_model_config,\n", + ")\n", + "benchmark.run_sequential()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.16" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}