I was trying to program my turtles to move with a heading that's the mean heading of its neighbors (turtles within a specific radius). Should I use in-radius to achieve this?
2 Answers
Given Nicolas' response to Arthur's answer, here's the code to get what wikipedia considers to be the mean of angles:
to-report mean-heading [ headings ]
let mean-x mean map sin headings
let mean-y mean map cos headings
report atan mean-x mean-y
end
Note that because up is 0 is NetLogo angles, sin heading
is the x
instead of y
. Next, we can use that to set the heading
of our turtles:
ask turtles [
set heading mean-heading [ heading ] of turtles in-radius 3
]
where you'd replace 3 with the radius of your choice of course. You didn't say if you wanted a turtle to take into account its own heading when computing the mean or not. Here, they do take their own heading into account, which means that we don't have to do an any?
check (since turtles in-radius r
will always include the turtle itself!).

- 12,360
- 5
- 32
- 50
Using in-radius would work. So something like
ask turtles [
let neighbor-turtles other turtles in-radius 3
if any? neighbor-turtles [
set heading mean [heading] of neighbor-turtles]
]
would work.
Depending on how many turtles you have, you might find that it runs a bit slow. In-radius finds all patches that are within the radius, and then calculates the distance to each of the turtles on that patch. This means that sometimes it will have to do calculations on turtles that are outside the radius.
If that becomes a problem, another and slightly faster way is to find turtles on neighboring patches.
ask turtles[
;; create a turtle set of all turtles on same and neighboring patches as turtle
let neighbor-turtles (turtle-set other turtles-here [turtles-here] of neighbors)
if any? neighbor-turtles[
set heading mean [heading] of neighbor-turtles
]
]
This offers a bit less flexibility though, since you can only find turtles on the neighboring patches. But if that is a high enough granularity for you, then that's at least an option.

- 889
- 4
- 10
-
`in-radius` actually works by iterating through the patches in the given radius and getting the turtles on those (sort of...), so shouldn't see much difference in performance between the two (for radii < 1.5 of course). Also, the first example unfortunately needs to do an `any?` check as well. Chaining neighbor checks may not give you a performance boost either as the chain will check some turtles over and over again (it's also hard to control). – Bryan Head Jul 17 '14 at 15:36
-
Note that you can use `turtles-on neighbors` instead of `[turtles-here] of neighbors`, which will give you a single agentset rather than a list of agentsets. It doesn't matter here because you're passing the result to `turtle-set` anyway, but it's often much nicer. – Nicolas Payette Jul 17 '14 at 16:49
-
Using `mean [ heading ] of turtles` may also be problematic: `ca crt 1 [ set heading -5 ] crt 1 [ set heading 5 ] show mean [heading] of turtles` will give you 180, which is probably not what you want in this case. – Nicolas Payette Jul 17 '14 at 16:52
-
Using something like `rt mean [ subtract-headings heading [ heading ] of myself ] of other turtles in-radius 3` at least avoids the arbitrary cutoff at angle 0, but won't turn you around if you are facing the opposite direction. The right solution probably involves finding the heading that minimizes the differences with all other headings. Not sure how to do that efficiently, though... – Nicolas Payette Jul 17 '14 at 17:13
-
Great point @NicolasPayette. Wikipedia to the rescue: http://en.wikipedia.org/wiki/Mean_of_circular_quantities#Mean_of_angles – Bryan Head Jul 18 '14 at 03:01
-
That is totally true - see @BryanHead 's code below. – Arthur Hjorth Jul 18 '14 at 16:03