2929import mesa
3030import numpy as np
3131import pandas as pd
32- from bank_reserves .agents import Bank , Person
33-
34- # Start of datacollector functions
35-
36-
37- def get_num_rich_agents (model ):
38- """list of rich agents"""
39-
40- rich_agents = [a for a in model .agents if a .savings > model .rich_threshold ]
41- # return number of rich agents
42- return len (rich_agents )
43-
44-
45- def get_num_poor_agents (model ):
46- """list of poor agents"""
47-
48- poor_agents = [a for a in model .agents if a .loans > 10 ]
49- # return number of poor agents
50- return len (poor_agents )
51-
52-
53- def get_num_mid_agents (model ):
54- """list of middle class agents"""
55-
56- mid_agents = [
57- a for a in model .agents if a .loans < 10 and a .savings < model .rich_threshold
58- ]
59- # return number of middle class agents
60- return len (mid_agents )
61-
62-
63- def get_total_savings (model ):
64- """list of amounts of all agents' savings"""
65-
66- agent_savings = [a .savings for a in model .agents ]
67- # return the sum of agents' savings
68- return np .sum (agent_savings )
69-
70-
71- def get_total_wallets (model ):
72- """list of amounts of all agents' wallets"""
73-
74- agent_wallets = [a .wallet for a in model .agents ]
75- # return the sum of all agents' wallets
76- return np .sum (agent_wallets )
77-
78-
79- def get_total_money (model ):
80- """sum of all agents' wallets"""
81-
82- wallet_money = get_total_wallets (model )
83- # sum of all agents' savings
84- savings_money = get_total_savings (model )
85- # return sum of agents' wallets and savings for total money
86- return wallet_money + savings_money
87-
88-
89- def get_total_loans (model ):
90- """list of amounts of all agents' loans"""
91-
92- agent_loans = [a .loans for a in model .agents ]
93- # return sum of all agents' loans
94- return np .sum (agent_loans )
9532
33+ from bank_reserves .model import BankReservesModel
9634
9735def track_params (model ):
9836 return (model .init_people , model .rich_threshold , model .reserve_percent )
@@ -101,79 +39,6 @@ def track_params(model):
10139def track_run (model ):
10240 return model .uid
10341
104-
105- class BankReservesModel (mesa .Model ):
106- # id generator to track run number in batch run data
107- id_gen = itertools .count (1 )
108-
109- # grid height
110- grid_h = 20
111- # grid width
112- grid_w = 20
113-
114- """init parameters "init_people", "rich_threshold", and "reserve_percent"
115- are all set via Slider"""
116-
117- def __init__ (
118- self ,
119- height = grid_h ,
120- width = grid_w ,
121- init_people = 2 ,
122- rich_threshold = 10 ,
123- reserve_percent = 50 ,
124- ):
125- super ().__init__ ()
126- self .uid = next (self .id_gen )
127- self .height = height
128- self .width = width
129- self .init_people = init_people
130-
131- self .grid = mesa .space .MultiGrid (self .width , self .height , torus = True )
132- # rich_threshold is the amount of savings a person needs to be considered "rich"
133- self .rich_threshold = rich_threshold
134- self .reserve_percent = reserve_percent
135- # see datacollector functions above
136- self .datacollector = mesa .DataCollector (
137- model_reporters = {
138- "Rich" : get_num_rich_agents ,
139- "Poor" : get_num_poor_agents ,
140- "Middle Class" : get_num_mid_agents ,
141- "Savings" : get_total_savings ,
142- "Wallets" : get_total_wallets ,
143- "Money" : get_total_money ,
144- "Loans" : get_total_loans ,
145- "Model Params" : track_params ,
146- "Run" : track_run ,
147- },
148- agent_reporters = {"Wealth" : "wealth" },
149- )
150-
151- # create a single bank object for the model
152- self .bank = Bank (self , self .reserve_percent )
153-
154- # create people for the model according to number of people set by user
155- for i in range (self .init_people ):
156- # set x coordinate as a random number within the width of the grid
157- x = self .random .randrange (self .width )
158- # set y coordinate as a random number within the height of the grid
159- y = self .random .randrange (self .height )
160- p = Person (i , (x , y ), self , True , self .bank , self .rich_threshold )
161- # place the Person object on the grid at coordinates (x, y)
162- self .grid .place_agent (p , (x , y ))
163-
164- self .running = True
165-
166- def step (self ):
167- # collect data
168- self .datacollector .collect (self )
169- # tell all the agents in the model to run their step function
170- self .agents .shuffle ().do ("step" )
171-
172- def run_model (self ):
173- for i in range (self .run_time ):
174- self .step ()
175-
176-
17742# parameter lists for each parameter to be tested in batch run
17843br_params = {
17944 "init_people" : [25 , 100 ],
0 commit comments