Skip to content

Commit 30a3475

Browse files
committed
mesa 3, rl wolf-sheep
- wolfsheep updates to 3
1 parent 4bce8d4 commit 30a3475

File tree

15 files changed

+235
-259
lines changed

15 files changed

+235
-259
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ var/
2323
.installed.cfg
2424
*.egg
2525

26+
# ignore RL file - users download model on own
27+
rl
28+
2629
# PyInstaller
2730
# Usually these files are written by a python script from a template
2831
# before PyInstaller builds the exe, so as to inject date/other infos into it.
@@ -58,6 +61,7 @@ target/
5861

5962
# Jupyter and iPython notebook checkpoints
6063
*.ipynb_checkpoints
64+
*.virtual_documents
6165

6266
# Spyder app workspace config file
6367
.spyderworkspace

rl/README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,28 @@ This repository demonstrates various applications of reinforcement learning (RL)
1414
1. **Install Mesa Models**
1515
Begin by installing the Mesa models:
1616

17+
#TODO: Update this -- do release?
18+
1719
```bash
1820
pip install -U -e git+https://github.com/projectmesa/[email protected]#egg=mesa-models
1921
```
2022

21-
2. **Install RLlib for Multi-Agent Training**
23+
3. **Install RLlib for Multi-Agent Training**
2224
Next, install RLlib along with TensorFlow and PyTorch to support multi-agent training algorithms:
2325

2426
```bash
2527
pip install "ray[rllib]" tensorflow torch
2628
```
29+
#TODO Update requirements to mesa[rec] >3.0
2730

28-
3. **Install Additional Dependencies**
31+
4. **Install Additional Dependencies**
2932
Finally, install any remaining dependencies:
3033

3134
```bash
3235
pip install -r requirements.txt
3336
```
3437

35-
4. **Download Pre-Trained Weights**
38+
5. **Download Pre-Trained Weights**
3639
Download pre-trained weights from hugging face:
3740

