我试图修改植绒模型代码示例,以表示形成学校(鸡群)的鱼类在遇到彼此时,然后使用其余代码的逻辑移动到一起。不幸的是,坚持这种逻辑要求他们同时占用相同的补丁。问题出现在平均向校友的报告中:
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
在代码的前面,所以位置会有一些差异,但它没有帮助。我想要它做的是,如果它不能决定一个标题然后调用"搜索"协议。
任何有关解决这个难题的创意都将受到高度赞赏!
答案 0 :(得分:2)
您需要对补丁何时相同进行错误检查。对于您的模型,您需要考虑在代理处于同一补丁时的情况。在下面的代码中,我忽略了它们。
来自towards的文档:
注意:要求从代理商到自己或代理商的标题 相同的位置,将导致运行时错误。
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