Identify nearby turtles using radius or distance

I would like to define neighbors using radius or distance. My solution so far:

turtles-own [
  my-neighbors
  num-neighbors]

to setup

ca
crt 100 [
  move-to one-of patches with [ not any? turtles-here ]
  set my-neighbors (other turtles) in-radius 3
  set num-neighbors count my-neighbors
]

end

      

The problem is that most turtles have 0 to 4 neighbors, but some have relatively large numbers of neighbors (34 and 65 for example). These turtles are located near the center of the world.

Any ideas on what I am doing wrong?

+3


source to share


1 answer


This is due to the timing of the side effects in your program.

Suppose the very first turtle moves near the center. None of the other turtles have moved yet, so they are still on patch 0 0

and set my-neighbors (other turtles) in-radius 3

will capture them all. And even after they move to another location, they will still be included in the first set of turtle agents my-neighbors

.



You can avoid the problem by first moving all the turtles and then calculating your neighbors:

to setup
  clear-all
  crt 100 [
    move-to one-of patches with [ not any? turtles-here ]
  ]
  ask turtles [
    set my-neighbors (other turtles) in-radius 3
    set num-neighbors count my-neighbors
  ]
end

      

+3


source







All Articles