store/count 单个簇的大小以及如何在 NetLogo 中绘制它们
How store/count individual cluster sizes and plot them in NetLogo
我有一个生成黄色斑块簇的模型,我有兴趣查看簇大小的频率分布。为此,我在 NetLogo 的代码库中选择了 'Patch Clusters Example' 中的代码。它似乎在寻找集群方面有效(见下图)(虽然我更希望它不计算集群中的绿色补丁),但我无法弄清楚如何获得大小(或补丁计数)这些集群中的每一个。理想情况下,我想制作簇大小频率分布的直方图(不包括绿色补丁)并能够导出该数据。
此外,如果我能想出一种方法来在模型为 运行 时获取簇大小频率的直方图,那就太好了。
我用来获取集群的代码直接来自 'Patch Clusters Example' 除了我杀死所有代理以便我可以读取数字。这是...
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
感谢您的帮助!
Model BEFORE finding clusters
Model after finding clusters
基本上,对于模型中的每个聚类值,您想要计算具有相同标识符的所有补丁。这可以通过使用地图来实现。它使用一个唯一的补丁簇值列表 (remove-duplicates [cluster] of patches
),然后对于该列表中的每个条目,它计算具有该簇值的所有补丁。结果存储在一个列表中,它们代表所有集群的大小。可以使用 histogram
基元将此列表绘制为频率直方图。请务必将绘图的 x-Axis 设置为合理的最大值,并将绘图笔设置为条形模式。
NetLogo 6 语法:
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 语法:
to calc-frequency
let freq map [count patches with [cluster = ?]] remove-duplicates [cluster] of patches
set-current-plot "hist"
histogram freq
end
我有一个生成黄色斑块簇的模型,我有兴趣查看簇大小的频率分布。为此,我在 NetLogo 的代码库中选择了 'Patch Clusters Example' 中的代码。它似乎在寻找集群方面有效(见下图)(虽然我更希望它不计算集群中的绿色补丁),但我无法弄清楚如何获得大小(或补丁计数)这些集群中的每一个。理想情况下,我想制作簇大小频率分布的直方图(不包括绿色补丁)并能够导出该数据。
此外,如果我能想出一种方法来在模型为 运行 时获取簇大小频率的直方图,那就太好了。
我用来获取集群的代码直接来自 'Patch Clusters Example' 除了我杀死所有代理以便我可以读取数字。这是...
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
感谢您的帮助!
Model BEFORE finding clusters
Model after finding clusters
基本上,对于模型中的每个聚类值,您想要计算具有相同标识符的所有补丁。这可以通过使用地图来实现。它使用一个唯一的补丁簇值列表 (remove-duplicates [cluster] of patches
),然后对于该列表中的每个条目,它计算具有该簇值的所有补丁。结果存储在一个列表中,它们代表所有集群的大小。可以使用 histogram
基元将此列表绘制为频率直方图。请务必将绘图的 x-Axis 设置为合理的最大值,并将绘图笔设置为条形模式。
NetLogo 6 语法:
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 语法:
to calc-frequency
let freq map [count patches with [cluster = ?]] remove-duplicates [cluster] of patches
set-current-plot "hist"
histogram freq
end