Example #1
0
        void WardMethod_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            clusterList.Clear();
            Normalize();
            foreach (var item in inputDataList)
            {
                Cluster c = new Cluster();
                c.Datas.Add(item);
                c.Layer = 0;

                clusterList.Add(c);
            }
            int layer = 1;
            while (clusterList.Count > 1)
            {
                List<Pair> pairList = new List<Pair>();
                int i = 1;
                foreach (var item in clusterList.Take(clusterList.Count() - 1))
                {
                    foreach (var item2 in clusterList.Skip(i))
                    {
                        var pair = new Pair()
                        {
                            ClusterA = item2,
                            ClusterB = item
                        };
                        pairList.Add(pair);
                    }
                    i++;
                }
                System.Threading.Tasks.Parallel.ForEach(pairList, (n) =>
                {
                    n.Value = n.ClusterA.GetWardValue(n.ClusterB);
                });

                var min = pairList.Min(n => n.Value);
                List<List<Cluster>> cList = new List<List<Cluster>>();
                foreach (var item in pairList.Where(n => n.Value == min))
                {
                    bool flag = true;
                    foreach (var item2 in cList)
                    {
                        if (item2.Any(n => n == item.ClusterA || n == item.ClusterB))
                        {
                            item2.Add(item.ClusterA);
                            item2.Add(item.ClusterB);
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                    {
                        cList.Add(new List<Cluster> { item.ClusterA, item.ClusterB });
                    }
                }

                foreach (var item in cList)
                {
                    var nCluster = new Cluster() { Layer = layer };
                    foreach (var item2 in item.Distinct())
                    {
                        clusterList.Remove(item2);
                        nCluster.AddChild(item2);
                    }
                    nCluster.SetDatas();
                    clusterList.Add(nCluster);

                }
                layer++;
                ReportProgress(100 - clusterList.Count * 100 / inputDataList.Count);
            }
            ReportProgress(100);
        }