1个补丁中多个海龟的Netlogo植绒模型修改

Netlogo flocking model modification for multiple turtles in 1 patch

我正在尝试修改 Flocking 模型代码示例,以表示鱼在遇到彼此时形成鱼群(鱼群),然后使用其余代码的逻辑一起移动。不幸的是,坚持这个逻辑需要他们同时占据同一个补丁。 average-heading-towards-schoolmates 报告期间出现问题:

to-report average-heading-towards-schoolmates  ;; turtle procedure

  let x-component mean [sin (towards myself + 180)] of schoolmates
  let y-component mean [cos (towards myself + 180)] of schoolmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end

当最近的同学与乌龟位于同一块区域时 运行 "towards-myself" 它会出错,因为没有朝向您的确切位置的航向。我试过添加

set xcor xcor - 0.001

forward 0.001

到代码的前面,所以位置会有一些差异,但这没有帮助。我希望它做的是,如果它无法决定标题,则调用 "search" 协议。

任何解决这个难题的创意都将不胜感激!

当补丁相同时,您需要进行错误检查。对于您的模型,您需要考虑在代理位于同一补丁中的情况下要做什么。在下面的代码中,我忽略了它们。

来自 towards 上的文档:

Note: asking for the heading from an agent to itself, or an agent on the same location, will cause a runtime error.

to-report average-heading-towards-schoolmates  ;; turtle procedure

  let my-schoolmates schoolmates with [patch-here != [patch-here] of myself]
  ifelse any? my-schoolmates
  [
    let x-component mean [sin (towards myself + 180)] of my-schoolmates
    let y-component mean [cos (towards myself + 180)] of my-schoolmates
    report atan x-component y-component 
  ]
  [report heading]
end

您可能想尝试将同一块上的海龟合并到航向计算中:

to-report average-heading-towards-schoolmates  ;; turtle procedure

  let x-component mean [ifelse-value patch-here != [patch-here] of myself [0] [sin (towards myself + 180)]] of schoolmates
  let y-component mean [ifelse-value patch-here != [patch-here] of myself [0][cos (towards myself + 180)]] of schoolmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end