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

Fix: Schelling Model Neighbor Similarity Calculation #2518

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 18 additions & 8 deletions mesa/examples/basic/schelling/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

def __init__(self, model, agent_type: int) -> None:
"""Create a new Schelling agent.

Args:
model: The model instance the agent belongs to
agent_type: Indicator for the agent's type (minority=1, majority=0)
Expand All @@ -20,11 +19,22 @@
self.pos, moore=True, radius=self.model.radius
)

# Count similar neighbors
similar = sum(neighbor.type == self.type for neighbor in neighbors)
# Filter out empty cells
similar_neighbors = [
neighbor
for neighbor in neighbors
if hasattr(neighbor, "type") and neighbor.type == self.type
]
total_neighbors = [
neighbor for neighbor in neighbors if hasattr(neighbor, "type")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you iterate twice, with one check being done in both. You can make this more efficient by looping only once.

]

# Calculate fraction of similar neighbors
if len(total_neighbors) > 0:
similarity_fraction = len(similar_neighbors) / len(total_neighbors)

Check warning on line 34 in mesa/examples/basic/schelling/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/schelling/agents.py#L34

Added line #L34 was not covered by tests

# If unhappy, move to a random empty cell:
if similar < self.model.homophily:
self.model.grid.move_to_empty(self)
else:
self.model.happy += 1
# If unhappy, move to a random empty cell
if similarity_fraction < self.model.homophily / 8.0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not change homopily at the model level to be a fraction? That makes the model independent of the neighborhood size.

self.model.grid.move_to_empty(self)

Check warning on line 38 in mesa/examples/basic/schelling/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/schelling/agents.py#L38

Added line #L38 was not covered by tests
else:
self.model.happy += 1

Check warning on line 40 in mesa/examples/basic/schelling/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/schelling/agents.py#L40

Added line #L40 was not covered by tests
Loading