From a51aa5cbd2d39e289ae4697a332714ab0aa24b37 Mon Sep 17 00:00:00 2001 From: Sahil Chhoker Date: Mon, 25 Nov 2024 19:24:29 +0530 Subject: [PATCH 1/2] fix: update schelling model neighbor similarity calculation --- mesa/examples/basic/schelling/agents.py | 32 +++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/mesa/examples/basic/schelling/agents.py b/mesa/examples/basic/schelling/agents.py index 67940b5654e..2e01e4cdecf 100644 --- a/mesa/examples/basic/schelling/agents.py +++ b/mesa/examples/basic/schelling/agents.py @@ -1,12 +1,9 @@ from mesa import Agent - class SchellingAgent(Agent): """Schelling segregation agent.""" - 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) @@ -19,12 +16,23 @@ def step(self) -> None: neighbors = self.model.grid.iter_neighbors( self.pos, moore=True, radius=self.model.radius ) - - # Count similar neighbors - similar = sum(neighbor.type == self.type for neighbor in neighbors) - - # 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 + + # 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') + ] + + # Calculate fraction of similar neighbors + if len(total_neighbors) > 0: + similarity_fraction = len(similar_neighbors) / len(total_neighbors) + + # If unhappy, move to a random empty cell + if similarity_fraction < self.model.homophily / 8.0: + self.model.grid.move_to_empty(self) + else: + self.model.happy += 1 From 178d3d1cae583d3af7107cb87b5806989633f636 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:07:40 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/examples/basic/schelling/agents.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mesa/examples/basic/schelling/agents.py b/mesa/examples/basic/schelling/agents.py index 2e01e4cdecf..49825825127 100644 --- a/mesa/examples/basic/schelling/agents.py +++ b/mesa/examples/basic/schelling/agents.py @@ -1,7 +1,9 @@ from mesa import Agent + class SchellingAgent(Agent): """Schelling segregation agent.""" + def __init__(self, model, agent_type: int) -> None: """Create a new Schelling agent. Args: @@ -16,21 +18,21 @@ def step(self) -> None: neighbors = self.model.grid.iter_neighbors( self.pos, moore=True, radius=self.model.radius ) - + # Filter out empty cells similar_neighbors = [ - neighbor for neighbor in neighbors - if hasattr(neighbor, 'type') and neighbor.type == self.type + 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') + neighbor for neighbor in neighbors if hasattr(neighbor, "type") ] - + # Calculate fraction of similar neighbors if len(total_neighbors) > 0: similarity_fraction = len(similar_neighbors) / len(total_neighbors) - + # If unhappy, move to a random empty cell if similarity_fraction < self.model.homophily / 8.0: self.model.grid.move_to_empty(self)