我有一个由作物模拟产生的丛生景观。我想确定团块的大小(连接的深绿色斑块的数量),并且只有当它们比20个连接的斑块更大时我才想保留它们。
这可能相当于R中的“筛选”,但我无法弄清楚如何在NetLogo中制作它?
如何实现这一目标的任何帮助和想法都非常感谢!
问题是基于前一个问题: NetLogo: how to identify the most extended cluster (patch cluster example)?
我的代码实际上无法正常工作:
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
答案 0 :(得分:2)
基本上,您应该对所有组进行深度优先搜索并将聚类标记到特定组(即,没有未探测的组)。标记后,收集所有组。并删除群集,使组大小小于阈值。在下面的例子中,我将它们涂成白色......深绿色的是剩下的那个。
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