Collect data from one type of agents

248 views
Skip to first unread message

nada gh

unread,
Mar 21, 2020, 1:58:58 PM3/21/20
to ProjectMesa
Hi,
I have two agent types, The datacollector will collect data from all agents.
Is there a way to specify an agent type for data collection?

Thanks

Jeremy Foote

unread,
Mar 22, 2020, 3:52:37 PM3/22/20
to ProjectMesa
Hi,

I've done this a couple of ways:

One is to create a list of the agents of each type on the model object, and to write datacollector functions that just access the agents in that list.

The other is to set an instance attribute like 'type' on each agent and then write the datacollector function so that it filters to just the agents with a certain type.

Best,
Jeremy

--
Project repos: github.com/projectmesa
---
You received this message because you are subscribed to the Google Groups "ProjectMesa" group.
To unsubscribe from this group and stop receiving emails from it, send an email to projectmesa...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/projectmesa/37eae279-5fa9-43b0-aa05-0fba6c822c7e%40googlegroups.com.

nada gh

unread,
Mar 23, 2020, 1:27:27 PM3/23/20
to ProjectMesa
Thanks Jeremy,
 I tried the second option as follows:

self.datacollector = DataCollector(
model_reporters={"model_var": lambda m:
m.model_var},
agent_reporters={"agent_v1": lambda a: a.agent_v1 if a.__class__.__name__ == 'Type1class' else None})

The type 2 is added to the dataframe with empty fields!

Regards

Jeremy Foote

unread,
Mar 23, 2020, 4:39:06 PM3/23/20
to ProjectMesa
Hi,

How I would do this is just have two agent classes, something like this:

class Type1(Agent):

    def __init__(self):
        self.type = "Type1"
        self.val = 1


class Type2(Agent):

    def __init__(self):
        self.type = "Type2"
        self.val = 2

Then, for the datacollector, you just do a lambda like:
{'type_1_sum': lambda a: a.val if a.type == 'Type1' else None}

Best,
Jeremy

--
Project repos: github.com/projectmesa
---
You received this message because you are subscribed to the Google Groups "ProjectMesa" group.
To unsubscribe from this group and stop receiving emails from it, send an email to projectmesa...@googlegroups.com.

nada gh

unread,
Mar 29, 2020, 8:23:59 AM3/29/20
to ProjectMesa
Thanks a lot Jeremy!
To unsubscribe from this group and stop receiving emails from it, send an email to proje...@googlegroups.com.

Yaniv Proselkov

unread,
Feb 10, 2021, 6:27:59 PM2/10/21
to ProjectMesa
Hi Jeremy,


I'm looking for a similar thing. Could I please ask to know how you do the first method you suggested? I've been trying to implement the second and it gives me a couple of issues.


Many Thanks,

Yaniv

Jeremy Foote

unread,
Feb 11, 2021, 10:05:06 AM2/11/21
to Yaniv Proselkov, ProjectMesa
Hi Yaniv,

Sure - you'd just do something like this in the Model class for each agent type:

self.agent_type_1 = []
for i in range(num_agent_type_1):
    a = AgentType1()
    self.agent_type_1.append(a)
    self.schedule.add(a)

Then, when you want to collect data, you just make a model datacollector like:

{'type_1_sum': lambda m: sum([a.val for a in m.agent_type_1])}

Jeremy

Yaniv Proselkov

unread,
Feb 11, 2021, 4:21:00 PM2/11/21
to ProjectMesa
Thank you very much Jeremy
Reply all
Reply to author
Forward
0 new messages