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

Added homophily ratio in basic schelling example #2520

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
12 changes: 10 additions & 2 deletions mesa/examples/basic/schelling/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ def step(self) -> None:
self.pos, moore=True, radius=self.model.radius
)

type_of_neighbors = [neighbor.type == self.type for neighbor in neighbors]

# Count similar neighbors
similar = sum(neighbor.type == self.type for neighbor in neighbors)
similar = sum(type_of_neighbors)

# Count total neighbors
total_neighbors = len(type_of_neighbors)
EwoutH marked this conversation as resolved.
Show resolved Hide resolved

# If unhappy, move to a random empty cell:
if similar < self.model.homophily:
if (
total_neighbors != 0
and similar / total_neighbors < self.model.homophily_ratio
):
self.model.grid.move_to_empty(self)
else:
self.model.happy += 1
2 changes: 1 addition & 1 deletion mesa/examples/basic/schelling/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def agent_portrayal(agent):
},
"density": Slider("Agent density", 0.8, 0.1, 1.0, 0.1),
"minority_pc": Slider("Fraction minority", 0.2, 0.0, 1.0, 0.05),
"homophily": Slider("Homophily", 3, 0, 8, 1),
"homophily_ratio": Slider("Homophily_ratio", 0.3, 0, 1, 0.1),
"width": 20,
"height": 20,
}
Expand Down
6 changes: 3 additions & 3 deletions mesa/examples/basic/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(
width: int = 40,
density: float = 0.8,
minority_pc: float = 0.5,
homophily: int = 3,
homophily_ratio: float = 0.3,
Copy link
Member

Choose a reason for hiding this comment

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

why 0.3? It would make more sense to go to 3/8 (8 is the neighborhood size of a Moore grid with a radius of 1 and all fields occupied.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for reviewing. I will change 0.3 to 3/8 (0.375).

Copy link
Author

Choose a reason for hiding this comment

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

@quaquel The step size of the slider is 0.1. So the ratio can either be 0.3 or 0.4.
If the ratio is 0.3, agents with 3 similar neighbors and 8 total neighbors will be happy, as 3/8 = 0.375.
And if the ratio is 0.4, agents with 3 similar neighbors and 8 total neighbors won't be happy.
So should I change 0.3 to 0.4?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can change the step size to 0.125 as well?

radius: int = 1,
seed=None,
):
Expand All @@ -24,7 +24,7 @@ def __init__(
height: Height of the grid
density: Initial chance for a cell to be populated (0-1)
minority_pc: Chance for an agent to be in minority class (0-1)
homophily: Minimum number of similar neighbors needed for happiness
homophily_ratio: Ratio of similar neighbors to total neighbors needed for happiness
radius: Search radius for checking neighbor similarity
seed: Seed for reproducibility
"""
Expand All @@ -35,7 +35,7 @@ def __init__(
self.width = width
self.density = density
self.minority_pc = minority_pc
self.homophily = homophily
self.homophily_ratio = homophily_ratio
self.radius = radius

# Initialize grid
Expand Down
Loading