Skip to content

Commit 95f862e

Browse files
committed
Merge remote-tracking branch 'upstream/main' into new_spaces
2 parents e5c69b6 + d63ce06 commit 95f862e

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

examples/schelling/model.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ class SchellingAgent(CellAgent):
77
Schelling segregation agent
88
"""
99

10-
def __init__(self, model, agent_type):
10+
def __init__(self, model: mesa.Model, agent_type: int) -> None:
1111
"""
1212
Create a new Schelling agent.
1313
1414
Args:
15-
x, y: Agent initial location.
1615
agent_type: Indicator for the agent's type (minority=1, majority=0)
1716
"""
1817
super().__init__(model)
1918
self.type = agent_type
2019

2120
def step(self):
22-
similar = 0
23-
for neighbor in self.cell.get_neighborhood(radius=self.model.radius).agents:
24-
if neighbor.type == self.type:
25-
similar += 1
21+
neighbors = self.cell.get_neighborhood(radius=self.model.radius).agents
22+
similar = len([neighbor for neighbor in neighbors if neighbor.type == self.type])
2623

2724
# If unhappy, move:
2825
if similar < self.model.homophily:
@@ -59,10 +56,6 @@ def __init__(
5956
"""
6057

6158
super().__init__(seed=seed)
62-
self.height = height
63-
self.width = width
64-
self.density = density
65-
self.minority_pc = minority_pc
6659
self.homophily = homophily
6760
self.radius = radius
6861

@@ -78,8 +71,8 @@ def __init__(
7871
# the coordinates of a cell as well as
7972
# its contents. (coord_iter)
8073
for cell in self.grid.all_cells:
81-
if self.random.random() < self.density:
82-
agent_type = 1 if self.random.random() < self.minority_pc else 0
74+
if self.random.random() < density:
75+
agent_type = 1 if self.random.random() < minority_pc else 0
8376
agent = SchellingAgent(self, agent_type)
8477
agent.cell = cell
8578

@@ -94,5 +87,4 @@ def step(self):
9487

9588
self.datacollector.collect(self)
9689

97-
if self.happy == len(self.agents):
98-
self.running = False
90+
self.running = self.happy != len(self.agents)

0 commit comments

Comments
 (0)