Skip to content

Commit c4b4c75

Browse files
Add seeding RL examples folder (#178)
Add seeding RL examples folder (#178) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 831be82 commit c4b4c75

File tree

16 files changed

+837
-0
lines changed

16 files changed

+837
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ $ pip install -U -e git+https://github.com/projectmesa/mesa-examples@mesa-2.x#eg
1919
```
2020
```python
2121
from mesa_models.boltzmann_wealth_model.model import BoltzmannWealthModel
22+
2223
```
2324
You can see the available models at [setup.cfg](https://github.com/projectmesa/mesa-examples/blob/main/setup.cfg).
2425

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ test_gis = [
1818
"pytest",
1919
"momepy",
2020
]
21+
rl_example = [
22+
"stable-baselines3",
23+
"seaborn",
24+
"mesa",
25+
"tensorboard"
26+
]
2127

2228
[build-system]
2329
requires = [

rl/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/

rl/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Reinforcement Learning Implementations with Mesa
2+
3+
This repository demonstrates various applications of reinforcement learning (RL) using the Mesa agent-based modeling framework.
4+
5+
<p align="center">
6+
<img src="wolf_sheep/resources/wolf_sheep.gif" width="500" height="400">
7+
</p>
8+
9+
## Getting Started
10+
11+
### Installation
12+
13+
*Given the number of dependencies required, we recommend starting by creating a Conda environment or a Python virtual environment.*
14+
1. **Install Mesa Models**
15+
Begin by installing the Mesa models:
16+
17+
```bash
18+
pip install -U -e git+https://github.com/projectmesa/mesa-examples@mesa-2.x#egg=mesa-models
19+
```
20+
21+
2. **Install RLlib for Multi-Agent Training**
22+
Next, install RLlib along with TensorFlow and PyTorch to support multi-agent training algorithms:
23+
24+
```bash
25+
pip install "ray[rllib]" tensorflow torch
26+
```
27+
28+
3. **Install Additional Dependencies**
29+
Finally, install any remaining dependencies:
30+
31+
```bash
32+
pip install -r requirements.txt
33+
```
34+
35+
4. **Download Pre-Trained Weights**
36+
Download pre-trained weights from hugging face:
37+
38+
```bash
39+
git clone https://huggingface.co/projectmesa/rl_models/
40+
```
41+
42+
### Running the Examples
43+
44+
To test the code, simply execute `example.py`:
45+
46+
```bash
47+
python example.py
48+
```
49+
50+
*Note: Pre-trained models might not work in some cases because of differnce in versions of libraries used to train and test.*
51+
52+
To learn about individual implementations, please refer to the README files of specific environments.
53+
54+
55+
## Tutorials
56+
57+
For detailed tutorials on how to use these implementations and guidance on starting your own projects, please refer to [Tutorials.md](./Tutorials.md).
58+
59+
Here's a refined version of your contribution guide:
60+
61+
62+
## Contribution Guide
63+
64+
We welcome contributions to our project! A great way to get started is by implementing the remaining examples listed in the [Mesa-Examples](https://github.com/projectmesa/mesa-examples) repository with reinforcement learning (RL).
65+
66+
Additionally, if you have your own Mesa environments that you think would benefit from RL integration, we encourage you to share them with us. Simply start an issue on our GitHub repository with your suggestion, and we can collaborate on bringing it to life!

rl/Tutorials.ipynb

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}

rl/epstein_civil_violence/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Modelling Violence: Epstein Civil Violence Model
2+
3+
This project demonstrates the use of the RLlib library to implement Multi-Agent Reinforcement Learning (MARL) in the classic Epstein-Civil Violence problem. The environment details can be found on the Mesa project's GitHub repository [here](https://github.com/projectmesa/mesa-examples/tree/main/examples/epstein_civil_violence).
4+
5+
## Key Features
6+
7+
**RLlib and Multi-Agent Learning**:
8+
- **Library Utilized**: The project leverages the RLlib library to concurrently train two independent PPO (Proximal Policy Optimization) agents.
9+
- **Agents**:
10+
- **Police**: Aims to control violence (Reduce active agent)
11+
- **Citizen**: Aims to show resistence (be active) without getting arrested
12+
13+
**Input and Observation Space**:
14+
- **Observation Grid**: Each agent's policy receives a 4 radius grid centered on itself as input.
15+
16+
**Action Space**:
17+
- **Action Space**: For citizen the action space is the ID of the neighboring tile to which the agent wants to move along with choice to be active. For cop the action space is ID of neighbourng tile it wants to move along with ID of active citizen in it's neigbhood that it wants to arrest.
18+
**Behavior and Training Outcomes**:
19+
20+
**Optimal Behavior**:
21+
- **Cops**: Learns to move towards active agents and arrest them.
22+
- **Citizens**: Learns to run away from cops and be active only if a cop isn't around.
23+
- **Density Variations**: You can vary the densities of sheep and wolves to observe different results.
24+
25+
By leveraging RLlib and Multi-Agent Learning, this project provides insights into the dynamics of violence in a society and various strategies in a simulated environment.
26+
27+
28+
<p align="center">
29+
<img src="resources/epstein.gif" width="500" height="400">
30+
</p>

rl/epstein_civil_violence/agent.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from mesa_models.epstein_civil_violence.agent import Citizen, Cop
2+
3+
from .utility import move
4+
5+
6+
class CitizenRL(Citizen):
7+
def step(self):
8+
# Get action from action_dict
9+
action_tuple = self.model.action_dict[self.unique_id]
10+
# If in jail decrease sentence, else update condition
11+
if self.jail_sentence > 0:
12+
self.jail_sentence -= 1
13+
else:
14+
# RL Logic
15+
# Update condition and postion based on action
16+
self.condition = "Active" if action_tuple[0] == 1 else "Quiescent"
17+
# Update neighbors for updated empty neighbors
18+
self.update_neighbors()
19+
if self.model.movement:
20+
move(
21+
self,
22+
action_tuple[1],
23+
self.empty_neighbors,
24+
)
25+
26+
# Update the neighbors for observation space
27+
self.update_neighbors()
28+
29+
30+
class CopRL(Cop):
31+
def step(self):
32+
# RL Logics
33+
# Arrest if active citizen is indicated in action
34+
action_tuple = self.model.action_dict[self.unique_id]
35+
arrest_pos = self.neighborhood[action_tuple[0]]
36+
for agent in self.model.grid.get_cell_list_contents(self.neighborhood):
37+
if (
38+
isinstance(agent, CitizenRL)
39+
and agent.condition == "Active"
40+
and agent.jail_sentence == 0
41+
and agent.pos == arrest_pos
42+
):
43+
agent.jail_sentence = self.random.randint(1, self.model.max_jail_term)
44+
agent.condition = "Quiescent"
45+
self.arrest_made = True
46+
break
47+
else:
48+
self.arrest_made = False
49+
# Update neighbors for updated empty neighbors
50+
self.update_neighbors()
51+
# Move based on action
52+
if self.model.movement:
53+
move(self, action_tuple[1], self.empty_neighbors)
54+
# Update the neighbors for observation space
55+
self.update_neighbors()

0 commit comments

Comments
 (0)