From 5c1d312384cc0bd014352da901c4e5af8d992ed3 Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <88285949+vbv-shm@users.noreply.github.com> Date: Wed, 27 Nov 2024 03:40:26 +0530 Subject: [PATCH] Update agents.py --- mesa/examples/basic/schelling/agents.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mesa/examples/basic/schelling/agents.py b/mesa/examples/basic/schelling/agents.py index 67940b5654e..ac0664e6da4 100644 --- a/mesa/examples/basic/schelling/agents.py +++ b/mesa/examples/basic/schelling/agents.py @@ -22,9 +22,11 @@ def step(self) -> None: # Count similar neighbors similar = sum(neighbor.type == self.type for neighbor in neighbors) - + # Count total neighbors + total_neighbors=len(self.model.grid.get_neighbors(self.pos,moore=True)) + # 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