Skip to content

Commit 1d67252

Browse files
authored
Ensure grasspatches info is only collected if gras is true (#181)
the datacollector allways includes grasspatches even if grass is False. This is breaking (the proposed changes to agent storage in Model. The underlying problem is that Model.get_agents_of_type now raises a KeyError if type does not exist. I think this is desirable behavior becuase errrors should not be passed over in silence. But this requires fixing the wolf sheep example as done here.
1 parent 269ff1a commit 1d67252

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

examples/wolf_sheep/wolf_sheep/model.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,16 @@ def __init__(
7979
self.sheep_gain_from_food = sheep_gain_from_food
8080

8181
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
82-
self.datacollector = mesa.DataCollector(
83-
{
84-
"Wolves": lambda m: len(m.get_agents_of_type(Wolf)),
85-
"Sheep": lambda m: len(m.get_agents_of_type(Sheep)),
86-
"Grass": lambda m: len(
87-
m.get_agents_of_type(GrassPatch).select(lambda a: a.fully_grown)
88-
),
89-
}
90-
)
82+
83+
collectors = {
84+
"Wolves": lambda m: len(m.get_agents_of_type(Wolf)),
85+
"Sheep": lambda m: len(m.get_agents_of_type(Sheep)),
86+
}
87+
88+
if grass:
89+
collectors["Grass"] = lambda m: len(m.get_agents_of_type(GrassPatch))
90+
91+
self.datacollector = mesa.DataCollector(collectors)
9192

9293
# Create sheep:
9394
for i in range(self.initial_sheep):

0 commit comments

Comments
 (0)