How store/count individual cluster sizes and plot

2019-05-22 11:49发布

I have a model that generates clusters of yellow patches and I am interested in looking at the frequency distribution of cluster sizes. To do this I have co-opted the code from 'Patch Clusters Example' in the Code Library of NetLogo. It seems to be working (see photos below) in terms of finding the clusters (although I would prefer that it did not count green patches in the clusters), but I can't figure out how to get the sizes (or patch counts) of each of these clusters. Ideally I would like to make a histogram of the frequency distribution of cluster sizes (excluding green patches) and be able to export that data.

Furthermore, it would be great if I could figure out a way to get the histogram of cluster size frequencies while the model is running.

The code I am using to get the clusters is right out of 'Patch Clusters Example' except I kill all the agents so I can read the numbers. Here it is...

to find-clusters
ask turtles [die] ;; this clears the board so I can see the clusters
  loop [
    ;; pick a random patch that isn't in a cluster yet
    let seed one-of patches with [(cluster = nobody)]
    ;; if we can't find one, then we're done!
    if seed = nobody
    [ show-clusters
      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 neighbors with [(cluster = nobody) and
    (pcolor = [pcolor] of myself )]
  [ set cluster [cluster] of myself
    grow-cluster ]
end

;; once all the clusters have been found, this is called
;; to put numeric labels on them so the user can see
;; that the clusters were identified correctly
to show-clusters
  let counter 0
  loop
  [ ;; pick a random patch we haven't labeled yet
    let p one-of patches with [plabel = ""]
    if p = nobody
      [ stop ]
    ;; give all patches in the chosen patch's cluster
    ;; the same label
    ask p
    [ ask patches with [cluster = [cluster] of myself]
      [ set plabel counter] ]
    set counter counter + 1 ]

end

Thanks for your help!

Model BEFORE finding clusters

Model after finding clusters

1条回答
再贱就再见
2楼-- · 2019-05-22 12:16

Basically, for each cluster value in your model, you want to count all patches with the same identifier. This can be acchieved by using map. It uses a list of unique patch cluster values (remove-duplicates [cluster] of patches) and then for each entry in that list it counts all patches with this cluster value. The results are stored in a list and they represent the sizes of all your clusters. This list can be plotted as a frequency histogram by using the histogram primitive. Be sure to set the x-Axis of your plot to a plausible maximum value and set the plot pen to bar mode.

NetLogo 6 syntax:

to calc-frequency
  let freq map [[i] -> count patches with [cluster = i]] remove-duplicates [cluster] of patches

  set-current-plot "hist"
  histogram freq
end

NetLogo 5 syntax:

to calc-frequency
  let freq map [count patches with [cluster = ?]] remove-duplicates [cluster] of patches

  set-current-plot "hist"
  histogram freq
end
查看更多
登录 后发表回答