import mesa def compute_gini(model): agent_wealths = [agent.wealth for agent in model.schedule.agents] x = sorted(agent_wealths) N = model.num_agents B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x)) return 1 + (1 / N) - 2 * B class MoneyAgent(mesa.Agent): """An agent with fixed initial wealth.""" def __init__(self, unique_id, model): super().__init__(unique_id, model) self.wealth = 1 def move(self): possible_steps = self.model.grid.get_neighborhood( self.pos, moore=True, include_center=False ) new_position = self.random.choice(possible_steps) self.model.grid.move_agent(self, new_position) def give_money(self): cellmates = self.model.grid.get_cell_list_contents([self.pos]) if len(cellmates) > 1: other_agent = self.random.choice(cellmates) other_agent.wealth += 1 self.wealth -= 1 def step(self): self.move() if self.wealth > 0: self.give_money() class MoneyModel(mesa.Model): """A model with some number of agents.""" def __init__(self, N, width, height): self.num_agents = N self.grid = mesa.space.MultiGrid(width, height, True) self.schedule = mesa.time.RandomActivation(self) # Create agents for i in range(self.num_agents): a = MoneyAgent(i, self) self.schedule.add(a) # Add the agent to a random grid cell x = self.random.randrange(self.grid.width) y = self.random.randrange(self.grid.height) self.grid.place_agent(a, (x, y)) def step(self): self.datacollector.collect(self) self.schedule.step() def agent_portrayal(agent): portrayal = {"Shape": "circle", "Filled": "true", "r": 0.5} if agent.wealth > 0: portrayal["Color"] = "red" portrayal["Layer"] = 0 else: portrayal["Color"] = "grey" portrayal["Layer"] = 1 portrayal["r"] = 0.2 return portrayal grid = mesa.visualization.CanvasGrid(agent_portrayal, 10, 10, 500, 500) server = mesa.visualization.ModularServer( MoneyModel, [grid], "Money Model", {"N": 100, "width": 10, "height": 10} ) chart = mesa.visualization.ChartModule([{"Label": "Gini", "Color": "Black"}], data_collector_name='datacollector') server = mesa.visualization.ModularServer(MoneyModel, [grid, chart], "Money Model", {"N":100, "width":10, "height":10}) server.port = 7860 if __name__ == "__main__": server.launch()