Trouble using with-min

240 views Asked by At

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

1

There are 1 answers

0
Seth Tisue On BEST ANSWER

There are two problems here.

One is that create-links-with is wrong because one-of returns a single agent, not an agentset. You need create-link-with.

But the main problem is with this part:

 other turtles with-min [...]

NetLogo understands this as other (turtles with-min [...]). This reports an empty agentset, because the turtle itself wins the with-min competition because its distance is zero, then other eliminates that turtle, leaving the empty agentset.

Instead, you must write:

(other turtles) with-min [...]

So with both fixes together, we get:

ask turtles [
  create-link-with one-of (other turtles) with-min [distance myself]
]

If you want, this can be further shortened further by using min-one-of instead of with-min, like this:

ask turtles [
  create-link-with min-one-of other turtles [distance myself]
]

I made some turtles and tried it out in NetLogo's Command Center, and I got:

preview