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

refactor: Simplify Schelling code #2353

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Changes from 1 commit
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
10 changes: 3 additions & 7 deletions benchmarks/Schelling/schelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ def __init__(

def step(self):
"""Run one step of the agent."""
similar = 0
neighborhood = self.cell.get_neighborhood(radius=self.radius)
for neighbor in neighborhood.agents:
if neighbor.type == self.type:
similar += 1
neighbors = self.cell.get_neighborhood(radius=self.radius).agents
similar = sum(1 for neighbor in neighbors if neighbor.type == self.type)
quaquel marked this conversation as resolved.
Show resolved Hide resolved

# If unhappy, move:
if similar < self.homophily:
Expand Down Expand Up @@ -76,7 +73,6 @@ def __init__(
"""
super().__init__(seed=seed)
self.simulator = simulator
self.minority_pc = minority_pc
self.happy = 0

self.grid = OrthogonalMooreGrid(
Expand All @@ -92,7 +88,7 @@ def __init__(
# its contents. (coord_iter)
for cell in self.grid:
if self.random.random() < density:
agent_type = 1 if self.random.random() < self.minority_pc else 0
agent_type = 1 if self.random.random() < minority_pc else 0
SchellingAgent(self, agent_type, radius, homophily, cell)

def step(self):
Expand Down
Loading