Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions examples/schelling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ To run the model interactively, run ``mesa runserver`` in this directory. e.g.
$ mesa runserver
```

or

Directly run the file ``run.py`` in the terminal. e.g.

```
$ python run.py
```

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

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/).
Expand Down
54 changes: 36 additions & 18 deletions examples/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SchellingAgent(mesa.Agent):
Schelling segregation agent
"""

def __init__(self, pos, model, agent_type):
def __init__(self, unique_id, model, agent_type):
"""
Create a new Schelling agent.

Expand All @@ -15,13 +15,14 @@ def __init__(self, pos, model, agent_type):
x, y: Agent initial location.
agent_type: Indicator for the agent's type (minority=1, majority=0)
"""
super().__init__(pos, model)
self.pos = pos
super().__init__(unique_id, model)
self.type = agent_type

def step(self):
similar = 0
for neighbor in self.model.grid.iter_neighbors(self.pos, True):
for neighbor in self.model.grid.iter_neighbors(
self.pos, moore=True, radius=self.model.radius
):
if neighbor.type == self.type:
similar += 1

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

def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=3):
super().__init__()
self.width = width
def __init__(
self,
height=20,
width=20,
homophily=3,
radius=1,
density=0.8,
minority_pc=0.2,
seed=None,
):
"""
Create a new Schelling model.

Args:
width, height: Size of the space.
density: Initial Chance for a cell to populated
minority_pc: Chances for an agent to be in minority class
homophily: Minimum number of agents of same class needed to be happy
radius: Search radius for checking similarity
seed: Seed for Reproducibility
"""

super().__init__(seed=seed)
self.height = height
self.width = width
self.density = density
self.minority_pc = minority_pc
self.homophily = homophily
self.radius = radius

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

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

# Set up agents
# We use a grid iterator that returns
# the coordinates of a cell as well as
# its contents. (coord_iter)
for cell in self.grid.coord_iter():
x, y = cell[1]
for _, pos in self.grid.coord_iter():
if self.random.random() < self.density:
agent_type = 1 if self.random.random() < self.minority_pc else 0

agent = SchellingAgent((x, y), self, agent_type)
self.grid.place_agent(agent, (x, y))
agent = SchellingAgent(self.next_id(), self, agent_type)
self.grid.place_agent(agent, pos)
self.schedule.add(agent)

self.running = True
self.datacollector.collect(self)

def step(self):
"""
Run one step of the model. If All agents are happy, halt the model.
Run one step of the model.
"""
self.happy = 0 # Reset counter of happy agents
self.schedule.step()
# collect data

self.datacollector.collect(self)

if self.happy == self.schedule.get_agent_count():
Expand Down
31 changes: 23 additions & 8 deletions examples/schelling/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@ def schelling_draw(agent):
return portrayal


canvas_element = mesa.visualization.CanvasGrid(schelling_draw, 20, 20, 500, 500)
canvas_element = mesa.visualization.CanvasGrid(
portrayal_method=schelling_draw,
grid_width=20,
grid_height=20,
canvas_width=500,
canvas_height=500,
)
happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}])

model_params = {
"height": 20,
"width": 20,
"density": mesa.visualization.Slider("Agent density", 0.8, 0.1, 1.0, 0.1),
"minority_pc": mesa.visualization.Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05),
"homophily": mesa.visualization.Slider("Homophily", 3, 0, 8, 1),
"density": mesa.visualization.Slider(
name="Agent density", value=0.8, min_value=0.1, max_value=1.0, step=0.1
),
"minority_pc": mesa.visualization.Slider(
name="Fraction minority", value=0.2, min_value=0.00, max_value=1.0, step=0.05
),
"homophily": mesa.visualization.Slider(
name="Homophily", value=3, min_value=0, max_value=8, step=1
),
"radius": mesa.visualization.Slider(
name="Search Radius", value=1, min_value=1, max_value=5, step=1
),
}

server = mesa.visualization.ModularServer(
Schelling,
[canvas_element, get_happy_agents, happy_chart],
"Schelling",
model_params,
model_cls=Schelling,
visualization_elements=[canvas_element, get_happy_agents, happy_chart],
name="Schelling Segregation Model",
model_params=model_params,
)