Skip to content

Commit efb7d45

Browse files
authored
Reorganize advanced examples: wolf_sheep and sugarscape_g1mt (#2410)
1 parent 27e320b commit efb7d45

20 files changed

+144
-338
lines changed

mesa/examples/advanced/sugarscape_g1mt/Readme.md

+6-29
Original file line numberDiff line numberDiff line change
@@ -34,51 +34,28 @@ cross over one another otherwise *end*.
3434
(Epstein and Axtell, 1996, p. 105)
3535

3636
The model demonstrates several Mesa concepts and features:
37-
- MultiGrid
37+
- OrthogonalMooreGrid
3838
- Multiple agent types (traders, sugar, spice)
3939
- Dynamically removing agents from the grid and schedule when they die
4040
- Data Collection at the model and agent level
41-
- Batchrunner (i.e. parameter sweeps)
41+
- custom solara matplotlib space visualization
4242

43-
## Installation
44-
45-
To install the dependencies use pip and the requirements.txt in this directory. e.g.
46-
47-
```
48-
$ pip install -r requirements.txt
49-
```
5043

5144
## How to Run
52-
53-
To run the model a single instance of the model:
54-
55-
```
56-
$ python run.py -s
57-
```
58-
59-
To run the model with BatchRunner:
60-
61-
```
62-
$ python run.py -b
63-
```
64-
6545
To run the model interactively:
6646

6747
```
68-
$ mesa runserver
48+
$ solara run app.py
6949
```
7050

7151
Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
7252

7353
## Files
7454

75-
* `sugarscape_g1mt/trader_agents.py`: Defines the Trader agent class.
76-
* `sugarscape_g1mt/resource_agents.py`: Defines the Resource agent class which contains an amount of sugar and spice.
77-
* `sugarscape_g1mt/model.py`: Manages the Sugarscape Constant Growback with Traders model.
78-
* `sugarscape_g1mt/sugar_map.txt`: Provides sugar and spice landscape in raster type format.
79-
* `server.py`: Sets up an interactive visualization server.
80-
* `run.py`: Runs Server, Single Run or Batch Run with data collection and basic analysis.
55+
* `model.py`: The Sugarscape Constant Growback with Traders model.
56+
* `agents.py`: Defines the Trader agent class and the Resource agent class which contains an amount of sugar and spice.
8157
* `app.py`: Runs a visualization server via Solara (`solara run app.py`).
58+
* `sugar_map.txt`: Provides sugar and spice landscape in raster type format.
8259
* `tests.py`: Has tests to ensure that the model reproduces the results in shown in Growing Artificial Societies.
8360

8461
## Additional Resources

mesa/examples/advanced/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py mesa/examples/advanced/sugarscape_g1mt/agents.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import math
22

33
from mesa.experimental.cell_space import CellAgent
4+
from mesa.experimental.cell_space import FixedAgent
45

5-
from .resource_agents import Resource
66

77

88
# Helper function
@@ -18,6 +18,32 @@ def get_distance(cell_1, cell_2):
1818
dx = x1 - x2
1919
dy = y1 - y2
2020
return math.sqrt(dx**2 + dy**2)
21+
class Resource(FixedAgent):
22+
"""
23+
Resource:
24+
- contains an amount of sugar and spice
25+
- grows 1 amount of sugar at each turn
26+
- grows 1 amount of spice at each turn
27+
"""
28+
29+
def __init__(self, model, max_sugar, max_spice, cell):
30+
super().__init__(model)
31+
self.sugar_amount = max_sugar
32+
self.max_sugar = max_sugar
33+
self.spice_amount = max_spice
34+
self.max_spice = max_spice
35+
self.cell = cell
36+
37+
def step(self):
38+
"""
39+
Growth function, adds one unit of sugar and spice each step up to
40+
max amount
41+
"""
42+
self.sugar_amount = min([self.max_sugar, self.sugar_amount + 1])
43+
self.spice_amount = min([self.max_spice, self.spice_amount + 1])
44+
45+
46+
2147

2248

2349
class Trader(CellAgent):

mesa/examples/advanced/sugarscape_g1mt/app.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import sys
2+
import os.path
3+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../../')))
4+
5+
16
import numpy as np
27
import solara
38
from matplotlib.figure import Figure
49
from mesa.visualization import SolaraViz, make_plot_measure
10+
511
from sugarscape_g1mt.model import SugarscapeG1mt
612
from sugarscape_g1mt.trader_agents import Trader
713

@@ -14,19 +20,19 @@ def portray(g):
1420
"trader": {"x": [], "y": [], "c": "tab:red", "marker": "o", "s": 10},
1521
}
1622

