NetLogo:避免在一个时间步中在一个补丁上有太多甲虫?

时间:2015-10-01 16:00:17

标签: debugging netlogo

我想对我的海龟和补丁之间的关系实施某些规则。我的补丁变量是:

  • n_min - 如果有足够的海龟,将pcolor改为粉红色,将补丁上的黄色海龟改为橙色
  • n_max - 如果有太多的海龟,将pcolor设置为棕色,让所有海龟都避免这个补丁

我的海龟状态是:黄色(移动) - >橙色(逗留) - >红色(感染) 补丁状态是: 绿色(n_min<然后一个贴片上的橙色龟数) 粉红色(橙色龟的数量是> n_min和< n_max) 棕色(没有乌龟)

我的问题如果它们全部同时移动并且因此针对相同的补丁,我怎么能避免在一个补丁上有更多n_max海龟?我怎样才能包括那个条件“如果你看到有一些颜色为橙色/红色的海龟,只需继续移动以找到另一个补丁?”另外,如果我的补丁已经是粉红色,并且n_min!= n_max要求乌龟直接将其颜色变为红色?

非常感谢你!

我不成功的例子:

globals [
  available-patch
]


patches-own [
  nmax   ; maximum nuber of turtles per patch, and change color
  nmin   ; minimum number of turtles to change patch color
  n.yellow  ; how many turtles are on the patch with yellow
  n.orange  ; how many orange (staying) turtles are on 1 patch?
]

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

to setup-patches
  ask n-of n_patches patches [
    set pcolor green
    set nmin n_min     ; how many orange turtles can be there to set the patch to pink? 
    set nmax n_max     ; max number of orange beetles to turn the patch to brown
  ]
end

to setup-turtles
  crt n_turtles [
    set color yellow
  ]
end

to go
  tick
  ask turtles with [color = yellow ] [   ; move only yellow turtles
    move-turtles
  ]
  ask patches [  ; if n.orange = nmin, turn all turtles to red and turn patch to pink
    set n.orange count turtles-here with [color = orange]
  ] 
end

to move-turtles
   ;  if color = yellow ;and n.yellow < nmin 
   ;ifelse count other turtles-on
;   move-to one-of patches with [pcolor = green and n.orange <= nmin]
;   if [n.orange <= nmin] of patch-here [
;     set color orange  ; not to move anymore. However, the n.orange should be always less then nmin !!
;   ]
   set available-patch patches with [pcolor = green and count turtles-here with [color = orange] < 2] ; agentset 
   ifelse (count other turtles-on available-patch <= nmin) ; how to change the code for nmin here?
     [ move-to one-of available-patch                         
       set color orange
       ask patch-here [
         set n.orange count turtles-here with [color = orange]
       ]
     ]
     [ fd 2 ]


end

结果:

绿色斑块上的一只乌龟已经将它的颜色变成橙色,但是我想要2只乌龟(nmin)找到绿色斑块并变成橙色。此外,黄色海龟只需按[fd 2]移动下一步,每次步骤都不会重新运行“ifelse”条件。

enter image description here

2 个答案:

答案 0 :(得分:0)

首先,一条具体的建议:

您已经给available-patch一个单调的名字,但它实际上是复数:它是所有补丁的补丁集,满足您设置的条件。

然后当你写:

count other turtles-on available-patch <= nmin

available-patch实际上是很多补丁,count turtles-on available-patch正在计算所有这些补丁上的所有乌龟计数。这没有任何意义。

也许你的意思是:

set available-patch one-of patches with ...

好的,这是您的代码中的一个错误,但我不认为它是唯一的错误。我有一个更一般的建议:你试图一次写太多代码。这是一个鞭打的食谱。您应该首先尝试使用更简单的规则编写更简单的模型,并使其正常工作。一旦它正常工作,通过添加另一个规则使其更复杂,使其工作,等等。逐步向完整模型迈进。如果您遇到困难,请在Stack Overflow上提问。你有一个很好的,具体的问题,而不是你目前的情况,你的问题似乎是&#34;这里有一堆代码,在任何地方都没有工作,帮助!&# 34。

P.S。与您的问题无关,但reset-ticks始终位于setup的末尾,从未接近开头。 tick始终位于go的末尾,从未接近开头。

答案 1 :(得分:0)

这段代码应该满足我的需求 - 每个补丁只保留一定数量的海龟,并且根据每个补丁的特定颜色(nmin,nmax)的海龟数量将补丁变为粉红色和棕色。

patches-own [
  nmax   ; maximum nuber of turtles per patch, and change color
  nmin   ; minimum number of turtles to change patch color
  n.orange  ; how many orange (staying) turtles are on 1 patch?
  n.red     ; how mony infesting turtles are on the patch??
]

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

to setup-patches
  ask n-of n_patches patches with [pcolor = black][
    set pcolor green
    set nmin n_min     ; how many orange turtles can be there to set the patch to pink? 
    set nmax n_max     ; max number of orange beetles to turn the patch to brown
  ]
end

to setup-turtles
  crt n_turtles [
    set color yellow
  ]
end

to go 
   ; add turtles and patches by time steps, to see the effect 
  if ticks mod 5 = 0 [
    ask n-of 1 patches with [pcolor = black][
       set pcolor green
       set nmin n_min     ; how many orange turtles can be there to set the patch to pink? 
       set nmax n_max     ; max number of orange beetles to turn the patch to brown
     ]
    ]

  if ticks mod 3 = 0 [
    crt 1 [
      set color yellow
    ]
  ] 

  ask turtles with [color = yellow ] [   ; move only yellow turtles
    move-turtles
  ]
  tick 
end

to move-turtles       
    ; specify the target
    let available-patches (patches with [(pcolor = pink and count turtles-here < nmax) or   ; 
                                         (pcolor = green and count turtles-here < nmin)] )
    ; select only one patch, nmin is related to "self"

   ifelse any? available-patches ; don't work if there is <= nmin, then there are nmin + 1
     [ move-to one-of available-patches
       if pcolor = green [
         set color orange
         set n.orange (n.orange + 1)
         infest.patch   ; if there is enough turtles, turn patch to pink, turn turtles to red
       ]
       if pcolor = pink [
         set color red
         set n.red (count turtles-here with [color = red])
         kill.patch
       ]
       ]
     [ fd 1 ]
end

to infest.patch   ; change color of patch only if there is n.orange = nmin of this specific patch 
  if n.orange = nmin [
     set pcolor pink  ; patch
     ask turtles-here with [color = orange] [
        set color red ]; turtles
  ]
end

to kill.patch
  if n.red = nmax [      ; turn patch brown when red turteles on this patch are = nmax
    set pcolor brown
;    ask turtles-here with [color = red] [
;      die ]
  ]
end