mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Collecting data from different types of agents (agenttype_reporters)

Open rklymentiev opened this issue 6 months ago • 2 comments

Hi,

I am not sure if I am misunderstanding the usage of agenttype_reporters parameter in DataCollector() function, but I cannot make it work. For example, I was trying to implement it in the Money Model (https://mesa.readthedocs.io/latest/tutorials/2_collecting_data.html) by replacing

self.datacollector = mesa.DataCollector(
    model_reporters={"Gini": compute_gini}, 
    agent_reporters={"Wealth": "wealth"}
)

with

self.datacollector = mesa.DataCollector(
    model_reporters={"Gini": compute_gini}, 
    agenttype_reporters={MoneyAgent: {'Wealth': 'wealth'}}
)

In this case no errors are popping up, but at the end there are no Wealth data in the results. Running using batch_run():

params = {"width": 10, "height": 10, "n": range(5, 105, 5)}

results = mesa.batch_run(
    MoneyModel,
    parameters=params,
    iterations=5,
    max_steps=100,
    number_processes=1,
    data_collection_period=-1,
    display_progress=True,
)
results_df = pd.DataFrame(results)

Am I missing something? If I use both agent_reporters and agenttype_reporters at the same time, then only measures defined in agent_reporters are saved in the results. Mesa version 3.2.0

Thank you!

rklymentiev avatar Jun 19 '25 12:06 rklymentiev

Thanks for reporting this potential issue!

Could you try if it does collect data when not running with batch_run?

EwoutH avatar Jun 19 '25 13:06 EwoutH

Good point, I haven't checked that. The following code works fine:

model = MoneyModel(100, 10, 10)
for _ in range(100):
    model.step()

model.datacollector.get_agenttype_vars_dataframe(agent_type=MoneyAgent)

It appears that the problem lies with batch_run. Perhaps there are some alternative options to save the data from specific agent types in batch_run (to avoid saving data for all the agents you don't need)?

rklymentiev avatar Jun 19 '25 14:06 rklymentiev

I would like to work on this issue, looking forward to solve.

Srinath0916 avatar Dec 12 '25 11:12 Srinath0916