我试图修改Flocking模型的代码示例,以表示当鱼类相遇时形成鱼群,然后使用代码其余部分的逻辑一起移动。不幸的是,坚持这一逻辑需要他们同时占据同一块土地。问题出现在平均走向同学报告中:
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
当最近的同学和乌龟在同一块空地上"朝我自己跑"时,它会出错,因为没有朝你的确切位置跑。我试着添加
set xcor xcor - 0.001
和
forward 0.001
在代码的前面,所以在位置上会有一些差异,但这并没有帮助。我想让它做的是,如果它不能决定标题,那么就调用"搜索"协议。
任何解决这个难题的创意都将不胜感激!
当补丁相同时,您需要进行错误检查。对于您的模型,您需要考虑在代理处于同一补丁中的情况下该怎么办。在下面的代码中,我忽略了它们。
从上的文档到:
注意:向代理或上的代理请求标题相同的位置,将导致运行时错误。
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