Get mean heading of neighboring turtles

一笑奈何 提交于 2019-12-29 09:28:11

问题


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?


回答1:


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!).




回答2:


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.



来源:https://stackoverflow.com/questions/24786908/get-mean-heading-of-neighboring-turtles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!