diff --git a/examples/schelling_experimental/model.py b/examples/schelling_experimental/model.py index c7fe766a..8be73531 100644 --- a/examples/schelling_experimental/model.py +++ b/examples/schelling_experimental/model.py @@ -12,7 +12,7 @@ def __init__(self, pos, model, agent_type): Args: unique_id: Unique identifier for the agent. - x, y: Agent initial location. + pos: Agent initial location. agent_type: Indicator for the agent's type (minority=1, majority=0) """ super().__init__(pos, model) @@ -38,38 +38,28 @@ class Schelling(mesa.Model): """ def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=3): - """ """ - + super().__init__() self.width = width self.height = height - self.density = density - self.minority_pc = minority_pc self.homophily = homophily - 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]}, ) # 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] - 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)) - self.schedule.add(agent) + for _, pos in self.grid.coord_iter(): + if self.random.random() < density: + agent_type = 1 if self.random.random() < minority_pc else 0 + agent = SchellingAgent(pos, self, agent_type) + self.grid.place_agent(agent, pos) - self.running = True self.datacollector.collect(self) def step(self): @@ -77,9 +67,9 @@ def step(self): Run one step of the model. If All agents are happy, halt the model. """ self.happy = 0 # Reset counter of happy agents - self.schedule.step() + self.agents.shuffle().do("step") # collect data self.datacollector.collect(self) - if self.happy == self.schedule.get_agent_count(): + if self.happy == len(self.agents): self.running = False