2016-05-16 94 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

相關問題