NetLogo:如果连接了一定大小的补丁,则保持团块
NetLogo: keep clumps if connected patches of certain size
我有一个由作物模拟产生的块状景观。我想确定团块的大小(连接的深绿色补丁的数量),并且我只想在它们大于 20 个连接的补丁时保留它们。
这可能相当于 R 中的 "sieve",但我不知道如何在 NetLogo 中实现它?
非常感谢您提供有关如何实现此目标的任何帮助和想法!
问题基于上一个问题:
我的代码实际上不起作用:
to find-clusters
; assess the pcolors by timber value
ask patches with [road_by_harvest? = FALSE] [
set pcolor 53 ; dark green
]
loop [
;; pick a random patch that isn't in a cluster yet
let seed one-of patches with [cluster = nobody]
if seed = nobody [
;show-clusters
set plabel-list [pcolor] of patches
stop ]
;; otherwise, make the patch the "leader" of a new cluster
;; by assigning itself to its own cluster, then call
;; grow-cluster to find the rest of the cluster
ask seed
[ set cluster self
grow-cluster ]
]
end
to grow-cluster ;; patch procedure
ask neighbors4 with [(cluster = nobody) and
(pcolor = [pcolor] of myself)]
[ set cluster [cluster] of myself
grow-cluster ]
end
本质上,您应该进行深度优先搜索并将集群标记为特定组,适用于所有组(即没有未探索的组)。给它们贴上标签后,收集所有的组。并删除集群,使组大小小于阈值。在下面的示例中,我将它们涂成白色……深绿色的是剩下的。
patches-own [cluster-id]
to setup
clear-all
let threshold 10
ask patches [if (random 3) = 1[ set pcolor 53]]
ask patches [set cluster-id -1]
label-patches
let clusters remove-duplicates [cluster-id] of patches
remove-clusters clusters threshold
end
;; this will label all clusters
to label-patches
let num-clusters 0
while [any? patches with [cluster-id = -1 and pcolor = 53]]
[
ask one-of patches with [cluster-id = -1 and pcolor = 53]
[label-neighbors num-clusters]
set num-clusters num-clusters + 1
]
end
;; this will label the whole cluster that a green patch is connected to
to label-neighbors [a-cluster-id]
set cluster-id a-cluster-id
ask neighbors4 with [cluster-id = -1 and pcolor = 53][label-neighbors a-cluster-id]
end
to remove-clusters [ clusters threshold]
foreach clusters
[
if count patches with [cluster-id = ?] < threshold
[
ask patches with [cluster-id = ?] [set pcolor white]
]
]
end
我有一个由作物模拟产生的块状景观。我想确定团块的大小(连接的深绿色补丁的数量),并且我只想在它们大于 20 个连接的补丁时保留它们。
这可能相当于 R 中的 "sieve",但我不知道如何在 NetLogo 中实现它?
非常感谢您提供有关如何实现此目标的任何帮助和想法!
问题基于上一个问题:
我的代码实际上不起作用:
to find-clusters
; assess the pcolors by timber value
ask patches with [road_by_harvest? = FALSE] [
set pcolor 53 ; dark green
]
loop [
;; pick a random patch that isn't in a cluster yet
let seed one-of patches with [cluster = nobody]
if seed = nobody [
;show-clusters
set plabel-list [pcolor] of patches
stop ]
;; otherwise, make the patch the "leader" of a new cluster
;; by assigning itself to its own cluster, then call
;; grow-cluster to find the rest of the cluster
ask seed
[ set cluster self
grow-cluster ]
]
end
to grow-cluster ;; patch procedure
ask neighbors4 with [(cluster = nobody) and
(pcolor = [pcolor] of myself)]
[ set cluster [cluster] of myself
grow-cluster ]
end
本质上,您应该进行深度优先搜索并将集群标记为特定组,适用于所有组(即没有未探索的组)。给它们贴上标签后,收集所有的组。并删除集群,使组大小小于阈值。在下面的示例中,我将它们涂成白色……深绿色的是剩下的。
patches-own [cluster-id]
to setup
clear-all
let threshold 10
ask patches [if (random 3) = 1[ set pcolor 53]]
ask patches [set cluster-id -1]
label-patches
let clusters remove-duplicates [cluster-id] of patches
remove-clusters clusters threshold
end
;; this will label all clusters
to label-patches
let num-clusters 0
while [any? patches with [cluster-id = -1 and pcolor = 53]]
[
ask one-of patches with [cluster-id = -1 and pcolor = 53]
[label-neighbors num-clusters]
set num-clusters num-clusters + 1
]
end
;; this will label the whole cluster that a green patch is connected to
to label-neighbors [a-cluster-id]
set cluster-id a-cluster-id
ask neighbors4 with [cluster-id = -1 and pcolor = 53][label-neighbors a-cluster-id]
end
to remove-clusters [ clusters threshold]
foreach clusters
[
if count patches with [cluster-id = ?] < threshold
[
ask patches with [cluster-id = ?] [set pcolor white]
]
]
end