NetLogo: how to identify the most extended cluster (patch cluster example)?

感情迁移 提交于 2019-12-10 10:07:34

问题


I'm working with Patch Cluster Example from Model Library and I wish to identify the cluster with the highest number of patches = the most extended one and then turn this cluster red.

I know that I can simply

count patches with [pvalue = X]  ; X - whatever plabel of patches in connected cluster

to know what is the extent of specific cluster with [plabel = 1], cluster with [plabel = 2], cluster with [plabel = 3]... To color it, it is simply:

ask patches with [plabel = X] [
    set pcolor red ]

However, can I automatically identify the most extended cluster and get the count of connected patches?

Thank you for all suggestions,

* * *

Which cluster is the most extended one?


回答1:


You can easily adapt the find-all-components procedure from the Giant Component example in the NetLogo Models Library. For convenience, you will need to add global-variables component-size, giant-component-size, and giant-start-node, along with a patch attribute explored?. A quick adaptation (warning: untested!) would be something like:

    globals [component-size giant-component-size giant-start-node]
    patches-own [explored?]

    ;; find connected components and their sizes
    to find-all-components
      set giant-component-size 0
      set giant-start-node nobody
      ask patches [ set explored? false ]
      ;; keep exploring till all turtles get explored
      loop [
        ;; pick a node that has not yet been explored
        let start one-of patches with [ not explored? ]
        if start = nobody [ stop ]
        ;; reset the number of patches found to 0
        set component-size 0
        ;; at this stage, we recolor everything to light gray
        ask start [explore]
        ;; the explore procedure updates the component-size variable.
        ;; so check, have we found a new giant component?
        if component-size > giant-component-size [
          set giant-component-size component-size
          set giant-start-node start
        ]
      ]
    end

    ;; Finds all patches reachable from this node
    to explore ;; node procedure
      if explored? [ stop ]
      set explored? true
      set component-size component-size + 1
      ask neighbors4 with [pcolor = [pcolor] of myself] [
        explore
      ]
    end


来源:https://stackoverflow.com/questions/36784719/netlogo-how-to-identify-the-most-extended-cluster-patch-cluster-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!