def __init__(self, N, width, height, distance_coefficent=0, density_coefficent=10, density_vision=3): super().__init__() self.max_Astar = 0 self.num_agents = N self.grid = MultiGrid(width, height, False) self.schedule = BaseScheduler(self) self.POI_cost = {} self.grid_density = [[0 for i in range(width)] for j in range(height)] self.serving_time = {'BAR': 3, 'BAR2': 3, 'BAR3': 3, 'BAR4': 3} POI_points = [(24, 49), (24, 1)] POI_names = ['STAGE', 'BAR'] POI_dict = {} for i in range(len(POI_points)): POI_dict[POI_names[i]] = POI_points[i] for i in range(width): for j in range(height): nd = nodeAgent((i, j), self, POI_names) self.grid.place_agent(nd, (i, j)) self.blocks = block_positions create_block(self, block_positions, POI_names) create_POI(self, POI_points, 2) for r in range(len(POI_points)): x = POI_points[r][0] y = POI_points[r][1] A_star_node(self, (x, y), POI_names[r], self.blocks, 0.5, 2) self.POI_cost[POI_names[r]] =\ A_star_array(width, height, (x, y), self.blocks, distance_coefficent, 1) commuters_list = [] for k in range(self.num_agents): a = commuterAgent(k, self, POI_dict, density_coefficent, density_vision) commuters_list.append(a) selected_cells = [] for commuter in commuters_list: self.schedule.add(commuter) notblock = False selected_cell = False while(True): x = random.randrange(self.grid.width) y = random.randrange(self.grid.height) this_cell = self.grid.get_cell_list_contents((x, y)) if (x, y) not in self.blocks and (x, y) not in selected_cells: selected_cells.append((x, y)) break self.grid_density[x][y] += 1 self.grid.place_agent(commuter, (24, 1)) self.datacollector = DataCollector( agent_reporters={"utility": lambda a: a.utility, 'numOfPOIVisits': lambda a: a.numOfPOIVisits})
class Model(Model): """ This class is the main model which inherits the Model class in Mesa Attributes: max_Astar:= maximum value of cost from A_star algorithm, used for visualization num_agents:= total number of agents POI_cost:= a dictionary where the keys hold the POI_names and the values are a 2D arrary holding the cost value of reaching to that POI grid_density:= a 2D arrary that stores the number of agents in each cell of the grid, and is updated within every movement of the agents. service_time:= stores the service time at the servicable POIs """ def __init__(self, N, width, height, distance_coefficent=0, density_coefficent=10, density_vision=3): super().__init__() self.max_Astar = 0 self.num_agents = N self.grid = MultiGrid(width, height, False) self.schedule = BaseScheduler(self) self.POI_cost = {} self.grid_density = [[0 for i in range(width)] for j in range(height)] self.serving_time = {'BAR': 3, 'BAR2': 3, 'BAR3': 3, 'BAR4': 3} POI_points = [(24, 49), (24, 1)] POI_names = ['STAGE', 'BAR'] POI_dict = {} for i in range(len(POI_points)): POI_dict[POI_names[i]] = POI_points[i] for i in range(width): for j in range(height): nd = nodeAgent((i, j), self, POI_names) self.grid.place_agent(nd, (i, j)) self.blocks = block_positions create_block(self, block_positions, POI_names) create_POI(self, POI_points, 2) for r in range(len(POI_points)): x = POI_points[r][0] y = POI_points[r][1] A_star_node(self, (x, y), POI_names[r], self.blocks, 0.5, 2) self.POI_cost[POI_names[r]] =\ A_star_array(width, height, (x, y), self.blocks, distance_coefficent, 1) commuters_list = [] for k in range(self.num_agents): a = commuterAgent(k, self, POI_dict, density_coefficent, density_vision) commuters_list.append(a) selected_cells = [] for commuter in commuters_list: self.schedule.add(commuter) notblock = False selected_cell = False while(True): x = random.randrange(self.grid.width) y = random.randrange(self.grid.height) this_cell = self.grid.get_cell_list_contents((x, y)) if (x, y) not in self.blocks and (x, y) not in selected_cells: selected_cells.append((x, y)) break self.grid_density[x][y] += 1 self.grid.place_agent(commuter, (24, 1)) self.datacollector = DataCollector( agent_reporters={"utility": lambda a: a.utility, 'numOfPOIVisits': lambda a: a.numOfPOIVisits}) def step(self): self.datacollector.collect(self) self.schedule.step()