我想对我的海龟和我的补丁之间的关系实施某些规则。我的补丁变量是:
我的海龟状态是:黄色(移动)->橙色(停留)->红色(感染) 补丁状态为: 绿色(n_min< then number of orange turtles on one patch) pink (number of orange turtles is > n_min 和 < n_max) brown (no turtles)
我的问题我如何避免在一个补丁上有超过 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”条件不会在每个时间步重新运行。
首先,一个具体的建议:
您给了
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 上提问。您将有一个很好的、具体的问题要问,而不是您当前的情况,您的问题似乎是“这里有一堆无法正常工作的代码,请帮忙!”。
附注与您的问题无关,但
reset-ticks
总是出现在 setup
的末尾,而不是靠近开头。 tick
总是出现在 go
的末尾,从不接近开头。
这段代码应该满足我的需求 - 每个补丁只保留一定数量的海龟,并根据每个补丁特定颜色(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