|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Tutorial: Reinforcement Learning with Mesa Environments\n", |
| 8 | + "\n", |
| 9 | + "# Welcome to this comprehensive guide on integrating reinforcement learning (RL) with Mesa environments. \n", |
| 10 | + "# Mesa, an agent-based modeling framework, offers an excellent platform to experiment with RL algorithms. \n", |
| 11 | + "# In this tutorial, we'll explore several examples of how RL can be applied to various Mesa environments, \n", |
| 12 | + "# starting with the **Epstein Civil Violence model**.\n", |
| 13 | + "\n", |
| 14 | + "# ## Getting Started\n", |
| 15 | + "\n", |
| 16 | + "# Before diving into the implementation, take a moment to familiarize yourself with the Epstein Civil Violence model.\n", |
| 17 | + "# This will give you a solid understanding of the environment we’ll be working with.\n", |
| 18 | + "\n", |
| 19 | + "# Next, ensure all dependencies are installed by following the instructions in the `README.md`.\n" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": null, |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "# ### Step 1: Importing the Necessary Modules\n", |
| 29 | + "# To begin, let’s import the required modules for the Epstein Civil Violence model:\n", |
| 30 | + "\n", |
| 31 | + "from epstein_civil_violence.model import EpsteinCivilViolenceRL\n", |
| 32 | + "from epstein_civil_violence.server import run_model\n", |
| 33 | + "from epstein_civil_violence.train_config import config\n", |
| 34 | + "from train import train_model\n", |
| 35 | + "\n", |
| 36 | + "# Here’s a breakdown of the modules:\n", |
| 37 | + "# - `EpsteinCivilViolenceRL`: Contains the core model and environment.\n", |
| 38 | + "# - `run_model`: Configures and runs the model for inference.\n", |
| 39 | + "# - `config`: Defines the parameters for training the model.\n", |
| 40 | + "# - `train_model`: Includes functions for training the RL agent using RLlib." |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "# ### Step 2: Initializing the Environment\n", |
| 50 | + "\n", |
| 51 | + "# Let's load and reset the environment. This also allows us to inspect the observation space:\n", |
| 52 | + "\n", |
| 53 | + "env = EpsteinCivilViolenceRL()\n", |
| 54 | + "observation, info = env.reset(seed=42)\n", |
| 55 | + "\n", |
| 56 | + "# Display initial observation and info\n", |
| 57 | + "print(\"Initial Observation:\", observation)\n", |
| 58 | + "print(\"Info:\", info)" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "# ### Step 3: Running the Environment with Random Actions\n", |
| 68 | + "\n", |
| 69 | + "# To get a feel for how the environment operates, let's run it for a few steps using random actions.\n", |
| 70 | + "# We’ll sample the action space for these actions:\n", |
| 71 | + "\n", |
| 72 | + "for _ in range(10):\n", |
| 73 | + " action_dict = {}\n", |
| 74 | + " for agent in env.schedule.agents:\n", |
| 75 | + " action_dict[agent.unique_id] = env.action_space.sample()\n", |
| 76 | + " observation, reward, terminated, truncated, info = env.step(action_dict)\n", |
| 77 | + "\n", |
| 78 | + " print(\n", |
| 79 | + " f\"Observation: {observation}, Reward: {reward}, Terminated: {terminated}, Truncated: {truncated}, Info: {info}\"\n", |
| 80 | + " )\n", |
| 81 | + "\n", |
| 82 | + " if terminated or truncated:\n", |
| 83 | + " observation, info = env.reset()" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "# ### Step 4: Training the Model\n", |
| 93 | + "\n", |
| 94 | + "# Now that you're familiar with the environment, let's train the RL model using the preset configuration:\n", |
| 95 | + "\n", |
| 96 | + "train_model(\n", |
| 97 | + " config, num_iterations=1, result_path=\"results.txt\", checkpoint_dir=\"checkpoints\"\n", |
| 98 | + ")\n", |
| 99 | + "\n", |
| 100 | + "# You can modify the training parameters in the `train_config.py` file to experiment with different outcomes." |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "# ### Step 5: Visualizing the Results\n", |
| 110 | + "\n", |
| 111 | + "# After training, you can visualize the results by running inference on the model.\n", |
| 112 | + "# Mesa's built-in visualization tools will help you launch a webpage to view the model's performance:\n", |
| 113 | + "\n", |
| 114 | + "# server = run_model(path=\"checkpoints\")\n", |
| 115 | + "# You can also try running pre-trained checkpoints present in model folder\n", |
| 116 | + "server = run_model(model_path=\"rl_models/epstein_civil_violence\")\n", |
| 117 | + "server.port = 6005\n", |
| 118 | + "server.launch(open_browser=True)" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "markdown", |
| 123 | + "metadata": {}, |
| 124 | + "source": [ |
| 125 | + "# ### Alternative Approach: Using Stable-Baselines with Mesa\n", |
| 126 | + "\n", |
| 127 | + "# In the example above, we utilized RLlib to integrate reinforcement learning algorithms with the Mesa environment, \n", |
| 128 | + "# which is particularly useful when you want different policies for different agents. \n", |
| 129 | + "# However, if your use case requires a simpler setup where all agents follow the same policy, \n", |
| 130 | + "# you can opt for Stable-Baselines. An example of integrating Stable-Baselines with Mesa can be found in the Boltzmann Money model.\n", |
| 131 | + "\n", |
| 132 | + "# You can explore more on how to use Stable-Baselines with Mesa by following the respective documentation.\n" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "# ### Implementing Your Own Cases\n", |
| 140 | + "\n", |
| 141 | + "# If you're ready to explore RL in different agent-based scenarios, you can start by experimenting with various examples we provide at Mesa-Examples:\n", |
| 142 | + "# Link: https://github.com/projectmesa/mesa-examples\n", |
| 143 | + "\n", |
| 144 | + "# These examples cover a range of scenarios and offer a great starting point for understanding how to apply RL within Mesa environments.\n", |
| 145 | + "\n", |
| 146 | + "# If you have your own scenario in mind, you can create it as a Mesa model by following this series of Tutorials:\n", |
| 147 | + "# Link: https://mesa.readthedocs.io/en/stable/tutorials/intro_tutorial.html\n", |
| 148 | + "\n", |
| 149 | + "# Once your scenario is set up as a Mesa model, you can refer to the code in the provided implementations to see how the RL components are built on top of the respective Mesa models.\n" |
| 150 | + ] |
| 151 | + } |
| 152 | + ], |
| 153 | + "metadata": { |
| 154 | + "kernelspec": { |
| 155 | + "display_name": "test", |
| 156 | + "language": "python", |
| 157 | + "name": "python3" |
| 158 | + }, |
| 159 | + "language_info": { |
| 160 | + "codemirror_mode": { |
| 161 | + "name": "ipython", |
| 162 | + "version": 3 |
| 163 | + }, |
| 164 | + "file_extension": ".py", |
| 165 | + "mimetype": "text/x-python", |
| 166 | + "name": "python", |
| 167 | + "nbconvert_exporter": "python", |
| 168 | + "pygments_lexer": "ipython3", |
| 169 | + "version": "3.10.0" |
| 170 | + } |
| 171 | + }, |
| 172 | + "nbformat": 4, |
| 173 | + "nbformat_minor": 2 |
| 174 | +} |
0 commit comments