From a830b88c6652e0cbf3857fe3884fdebbaa74312e Mon Sep 17 00:00:00 2001 From: Mirchuk Liliana Date: Tue, 1 Oct 2024 18:08:13 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B0=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B8=D0=B9=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D1=80=20=D0=B4=D0=BE=20=D0=BC=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D1=96=20move=5Fprobability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/schelling/model.py | 42 ++++++++++++++---------------------- examples/schelling/server.py | 4 ++-- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/examples/schelling/model.py b/examples/schelling/model.py index e995f31e..c4dda18c 100644 --- a/examples/schelling/model.py +++ b/examples/schelling/model.py @@ -1,20 +1,16 @@ import mesa - class SchellingAgent(mesa.Agent): - """ - Schelling segregation agent - """ - def __init__(self, model, agent_type): + def __init__(self, unique_id, model, agent_type): """ Create a new Schelling agent. Args: - x, y: Agent initial location. + unique_id: A unique identifier for the agent. agent_type: Indicator for the agent's type (minority=1, majority=0) """ - super().__init__(model) + super().__init__(unique_id, model) self.type = agent_type def step(self): @@ -25,13 +21,18 @@ def step(self): if neighbor.type == self.type: similar += 1 - # If unhappy, move: + # If unhappy, decide whether to move: if similar < self.model.homophily: - self.model.grid.move_to_empty(self) + if self.random.random() < self.model.move_probability: # Add randomness to moving + self.model.grid.move_to_empty(self) + else: + # Agent decides to stay even if unhappy + pass else: self.model.happy += 1 + class Schelling(mesa.Model): """ Model class for the Schelling segregation model. @@ -45,20 +46,9 @@ def __init__( radius=1, density=0.8, minority_pc=0.2, + move_probability=0.9, seed=None, ): - """ - Create a new Schelling model. - - Args: - width, height: Size of the space. - density: Initial Chance for a cell to populated - minority_pc: Chances for an agent to be in minority class - homophily: Minimum number of agents of same class needed to be happy - radius: Search radius for checking similarity - seed: Seed for Reproducibility - """ - super().__init__(seed=seed) self.height = height self.width = width @@ -66,6 +56,7 @@ def __init__( self.minority_pc = minority_pc self.homophily = homophily self.radius = radius + self.move_probability = move_probability self.grid = mesa.space.SingleGrid(width, height, torus=True) @@ -74,15 +65,14 @@ def __init__( model_reporters={"happy": "happy"}, # Model-level count of happy agents ) - # Set up agents - # We use a grid iterator that returns - # the coordinates of a cell as well as - # its contents. (coord_iter) + # Set up agents with unique IDs + unique_id = 0 # Initialize unique_id counter 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 - agent = SchellingAgent(self, agent_type) + agent = SchellingAgent(unique_id, self, agent_type) # Pass unique_id self.grid.place_agent(agent, pos) + unique_id += 1 # Increment the unique_id for the next agent self.datacollector.collect(self) diff --git a/examples/schelling/server.py b/examples/schelling/server.py index 1f0d5f92..cec9a0e1 100644 --- a/examples/schelling/server.py +++ b/examples/schelling/server.py @@ -45,10 +45,10 @@ def schelling_draw(agent): name="Fraction minority", value=0.2, min_value=0.00, max_value=1.0, step=0.05 ), "homophily": mesa.visualization.Slider( - name="Homophily", value=3, min_value=0, max_value=8, step=1 + name="Homophily", value=3, min_value=0, max_value=100, step=1 ), "radius": mesa.visualization.Slider( - name="Search Radius", value=1, min_value=1, max_value=5, step=1 + name="Search Radius", value=1, min_value=1, max_value=25, step=1 ), } From 13d1605d4d4eebefef675e4d615d6106e59dfbcf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:10:28 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/schelling/model.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/schelling/model.py b/examples/schelling/model.py index c4dda18c..08e63b36 100644 --- a/examples/schelling/model.py +++ b/examples/schelling/model.py @@ -1,7 +1,7 @@ import mesa -class SchellingAgent(mesa.Agent): +class SchellingAgent(mesa.Agent): def __init__(self, unique_id, model, agent_type): """ Create a new Schelling agent. @@ -23,7 +23,9 @@ def step(self): # If unhappy, decide whether to move: if similar < self.model.homophily: - if self.random.random() < self.model.move_probability: # Add randomness to moving + if ( + self.random.random() < self.model.move_probability + ): # Add randomness to moving self.model.grid.move_to_empty(self) else: # Agent decides to stay even if unhappy @@ -32,7 +34,6 @@ def step(self): self.model.happy += 1 - class Schelling(mesa.Model): """ Model class for the Schelling segregation model. @@ -56,7 +57,7 @@ def __init__( self.minority_pc = minority_pc self.homophily = homophily self.radius = radius - self.move_probability = move_probability + self.move_probability = move_probability self.grid = mesa.space.SingleGrid(width, height, torus=True)