2015-09-06 130 views
1

我想節省烏龜運動的計算時間(這裏發佈的問題:NetLogo: how to make the calculation of turtle movement easier?)。在原來的移動龜過程作者使用許多「讓」 - 局部變量。我想我可以用內置的NetLogo原語p.ex輕鬆地替換這些「let」變量。這裏:NetLogo:使用局部變量「let」保存或增加計算時間?

; original code with "let" local variables 

    let np patches in-radius 15     ; define your perceptual range       
    let bnp max-one-of np [totalattract]   ; max of [totalattract] of patches in your neighborhood          
    let ah [totalattract] of patch-here   ; [totalattract] of my patch 
    let xcorhere [pxcor] of patch-here 
    let ycorhere [pycor] of patch-here            
    let abnp [totalattract] of bnp             
ifelse abnp - ah > 2 [ ... 

可以用這個條件代替嗎?

; make the same condition with NetLogo primitives 

ifelse ([totalattract] of max-one-of patches in-radius 15 [totalattract] - [totalattract] of patch-here > 2 [ ... 

請問,利用「讓」局部變量節省計算時間還是會花費更多時間?我怎樣才能輕鬆驗證它?感謝您的時間 !

(PS:以下評論我剛纔的問題我想是原語的變量將更有效率,我只是喜歡更肯定)

回答

2

區別在於每個記者的計算次數。如果您說let np patches in-radius 15那麼它實際上計算15個距離內的補丁數並將該值給予名爲np的變量。在計算中使用np直接替換保存的值。如果在代碼中必須使用它10次,那麼使用let意味着它被計算一次並簡單地讀取10次。另外,如果你不把它存儲在一個變量中,那麼你需要在代碼中的10個不同的地方使用patches in-radius 15,並且每次使用NetLogo都需要計算這個值。

+0

謝謝@JenB現在更清楚了! – maycca

0

顯然是看起來像局部變量[]內工作快則元的NetLogo變量。

比較1)僅NL元

let flightdistnow sqrt (
;  (([pxcor] of max-one-of patches in-radius 15 [totalattract] - [pxcor] of patch-here)^2) + 
;  ([pycor] of max-one-of patches in-radius 15 [totalattract] - [pycor] of patch-here)^2 
;  ) 

VS 2)使用局部變量,然後計算龜運動

to move-turtles 
    let np patches in-radius 15     ; define your perceptual range       
    let bnp max-one-of np [totalattract]   ; max of [totalattract] of patches in your neighborhood          
    let ah [totalattract] of patch-here   ; [totalattract] of my patch 
    let xcorhere [pxcor] of patch-here 
    let ycorhere [pycor] of patch-here            
    let abnp [totalattract] of bnp             
    ifelse abnp - ah > 2 [    
     move-to bnp      ; move if attractiveness of patches-here is lower then patches in-radius 
     let xbnp [pxcor] of bnp         
     let ybnp [pycor] of bnp 
     let flightdistnow sqrt ((xbnp - xcorhere) * (xbnp - xcorhere) + (ybnp - ycorhere) * (ybnp - ycorhere)) 
     set t_dispers (t_dispers + flightdistnow)     
     set energy (energy - (flightdistnow/efficiency))   
     set flightdist (flightdist + flightdistnow) 
;    if ([pxcor] of patch-here = max-pxcor) or ([pycor] of patch-here = max-pycor) or ([pxcor] of patch-here = min-pxcor) or ([pycor] of patch-here = min-pycor) 
;     [set status "lost"          
;     set beetle_lost (beetle_lost + 1)] 
       ] ; if attractivity of [totalattract] is higher the the one of my patch 

,並使用秒錶爲5000個海龜我的結果運動是:

- 1)10秒 - 2)5秒

所以我想在耗時的計算中使用局部變量。

如果我錯了,你會糾正我的結論,我將不勝感激。謝謝 !!

+0

看到Jen的回答。如果使用'let'意味着您避免重複相同的計算,那麼您可以節省時間。否則,你不會。 –