NetLogo - 海龟去最近的海龟集中

NetLogo - turtle to go to the closest concentration of turtles

如果给定变量的阈值达到 5 个刻度,我希望乌龟去到最近的乌龟最多的斑块。

我的代码是:

to move
  let count-tick 5
  if var >= 9.5 [
    set count-tick count-tick - 1
    if count-tick = 0 [
      ask turtle [
        let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
        move-to nearest-group ;; go to the biggest crowd near you
        ask turtle [ ;; once there do the following
          set shape "star"
          set color red
        ]
      ]
    ]
  ]  
end

我遇到的问题是 a) 我不确定如何说 the patch with >= 3 turtles closest to you at the given range of 3(上面尝试的代码)和 b) 如何说 once there, change your shape.

修改为保留一个永久变量以跟踪变量是否连续 5 次足够高。

turtles-own
[ count-tick
]

; wherever you create the turtles, you need to `set count-tick 5`

to move
  ifelse var >= 9.5
  [ set count-tick count-tick - 1 ]
  [ set count-tick 5 ]
  if count-tick = 0
  [ let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
    move-to nearest-group ;; go to the biggest crowd near you
    set shape "star"
    set color red
  ]
end

首先,您已经位于调用此移动过程的过程的 ask turtles 代码块中。所以你不需要额外的 ask turtles。在 NetLogo Dictionary 中查找 ask,它遍历海龟,依次 运行 每个海龟的所有代码。

其次,您需要 count turtles 而不是 sum turtles,因为 sum 是累加值。

请注意,这里没有错误检查,如果在 3 半径范围内没有至少有 3 只海龟的补丁,您可能会遇到问题。