I would like to define neighbors using in-radius or distance. My solution so far is:
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 with this is that most of the turtles have between 0 to 4 neighbors, but a few of them have a relatively huge number of neighbors (e.g., 34 and 65). Those turtles are located close to the center of the world.
Any ideas about what I am doing wrong?
It has to do with the timing of side effects in your program.
Suppose that the very first turtle to move moves near the center. None of the other turtles have moved yet, so they're all still on patch 0 0
and set my-neighbors (other turtles) in-radius 3
will capture them all. And even after they move elsewhere, they will still be included in the first turtle's my-neighbors
agentset.
You can avoid the problem by first moving all the turtles and then calculate their 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