Skip to content

Commit 64b43d2

Browse files
Improve schelling model documentation (#103)
* Improve schelling model * Update server.py * Update model.py * Update model.py * Update server.py * Update model.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update model.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3a593b3 commit 64b43d2

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

examples/schelling/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ To run the model interactively, run ``mesa runserver`` in this directory. e.g.
2222
$ mesa runserver
2323
```
2424

25+
or
26+
27+
Directly run the file ``run.py`` in the terminal. e.g.
28+
29+
```
30+
$ python run.py
31+
```
32+
2533
Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
2634

2735
To view and run some example model analyses, launch the IPython Notebook and open ``analysis.ipynb``. Visualizing the analysis also requires [matplotlib](http://matplotlib.org/).

examples/schelling/model.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class SchellingAgent(mesa.Agent):
66
Schelling segregation agent
77
"""
88

9-
def __init__(self, pos, model, agent_type):
9+
def __init__(self, unique_id, model, agent_type):
1010
"""
1111
Create a new Schelling agent.
1212
@@ -15,13 +15,14 @@ def __init__(self, pos, model, agent_type):
1515
x, y: Agent initial location.
1616
agent_type: Indicator for the agent's type (minority=1, majority=0)
1717
"""
18-
super().__init__(pos, model)
19-
self.pos = pos
18+
super().__init__(unique_id, model)
2019
self.type = agent_type
2120

2221
def step(self):
2322
similar = 0
24-
for neighbor in self.model.grid.iter_neighbors(self.pos, True):
23+
for neighbor in self.model.grid.iter_neighbors(
24+
self.pos, moore=True, radius=self.model.radius
25+
):
2526
if neighbor.type == self.type:
2627
similar += 1
2728

@@ -37,47 +38,64 @@ class Schelling(mesa.Model):
3738
Model class for the Schelling segregation model.
3839
"""
3940

40-
def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=3):
41-
super().__init__()
42-
self.width = width
41+
def __init__(
42+
self,
43+
height=20,
44+
width=20,
45+
homophily=3,
46+
radius=1,
47+
density=0.8,
48+
minority_pc=0.2,
49+
seed=None,
50+
):
51+
"""
52+
Create a new Schelling model.
53+
54+
Args:
55+
width, height: Size of the space.
56+
density: Initial Chance for a cell to populated
57+
minority_pc: Chances for an agent to be in minority class
58+
homophily: Minimum number of agents of same class needed to be happy
59+
radius: Search radius for checking similarity
60+
seed: Seed for Reproducibility
61+
"""
62+
63+
super().__init__(seed=seed)
4364
self.height = height
65+
self.width = width
4466
self.density = density
4567
self.minority_pc = minority_pc
4668
self.homophily = homophily
69+
self.radius = radius
4770

4871
self.schedule = mesa.time.RandomActivation(self)
4972
self.grid = mesa.space.SingleGrid(width, height, torus=True)
5073

5174
self.happy = 0
5275
self.datacollector = mesa.DataCollector(
53-
{"happy": "happy"}, # Model-level count of happy agents
54-
# For testing purposes, agent's individual x and y
55-
{"x": lambda a: a.pos[0], "y": lambda a: a.pos[1]},
76+
model_reporters={"happy": "happy"}, # Model-level count of happy agents
5677
)
5778

5879
# Set up agents
5980
# We use a grid iterator that returns
6081
# the coordinates of a cell as well as
6182
# its contents. (coord_iter)
62-
for cell in self.grid.coord_iter():
63-
x, y = cell[1]
83+
for _, pos in self.grid.coord_iter():
6484
if self.random.random() < self.density:
6585
agent_type = 1 if self.random.random() < self.minority_pc else 0
66-
67-
agent = SchellingAgent((x, y), self, agent_type)
68-
self.grid.place_agent(agent, (x, y))
86+
agent = SchellingAgent(self.next_id(), self, agent_type)
87+
self.grid.place_agent(agent, pos)
6988
self.schedule.add(agent)
7089

71-
self.running = True
7290
self.datacollector.collect(self)
7391

7492
def step(self):
7593
"""
76-
Run one step of the model. If All agents are happy, halt the model.
94+
Run one step of the model.
7795
"""
7896
self.happy = 0 # Reset counter of happy agents
7997
self.schedule.step()
80-
# collect data
98+
8199
self.datacollector.collect(self)
82100

83101
if self.happy == self.schedule.get_agent_count():

examples/schelling/server.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,35 @@ def schelling_draw(agent):
2626
return portrayal
2727

2828

29-
canvas_element = mesa.visualization.CanvasGrid(schelling_draw, 20, 20, 500, 500)
29+
canvas_element = mesa.visualization.CanvasGrid(
30+
portrayal_method=schelling_draw,
31+
grid_width=20,
32+
grid_height=20,
33+
canvas_width=500,
34+
canvas_height=500,
35+
)
3036
happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}])
3137

3238
model_params = {
3339
"height": 20,
3440
"width": 20,
35-
"density": mesa.visualization.Slider("Agent density", 0.8, 0.1, 1.0, 0.1),
36-
"minority_pc": mesa.visualization.Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05),
37-
"homophily": mesa.visualization.Slider("Homophily", 3, 0, 8, 1),
41+
"density": mesa.visualization.Slider(
42+
name="Agent density", value=0.8, min_value=0.1, max_value=1.0, step=0.1
43+
),
44+
"minority_pc": mesa.visualization.Slider(
45+
name="Fraction minority", value=0.2, min_value=0.00, max_value=1.0, step=0.05
46+
),
47+
"homophily": mesa.visualization.Slider(
48+
name="Homophily", value=3, min_value=0, max_value=8, step=1
49+
),
50+
"radius": mesa.visualization.Slider(
51+
name="Search Radius", value=1, min_value=1, max_value=5, step=1
52+
),
3853
}
3954

4055
server = mesa.visualization.ModularServer(
41-
Schelling,
42-
[canvas_element, get_happy_agents, happy_chart],
43-
"Schelling",
44-
model_params,
56+
model_cls=Schelling,
57+
visualization_elements=[canvas_element, get_happy_agents, happy_chart],
58+
name="Schelling Segregation Model",
59+
model_params=model_params,
4560
)

0 commit comments

Comments
 (0)