Skip to content

Instantly share code, notes, and snippets.

@wabarr
Created November 8, 2016 16:32
Show Gist options
  • Save wabarr/684d175a9654e03e16c4268a3f9a9038 to your computer and use it in GitHub Desktop.
Save wabarr/684d175a9654e03e16c4268a3f9a9038 to your computer and use it in GitHub Desktop.
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