在哪里放"stop"结束模拟

Where to put "stop" to end the simulation

如果没有特定的补丁(目标)可以占用,我想停止我的模拟。我做了以下代码但仍然无法正常工作。它只是停止了乌龟,而不是模拟。我将这个 "target" 变量作为全局变量并将其包含在 "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]
  ]

这是完整的代码

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 not any? patches with [value < 11 and not any? turtles-here] [stop ]
    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
  let pot-target patches with [value < 11 and not any? turtles-here]
  let 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


原语 stop 将终止出现 stop 的代码块。在您的代码中,一旦满足条件,设置移动过程将结束,但这不会结束模拟。您需要做的是测试顶层的条件(go 过程),这将终止 运行。我对你的代码有点困惑,但我认为答案是将移动部分与检查是否停止分开。

因此,在设置移动更改中:

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 代码结构。我怀疑这是一个错误。您的意思是,只要调用 set-move ,所有移民都会尝试移动。我认为您打算只有新创建的移民尝试移动,因为您是从 sprout 代码块中调用它的。如果是这样,那么你的 set-move 是一个乌龟程序,应该看起来像:

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? 作为可读性建议。就个人而言,我也会将代理集命名为目标而不是目标,以提醒自己它可能有多个成员。

更新:您需要将 stop 置于 go 过程的顶层,以便它停止该过程。它应该看起来像这样:

to go
 if not any? patches with [value < 11 and not any? turtles-here] [stop ]
 ask patches
 [ if random 100 < 1
   [ sprout-migrants 1
     [ set color green

与其使用 [stop] 结束模拟,我建议您改用 while 循环。

while [not stop-condition][
  run
]