@@ -37,6 +37,78 @@ def track_run(model):
3737 return model .uid
3838
3939
40+ class BankReservesModel (mesa .Model ):
41+ # id generator to track run number in batch run data
42+ id_gen = itertools .count (1 )
43+
44+ # grid height
45+ grid_h = 20
46+ # grid width
47+ grid_w = 20
48+
49+ """init parameters "init_people", "rich_threshold", and "reserve_percent"
50+ are all set via Slider"""
51+
52+ def __init__ (
53+ self ,
54+ height = grid_h ,
55+ width = grid_w ,
56+ init_people = 2 ,
57+ rich_threshold = 10 ,
58+ reserve_percent = 50 ,
59+ ):
60+ super ().__init__ ()
61+ self .uid = next (self .id_gen )
62+ self .height = height
63+ self .width = width
64+ self .init_people = init_people
65+
66+ self .grid = mesa .space .MultiGrid (self .width , self .height , torus = True )
67+ # rich_threshold is the amount of savings a person needs to be considered "rich"
68+ self .rich_threshold = rich_threshold
69+ self .reserve_percent = reserve_percent
70+ # see datacollector functions above
71+ self .datacollector = mesa .DataCollector (
72+ model_reporters = {
73+ "Rich" : get_num_rich_agents ,
74+ "Poor" : get_num_poor_agents ,
75+ "Middle Class" : get_num_mid_agents ,
76+ "Savings" : get_total_savings ,
77+ "Wallets" : get_total_wallets ,
78+ "Money" : get_total_money ,
79+ "Loans" : get_total_loans ,
80+ "Model Params" : track_params ,
81+ "Run" : track_run ,
82+ },
83+ agent_reporters = {"Wealth" : "wealth" },
84+ )
85+
86+ # create a single bank object for the model
87+ self .bank = Bank (self , self .reserve_percent )
88+
89+ # create people for the model according to number of people set by user
90+ for i in range (self .init_people ):
91+ # set x coordinate as a random number within the width of the grid
92+ x = self .random .randrange (self .width )
93+ # set y coordinate as a random number within the height of the grid
94+ y = self .random .randrange (self .height )
95+ p = Person (i , (x , y ), self , True , self .bank , self .rich_threshold )
96+ # place the Person object on the grid at coordinates (x, y)
97+ self .grid .place_agent (p , (x , y ))
98+
99+ self .running = True
100+
101+ def step (self ):
102+ # collect data
103+ self .datacollector .collect (self )
104+ # tell all the agents in the model to run their step function
105+ self .agents .shuffle_do ("step" )
106+
107+ def run_model (self ):
108+ for i in range (self .run_time ):
109+ self .step ()
110+
111+
40112# parameter lists for each parameter to be tested in batch run
41113br_params = {
42114 "init_people" : [25 , 100 ],
0 commit comments