17-
for content, (i, j) in g.coord_iter():
18-
for agent in content:
19-
if isinstance(agent, Trader):
20-
layers["trader"]["x"].append(i)
21-
layers["trader"]["y"].append(j)
22-
else:
23-
# Don't visualize resource with value <= 1.
24-
layers["sugar"][i][j] = (
25-
agent.sugar_amount if agent.sugar_amount > 1 else np.nan
26-
)
27-
layers["spice"][i][j] = (
28-
agent.spice_amount if agent.spice_amount > 1 else np.nan
29-
)
23+
for agent in g.all_cells.agents:
24+
i, j = agent.cell.coordinate
25+
if isinstance(agent, Trader):
26+
layers["trader"]["x"].append(i)
27+
layers["trader"]["y"].append(j)
28+
else:
29+
# Don't visualize resource with value <= 1.
30+
layers["sugar"][i][j] = (
31+
agent.sugar_amount if agent.sugar_amount > 1 else np.nan
32+
)
33+
layers["spice"][i][j] = (
34+
agent.spice_amount if agent.spice_amount > 1 else np.nan
35+
)
3036
return layers
3137

3238
fig = Figure()

mesa/examples/advanced/sugarscape_g1mt/sugarscape_g1mt/model.py mesa/examples/advanced/sugarscape_g1mt/model.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import numpy as np
55
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid
66

7-
from .resource_agents import Resource
8-
from .trader_agents import Trader
7+
from mesa.examples.advanced.sugarscape_g1mt.agents import Resource, Trader
8+
99

1010

1111
# Helper Functions
@@ -53,8 +53,9 @@ def __init__(
5353
vision_min=1,
5454
vision_max=5,
5555
enable_trade=True,
56+
seed=None
5657
):
57-
super().__init__()
58+
super().__init__(seed=seed)
5859
# Initiate width and height of sugarscape
5960
self.width = width
6061
self.height = height

mesa/examples/advanced/sugarscape_g1mt/requirements.txt

-6
This file was deleted.

mesa/examples/advanced/sugarscape_g1mt/run.py

-105
This file was deleted.

mesa/examples/advanced/sugarscape_g1mt/sugarscape_g1mt/resource_agents.py

-26
This file was deleted.

mesa/examples/advanced/sugarscape_g1mt/sugarscape_g1mt/server.py

-61
This file was deleted.

mesa/examples/advanced/sugarscape_g1mt/tests.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import random
2-
31
import numpy as np
42
from scipy import stats
5-
from sugarscape_g1mt.model import SugarscapeG1mt, flatten
6-
from sugarscape_g1mt.trader_agents import Trader
7-
8-
random.seed(1)
3+
from .model import SugarscapeG1mt, flatten
4+
from .agents import Trader
95

106

117
def check_slope(y, increasing):
@@ -19,7 +15,7 @@ def check_slope(y, increasing):
1915
def test_decreasing_price_variance():
2016
# The variance of the average trade price should decrease over time (figure IV-3)
2117
# See Growing Artificial Societies p. 109.
22-
model = SugarscapeG1mt()
18+
model = SugarscapeG1mt(42)
2319
model.datacollector._new_model_reporter(
2420
"price_variance",
2521
lambda m: np.var(

0 commit comments

Comments
 (0)