Raíssa Fernandes
Raíssa Fernandes

Reputation: 21

How to extract the who number of agents in an agentset? (NetLogo)

I have a patch variable set as visitantBees []. The intention is that each patch has a record of which agents were in it. The method for registering the "visiting agents" is as follows:

to computing Frequency
  ask patches
  [
    if any? turtles-here
    [
      set visitantBees lput [who] of turtles-here visitantBees
    ]
  ]
end

However, this way a list of list is returned ([[3 1 0 2 4]], for example).

Would anyone know how I can add only the who number to the visitantBees list? Maybe a way to extract all the items from the turtles-here.

Upvotes: 1

Views: 516

Answers (1)

Matteo
Matteo

Reputation: 2926

The reason why you get a list of lists is that both visitantBees and [who] of turtles-here are lists.

While visitantBees is a list because you set it as a list, why is [who] of turtles-here a list? Because turtles-here is an agentset - and the only way to report a variable of an agentset is to create a list. For example, if you wanted to know [color] of turtles, the only way NetLogo has to give you this information is to put all the turtles' colors in a list.

So, why is turtles-here an agentset? Because, even if sometimes turtles-here can contain 0 or 1 turtle, it can also contain multiple turtles. And anything that is fit to contain multiple agents has to be an agentset.

On the other hand, when you ask a single agent to report one of its variables, you get the value as such, i.e. not a list (unless that value is a list in itself, of course). For example, [color] of turtle 0 is just its color, not a list containing a color.

Therefore, you can achieve your goal by individually asking every turtle on the patch to append their who to visitantBees:

to computingFrequency
  ask patches [
    ask turtles-here [
      set visitantBees lput who visitantBees
    ]
  ]
end

Or, given that turtles can automatically read and change the patches-own variables of the patch they are standing on, you can make it even simpler:

to computingFrequency
  ask turtles [
    set visitantBees lput who visitantBees
  ]
end

Which is also faster because it will only engage with turtles (who, by definition, are standing on a patch) rather than engaging with every patch even if there are no turtles on it.

Upvotes: 3

Related Questions