NetLogo:将子过程中的集合 [变量] 应用于全局过程?

NetLogo: Apply set [variable] from sub-procedure to global process?

我想在单个补丁只能使用一次的情况下及时模拟海龟的繁殖过程。如果补丁是红色的并且 ticks mod 50 = 0 那么 turtles-here(在这个补丁上)hatch 新的 10 只海龟。每个补丁在整个模拟过程中只能使用一次运行。

请问,如何将此条件包含到我的代码中?我尝试简单地将补丁颜色更改为 green,希望下一个孵化过程将 运行 只有 red 个。然而,下一次 NetLogo 不会让这个补丁保持绿色,而是将其变回红色。因此我从同一个补丁中复制 运行。

任何建议将不胜感激

我的部分代码:

to go
  if ticks mod 50 = 0 [ask patches with [pcolor= red] [reproduce] ]
end

to reproduce 
  ask one-of turtles-here 
      [hatch 10 ; 
        die]
       ; set pcolor green       - change infestlev from 2 to 5 only for specific tick, not for the rest of the simulation
end

您的代码应该可以正常工作。在您的描述中,您声明颜色变回红色 - 这就是此代码不起作用的原因,在其他地方您有着色程序。或者,如果您不想依赖颜色(或者如果您希望颜色有其他含义),那么您可以向每个补丁添加一个变量以跟踪它是否已经复制。

patches-own [reproduced?]

to setup
  ...
  ask patches [set reproduced? FALSE]
  ...
end

to go
  if ticks mod 50 = 0 [ask patches with [not reproduced?] [reproduce] ]
end

to reproduce
  ask one-of turtles-here 
  [ hatch 10
    die ]
  set reproduced? TRUE
end

就像一般性评论一样,当您真正想做的是让补丁上的乌龟重现时,要求补丁重现有点奇怪。从逻辑上讲,您是在说,一旦补丁上的一只乌龟繁殖了,那么该补丁上的其他乌龟就再也无法繁殖了。如果复制确实由补丁控制,则使用 sprout 而不是 hatch 会更常见。这让你的代码看起来像这样:

to reproduce
  sprout 10 [ any commands you want the new turtles to do ]
  set reproduced? TRUE
end

我的最终工作代码和步骤(可在此处获得:http://ulozto.cz/xRqtDDfV/timing-of-turtle-sprout-nlogo):

  1. 设置海龟
  2. 如果乌龟碰到红色补丁,把这个补丁变成蓝色
  3. 同时 - tick 10 -> 从每个蓝色补丁 10 nwe 海龟发芽
  4. 每个补丁只能在模拟过程中使用一次运行(变红,转载?变量保证)

enter code here

patches-own [reproduced?]    

to setup
  clear-all
  setup-turtles
  setup-patches
  change-color
  reset-ticks
end

to setup-patches
  ask patches [set reproduced? FALSE]
  ask patches [set pcolor green]           
  ask n-of 80 patches [set pcolor red]        ; identify turles which could be a source for new turtles
end  

to setup-turtles
  crt 1
  ask turtles [set color yellow]
end


to go
  if ticks mod 10 = 0 [
                       ask patches with [(pcolor = blue) and not (reproduced?)] 
                       [reproduce]               ; set reproduction to every 10 ticks for all blue patches
                      ]
  move-turtles
  change-color
  tick
end

to move-turtles
  ask turtles [fd 1]
end

to change-color                ; if turtle touch red patch, red turns blue
  ask turtles [if pcolor = red  
    [set pcolor blue]
  ]
end

to reproduce                  ; sprout 10 new turtles from blue patches at defined time step (multiply of 10 ticks)
  sprout 10   
  set reproduced? TRUE
end