What is the computational order of magnitude of "ask" versus "foreach"
with a list of one item? Foreach should just loop, so I assume that it
is O(n). Is "ask" a one step process or does it do some computation to
grab the object and return itself?
They are both O(n). `ask`, once it is given a turtleset, is essentially
just `foreach` upon that turtleset—aside from the fact that turtlesets
are randomly-orderered and, thus, cause you to incur a pretty slight
performance penalty in order to iterate over that collection randomly,
but with the added benefit of behaving more so within the NetLogo paradigm.
Getting a handle on the agentset that you're `ask`ing, though, is not
necessarily a constant-time operation like retrieving the list over
which to `foreach` is. If you do something like `ask turtles with [who
mod 2 = 0] [print who]`, the `turtles with [who mod 2 = 0]` is going to
be O(n) for figuring out which turtles are included in the desired
turtleset and which aren't. Then, `ask`ing the resulting turtleset will
also be O(n), but O(n) + O(n) is still O(n).
There's not really a way to get around the performance cost of building
an agentset, though. If you turn the agentset into a list (as described
below), you still need to compute the agentset, in the first place. If
you don't want to keep computing the agentset over and over, you can do
`let my-agentset turtles with [who mod 2 = 0]` just as well as you can
do `let my-agentlist [self] of turtles with [who mod 2 = 0]`. The
former is actually faster, because converting that agentset to a list
is, of course, going to take some amount of time—an amount greater than
0. If you want an unordered list of agents, I think you pretty much
always want to just keep your agentset.
Another question that has arisen is how to get an unordered list from
an agentset.
Well, pedantically speaking, there's no such thing as an unordered list,
but I believe what you're looking for is described here
<http://ccl.northwestern.edu/netlogo/5.0/docs/faq.html#agentsetlist>.
so should we use (list <agentset>) or [self] of agentset, which also
seems to work (I'm still intrigues by this, since it should not give
us back a list, but it does work with a foreach loop)? Which one is
less computationally intensive?
`list turtles` doesn't work, as it just returns a one-item list
containing the value of `turtles`. The recommended style for turning an
agentset into a list is is `[self] of turtles`, as stated in the link
above. There might be other ways to get a list from an agentset, but
they're certainly not going to be faster in any significant way. No
matter what you do within NetLogo, it's going to be O(n) (at best) to
convert that set to a list.
Regarding `[self] of turtles` returning a list (rather than an
agentset), though, I think it makes a good deal of sense when you think
about it. If you want to build a new agentset, you generally use
`with`; if you want to get values out of each agent in an agentset, you
use `of`. With `of`, there's no guarantee that whatever that reporter
block reports is going to evaluate to an agent for each of the agents in
the agentset—in fact, it very usually doesn't (e.g. `[who] of
turtles`)—so it doesn't make much set for `of` to always return an
agentset (since that's impossible), nor does it make sense for it to
sometimes return a list and sometimes return an agentset. It should
return one type of thing, ideally. Generally speaking, the only thing
that we can know it will return is a collection of /somethings/, and the
only structures that NetLogo has for collections of /somethings/ are
lists. Thus, `of` should return a list.
Jason Bertsche
Senior Software Developer - NetLogo
On 03/05/2014 03:35 PM, Era V wrote:
Hi everyone,
At my work, we've begun a debate on Netlogo and what is better to use.
I was wondering if you could settle this debate for us. What is the
computational order of magnitude of "ask" versus "foreach" with a list
of one item? Foreach should just loop, so I assume that it is O(n). Is
"ask" a one step process or does it do some computation to grab the
object and return itself?
Another question that has arisen is how to get an unordered list from
an agentset. Sort on an agentset does give us this, but it orders it;
we want to keep our computation as lightweight as possible, so how do
we go about converting an agentset to a list of agents? Value-of seems
to not exist anymore, so should we use (list <agentset>) or [self] of
agentset, which also seems to work (I'm still intrigues by this, since
it should not give us back a list, but it does work with a foreach
loop)? Which one is less computationally intensive?
What do you guys suggest?
Thanks!
EV
--
You received this message because you are subscribed to the Google
Groups "netlogo-devel" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups
"netlogo-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.