Skip to content

Commit

Permalink
Ensure grasspatches info is only collected if gras is true (projectme…
Browse files Browse the repository at this point in the history
…sa#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.
  • Loading branch information
quaquel authored and EwoutH committed Sep 20, 2024
1 parent c613d92 commit 81b50d6
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions examples/wolf_sheep/wolf_sheep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ def __init__(
self.sheep_gain_from_food = sheep_gain_from_food

self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
self.datacollector = mesa.DataCollector(
{
"Wolves": lambda m: len(m.get_agents_of_type(Wolf)),
"Sheep": lambda m: len(m.get_agents_of_type(Sheep)),
"Grass": lambda m: len(
m.get_agents_of_type(GrassPatch).select(lambda a: a.fully_grown)
),
}
)

collectors = {
"Wolves": lambda m: len(m.get_agents_of_type(Wolf)),
"Sheep": lambda m: len(m.get_agents_of_type(Sheep)),
}

if grass:
collectors["Grass"] = lambda m: len(m.get_agents_of_type(GrassPatch))

self.datacollector = mesa.DataCollector(collectors)

# Create sheep:
for i in range(self.initial_sheep):
Expand Down

0 comments on commit 81b50d6

Please sign in to comment.