NetLogo:使用局部变量“let”保存或增加计算时间?

时间:2015-09-06 22:49:27

标签: debugging time netlogo

我想节省龟运动的计算时间(问题发布在这里:NetLogo: how to make the calculation of turtle movement easier?)。在原始的移动 - 海龟程序中,作者使用了许多“让” - 局部变量。我想我可以用内置的NetLogo原语p.ex轻松替换这些“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 [ ...

请使用“let”局部变量来节省计算时间还是更耗时?我怎样才能轻松验证它?感谢您的时间 !

(PS:在对我之前的问题发表评论之后,我认为原语变量会更有效率,我更愿意更加确定)

2 个答案:

答案 0 :(得分:2)

不同之处在于每位记者的计算次数。如果你说let np patches in-radius 15那么它实际上计算了15个距离内的补丁数量,并将该值赋给名为np的变量。在计算中使用np直接替换保存的值。如果您必须在代码中使用10次,那么使用let表示计算一次并简单地读取10次。或者,如果您不将其存储在变量中,那么您需要在代码中的10个不同位置patches in-radius 15,并且每个时间,NetLogo都需要计算此值。

答案 1 :(得分: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秒

所以我想在耗时的计算中使用局部变量。

如果我错了,如果你能纠正我的结论,我将不胜感激。谢谢!!