gpt4 book ai didi

plot - 如何存储/计算单个簇大小并在 NetLogo 中绘制它们

转载 作者:行者123 更新时间:2023-12-02 05:37:53 25 4
gpt4 key购买 nike

我有一个生成黄色斑 block 簇的模型,我有兴趣查看簇大小的频率分布。为此,我从 NetLogo 代码库中的“补丁集群示例”中选择了代码。它似乎在查找簇方面起作用(参见下面的照片)(尽管我希望它不计算簇中的绿色斑 block ),但我不知道如何获取大小(或斑 block 计数)这些集群中的每一个。理想情况下,我想制作簇大小的频率分布的直方图(不包括绿色斑 block )并能够导出该数据。

此外,如果我能找到一种方法在模型运行时获取簇大小频率的直方图,那就太好了。

我用来获取集群的代码正好来自“补丁集群示例”,只是我杀死了所有代理,以便我可以读取数字。这是...

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

最佳答案

基本上,对于模型中的每个聚类值,您希望对具有相同标识符的所有补丁进行计数。这可以通过使用 map 来实现。它使用唯一补丁簇值的列表(删除补丁的重复[簇]),然后对于该列表中的每个条目,它会计算具有该簇值的所有补丁。结果存储在一个列表中,它们代表所有集群的大小。可以使用 histogram 原语将该列表绘制为频率直方图。请务必将绘图的 x 轴设置为合理的最大值,并将绘图笔设置为条形模式。

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

关于plot - 如何存储/计算单个簇大小并在 NetLogo 中绘制它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com