如果没有特定的补丁(目标),我想停止仿真。我做了以下代码,但仍然无法正常工作。它只是停止了乌龟,而不是模拟。我将此“目标”变量作为全局变量进行了处理,并将其包含在“ go”中,但也无法停止模拟。
to set-move
ask migrants
[set pot-target patches with [value < 11 and not any? turtles-here]
set target pot-target with [count neighbors with [any? turtles-here with [value < 11]] >= 1]
ifelse (count target != 0 and (status != "resident")) [move-to min-one-of target [value]
set status "resident"
set color blue]
[stop]
]
这里是完整代码
globals [target pot-target]
breed [migrants migrant]
breed [residents resident]
patches-own [value]
turtles-own [income
status]
to setup
ca
let total problo + probmid + probhi
if (total != 100)
[print (word "prob is more than 100")]
ask patches [set value random-normal 10 3
let patch-value value
set pcolor scale-color (gray - 5) patch-value 10 3]
ask patches
[if random 100 < 3
[sprout-residents 1
[set color red
set shape "default"
set size 1
set status "resident"
]
]
]
end
to go
ask patches
[if random 100 < 1
[sprout-migrants 1
[set color green
set shape "default"
set size 1
set status "migrant"
set-move
]]]
end
to set-move
ask migrants
[set pot-target patches with [value < 11 and not any? turtles-here]
set target pot-target with [count neighbors with [any? turtles-here with [value < 11]] >= 1]
ifelse (count target != 0 and (status != "resident")) [move-to min-one-of target [value]
set status "resident"
set color blue]
[stop]
]
end
原语stop
将终止在其中出现stop
的代码块。在您的代码中,一旦满足条件,设置移动过程将结束,但并不会结束模拟。您需要做的是在顶层测试条件(执行过程),这将终止运行。我对您的代码有些困惑,但是我认为答案是将运动部件与检查是否停止分开。
因此,在设定移动更改中:
ifelse (count target != 0 and (status != "resident"))
[ move-to min-one-of target [value]
set status "resident"
set color blue
]
[ stop ]
仅是if
块而不是ifelse
(也删除了stop
)。然后,在顶层添加类似以下内容的行:
if not any? patches with [value < 11 and not any? turtles-here] [stop]
我也担心您以ask migrants
开头的set-move的代码结构。我怀疑这是一个错误。您的意思是,只要有人打电话给定居人士,所有移民都将努力前进。我认为您的意图是,因为您是从sprout
代码块中调用的,因此只有新创建的迁移者会尝试移动。如果是这样,那么您的设定动作就是乌龟程序,应该看起来像:
to set-move
set pot-target patches with [value < 11 and not any? turtles-here]
set target pot-target with [count neighbors with [any? turtles-here with [value < 11]] >= 1]
if any? target and (status != "resident")
[ move-to min-one-of target [value]
set status "resident"
set color blue
]
end
请注意,作为可读性建议,我也将您的count != 0
更改为not any?
。就个人而言,我也将命名为agentset目标,而不是为了提醒自己它可能有多个成员。