Skip to content

Commit 2c43b3f

Browse files
committed
Merge branch 'main' of https://github.com/projectmesa/mesa-frames into 146-enhancement-consider-using-a-key-based-structure-for-agentsets-instead-of-list-in-agentsdf
2 parents 5c68bd8 + 51c54cd commit 2c43b3f

File tree

17 files changed

+5024
-3
lines changed

17 files changed

+5024
-3
lines changed

docs/general/user-guide/1_classes.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ You can access the underlying DataFrame where agents are stored with `self.df`.
2727

2828
## Model 🏗️
2929

30+
<<<<<<< HEAD
3031
To add your AgentSet to your Model, use the registry `self.sets` with `+=` or `add`.
3132

3233
Note: All agent sets live inside `AgentSetRegistry` (available as `model.sets`). Access sets through the registry, and access DataFrames from the set itself. For example: `self.sets["Preys"].df`.
34+
=======
35+
To add your AgentSet to your Model, you should also add it to the sets with `+=` or `add`.
36+
37+
NOTE: Model.sets are stored in a class which is entirely similar to AgentSet called AgentSetRegistry. The API of the two are the same. If you try accessing AgentSetRegistry.df, you will get a dictionary of `[AgentSet, DataFrame]`.
38+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
3339
3440
Example:
3541

@@ -43,8 +49,12 @@ class EcosystemModel(Model):
4349
def step(self):
4450
self.sets.do("move")
4551
self.sets.do("hunt")
52+
<<<<<<< HEAD
4653
# Access specific sets via the registry
4754
self.sets["Preys"].do("reproduce")
55+
=======
56+
self.prey.do("reproduce")
57+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
4858
```
4959

5060
## Space: Grid 🌐
@@ -77,6 +87,7 @@ Example:
7787
class ExampleModel(Model):
7888
def __init__(self):
7989
super().__init__()
90+
<<<<<<< HEAD
8091
# Add the set to the registry
8192
self.sets.add(MoneyAgents(100, self))
8293
# Configure reporters: use the registry to locate sets; get df from the set
@@ -85,15 +96,25 @@ class ExampleModel(Model):
8596
model_reporters={
8697
"total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(),
8798
},
99+
=======
100+
self.sets = MoneyAgent(self)
101+
self.datacollector = DataCollector(
102+
model=self,
103+
model_reporters={"total_wealth": lambda m: lambda m: list(m.sets.df.values())[0]["wealth"].sum()},
104+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
88105
agent_reporters={"wealth": "wealth"},
89106
storage="csv",
90107
storage_uri="./data",
91108
trigger=lambda m: m.steps % 2 == 0,
92109
)
93110

94111
def step(self):
112+
<<<<<<< HEAD
95113
# Step all sets via the registry
96114
self.sets.do("step")
115+
=======
116+
self.sets.step()
117+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
97118
self.datacollector.conditional_collect()
98119
self.datacollector.flush()
99120
```

