Get mean heading of neighboring turtles

前端 未结 2 1480
太阳男子
太阳男子 2020-12-22 02:36

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 thi

相关标签:
2条回答
  • 2020-12-22 02:46

    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.

    0 讨论(0)
  • 2020-12-22 02:51

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

    0 讨论(0)
提交回复
热议问题