diff --git a/examples/schelling/README.md b/examples/schelling/README.md index 64cc9c83..fe8971f6 100644 --- a/examples/schelling/README.md +++ b/examples/schelling/README.md @@ -22,6 +22,14 @@ To run the model interactively, run ``mesa runserver`` in this directory. e.g. $ mesa runserver ``` +or + +Directly run the file ``run.py`` in the terminal. e.g. + +``` + $ python run.py +``` + Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run. To view and run some example model analyses, launch the IPython Notebook and open ``analysis.ipynb``. Visualizing the analysis also requires [matplotlib](http://matplotlib.org/). diff --git a/examples/schelling/model.py b/examples/schelling/model.py index 2eb148df..dfba4efb 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, pos, model, agent_type): + def __init__(self, unique_id, model, agent_type): """ Create a new Schelling agent. @@ -15,13 +15,14 @@ def __init__(self, pos, model, agent_type): x, y: Agent initial location. agent_type: Indicator for the agent's type (minority=1, majority=0) """ - super().__init__(pos, model) - self.pos = pos + super().__init__(unique_id, model) self.type = agent_type def step(self): similar = 0 - for neighbor in self.model.grid.iter_neighbors(self.pos, True): + for neighbor in self.model.grid.iter_neighbors( + self.pos, moore=True, radius=self.model.radius + ): if neighbor.type == self.type: similar += 1 @@ -37,47 +38,64 @@ class Schelling(mesa.Model): Model class for the Schelling segregation model. """ - def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=3): - super().__init__() - self.width = width + def __init__( + self, + height=20, + width=20, + homophily=3, + radius=1, + density=0.8, + minority_pc=0.2, + 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 self.density = density self.minority_pc = minority_pc self.homophily = homophily + self.radius = radius self.schedule = mesa.time.RandomActivation(self) self.grid = mesa.space.SingleGrid(width, height, torus=True) self.happy = 0 self.datacollector = mesa.DataCollector( - {"happy": "happy"}, # Model-level count of happy agents - # For testing purposes, agent's individual x and y - {"x": lambda a: a.pos[0], "y": lambda a: a.pos[1]}, + 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) - for cell in self.grid.coord_iter(): - x, y = cell[1] + 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((x, y), self, agent_type) - self.grid.place_agent(agent, (x, y)) + agent = SchellingAgent(self.next_id(), self, agent_type) + self.grid.place_agent(agent, pos) self.schedule.add(agent) - self.running = True self.datacollector.collect(self) def step(self): """ - Run one step of the model. If All agents are happy, halt the model. + Run one step of the model. """ self.happy = 0 # Reset counter of happy agents self.schedule.step() - # collect data + self.datacollector.collect(self) if self.happy == self.schedule.get_agent_count(): diff --git a/examples/schelling/server.py b/examples/schelling/server.py index 1396e9c7..1f0d5f92 100644 --- a/examples/schelling/server.py +++ b/examples/schelling/server.py @@ -26,20 +26,35 @@ def schelling_draw(agent): return portrayal -canvas_element = mesa.visualization.CanvasGrid(schelling_draw, 20, 20, 500, 500) +canvas_element = mesa.visualization.CanvasGrid( + portrayal_method=schelling_draw, + grid_width=20, + grid_height=20, + canvas_width=500, + canvas_height=500, +) happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}]) model_params = { "height": 20, "width": 20, - "density": mesa.visualization.Slider("Agent density", 0.8, 0.1, 1.0, 0.1), - "minority_pc": mesa.visualization.Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05), - "homophily": mesa.visualization.Slider("Homophily", 3, 0, 8, 1), + "density": mesa.visualization.Slider( + name="Agent density", value=0.8, min_value=0.1, max_value=1.0, step=0.1 + ), + "minority_pc": mesa.visualization.Slider( + 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 + ), + "radius": mesa.visualization.Slider( + name="Search Radius", value=1, min_value=1, max_value=5, step=1 + ), } server = mesa.visualization.ModularServer( - Schelling, - [canvas_element, get_happy_agents, happy_chart], - "Schelling", - model_params, + model_cls=Schelling, + visualization_elements=[canvas_element, get_happy_agents, happy_chart], + name="Schelling Segregation Model", + model_params=model_params, )