docs/general/user-guide/4_datacollector.ipynb

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
},
2727
{
2828
"cell_type": "code",
29+
<<<<<<< HEAD
2930
"execution_count": 6,
31+
=======
32+
"execution_count": 18,
33+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
3034
"id": "9a63283cbaf04dbcab1f6479b197f3a8",
3135
"metadata": {
3236
"editable": true
@@ -63,18 +67,27 @@
6367
" │ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
6468
" │ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n",
6569
" ╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n",
70+
<<<<<<< HEAD
6671
" │ 2 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
6772
" │ 4 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
6873
" │ 6 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
6974
" │ 8 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
7075
" │ 10 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
76+
=======
77+
" │ 2 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
78+
" │ 4 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
79+
" │ 6 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
80+
" │ 8 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
81+
" │ 10 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n",
82+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
7183
" └──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘,\n",
7284
" 'agent': shape: (5_000, 4)\n",
7385
" ┌────────────────────┬──────┬─────────────────────────────────┬───────┐\n",
7486
" │ wealth_MoneyAgents ┆ step ┆ seed ┆ batch │\n",
7587
" │ --- ┆ --- ┆ --- ┆ --- │\n",
7688
" │ f64 ┆ i32 ┆ str ┆ i32 │\n",
7789
" ╞════════════════════╪══════╪═════════════════════════════════╪═══════╡\n",
90+
<<<<<<< HEAD
7891
" │ 3.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n",
7992
" │ 0.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n",
8093
" │ 2.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n",
@@ -90,6 +103,23 @@
90103
]
91104
},
92105
"execution_count": 7,
106+
=======
107+
" │ 0.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n",
108+
" │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n",
109+
" │ 1.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n",
110+
" │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n",
111+
" │ 6.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n",
112+
" │ … ┆ … ┆ … ┆ … │\n",
113+
" │ 4.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n",
114+
" │ 1.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n",
115+
" │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n",
116+
" │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n",
117+
" │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n",
118+
" └────────────────────┴──────┴─────────────────────────────────┴───────┘}"
119+
]
120+
},
121+
"execution_count": 19,
122+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
93123
"metadata": {},
94124
"output_type": "execute_result"
95125
}
@@ -120,8 +150,13 @@
120150
" self.dc = DataCollector(\n",
121151
" model=self,\n",
122152
" model_reporters={\n",
153+
<<<<<<< HEAD
123154
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
124155
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
156+
=======
157+
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
158+
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
159+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
125160
" },\n",
126161
" agent_reporters={\n",
127162
" \"wealth\": \"wealth\", # pull existing column\n",
@@ -175,7 +210,7 @@
175210
},
176211
{
177212
"cell_type": "code",
178-
"execution_count": null,
213+
"execution_count": 20,
179214
"id": "5f14f38c",
180215
"metadata": {},
181216
"outputs": [
@@ -185,7 +220,11 @@
185220
"[]"
186221
]
187222
},
223+
<<<<<<< HEAD
188224
"execution_count": 8,
225+
=======
226+
"execution_count": 20,
227+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
189228
"metadata": {},
190229
"output_type": "execute_result"
191230
}
@@ -198,8 +237,13 @@
198237
"model_csv.dc = DataCollector(\n",
199238
" model=model_csv,\n",
200239
" model_reporters={\n",
240+
<<<<<<< HEAD
201241
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
202242
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
243+
=======
244+
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
245+
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
246+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
203247
" },\n",
204248
" agent_reporters={\n",
205249
" \"wealth\": \"wealth\",\n",
@@ -226,7 +270,7 @@
226270
},
227271
{
228272
"cell_type": "code",
229-
"execution_count": null,
273+
"execution_count": 21,
230274
"id": "8763a12b2bbd4a93a75aff182afb95dc",
231275
"metadata": {
232276
"editable": true
@@ -238,7 +282,11 @@
238282
"[]"
239283
]
240284
},
285+
<<<<<<< HEAD
241286
"execution_count": 9,
287+
=======
288+
"execution_count": 21,
289+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
242290
"metadata": {},
243291
"output_type": "execute_result"
244292
}
@@ -249,8 +297,13 @@
249297
"model_parq.dc = DataCollector(\n",
250298
" model=model_parq,\n",
251299
" model_reporters={\n",
300+
<<<<<<< HEAD
252301
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
253302
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
303+
=======
304+
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
305+
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
306+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
254307
" },\n",
255308
" agent_reporters={\n",
256309
" \"wealth\": \"wealth\",\n",
@@ -279,7 +332,7 @@
279332
},
280333
{
281334
"cell_type": "code",
282-
"execution_count": null,
335+
"execution_count": 22,
283336
"id": "7cdc8c89c7104fffa095e18ddfef8986",
284337
"metadata": {
285338
"editable": true
@@ -290,8 +343,13 @@
290343
"model_s3.dc = DataCollector(\n",
291344
" model=model_s3,\n",
292345
" model_reporters={\n",
346+
<<<<<<< HEAD
293347
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
294348
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
349+
=======
350+
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
351+
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
352+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
295353
" },\n",
296354
" agent_reporters={\n",
297355
" \"wealth\": \"wealth\",\n",
@@ -319,7 +377,11 @@
319377
},
320378
{
321379
"cell_type": "code",
380+
<<<<<<< HEAD
322381
"execution_count": 11,
382+
=======
383+
"execution_count": 23,
384+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
323385
"id": "938c804e27f84196a10c8828c723f798",
324386
"metadata": {
325387
"editable": true
@@ -381,7 +443,11 @@
381443
},
382444
{
383445
"cell_type": "code",
446+
<<<<<<< HEAD
384447
"execution_count": 12,
448+
=======
449+
"execution_count": 24,
450+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
385451
"id": "59bbdb311c014d738909a11f9e486628",
386452
"metadata": {
387453
"editable": true
@@ -410,7 +476,11 @@
410476
},
411477
{
412478
"cell_type": "code",
479+
<<<<<<< HEAD
413480
"execution_count": 13,
481+
=======
482+
"execution_count": 25,
483+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
414484
"id": "8a65eabff63a45729fe45fb5ade58bdc",
415485
"metadata": {
416486
"editable": true
@@ -426,7 +496,11 @@
426496
" white-space: pre-wrap;\n",
427497
"}\n",
428498
"</style>\n",
499+
<<<<<<< HEAD
429500
"<small>shape: (5, 5)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>step</th><th>seed</th><th>batch</th><th>total_wealth</th><th>n_agents</th></tr><tr><td>i64</td><td>str</td><td>i64</td><td>f64</td><td>i64</td></tr></thead><tbody><tr><td>2</td><td>&quot;540832786058427425452319829502…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>4</td><td>&quot;540832786058427425452319829502…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>6</td><td>&quot;540832786058427425452319829502…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>8</td><td>&quot;540832786058427425452319829502…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>10</td><td>&quot;540832786058427425452319829502…</td><td>0</td><td>100.0</td><td>100</td></tr></tbody></table></div>"
501+
=======
502+
"<small>shape: (5, 5)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>step</th><th>seed</th><th>batch</th><th>total_wealth</th><th>n_agents</th></tr><tr><td>i64</td><td>str</td><td>i64</td><td>f64</td><td>i64</td></tr></thead><tbody><tr><td>2</td><td>&quot;732054881101029867447298951813…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>4</td><td>&quot;732054881101029867447298951813…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>6</td><td>&quot;732054881101029867447298951813…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>8</td><td>&quot;732054881101029867447298951813…</td><td>0</td><td>100.0</td><td>100</td></tr><tr><td>10</td><td>&quot;732054881101029867447298951813…</td><td>0</td><td>100.0</td><td>100</td></tr></tbody></table></div>"
503+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
430504
],
431505
"text/plain": [
432506
"shape: (5, 5)\n",
@@ -435,6 +509,7 @@
435509
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
436510
"│ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n",
437511
"╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n",
512+
<<<<<<< HEAD
438513
"│ 2 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n",
439514
"│ 4 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n",
440515
"│ 6 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n",
@@ -444,6 +519,17 @@
444519
]
445520
},
446521
"execution_count": 13,
522+
=======
523+
"│ 2 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n",
524+
"│ 4 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n",
525+
"│ 6 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n",
526+
"│ 8 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n",
527+
"│ 10 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n",
528+
"└──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘"
529+
]
530+
},
531+
"execution_count": 25,
532+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
447533
"metadata": {},
448534
"output_type": "execute_result"
449535
}

examples/sugarscape_ig/ss_polars/model.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,27 @@ def __init__(
3333
sugar=sugar_grid.flatten(), max_sugar=sugar_grid.flatten()
3434
)
3535
self.space.set_cells(sugar_grid)
36+
<<<<<<< HEAD
3637
# Create and register the main agent set; keep its name for later lookups
3738
main_set = agent_type(self, n_agents, initial_sugar, metabolism, vision)
3839
self.sets += main_set
3940
self._main_set_name = main_set.name
41+
=======
42+
self.sets += agent_type(self, n_agents, initial_sugar, metabolism, vision)
43+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
4044
if initial_positions is not None:
4145
self.space.place_agents(self.sets, initial_positions)
4246
else:
4347
self.space.place_to_empty(self.sets)
4448

4549
def run_model(self, steps: int) -> list[int]:
4650
for _ in range(steps):
51+
<<<<<<< HEAD
4752
# Stop if the main agent set is empty
4853
if len(self.sets[self._main_set_name]) == 0: # type: ignore[index]
54+
=======
55+
if len(list(self.sets.df.values())[0]) == 0:
56+
>>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956
4957
return
5058
empty_cells = self.space.empty_cells
5159
full_cells = self.space.full_cells

0 commit comments

Comments
 (0)