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

Conversation

Sahil-Chhoker
Copy link

Summary

Fixed #2515 Schelling segregation model to calculate agent happiness using neighbor similarity fraction, aligning with Wikipedia and NetLogo standards.

Bug / Issue

Current model incorrectly counts empty spaces as neighbors, leading to inaccurate agent happiness determination.

Implementation

Modified SchellingAgent.step() to:

  • Filter out empty cells
  • Calculate similar neighbor fraction
  • Scale homophily threshold
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')
]

if len(total_neighbors) > 0:
    similarity_fraction = len(similar_neighbors) / len(total_neighbors)
    
    if similarity_fraction < self.model.homophily / 8.0:
        self.model.grid.move_to_empty(self)
    else:
        self.model.happy += 1

@EwoutH EwoutH added the example Changes the examples or adds to them. label Nov 25, 2024
Copy link

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +5.4% [+3.5%, +7.2%] 🔵 +0.4% [+0.3%, +0.5%]
BoltzmannWealth large 🔵 -0.3% [-0.9%, +0.2%] 🔵 -3.5% [-6.0%, -1.0%]
Schelling small 🔵 +0.6% [+0.3%, +0.9%] 🟢 -16.1% [-16.3%, -15.8%]
Schelling large 🔵 +0.4% [-0.0%, +0.9%] 🟢 -6.5% [-7.6%, -5.4%]
WolfSheep small 🔵 +0.2% [-0.1%, +0.5%] 🔵 -0.0% [-0.2%, +0.1%]
WolfSheep large 🔵 -1.4% [-2.3%, -0.5%] 🔵 -3.2% [-5.9%, +0.1%]
BoidFlockers small 🔵 +0.9% [+0.4%, +1.5%] 🔵 +0.7% [-0.2%, +1.7%]
BoidFlockers large 🔵 +0.4% [-0.4%, +1.2%] 🔵 +0.1% [-0.3%, +0.5%]

@EwoutH EwoutH requested a review from quaquel November 25, 2024 14:12
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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example Changes the examples or adds to them.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

homophily parameter in basic schelling example
3 participants