R NetLogo:如果连接了一定大小的补丁,请保留束

R NetLogo:如果连接了一定大小的补丁,请保留束,r,netlogo,R,Netlogo,我有一个由作物模拟生成的块状景观。我想确定束的大小(暗绿色连接的面片的数量),并且仅当它们大于20个连接的面片时,我想保留它们 这可能相当于R中的“筛子”,但我不知道如何在NetLogo中实现它 任何帮助和想法,如何实现这一点是高度赞赏 该问题基于上一个问题: 我的代码实际上不起作用: to find-clusters ; assess the pcolors by timber value ask patches with [road_by_harvest? = FALSE]

我有一个由作物模拟生成的块状景观。我想确定束的大小(暗绿色连接的面片的数量),并且仅当它们大于20个连接的面片时,我想保留它们

这可能相当于R中的“筛子”,但我不知道如何在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
补丁程序拥有[群集id]
设置
清除所有
让阈值为10
询问补丁[如果(随机3)=1[设置pcolor 53]]
询问修补程序[设置群集id-1]
标签补丁
让群集删除修补程序的重复[cluster id]
移除群集阈值
结束
;; 这将标记所有集群
标记修补程序的步骤
让num集群为0
而[any?具有[cluster id=-1和pcolor=53]的修补程序]
[
使用[cluster id=-1和pcolor=53]询问其中一个修补程序
[为群集添加标签]
设置num clusters num clusters+1
]
结束
;; 这将标记绿色补丁连接到的整个集群
标记邻居[a-cluster-id]
设置集群id a-cluster-id
使用[cluster id=-1和pcolor=53]询问邻居4[标记邻居a-cluster-id]
结束
要删除群集[群集阈值]
foreach簇
[
如果对[cluster id=?]小于阈值的修补程序计数
[
使用[cluster id=?][set pcolor white]询问修补程序
]
]
结束