Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize advanced examples: wolf_sheep and sugarscape_g1mt #2410

Merged
merged 7 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions mesa/examples/advanced/sugarscape_g1mt/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,51 +34,28 @@ cross over one another otherwise *end*.
(Epstein and Axtell, 1996, p. 105)

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

## Installation

To install the dependencies use pip and the requirements.txt in this directory. e.g.

```
$ pip install -r requirements.txt
```

## How to Run

To run the model a single instance of the model:

```
$ python run.py -s
```

To run the model with BatchRunner:

```
$ python run.py -b
```

To run the model interactively:

```
$ mesa runserver
$ solara run app.py
```

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

## Files

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

## Additional Resources
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import math

from mesa.experimental.cell_space import CellAgent
from mesa.experimental.cell_space import FixedAgent

Check warning on line 4 in mesa/examples/advanced/sugarscape_g1mt/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/agents.py#L4

Added line #L4 was not covered by tests

from .resource_agents import Resource


# Helper function
Expand All @@ -18,6 +18,32 @@
dx = x1 - x2
dy = y1 - y2
return math.sqrt(dx**2 + dy**2)
class Resource(FixedAgent):

Check warning on line 21 in mesa/examples/advanced/sugarscape_g1mt/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/agents.py#L21

Added line #L21 was not covered by tests
"""
Resource:
- contains an amount of sugar and spice
- grows 1 amount of sugar at each turn
- grows 1 amount of spice at each turn
"""

def __init__(self, model, max_sugar, max_spice, cell):
super().__init__(model)
self.sugar_amount = max_sugar
self.max_sugar = max_sugar
self.spice_amount = max_spice
self.max_spice = max_spice
self.cell = cell

Check warning on line 35 in mesa/examples/advanced/sugarscape_g1mt/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/agents.py#L29-L35

Added lines #L29 - L35 were not covered by tests

def step(self):

Check warning on line 37 in mesa/examples/advanced/sugarscape_g1mt/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/agents.py#L37

Added line #L37 was not covered by tests
"""
Growth function, adds one unit of sugar and spice each step up to
max amount
"""
self.sugar_amount = min([self.max_sugar, self.sugar_amount + 1])
self.spice_amount = min([self.max_spice, self.spice_amount + 1])

Check warning on line 43 in mesa/examples/advanced/sugarscape_g1mt/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/agents.py#L42-L43

Added lines #L42 - L43 were not covered by tests





class Trader(CellAgent):
Expand Down
32 changes: 19 additions & 13 deletions mesa/examples/advanced/sugarscape_g1mt/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import sys
import os.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../../')))

Check warning on line 3 in mesa/examples/advanced/sugarscape_g1mt/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/app.py#L1-L3

Added lines #L1 - L3 were not covered by tests


import numpy as np
import solara
from matplotlib.figure import Figure
from mesa.visualization import SolaraViz, make_plot_measure

from sugarscape_g1mt.model import SugarscapeG1mt
from sugarscape_g1mt.trader_agents import Trader

Expand All @@ -14,19 +20,19 @@
"trader": {"x": [], "y": [], "c": "tab:red", "marker": "o", "s": 10},
}

for content, (i, j) in g.coord_iter():
for agent in content:
if isinstance(agent, Trader):
layers["trader"]["x"].append(i)
layers["trader"]["y"].append(j)
else:
# Don't visualize resource with value <= 1.
layers["sugar"][i][j] = (
agent.sugar_amount if agent.sugar_amount > 1 else np.nan
)
layers["spice"][i][j] = (
agent.spice_amount if agent.spice_amount > 1 else np.nan
)
for agent in g.all_cells.agents:
i, j = agent.cell.coordinate

Check warning on line 24 in mesa/examples/advanced/sugarscape_g1mt/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/app.py#L24

Added line #L24 was not covered by tests
if isinstance(agent, Trader):
layers["trader"]["x"].append(i)
layers["trader"]["y"].append(j)

Check warning on line 27 in mesa/examples/advanced/sugarscape_g1mt/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/app.py#L26-L27

Added lines #L26 - L27 were not covered by tests
else:
# Don't visualize resource with value <= 1.
layers["sugar"][i][j] = (

Check warning on line 30 in mesa/examples/advanced/sugarscape_g1mt/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/app.py#L30

Added line #L30 was not covered by tests
agent.sugar_amount if agent.sugar_amount > 1 else np.nan
)
layers["spice"][i][j] = (

Check warning on line 33 in mesa/examples/advanced/sugarscape_g1mt/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/app.py#L33

Added line #L33 was not covered by tests
agent.spice_amount if agent.spice_amount > 1 else np.nan
)
return layers

fig = Figure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import numpy as np
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid

from .resource_agents import Resource
from .trader_agents import Trader
from mesa.examples.advanced.sugarscape_g1mt.agents import Resource, Trader

Check warning on line 7 in mesa/examples/advanced/sugarscape_g1mt/model.py

View check run for this annotation

Codecov / codecov/patch

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

Added line #L7 was not covered by tests



# Helper Functions
Expand Down Expand Up @@ -53,8 +53,9 @@
vision_min=1,
vision_max=5,
enable_trade=True,
seed=None
):
super().__init__()
super().__init__(seed=seed)

Check warning on line 58 in mesa/examples/advanced/sugarscape_g1mt/model.py

View check run for this annotation

Codecov / codecov/patch

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

Added line #L58 was not covered by tests
# Initiate width and height of sugarscape
self.width = width
self.height = height
Expand Down
6 changes: 0 additions & 6 deletions mesa/examples/advanced/sugarscape_g1mt/requirements.txt

This file was deleted.

105 changes: 0 additions & 105 deletions mesa/examples/advanced/sugarscape_g1mt/run.py

This file was deleted.

This file was deleted.

61 changes: 0 additions & 61 deletions mesa/examples/advanced/sugarscape_g1mt/sugarscape_g1mt/server.py

This file was deleted.

10 changes: 3 additions & 7 deletions mesa/examples/advanced/sugarscape_g1mt/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import random

import numpy as np
from scipy import stats
from sugarscape_g1mt.model import SugarscapeG1mt, flatten
from sugarscape_g1mt.trader_agents import Trader

random.seed(1)
from .model import SugarscapeG1mt, flatten
from .agents import Trader

Check warning on line 4 in mesa/examples/advanced/sugarscape_g1mt/tests.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/tests.py#L3-L4

Added lines #L3 - L4 were not covered by tests


def check_slope(y, increasing):
Expand All @@ -19,7 +15,7 @@
def test_decreasing_price_variance():
# The variance of the average trade price should decrease over time (figure IV-3)
# See Growing Artificial Societies p. 109.
model = SugarscapeG1mt()
model = SugarscapeG1mt(42)

Check warning on line 18 in mesa/examples/advanced/sugarscape_g1mt/tests.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/tests.py#L18

Added line #L18 was not covered by tests
model.datacollector._new_model_reporter(
"price_variance",
lambda m: np.var(
Expand Down
Loading
Loading