2016-05-16 45 views
1

我试图修改植绒模型代码示例来表示当它们遇到对方时形成学校(群),然后一起使用其余代码的逻辑移动。不幸的是,坚持这一逻辑要求他们同时占用相同的补丁。问题就出现了在平均标题入,向同学们报告:1个补丁中的多个海龟的Netlogo植绒模型修改

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 

的代码的前面,所以会有中的位置有些差距,但它并没有帮助。我希望它做的是,如果它不能决定一个标题,然后调用“搜索”协议。

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

回答

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 
+0

十分感谢马特看起来是没有的伎俩,或者至少我没有变错误了。快速评论,您的第二位代码中有一个小的语法错误: patch-here!=我自己的[patch-here] 需要在括号内,否则在代码检查过程中出现错误。这样一个小错误,虽然我不能编辑你的代码哈哈 – Jesse001