Skip to content

Commit dea931d

Browse files
rhtquaquelEwoutHpre-commit-ci[bot]
authored
refactor: Simplify Schelling code (#2353)
* refactor: Simplify Schelling code Ported from projectmesa/mesa-examples#222. * Update benchmarks/Schelling/schelling.py Co-authored-by: Ewout ter Hoeven <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Jan Kwakkel <[email protected]> Co-authored-by: Ewout ter Hoeven <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4ed0d6d commit dea931d

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

benchmarks/Schelling/schelling.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ def __init__(
3434

3535
def step(self):
3636
"""Run one step of the agent."""
37-
similar = 0
38-
neighborhood = self.cell.get_neighborhood(radius=self.radius)
39-
for neighbor in neighborhood.agents:
40-
if neighbor.type == self.type:
41-
similar += 1
37+
neighbors = self.cell.get_neighborhood(radius=self.radius).agents
38+
similar = len(
39+
[neighbor for neighbor in neighbors if neighbor.type == self.type]
40+
)
4241

4342
# If unhappy, move:
4443
if similar < self.homophily:
@@ -76,7 +75,6 @@ def __init__(
7675
"""
7776
super().__init__(seed=seed)
7877
self.simulator = simulator
79-
self.minority_pc = minority_pc
8078
self.happy = 0
8179

8280
self.grid = OrthogonalMooreGrid(
@@ -92,7 +90,7 @@ def __init__(
9290
# its contents. (coord_iter)
9391
for cell in self.grid:
9492
if self.random.random() < density:
95-
agent_type = 1 if self.random.random() < self.minority_pc else 0
93+
agent_type = 1 if self.random.random() < minority_pc else 0
9694
SchellingAgent(self, agent_type, radius, homophily, cell)
9795

9896
def step(self):

0 commit comments

Comments
 (0)