37
37
def get_num_rich_agents (model ):
38
38
"""list of rich agents"""
39
39
40
- rich_agents = [a for a in model .schedule . agents if a .savings > model .rich_threshold ]
40
+ rich_agents = [a for a in model .agents if a .savings > model .rich_threshold ]
41
41
# return number of rich agents
42
42
return len (rich_agents )
43
43
44
44
45
45
def get_num_poor_agents (model ):
46
46
"""list of poor agents"""
47
47
48
- poor_agents = [a for a in model .schedule . agents if a .loans > 10 ]
48
+ poor_agents = [a for a in model .agents if a .loans > 10 ]
49
49
# return number of poor agents
50
50
return len (poor_agents )
51
51
@@ -54,9 +54,7 @@ def get_num_mid_agents(model):
54
54
"""list of middle class agents"""
55
55
56
56
mid_agents = [
57
- a
58
- for a in model .schedule .agents
59
- if a .loans < 10 and a .savings < model .rich_threshold
57
+ a for a in model .agents if a .loans < 10 and a .savings < model .rich_threshold
60
58
]
61
59
# return number of middle class agents
62
60
return len (mid_agents )
@@ -65,15 +63,15 @@ def get_num_mid_agents(model):
65
63
def get_total_savings (model ):
66
64
"""list of amounts of all agents' savings"""
67
65
68
- agent_savings = [a .savings for a in model .schedule . agents ]
66
+ agent_savings = [a .savings for a in model .agents ]
69
67
# return the sum of agents' savings
70
68
return np .sum (agent_savings )
71
69
72
70
73
71
def get_total_wallets (model ):
74
72
"""list of amounts of all agents' wallets"""
75
73
76
- agent_wallets = [a .wallet for a in model .schedule . agents ]
74
+ agent_wallets = [a .wallet for a in model .agents ]
77
75
# return the sum of all agents' wallets
78
76
return np .sum (agent_wallets )
79
77
@@ -91,7 +89,7 @@ def get_total_money(model):
91
89
def get_total_loans (model ):
92
90
"""list of amounts of all agents' loans"""
93
91
94
- agent_loans = [a .loans for a in model .schedule . agents ]
92
+ agent_loans = [a .loans for a in model .agents ]
95
93
# return sum of all agents' loans
96
94
return np .sum (agent_loans )
97
95
@@ -129,7 +127,7 @@ def __init__(
129
127
self .height = height
130
128
self .width = width
131
129
self .init_people = init_people
132
- self . schedule = mesa . time . RandomActivation ( self )
130
+
133
131
self .grid = mesa .space .MultiGrid (self .width , self .height , torus = True )
134
132
# rich_threshold is the amount of savings a person needs to be considered "rich"
135
133
self .rich_threshold = rich_threshold
@@ -150,8 +148,8 @@ def __init__(
150
148
agent_reporters = {"Wealth" : "wealth" },
151
149
)
152
150
153
- # create a single bank for the model
154
- self .bank = Bank (1 , self , self .reserve_percent )
151
+ # create a single bank object for the model
152
+ self .bank = Bank (self , self .reserve_percent )
155
153
156
154
# create people for the model according to number of people set by user
157
155
for i in range (self .init_people ):
@@ -162,16 +160,14 @@ def __init__(
162
160
p = Person (i , (x , y ), self , True , self .bank , self .rich_threshold )
163
161
# place the Person object on the grid at coordinates (x, y)
164
162
self .grid .place_agent (p , (x , y ))
165
- # add the Person object to the model schedule
166
- self .schedule .add (p )
167
163
168
164
self .running = True
169
165
170
166
def step (self ):
171
167
# collect data
172
168
self .datacollector .collect (self )
173
169
# tell all the agents in the model to run their step function
174
- self .schedule . step ( )
170
+ self .agents . shuffle (). do ( "step" )
175
171
176
172
def run_model (self ):
177
173
for i in range (self .run_time ):
0 commit comments