internal void AddPair(ClusterPair pair) { //mutex.WaitOne(); pairs.Add(pair); //mutex.ReleaseMutex(); }
public void Cluster2() { ClusterPairs pairs = new ClusterPairs(); for(int x=0; x< clusters.Count; ++x) //Parallel.For(0, clusters.Count, x => { for (int y = x + 1; y < clusters.Count; y++) { if (clusters[x] == clusters[y]) continue; ClusterPair pair = new ClusterPair(clusters[x], clusters[y], clusters[x].GetDistance(clusters[y], DistanceMatrix)); pairs.AddPair(pair); } }//); long timeStart = DateTime.Now.Ticks; while (clusters.Count > 1) { long time = DateTime.Now.Ticks - timeStart; timeStart = DateTime.Now.Ticks; System.Diagnostics.Debug.Print(String.Format("{0}, {1}", clusters.Count, TimeSpan.FromTicks(time).TotalMilliseconds)); // a) Merge: Create a new cluster and add the elements of the two old clusters ClusterPair lowestDistancePair = pairs.LowestDistancePair; Cluster newCluster = new Cluster(lowestDistancePair.Cluster1, lowestDistancePair.Cluster2); //newCluster.AddElements(lowestDistancePair.Cluster1.GetElements()); //newCluster.AddElements(lowestDistancePair.Cluster2.GetElements()); // b)Remove the two old clusters from clusters clusters.Remove(lowestDistancePair.Cluster1); clusters.Remove(lowestDistancePair.Cluster2); // c) Remove the two old clusters from pairs pairs.RemovePairsByOldClusters(lowestDistancePair.Cluster1, lowestDistancePair.Cluster2); // d) Calculate the distance of the new cluster to all other clusters and save each as pair for (int x = 0; x < clusters.Count; ++x ) //Parallel.For(0, clusters.Count, x => { ClusterPair pair = new ClusterPair(clusters[x], newCluster, clusters[x].GetDistance(newCluster, DistanceMatrix)); pairs.AddPair(pair); }//); // e) Add the new cluster to clusters clusters.Add(newCluster); } }