Lists of agents and agentsets are two different data types in NetLogo (and can be converted with turtle-set
and sort
). The documentation states that you use lists for an ordered collection of agents and sets for an unordered collection. It appears that anonymous procedures available to lists makes lists more flexible than agentsets. On the other hand, using 'ask' in combination with agentsets is more common in the example models and has been stated to be more readable.
Can one translate any combination of list and anonymous procedure to a statement using an agentset and 'ask'? In particular, can you provide an equivelant of filter
or map
using ask
and agentsets? Please advise some situations in which one or the other is more appropriate, and the benefit that the structure provides.
you use lists for an ordered collection of agents and sets for an unordered collection. I feel there is more to it though.
Ordered vs. unordered is an important difference, but there is another crucial one: lists can contain duplicate items, agentsets cannot.
I do not know how to properly implement an equivalent to
filter
ormap
usingask
.
You don't need to implement those: they already exist!
The agentset version of filter
is with
:
my-agent-set with [ color = red ]
is the same as:
filter [ a -> [ color = red ] of a ] my-agent-list
The agentset version of map
is of
:
[ color ] of my-agent-set
is the same as:
map [ a -> [ color ] of a ] my-agent-list
And, as you may have intuited by now, ask
is the equivalent of foreach
.
Grasping these similarities is an important step towards NetLogo enlightenment. (Or even general programming enlightenment.)
The idea of unifying (or somehow generalizing) the syntax and primitives for these two types of collection has been discussed before (e.g., here and here), but that kind of big language change tends not to happen very often.
using agentsets (and 'ask') is more in line with the idea of Netlogo. It might be faster or easier to read.
As the code examples above nicely illustrate, agentsets do have a nicer syntax. And that's a good thing, since unordered collections of unique agents are usually what you need when doing agent-based modeling.
They're not inherently faster, though. Adding to them, in particular, is slower than adding to a list. To use a contrived example, it is much faster to do:
let my-list []
repeat 50 [ set my-list lput one-of turtles my-list ]
let my-agentset turtle-set my-list
Than to do:
let my-agentset no-turtles
repeat 50 [ set my-agentset (turtle-set my-agenset one-of turtles) ]
What is your experience on this matter?
Agentsets are what's needed most of the time, but lists are also needed once in while.
Jen's example of using them for implementing a memory is a good one, but there are other use cases. Hopefully, you'll know them when you see them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With