Skip to content

Commit 647f5b6

Browse files
committed
Update documentation to clarify usage of AgentSetRegistry and improve DataCollector examples
1 parent 1c3a63b commit 647f5b6

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

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

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

2828
## Model 🏗️
2929

30-
To add your AgentSet to your Model, you should also add it to the sets with `+=` or `add`.
30+
To add your AgentSet to your Model, use the registry `self.sets` with `+=` or `add`.
3131

32-
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]`.
32+
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`.
3333

3434
Example:
3535

@@ -43,7 +43,8 @@ class EcosystemModel(Model):
4343
def step(self):
4444
self.sets.do("move")
4545
self.sets.do("hunt")
46-
self.prey.do("reproduce")
46+
# Access specific sets via the registry
47+
self.sets["Preys"].do("reproduce")
4748
```
4849

4950
## Space: Grid 🌐
@@ -76,18 +77,23 @@ Example:
7677
class ExampleModel(Model):
7778
def __init__(self):
7879
super().__init__()
79-
self.sets = MoneyAgent(self)
80+
# Add the set to the registry
81+
self.sets.add(MoneyAgents(100, self))
82+
# Configure reporters: use the registry to locate sets; get df from the set
8083
self.datacollector = DataCollector(
8184
model=self,
82-
model_reporters={"total_wealth": lambda m: lambda m: list(m.sets.df.values())[0]["wealth"].sum()},
85+
model_reporters={
86+
"total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(),
87+
},
8388
agent_reporters={"wealth": "wealth"},
8489
storage="csv",
8590
storage_uri="./data",
86-
trigger=lambda m: m.schedule.steps % 2 == 0
91+
trigger=lambda m: m.steps % 2 == 0,
8792
)
8893

8994
def step(self):
90-
self.sets.step()
95+
# Step all sets via the registry
96+
self.sets.do("step")
9197
self.datacollector.conditional_collect()
9298
self.datacollector.flush()
9399
```

docs/general/user-guide/2_introductory-tutorial.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@
7474
" self.sets += agents_cls(N, self)\n",
7575
" self.datacollector = DataCollector(\n",
7676
" model=self,\n",
77-
" model_reporters={\"total_wealth\": lambda m: m.agents[\"wealth\"].sum()},\n",
77+
" model_reporters={\n",
78+
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum()\n",
79+
" },\n",
7880
" agent_reporters={\"wealth\": \"wealth\"},\n",
7981
" storage=\"csv\",\n",
8082
" storage_uri=\"./data\",\n",

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@
120120
" self.dc = DataCollector(\n",
121121
" model=self,\n",
122122
" model_reporters={\n",
123-
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
124-
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
123+
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
124+
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
125125
" },\n",
126126
" agent_reporters={\n",
127127
" \"wealth\": \"wealth\", # pull existing column\n",
@@ -175,7 +175,7 @@
175175
},
176176
{
177177
"cell_type": "code",
178-
"execution_count": 8,
178+
"execution_count": null,
179179
"id": "5f14f38c",
180180
"metadata": {},
181181
"outputs": [
@@ -198,10 +198,8 @@
198198
"model_csv.dc = DataCollector(\n",
199199
" model=model_csv,\n",
200200
" model_reporters={\n",
201-
" \"total_wealth\": lambda m: sum(\n",
202-
" s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n",
203-
" ),\n",
204-
" \"n_agents\": lambda m: len(m.sets.ids),\n",
201+
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
202+
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
205203
" },\n",
206204
" agent_reporters={\n",
207205
" \"wealth\": \"wealth\",\n",
@@ -228,7 +226,7 @@
228226
},
229227
{
230228
"cell_type": "code",
231-
"execution_count": 9,
229+
"execution_count": null,
232230
"id": "8763a12b2bbd4a93a75aff182afb95dc",
233231
"metadata": {
234232
"editable": true
@@ -251,10 +249,8 @@
251249
"model_parq.dc = DataCollector(\n",
252250
" model=model_parq,\n",
253251
" model_reporters={\n",
254-
" \"total_wealth\": lambda m: sum(\n",
255-
" s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n",
256-
" ),\n",
257-
" \"n_agents\": lambda m: len(m.sets.ids),\n",
252+
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
253+
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
258254
" },\n",
259255
" agent_reporters={\n",
260256
" \"wealth\": \"wealth\",\n",
@@ -283,7 +279,7 @@
283279
},
284280
{
285281
"cell_type": "code",
286-
"execution_count": 10,
282+
"execution_count": null,
287283
"id": "7cdc8c89c7104fffa095e18ddfef8986",
288284
"metadata": {
289285
"editable": true
@@ -294,10 +290,8 @@
294290
"model_s3.dc = DataCollector(\n",
295291
" model=model_s3,\n",
296292
" model_reporters={\n",
297-
" \"total_wealth\": lambda m: sum(\n",
298-
" s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n",
299-
" ),\n",
300-
" \"n_agents\": lambda m: len(m.sets.ids),\n",
293+
" \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n",
294+
" \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n",
301295
" },\n",
302296
" agent_reporters={\n",
303297
" \"wealth\": \"wealth\",\n",

0 commit comments

Comments
 (0)