Spaces:
Sleeping
Sleeping
Commit
•
f1f325c
1
Parent(s):
b674049
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,90 @@
|
|
1 |
import mesa
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
def agent_portrayal(agent):
|
5 |
-
portrayal = {
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
"
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import mesa
|
2 |
|
3 |
+
def compute_gini(model):
|
4 |
+
agent_wealths = [agent.wealth for agent in model.schedule.agents]
|
5 |
+
x = sorted(agent_wealths)
|
6 |
+
N = model.num_agents
|
7 |
+
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
|
8 |
+
return 1 + (1 / N) - 2 * B
|
9 |
+
|
10 |
+
|
11 |
+
class MoneyAgent(mesa.Agent):
|
12 |
+
"""An agent with fixed initial wealth."""
|
13 |
|
14 |
+
def __init__(self, unique_id, model):
|
15 |
+
super().__init__(unique_id, model)
|
16 |
+
self.wealth = 1
|
17 |
+
|
18 |
+
def move(self):
|
19 |
+
possible_steps = self.model.grid.get_neighborhood(
|
20 |
+
self.pos, moore=True, include_center=False
|
21 |
+
)
|
22 |
+
new_position = self.random.choice(possible_steps)
|
23 |
+
self.model.grid.move_agent(self, new_position)
|
24 |
+
|
25 |
+
def give_money(self):
|
26 |
+
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
27 |
+
if len(cellmates) > 1:
|
28 |
+
other_agent = self.random.choice(cellmates)
|
29 |
+
other_agent.wealth += 1
|
30 |
+
self.wealth -= 1
|
31 |
+
|
32 |
+
def step(self):
|
33 |
+
self.move()
|
34 |
+
if self.wealth > 0:
|
35 |
+
self.give_money()
|
36 |
+
|
37 |
+
|
38 |
+
class MoneyModel(mesa.Model):
|
39 |
+
"""A model with some number of agents."""
|
40 |
+
|
41 |
+
def __init__(self, N, width, height):
|
42 |
+
self.num_agents = N
|
43 |
+
self.grid = mesa.space.MultiGrid(width, height, True)
|
44 |
+
self.schedule = mesa.time.RandomActivation(self)
|
45 |
+
# Create agents
|
46 |
+
for i in range(self.num_agents):
|
47 |
+
a = MoneyAgent(i, self)
|
48 |
+
self.schedule.add(a)
|
49 |
+
# Add the agent to a random grid cell
|
50 |
+
x = self.random.randrange(self.grid.width)
|
51 |
+
y = self.random.randrange(self.grid.height)
|
52 |
+
self.grid.place_agent(a, (x, y))
|
53 |
+
|
54 |
+
def step(self):
|
55 |
+
self.datacollector.collect(self)
|
56 |
+
self.schedule.step()
|
57 |
+
|
58 |
def agent_portrayal(agent):
|
59 |
+
portrayal = {"Shape": "circle",
|
60 |
+
"Filled": "true",
|
61 |
+
"r": 0.5}
|
62 |
+
|
63 |
+
if agent.wealth > 0:
|
64 |
+
portrayal["Color"] = "red"
|
65 |
+
portrayal["Layer"] = 0
|
66 |
+
else:
|
67 |
+
portrayal["Color"] = "grey"
|
68 |
+
portrayal["Layer"] = 1
|
69 |
+
portrayal["r"] = 0.2
|
70 |
+
return portrayal
|
71 |
+
|
72 |
+
|
73 |
+
grid = mesa.visualization.CanvasGrid(agent_portrayal, 10, 10, 500, 500)
|
74 |
+
server = mesa.visualization.ModularServer(
|
75 |
+
MoneyModel, [grid], "Money Model", {"N": 100, "width": 10, "height": 10}
|
76 |
+
)
|
77 |
+
|
78 |
+
chart = mesa.visualization.ChartModule([{"Label": "Gini",
|
79 |
+
"Color": "Black"}],
|
80 |
+
data_collector_name='datacollector')
|
81 |
+
|
82 |
+
server = mesa.visualization.ModularServer(MoneyModel,
|
83 |
+
[grid, chart],
|
84 |
+
"Money Model",
|
85 |
+
{"N":100, "width":10, "height":10})
|
86 |
+
|
87 |
+
server.port = 7860
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
server.launch()
|