Debugging NetLogo:避免一次在一个补丁上有太多的甲虫?
我想在我的海龟和补丁之间的关系中实施某些规则。我的补丁变量是:Debugging NetLogo:避免一次在一个补丁上有太多的甲虫?,debugging,netlogo,Debugging,Netlogo,我想在我的海龟和补丁之间的关系中实施某些规则。我的补丁变量是: n_min-如果有足够的海龟,将P颜色更改为粉红色,将补丁上的黄色海龟更改为橙色 n_max-如果有太多的海龟,请将pcolor设置为brown,让所有海龟避开此补丁 我的海龟状态是:黄色(移动)->橙色(停留)->红色(感染) 这些国家是: 绿色(n_min
- n_min-如果有足够的海龟,将P颜色更改为粉红色,将补丁上的黄色海龟更改为橙色
- n_max-如果有太多的海龟,请将pcolor设置为brown,让所有海龟避开此补丁
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
globals[
可用补丁
]
补丁自己[
nmax;每个补丁的最大海龟数量,以及更改颜色
nmin;改变斑块颜色的海龟的最小数量
n、 黄色;有多少只海龟在黄色的斑块上
n、 橙色;一块地上有多少只橙色海龟?
]
设置
清除所有
重置滴答声
安装补丁
安置海龟
终止
设置修补程序
询问n个补丁中的n个补丁[
将颜色设置为绿色
设置n分钟n_分钟;有多少只橙色海龟可以将补丁设置为粉红色?
设置nmax n_max;橙色甲虫的最大数量以将补丁变为棕色
]
终止
安置海龟
乌龟[
设置颜色为黄色
]
终止
外带
打上钩
用[颜色=黄色]询问海龟;只移动黄色海龟
搬海龟
]
询问补丁,如果n.orange=nmin,将所有海龟变成红色,将补丁变成粉红色
用[color=orange]
]
终止
搬海龟
; 如果颜色=黄色;n.yellow ; 首先,使用[pcolor=green和n.orange移动到其中一个补丁,具体建议如下:
您给了available patch
一个听起来单数的名称,但实际上是复数的:它是所有满足您设置的条件的补丁的补丁集
然后当你写的时候:
count other turtles-on available-patch <= nmin
好吧,这是你的代码中的一个错误,但我不认为这是唯一的错误。我有一个更一般的建议:你试图一次编写太多的代码。这是一个挥舞的诀窍。你应该先尝试编写一个更简单的模型,使用更简单的规则,然后让它工作。一旦它工作了,通过添加使它变得更复杂一点使用另一个规则,让它起作用,依此类推。一步一步地向完整模型前进。如果在任何时候遇到问题,请在堆栈溢出上问一个问题。您将有一个好的、具体的问题要问,而不是您当前的问题“这里有一堆代码根本不起作用,救命啊!”
注意:与您的问题无关,但是reset ticks
总是在setup
的末尾,从不靠近开始。tick
总是在go
的末尾,从不靠近开始。此代码应该满足我的需要-每个补丁只保留一定数量的海龟,具体取决于ce的海龟数量确定每个面片的颜色(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
自己的补丁程序[
nmax;每个补丁的最大海龟数量,以及更改颜色
nmin;改变斑块颜色的海龟的最小数量
n、 橙色;一块地上有多少只橙色海龟?
n、 红色;这块地上有多少海龟出没??
]
设置
清除所有
安装补丁
安置海龟
重置滴答声
终止
设置修补程序
使用[pcolor=黑色]询问n个贴片中的n个[
将颜色设置为绿色
设置n分钟n_分钟;有多少只橙色海龟可以将补丁设置为粉红色?
设置nmax n_max;橙色甲虫的最大数量以将补丁变为棕色
]
终止
安置海龟
乌龟[
设置颜色为黄色
]
终止
外带
;按时间步长添加海龟和补丁,以查看效果
如果刻度mod 5=0[
使用[pcolor=黑色]询问n-1个贴片[
将颜色设置为绿色
设置n分钟n_分钟;有多少只橙色海龟可以将补丁设置为粉红色?
设置nmax n_max;橙色甲虫的最大数量以将补丁变为棕色
]
]
如果刻度mod 3=0[
阴极射线管1[
设置颜色为黄色
]
]
用[颜色=黄色]询问海龟;只移动黄色海龟
搬海龟
]
打上钩
终止
搬海龟
;指定目标
让可用的补丁(带有[(pcolor=粉红色,此处计数海龟 如果还有?可用的补丁;如果有,则不起作用您是否在模型库的“代码示例”部分中查看了每个补丁示例一个海龟?如果您非常了解该代码的工作原理,则将其概括为“每个补丁n个海龟”“可能不会太难。Hi@SethTisue谢谢你的建议。我包括了我修改过的问题,因为我认为我无法正确插入每个补丁的nmin和nmax值-在这段代码中,我每个补丁只有一个turtle,但我希望有两个(nmin=2)但是它不起作用。请告诉我,我怎样才能修复这个条件?再次感谢你!谢谢!我真的很感谢你分享你的知识!我会重新编写我的代码。。
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