@@ -28,7 +28,7 @@ def __init__(self, unique_id, model):
28
28
def step (self ):
29
29
# Verify agent has some wealth
30
30
if self .wealth > 0 :
31
- other_agent = self .random .choice (self .model .schedule . agents )
31
+ other_agent = self .random .choice (self .model .agents )
32
32
if other_agent is not None :
33
33
other_agent .wealth += 1
34
34
self .wealth -= 1
@@ -41,19 +41,13 @@ def __init__(self, N):
41
41
super ().__init__ ()
42
42
self .num_agents = N
43
43
# Create scheduler and assign it to the model
44
- self .schedule = mesa .time .RandomActivation (self )
45
-
46
- # Create agents
47
- for i in range (self .num_agents ):
48
- a = MoneyAgent (i , self )
49
- # Add the agent to the scheduler
50
- self .schedule .add (a )
44
+ self .agents = [MoneyAgent (i , self ) for i in range (self .num_agents )]
51
45
52
46
def step (self ):
53
47
"""Advance the model by one step."""
54
-
55
- # The model's step will go here for now this will call the step method of each agent and print the agent's unique_id
56
- self . schedule .step ()
48
+ self . random . shuffle ( self . agents )
49
+ for agent in self . agents :
50
+ agent .step ()
57
51
58
52
def run_model (self , n_steps ) -> None :
59
53
for _ in range (n_steps ):
@@ -176,7 +170,9 @@ def __init__(self, n: int, model: ModelDF) -> None:
176
170
# 2. Adding the dataframe with add
177
171
# self.add(pd.DataFrame({"unique_id": np.arange(n), "wealth": np.ones(n)}))
178
172
# 3. Adding the dataframe with __iadd__
179
- self += pd .DataFrame ({"unique_id" : np .arange (n ), "wealth" : np .ones (n )})
173
+ self += pd .DataFrame (
174
+ {"unique_id" : np .arange (n , dtype = "int64" ), "wealth" : np .ones (n )}
175
+ )
180
176
181
177
def step (self ) -> None :
182
178
# The give_money method is called
@@ -212,7 +208,9 @@ class MoneyAgentPandasNative(AgentSetPandas):
212
208
def __init__ (self , n : int , model : ModelDF ) -> None :
213
209
super ().__init__ (model )
214
210
## Adding the agents to the agent set
215
- self += pd .DataFrame ({"unique_id" : np .arange (n ), "wealth" : np .ones (n )})
211
+ self += pd .DataFrame (
212
+ {"unique_id" : np .arange (n , dtype = "int64" ), "wealth" : np .ones (n )}
213
+ )
216
214
217
215
def step (self ) -> None :
218
216
# The give_money method is called
@@ -274,33 +272,68 @@ def mesa_frames_pandas_native(n_agents: int) -> None:
274
272
model .run_model (100 )
275
273
276
274
275
+ def plot_and_print_benchmark (labels , kernels , n_range , title , image_path ):
276
+ out = perfplot .bench (
277
+ setup = lambda n : n ,
278
+ kernels = kernels ,
279
+ labels = labels ,
280
+ n_range = n_range ,
281
+ xlabel = "Number of agents" ,
282
+ equality_check = None ,
283
+ title = title ,
284
+ )
285
+
286
+ plt .ylabel ("Execution time (s)" )
287
+ out .save (image_path )
288
+
289
+ print ("\n Execution times:" )
290
+ for i , label in enumerate (labels ):
291
+ print (f"---------------\n { label } :" )
292
+ for n , t in zip (out .n_range , out .timings_s [i ]):
293
+ print (f" Number of agents: { n } , Time: { t :.2f} seconds" )
294
+ print ("---------------" )
295
+
296
+
277
297
def main ():
278
298
sns .set_theme (style = "whitegrid" )
279
299
280
- labels = [
281
- # "mesa",
300
+ labels_0 = [
301
+ "mesa" ,
282
302
"mesa-frames (pl concise)" ,
283
303
"mesa-frames (pl native)" ,
284
304
"mesa-frames (pd concise)" ,
285
305
"mesa-frames (pd native)" ,
286
306
]
287
- out = perfplot .bench (
288
- setup = lambda n : n ,
289
- kernels = [
290
- # mesa_implementation,
291
- mesa_frames_polars_concise ,
292
- mesa_frames_polars_native ,
293
- mesa_frames_pandas_concise ,
294
- mesa_frames_pandas_native ,
295
- ],
296
- labels = labels ,
297
- n_range = [k for k in range (100 , 10000 , 1000 )],
298
- xlabel = "Number of agents" ,
299
- equality_check = None ,
300
- title = "100 steps of the Boltzmann Wealth model:\n " + " vs " .join (labels ),
301
- )
302
- plt .ylabel ("Execution time (s)" )
303
- out .save ("docs/images/readme_plot_2.png" )
307
+ kernels_0 = [
308
+ mesa_implementation ,
309
+ mesa_frames_polars_concise ,
310
+ mesa_frames_polars_native ,
311
+ mesa_frames_pandas_concise ,
312
+ mesa_frames_pandas_native ,
313
+ ]
314
+ n_range_0 = [k for k in range (0 , 100001 , 10000 )]
315
+ title_0 = "100 steps of the Boltzmann Wealth model:\n " + " vs " .join (labels_0 )
316
+ image_path_0 = "docs/images/readme_plot_0.png"
317
+
318
+ plot_and_print_benchmark (labels_0 , kernels_0 , n_range_0 , title_0 , image_path_0 )
319
+
320
+ labels_1 = [
321
+ "mesa-frames (pl concise)" ,
322
+ "mesa-frames (pl native)" ,
323
+ "mesa-frames (pd concise)" ,
324
+ "mesa-frames (pd native)" ,
325
+ ]
326
+ kernels_1 = [
327
+ mesa_frames_polars_concise ,
328
+ mesa_frames_polars_native ,
329
+ mesa_frames_pandas_concise ,
330
+ mesa_frames_pandas_native ,
331
+ ]
332
+ n_range_1 = [k for k in range (100000 , 1000001 , 100000 )]
333
+ title_1 = "100 steps of the Boltzmann Wealth model:\n " + " vs " .join (labels_1 )
334
+ image_path_1 = "docs/images/readme_plot_1.png"
335
+
336
+ plot_and_print_benchmark (labels_1 , kernels_1 , n_range_1 , title_1 , image_path_1 )
304
337
305
338
306
339
if __name__ == "__main__" :
0 commit comments