Netlogo:平均簇大小

时间:2018-06-13 17:50:01

标签: netlogo

我有一个城市增长模型,输出城市区域的集群,并希望衡量它们。已经取得了进展,以便按照此post中所述的代码绘制补丁大小的频率分布。 现在所需要的只是绘制平均簇大小,但我仍然陷入编码状态。我有使用mean原语的想法,但不知道如何使用模型已经输出的数据使模型估计这个平均簇大小。这是当前的代码:

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

和估算频率的代码:

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

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

我认为名为' freq'在最后一段代码中是一个簇大小列表。如果是,那么您只需使用mean freq即可获得平均群集大小。但是,您会因访问结果而陷入困境。如果你有一个全局变量来存储该平均值(在我的代码中称为aveCluster),那么你可以只包含一行set aveCluster mean freq

但是,更简洁的方法是将代码块更改为记者。类似的东西(注意我已经假定计算大小的代码是正确的):

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

然后,您可以使用histogram cluster-frequencies进行直方图,使用set aveCluster mean cluster-frequencies进行平均直方图。请注意,这样效率会降低,因为大小列表会计算两次 - 一次用于直方图,一次用于平均值。如果您将这两个要求放在一起,那么您可以改为:

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

并且只需计算一次。