3841
```bash

rl/Tutorials.ipynb

+58-27
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,50 @@
66
"source": [
77
"# Tutorial: Reinforcement Learning with Mesa Environments\n",
88
"\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",
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**."
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"## Getting Started\n",
1520
"\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",
21+
"Before diving into the implementation, take a moment to familiarize yourself with the Epstein Civil Violence model.\n",
22+
"This will give you a solid understanding of the environment we’ll be working with.\n",
1823
"\n",
19-
"# Next, ensure all dependencies are installed by following the instructions in the `README.md`.\n"
24+
"Next, ensure all dependencies are installed by following the instructions in the `README.md`."
2025
]
2126
},
2227
{
2328
"cell_type": "code",
24-
"execution_count": null,
29+
"execution_count": 1,
2530
"metadata": {},
26-
"outputs": [],
31+
"outputs": [
32+
{
33+
"name": "stdout",
34+
"output_type": "stream",
35+
"text": [
36+
"WARNING:tensorflow:From C:\\Users\\thoma\\miniconda3\\envs\\mesa_dev\\Lib\\site-packages\\ray\\rllib\\utils\\framework.py:130: The name tf.logging.set_verbosity is deprecated. Please use tf.compat.v1.logging.set_verbosity instead.\n",
37+
"\n"
38+
]
39+
},
40+
{
41+
"ename": "ModuleNotFoundError",
42+
"evalue": "No module named 'mesa_models.epstein_civil_violence'",
43+
"output_type": "error",
44+
"traceback": [
45+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
46+
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
47+
"Cell \u001b[1;32mIn[1], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# ### Step 1: Importing the Necessary Modules\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# To begin, let’s import the required modules for the Epstein Civil Violence model:\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mepstein_civil_violence\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m EpsteinCivilViolenceRL\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mepstein_civil_violence\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mserver\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m run_model\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mepstein_civil_violence\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtrain_config\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m config\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtrain\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m train_model\n",
48+
"File \u001b[1;32m~\\Documents\\GitHub\\dev\\mesa-examples\\rl\\epstein_civil_violence\\server.py:4\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mray\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmesa_models\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mepstein_civil_violence\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mportrayal\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m citizen_cop_portrayal\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mray\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m tune\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mray\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mrllib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01malgorithms\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01malgorithm\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Algorithm\n",
49+
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'mesa_models.epstein_civil_violence'"
50+
]
51+
}
52+
],
2753
"source": [
2854
"# ### Step 1: Importing the Necessary Modules\n",
2955
"# To begin, let’s import the required modules for the Epstein Civil Violence model:\n",
@@ -122,37 +148,42 @@
122148
"cell_type": "markdown",
123149
"metadata": {},
124150
"source": [
125-
"# ### Alternative Approach: Using Stable-Baselines with Mesa\n",
151+
"# Alternative Approach: Using Stable-Baselines with Mesa\n",
126152
"\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",
153+
"In the example above, we utilized RLlib to integrate reinforcement learning algorithms with the Mesa environment, which is particularly useful when you want different policies for different agents. \n",
154+
"However, if your use case requires a simpler setup where all agents follow the same policy, you can opt for Stable-Baselines. An example of integrating Stable-Baselines with Mesa can be found in the Boltzmann Money model.\n",
131155
"\n",
132-
"# You can explore more on how to use Stable-Baselines with Mesa by following the respective documentation.\n"
156+
"You can explore more on how to use Stable-Baselines with Mesa by following the respective documentation.\n"
133157
]
134158
},
135159
{
136160
"cell_type": "markdown",
137161
"metadata": {},
138162
"source": [
139-
"# ### Implementing Your Own Cases\n",
163+
"# Implementing Your Own RL Models\n",
140164
"\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",
165+
"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",
166+
"Link: https://github.com/projectmesa/mesa-examples\n",
143167
"\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",
168+
"These examples cover a range of scenarios and offer a great starting point for understanding how to apply RL within Mesa environments.\n",
145169
"\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",
170+
"If you have your own scenario in mind, you can create it as a Mesa model by following this series of Tutorials:\n",
171+
"Link: https://mesa.readthedocs.io/en/stable/tutorials/intro_tutorial.html\n",
148172
"\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"
173+
"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"
150174
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {},
180+
"outputs": [],
181+
"source": []
151182
}
152183
],
153184
"metadata": {
154185
"kernelspec": {
155-
"display_name": "test",
186+
"display_name": "Python 3 (ipykernel)",
156187
"language": "python",
157188
"name": "python3"
158189
},
@@ -166,9 +197,9 @@
166197
"name": "python",
167198
"nbconvert_exporter": "python",
168199
"pygments_lexer": "ipython3",
169-
"version": "3.10.0"
200+
"version": "3.12.5"
170201
}
171202
},
172203
"nbformat": 4,
173-
"nbformat_minor": 2
204+
"nbformat_minor": 4
174205
}

rl/epstein_civil_violence/agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mesa_models.epstein_civil_violence.agent import Citizen, Cop
1+
from mesa.examples.advanced.epstein_civil_violence.agents import Citizen, Cop
22

33
from .utility import move
44

rl/epstein_civil_violence/model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gymnasium as gym
22
import mesa
33
import numpy as np
4-
from mesa_models.epstein_civil_violence.model import EpsteinCivilViolence
4+
from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence
55
from ray.rllib.env import MultiAgentEnv
66

77
from .agent import CitizenRL, CopRL

rl/wolf_sheep/agents.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from mesa_models.wolf_sheep.agents import GrassPatch, Sheep, Wolf
2-
3-
from .utility import move
1+
from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf
2+
from utility import move
43

54

65
class SheepRL(Sheep):
@@ -28,7 +27,7 @@ def step(self):
2827
# Death
2928
if self.energy < 0:
3029
self.model.grid.remove_agent(self)
31-
self.model.schedule.remove(self)
30+
self.model.remove(self)
3231
living = False
3332

3433
if living and self.random.random() < self.model.sheep_reproduce:
@@ -38,7 +37,7 @@ def step(self):
3837
unique_id_str = f"sheep_{self.model.next_id()}"
3938
lamb = SheepRL(unique_id_str, self.pos, self.model, self.moore, self.energy)
4039
self.model.grid.place_agent(lamb, self.pos)
41-
self.model.schedule.add(lamb)
40+
self.model.add(lamb)
4241

4342

4443
class WolfRL(Wolf):
@@ -62,12 +61,12 @@ def step(self):
6261

6362
# Kill the sheep
6463
self.model.grid.remove_agent(sheep_to_eat)
65-
self.model.schedule.remove(sheep_to_eat)
64+
self.model.remove(sheep_to_eat)
6665

6766
# Death or reproduction
6867
if self.energy < 0:
6968
self.model.grid.remove_agent(self)
70-
self.model.schedule.remove(self)
69+
self.model.remove(self)
7170
else:
7271
if self.random.random() < self.model.wolf_reproduce:
7372
# Create a new wolf cub
@@ -77,4 +76,4 @@ def step(self):
7776
unique_id_str, self.pos, self.model, self.moore, self.energy
7877
)
7978
self.model.grid.place_agent(cub, cub.pos)
80-
self.model.schedule.add(cub)
79+
self.model.add(cub)

rl/wolf_sheep/app.py

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf
2+
from mesa.examples.advanced.wolf_sheep.model import WolfSheep
3+
from mesa.experimental.devs import ABMSimulator
4+
from mesa.visualization import (
5+
Slider,
6+
SolaraViz,
7+
make_plot_component,
8+
make_space_component,
9+
)
10+
from model import WolfSheepRL
11+
from ray import tune
12+
from ray.rllib.algorithms.algorithm import Algorithm
13+
14+
model_params = {
15+
"width": 20,
16+
"height": 20,
17+
"initial_sheep": 100,
18+
"initial_wolves": 25,
19+
"sheep_reproduce": 0.04,
20+
"wolf_reproduce": 0.05,
21+
"wolf_gain_from_food": 20,
22+
"grass": True,
23+
"grass_regrowth_time": 30,
24+
"sheep_gain_from_food": 4,
25+
"seed": 42,
26+
"simulator": ABMSimulator(),
27+
"vision": 4,
28+
"model_path": None,
29+
}
30+
31+
32+
class WolfSheepServer(WolfSheepRL):
33+
def __init__(self, **model_params):
34+
super().__init__(**model_params)
35+
36+
def env_creator(_):
37+
return WolfSheepRL(**model_params)
38+
39+
tune.register_env("WorldSheepModel-v0", env_creator)
40+
self.iteration = 0
41+
# Load the model from checkpoint
42+
checkpoint_path = self.model_path
43+
algo = Algorithm.from_checkpoint(checkpoint_path)
44+
self.wolf_policy = algo.get_policy("policy_wolf")
45+
self.sheep_policy = algo.get_policy("policy_sheep")
46+
47+
48+
def wolf_sheep_portrayal(agent):
49+
if agent is None:
50+
return
51+
52+
portrayal = {
53+
"size": 25,
54+
}
55+
56+
if isinstance(agent, Wolf):
57+
portrayal["color"] = "tab:red"
58+
portrayal["marker"] = "o"
59+
portrayal["zorder"] = 2
60+
elif isinstance(agent, Sheep):
61+
portrayal["color"] = "tab:cyan"
62+
portrayal["marker"] = "o"
63+
portrayal["zorder"] = 2
64+
elif isinstance(agent, GrassPatch):
65+
if agent.fully_grown:
66+
portrayal["color"] = "tab:green"
67+
else:
68+
portrayal["color"] = "tab:brown"
69+
portrayal["marker"] = "s"
70+
portrayal["size"] = 75
71+
72+
return portrayal
73+
74+
75+
model_params = {
76+
"seed": {
77+
"type": "InputText",
78+
"value": 42,
79+
"label": "Random Seed",
80+
},
81+
"grass": {
82+
"type": "Select",
83+
"value": True,
84+
"values": [True, False],
85+
"label": "grass regrowth enabled?",
86+
},
87+
"grass_regrowth_time": Slider("Grass Regrowth Time", 20, 1, 50),
88+
"initial_sheep": Slider("Initial Sheep Population", 100, 10, 300),
89+
"sheep_reproduce": Slider("Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01),
90+
"initial_wolves": Slider("Initial Wolf Population", 10, 5, 100),
91+
"wolf_reproduce": Slider(
92+
"Wolf Reproduction Rate",
93+
0.05,
94+
0.01,
95+
1.0,
96+
0.01,
97+
),
98+
"wolf_gain_from_food": Slider("Wolf Gain From Food Rate", 20, 1, 50),
99+
"sheep_gain_from_food": Slider("Sheep Gain From Food", 4, 1, 10),
100+
}
101+
102+
103+
def post_process_space(ax):
104+
ax.set_aspect("equal")
105+
ax.set_xticks([])
106+
ax.set_yticks([])
107+
108+
109+
def post_process_lines(ax):
110+
ax.legend(loc="center left", bbox_to_anchor=(1, 0.9))
111+
112+
113+
space_component = make_space_component(
114+
wolf_sheep_portrayal, draw_grid=False, post_process=post_process_space
115+
)
116+
lineplot_component = make_plot_component(
117+
{"Wolves": "tab:orange", "Sheep": "tab:cyan", "Grass": "tab:green"},
118+
post_process=post_process_lines,
119+
)
120+
121+
simulator = ABMSimulator()
122+
model = WolfSheep(simulator=simulator, grass=True)
123+
124+
page = SolaraViz(
125+
model,
126+
components=[space_component, lineplot_component],
127+
model_params=model_params,
128+
name="Wolf Sheep",
129+
simulator=simulator,
130+
)
131+
page # noqa

0 commit comments

Comments
 (0)