为什么 NetLogo 添加 + 1 两次 - 当它不是预期的时候?

Why NetLogo add + 1 twice - when it is not expected?

我的问题应该很微妙,但与这个问题有关:。最后一个示例有效,但我在扩展模型时遇到问题。

我的海龟和小块的状态主要取决于每个小块海龟的数量和状态。阈值由每个补丁的 nmin 和 nmax 设置。

乌龟可以移动。当它具有 运行 的能量时,它会感染斑块(并将颜色变为 橙色 )。一旦在这个补丁上有足够的 "staying = orange" 海龟 = nmin,所有 orange 海龟都会变成 magenta 并开始感染。该补丁仍然可以接收更多的海龟(最大数量是 nmax),但是所有这些海龟立即变成“magenta”,而不需要变成“orange[=46” =]”。一旦达到 patch 的 nmax,patch 就会变成“brown”并且对另一只海龟没有吸引力。

我的问题是我无法以正确的方式设置ninf(有多少"infesting = magenta")海龟实际上在补丁上。我尝试了

的不同组合
set ninf (ninf + 1)
set ninf (count turtles-here with [status = "infesting"])

但似乎一旦所有海龟都从“orange”变为“magenta”,我的最终 ninf 被 1 放大了两倍而不是就一次。因此,我每个补丁 magenta(出没)海龟的真实数量是 2,但在补丁变量 ninf 中是 3。

我不能只使用 ninf (count turtles with..) 因为我的海龟可能会在整个补丁被数字 nmax 占用之前死亡。因此,我想修补 "remember" 在模拟过程中有多少 ninf 海龟 运行。

很抱歉提出可能令人困惑的问题,但我真的不明白如何将我的工作示例扩展到工作模型...

非常感谢每一个建议,谢谢!

to turtles_infest

  if ([infestlev] of patch-here = 0 and count turtles-here with [status = "staying"] < [nmin] of patch-here) or
     ([infestlev] of patch-here = 1 and count turtles-here with [status = "infesting"] < [nmax] of patch-here) [
       let available? TRUE

       ifelse available? 
         [ if [infestlev] of patch-here = 0 [
              set color orange
              set status "staying"
              set nstay count turtles-here with [status = "staying"]
              ;set secattract ((0.1 * nstay)  + ninf)    
              ask patch-here [
                infest.patch
              ]
             ] 
          if [infestlev] of patch-here = 1 [
              set color magenta
              set status "infesting"
              set staytime 0
              set time_infest ticks
              set ninf count turtles with [status = "infesting"]
              set secattract ((0.1 * nstay)  + ninf)    
              set totalattract (primattract + secattract)
              kill.patch
              ]
          ]
         [ fd 1 ]
  ]
end

; ---------------------------------------------------------------------
;                          Patch infestation (nstay = nmin)
; ---------------------------------------------------------------------


to infest.patch

  if nstay = nmin [
    set pcolor pink    
    set infestlev 1
    set tree_infestat_time ticks
    set secattract ((0.1 * nstay)  + ninf)    
    set totalattract (primattract + secattract)
    ask turtles-here with [color = orange] [
      set color magenta
      set status "infesting"
      set staytime 0
      set time_infest ticks ]
    set ninf count turtles-here with [status = "infesting"]
  ]
end

; ---------------------------------------------------------------------
;                          Patch dead (ninf = nmax)
; ---------------------------------------------------------------------


to kill.patch  ; just kill the tree, but not put  it as "reproduced? TRUE" because beetles need to reproduce, just the tree should not be attractive for the betles

  if ninf = nmax [
    set pcolor brown
    set infestlev 2
    set time_dead_by_beetle ticks
    set primattract 0
    set secattract 0
    set totalattract 0
  ]
end     

更正后的代码:

to turtles_infest

  if ([infestlev] of patch-here = 0 and count turtles-here with [status = "staying"] < [nmin] of patch-here) or
     ([infestlev] of patch-here = 1 and count turtles-here with [status = "infesting"] < [nmax] of patch-here) [
       let available? TRUE
   ifelse available? 
     [ if [infestlev] of patch-here = 0 [
          set color orange
          set status "staying"
          set nstay count turtles-here with [status = "staying"]
          set ninf 0
          show ninf
          ;set secattract ((0.1 * nstay)  + ninf)    
          ask patch-here [
            infest.patch
          ]
         ] 
      if [infestlev] of patch-here = 1 [
          set color magenta
          set status "infesting"
          set staytime 0
          set time_infest ticks
          set ninf count turtles with [status = "infesting"]  ; update ninf only once per tick
          show ninf
          set secattract ((0.1 * nstay)  + ninf)    
          set totalattract (primattract + secattract)
          kill.patch
          ]
      ]
     [ fd 1 ]
  ]
end

; ---------------------------------------------------------------------
;                          Patch infestation (nstay = nmin)
; ---------------------------------------------------------------------


to infest.patch

  if nstay = nmin [
    set pcolor pink    
    set infestlev 1
    set tree_infestat_time ticks
    set secattract ((0.1 * nstay)  + ninf)    
    set totalattract (primattract + secattract)
    ask turtles-here with [color = orange] [
      set color magenta
      set status "infesting"
      set staytime 0
      set time_infest ticks ]
  ;  set ninf count turtles-here with [status = "infesting"] - delete this one
  ;  show ninf 
  ]
end

; ---------------------------------------------------------------------
;                          Patch dead (ninf = nmax)
; ---------------------------------------------------------------------


to kill.patch  ; just kill the tree, but not put  it as "reproduced? TRUE" because beetles need to reproduce, just the tree should not be attractive for the betles

  if ninf = nmax [
    set pcolor brown
    set infestlev 2
    set time_dead_by_beetle ticks
    set primattract 0
    set secattract 0
    set totalattract 0
  ]
end