Javier Sandoval
Javier Sandoval

Reputation: 507

Netlogo: average cluster size

I have an urban growth model that outputs clusters of urban areas and want to measure them. Progress has been made to the point of plotting the frequency distribution of patch size following the code stated in this post. Now all that is needed is to plot the average cluster size but I'm stuck in the coding. I have the idea of using the mean primitive but don't know how to make the model estimate this mean cluster size using the data that the model already outputs. Here is the current code:

to find-clusters
  loop [
    ;; pick a random patch that isn't in a cluster yet
    let seed one-of patches with [cluster = nobody
    and pcolor = 8
    ]
    ;; if we can't find one, then we're done!
if seed = nobody
[
  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 ]]
  display
end

to grow-cluster  ;; patch procedure
  ask neighbors4 with [(cluster = nobody
    and pcolor = 8
    ) and
    (pcolor = [pcolor] of myself)]
  [ set cluster [cluster] of myself
    grow-cluster ]
   end

and the code for estimating frequency:

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

  set-current-plot "Frequency distribution of urban patch size"
  histogram freq
end

Any help will be appreciated. Thanks.

Upvotes: 0

Views: 141

Answers (1)

JenB
JenB

Reputation: 17678

I think the object named 'freq' in your final block of code is a list of cluster sizes. If it is, then you can simply take mean freq to get the average cluster size. However, you then get into a tangle with accessing the result. If you have a global variable to store that average (called aveCluster in my code), then you can simply include a line set aveCluster mean freq.

However, a cleaner way to do it, would be to change your code block into a reporter. Something like (note that I have assumed the code for calculating sizes is correct):

to-report cluster-frequencies
  report map [[i] -> count patches with [cluster = i]] remove-duplicates [cluster] of patches
end

Then you can do your histogram with histogram cluster-frequencies and the average with set aveCluster mean cluster-frequencies. Note that this will be less efficient because the list of sizes is calculated twice - once for the histogram and once for the average. If you have the two requirements close together then you can instead:

...
let freqs cluster-frequencies
histogram freq
set aveCluster mean freq
...

and it will only have to calculate once.

Upvotes: 2

Related Questions