我正在 NetLogo 中实现 SFM。我很难在我的世界中设置固定数量的海龟。我用 sprout 基元创建了我的海龟(每个补丁 1 个),所以我不确定如何为所有海龟设置固定数量。我认为使用滑块是一个好主意,因为我想测试不同的情况,例如 400、500、600 等海龟,以分析它们疏散需要多长时间。
当然,当我在 go 程序中实现if count Turtles > 100 [stop] create_adults时,所有模拟都会停止。我不想让模拟停止。我只想停止创造海龟,这样世界上已有的海龟就可以继续移动。
这是我的程序:
to go
if ticks = 0 [reset-timer]
if random 100 > 30
[repeat random 3 [create_adults]]
tick
if count turtles > 400 [stop] create_adults
Run Code Online (Sandbox Code Playgroud)
这就是我创建海龟的方式:
to create_adults
let p one-of patches with [(pycor >= 1 and pycor <= 11 and pxcor = 3) and pcolor = violet + 2] ; this is the beginning of my world
ask p [ if not any? turtles-here
[sprout-adults 1
[ set shape "circle"
set color magenta
set heading 90
set goal one-of patches with [(pycor >= 1 and pycor <= 11 and pxcor = max-pxcor) and pcolor = brown + 2]
set vi (list (0.66) 0)
set r 0.35
set size (0.7)
set mass 74.15
]
]
]
if count turtles > 400 [ask p [stop]] ; not working either
stop-creating-turtles
Run Code Online (Sandbox Code Playgroud)
我还尝试创建一个新变量
to stop-creating_turtles
if count turtles > 400 [stop create_adults]
end
Run Code Online (Sandbox Code Playgroud)
然后我添加了to create_adults过程,但不起作用,因为我认为我对该方法有误解。
我的模型中没有全局变量
my turtles-own [
goal
vi
r
size
mass]
Run Code Online (Sandbox Code Playgroud)
我的简化搬家程序
to move
ask turtles
[ fd module vi / fps
]
Run Code Online (Sandbox Code Playgroud)
我想我对如何解决这个问题感到困惑,因为我不知道如何在create_adults过程和海龟计数报告之间建立关系。
我预先感谢您的想法。我希望这是可以理解的,因为这是我的第一篇文章 =)!
我实施了 Matteo 的长建议,但我的海龟在达到阈值后仍然被创建。
编辑后的简化版代码如下:
globals [turtles-reached-threshold?]
breed [adults adult]
turtles-own [
goal
]
to setup
clear-all
reset-ticks
set-environment
set turtles-reached-threshold? FALSE
end
to set-environment
;for walls in lime color
ask patches with [(pxcor >= min-pxcor and pxcor <= max-pxcor and pycor = 12)] [set pcolor lime + 1 ]
ask patches with [(pxcor >= min-pxcor and pxcor <= max-pxcor and pycor = min-pycor)] [set pcolor lime + 1 ]
;to create-agents
ask patches with [pycor >= 1 and pycor <= 11 and pxcor = -16] [set pcolor violet + 2]
;to kill-agents
ask patches with [pycor > 0 and pycor <= 11 and pxcor = max-pxcor] [set pcolor brown + 2]
end
to go
if ticks = 0 [reset-timer]
if count turtles > 0 [move]
if (random 100 > 30) [
repeat random 5 [create_adults]
]
if (count turtles < 21) AND (turtles-reached-threshold?) [
create_adults
if (not turtles-reached-threshold?) AND (count turtles > 20) [
set turtles-reached-threshold? TRUE
]
]
ask turtles [if pcolor = brown + 2 [die]]
tick
end
to create_adults
let p one-of patches with [(pycor >= 1 and pycor <= 11 and pxcor = -16) and pcolor = violet + 2] ; this is the beginning of my world
ask p [ if not any? turtles-here
[sprout-adults 1
[ set shape "circle"
set color magenta
set heading 90
set goal one-of patches with [(pycor >= 1 and pycor <= 11 and pxcor = max-pxcor) and pcolor = brown + 2]
]
]
]
end
to move
ask turtles
[
fd 1
]
end
Run Code Online (Sandbox Code Playgroud)
在你的go程序中,只需使用if (count turtles < 401) [create_adults]- 这样你就可以使用你的条件来创建海龟而不是停止程序。
上面的方法是最简单的。你的方法更像是一种ifelse类型,因为你明确地表达了你创造海龟的意图(用create_turtles)和你不这样做的意图(用stop)。然而,在这种情况下,使用stop过度,因为你的意思只是“不要创建海龟/不要做任何事情”,而不是“退出此过程”。
换句话说,ifelse它必须是类似的东西
ifelse (count turtles > 400)
[]
[create_turtles]
Run Code Online (Sandbox Code Playgroud)
但是那个空命令块表明我们使用一个简单的命令if(但相反,正如我在答案开头所示)就足够了。
上述方法的结果是,每次go迭代时,如果海龟数量少于 401 只,就会创建海龟。这肯定会在模拟开始时发生,这很好。但是,如果您的模型包含正在死亡的海龟,这意味着如果海龟数量低于 401 ,则会再次创建。这是您可能想要也可能不想要的事情。如果您不希望发生这种情况,一个可能的解决方案是创建一个全局变量来控制这种情况。例如,你可以有类似的东西
globals [
turtles-reached-threshold?
]
to setup
clear-all
reset-ticks
set turtles-reached-threshold? FALSE
end
to go
if ticks = 0 [reset-timer]
if (random 100 > 30) [
repeat random 3 [create_adults]
]
if (count turtles < 401) AND (turtles-reached-threshold?) [
create_adults
if (not turtles-reached-threshold?) AND (count turtles > 400) [
set turtles-reached-threshold? TRUE
]
]
tick
end
Run Code Online (Sandbox Code Playgroud)