From f6f9ccb35a643344c72c797aa2f9d9095dd34184 Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 13 Oct 2024 04:05:17 -0400 Subject: [PATCH 1/4] refactor: Simplify Schelling code 1. Remove unused model attributes 2. Make `similar` calculation more natural language readable --- examples/schelling/model.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/examples/schelling/model.py b/examples/schelling/model.py index e995f31e..88321fec 100644 --- a/examples/schelling/model.py +++ b/examples/schelling/model.py @@ -18,12 +18,10 @@ def __init__(self, model, agent_type): self.type = agent_type def step(self): - similar = 0 - for neighbor in self.model.grid.iter_neighbors( + neighbors = self.model.grid.iter_neighbors( self.pos, moore=True, radius=self.model.radius - ): - if neighbor.type == self.type: - similar += 1 + ) + similar = sum(1 for neighbor in neighbors if neighbor.type == self.type) # If unhappy, move: if similar < self.model.homophily: @@ -60,10 +58,6 @@ def __init__( """ super().__init__(seed=seed) - self.height = height - self.width = width - self.density = density - self.minority_pc = minority_pc self.homophily = homophily self.radius = radius @@ -79,8 +73,8 @@ def __init__( # the coordinates of a cell as well as # its contents. (coord_iter) for _, pos in self.grid.coord_iter(): - if self.random.random() < self.density: - agent_type = 1 if self.random.random() < self.minority_pc else 0 + if self.random.random() < density: + agent_type = 1 if self.random.random() < minority_pc else 0 agent = SchellingAgent(self, agent_type) self.grid.place_agent(agent, pos) From 44169b45445610a741084ebc0b53aea98f545510 Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 13 Oct 2024 04:13:12 -0400 Subject: [PATCH 2/4] Remove unused argument doc --- examples/schelling/model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/schelling/model.py b/examples/schelling/model.py index 88321fec..de2a6c8c 100644 --- a/examples/schelling/model.py +++ b/examples/schelling/model.py @@ -11,7 +11,6 @@ def __init__(self, model, agent_type): Create a new Schelling agent. Args: - x, y: Agent initial location. agent_type: Indicator for the agent's type (minority=1, majority=0) """ super().__init__(model) From 68ff95e19c08e7af07210ff4c0b72bb75f4a9df3 Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 13 Oct 2024 04:15:37 -0400 Subject: [PATCH 3/4] Add type hints to agent class --- examples/schelling/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/schelling/model.py b/examples/schelling/model.py index de2a6c8c..d343537a 100644 --- a/examples/schelling/model.py +++ b/examples/schelling/model.py @@ -6,7 +6,7 @@ class SchellingAgent(mesa.Agent): Schelling segregation agent """ - def __init__(self, model, agent_type): + def __init__(self, model: mesa.Model, agent_type: int) -> None: """ Create a new Schelling agent. @@ -16,7 +16,7 @@ def __init__(self, model, agent_type): super().__init__(model) self.type = agent_type - def step(self): + def step(self) -> None: neighbors = self.model.grid.iter_neighbors( self.pos, moore=True, radius=self.model.radius ) From defdb5a2807caa9c930fab6378f4d97a73680ce0 Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 13 Oct 2024 04:32:28 -0400 Subject: [PATCH 4/4] refactor: Simplify self.running expression --- examples/schelling/model.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/schelling/model.py b/examples/schelling/model.py index d343537a..b7523ef2 100644 --- a/examples/schelling/model.py +++ b/examples/schelling/model.py @@ -88,5 +88,4 @@ def step(self): self.datacollector.collect(self) - if self.happy == len(self.agents): - self.running = False + self.running = self.happy != len(self.agents)