NetLogo:将子程序中的set [variable]应用到全局进程?

时间:2015-08-17 06:37:49

标签: netlogo

我想在单个补丁只能使用一次的情况下及时模拟海龟的繁殖过程。如果补丁是红色且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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

  1. 设置海龟
  2. 如果乌龟接触红色补丁,请将此补丁变为蓝色
  3. 同时 - 勾10 - >从每个蓝色斑点发芽10 nwe turtles
  4. 每个补丁在模拟运行期间只能使用一次(变为红色,由再现?变量保证)
  5. enter image description here

    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