class ConveyModel(Model): """A model with some number of agents.""" def __init__(self, N, width, height): super().__init__() self.num_agents = N self.grid = MultiGrid(width, height, True) self.schedule = BaseScheduler(self) # Create agents for i in range(self.num_agents): a = ConwayAgent(i, self) # Add the agent to a random grid cell x = random.randrange(self.grid.width) y = random.randrange(self.grid.height) while(len(self.grid.get_cell_list_contents((x,y)))): x = random.randrange(self.grid.width) y = random.randrange(self.grid.height) self.grid.place_agent(a, (x, y)) self.schedule.add(a) self.i = i self.datacollector = DataCollector( agent_reporters={"State": lambda a: a.die}) def step(self): self.datacollector.collect(self) new_agents = [] for (x, y) in product(range(self.grid.width), range(self.grid.height)): ns = self.grid.iter_neighbors((x,y), True) neighbors = 0 for n in ns: if(n): neighbors += 1 if(self.grid[x][y]): # live cell if(neighbors < 2): # underpopulation list(self.grid[x][y])[0].die = 1 elif(neighbors > 3): # overpopulation list(self.grid[x][y])[0].die = 1 else: # dead cell if(neighbors == 3): new_agents.append((x, y)) for (x, y) in product(range(self.grid.width), range(self.grid.height)): if self.grid[x][y]: a = list(self.grid[x][y])[0] if a.die: self.grid.remove_agent(a) self.schedule.remove(a) for na in new_agents: self.i += 1 a = ConwayAgent(self.i, self) self.grid.place_agent(a, na) self.schedule.add(a)
class trafficSimulation(Model): def __init__(self, spawn_speed): self.spawn_speed = spawn_speed self.counter = 0 self.traffic_lights_schedule = TrafficScheduler(self) self.cars_schedule = SimultaneousActivation(self) self.grid = MultiGrid(10, 10, False) self.id = 0 self.spawnpoints = [(9, 5), (0, 4), (6, 0), (5, 9)] self.kill_agents = [] self.datacollector = DataCollector(model_reporters={"Grid": get_grid}) traffic_light_coords = [(7, 5), (4, 4), (6, 3), (5, 6)] for coord in traffic_light_coords: light = trafficLight(self.id, coord, self) self.id = self.id + 1 self.grid.place_agent(light, coord) self.traffic_lights_schedule.add(light) self.datacollector.collect(self) def get_data(self): traffic_lights = [{ "coords": { "x": agent.coords[0], "y": agent.coords[1] }, "state": stateToString(agent.state) } for agent in get_other_lights(-1, self)] cars = [{ "direction": { "x": agent.direction[0], "y": agent.direction[1] }, "coords": { "x": agent.coords[0], "y": agent.coords[1] } } for agent in self.cars_schedule.agents] return traffic_lights, cars def step(self): self.traffic_lights_schedule.step() self.cars_schedule.step() if self.counter == self.spawn_speed - 1 and random.random() > 0.5: orientation, coords = random.choice( [x for x in zip(direction.lst, self.spawnpoints)]) anyCar = any([ isinstance(agent, carAgent) for agent in self.grid.iter_neighbors(coords, False, True, 0) ]) if not anyCar: car = carAgent(self.id, coords, orientation, self) self.grid.place_agent(car, coords) self.cars_schedule.add(car) self.id = self.id + 1 self.counter = (self.counter + 1) % self.spawn_speed for x in self.kill_agents: self.grid.remove_agent(x) self.cars_schedule.remove(x) self.kill_agents.remove(x)