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