Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from mesa.datacollection import DataCollector
class MyDataCollector(DataCollector):
## subclass DataCollector to only collect data on certain agents
## in this case, I only report them if they are NOT alive
## self.alive is an attribute that I track for my agents
def collect(self, model):
""" Collect all the data for the given model object. """
if self.model_reporters:
for var, reporter in self.model_reporters.items():
self.model_vars[var].append(reporter(model))
if self.agent_reporters:
for var, reporter in self.agent_reporters.items():
agent_records = []
#add an if clause to only append to agent records if our agent meets a certain condition
for agent in model.schedule.agents:
if agent.alive == False:
agent_records.append((agent.unique_id, reporter(agent)))
self.agent_vars[var].append(agent_records)
## When I define the datacollector for my model, I use MyDataCollector rather than the default DataCollector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.