-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
datacollector: Allow collecting data from Agent (sub)classes (#2300)
Enhanced Mesa's DataCollector to allow collecting data from Agent (sub)classes, providing more flexible and granular data collection capabilities. ## Motive To enable more comprehensive data collection in multi-agent simulations, allowing researchers to track attributes and behaviors specific to different agent types, including custom Agent subclasses. ## Implementation - Modified `DataCollector` class to accept `agenttype_reporters` parameter - Added `_new_agenttype_reporter` method for handling agent-type-specific reporters - Updated `collect` method to handle agent-type-specific data collection - Added `get_agenttype_vars_dataframe` method for retrieving agent-type-specific data - Updated `Model` class to support `agenttype_reporters` in `initialize_data_collector` - Added support for collecting data from all Agent subclasses, not just predefined agent types - Updated docstrings and module-level documentation - Added comprehensive unit tests for the new functionality ## Usage Examples ```python class MyModel(Model): def __init__(self): super().__init__() self.datacollector = DataCollector( agent_reporters={"life_span": "life_span"}, # The new agenttype_reporters argument agenttype_reporters={ Wolf: {"sheep_eaten": "sheep_eaten"}, Sheep: {"wool": "wool_amount"}, Animal: {"energy": "energy"} # Collects from all animals } ) # Retrieve data for a specific agent type wolf_data = model.datacollector.get_agenttype_vars_dataframe(Wolf) # Retrieve data for all Animal subclasses, which are in this case all Wolf and Sheep animal_data = model.datacollector.get_agenttype_vars_dataframe(Animal) ``` ## Additional Notes - Backward compatible with existing DataCollector usage - Supports collecting data from custom Agent subclasses and superclasses
- Loading branch information
Showing
3 changed files
with
316 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.