A simple question, but one I'm totally failing on.
I have a group of turtles that need to locate the nearest neighbour, as I wish to create a link between them. I've tried the following code, but I keep coming back with a null set [nobody found]:
ask turtles [create-links-with one-of other turtles with-min [distance myself]]
Can someone please point me in the right direction.
Regards
Simon
There are two problems here.
One is that
create-links-with
is wrong becauseone-of
returns a single agent, not an agentset. You needcreate-link-with
.But the main problem is with this part:
NetLogo understands this as
other (turtles with-min [...])
. This reports an empty agentset, because the turtle itself wins thewith-min
competition because its distance is zero, thenother
eliminates that turtle, leaving the empty agentset.Instead, you must write:
So with both fixes together, we get:
If you want, this can be further shortened further by using
min-one-of
instead ofwith-min
, like this:I made some turtles and tried it out in NetLogo's Command Center, and I got: