} // of AnnexCluster() public static UnitCluster MergeClusters(UnitCluster p_C1, UnitCluster p_C2) { if (p_C2.ListPoints.Count > 0) { p_C1.AnnexCluster(p_C2); } return(p_C1); } // of MergeClusters()
public static List <UnitCluster> RunKmeans <T>(List <T> objList, double distance) where T : CacheObject { List <UnitCluster> LC_ = new List <UnitCluster>(); if (objList.Count == 0) { return(LC_); } List <CacheObject> l_ListUnits = new List <CacheObject>(objList.ToArray()); if (l_ListUnits.Count == 0) { return(LC_); } // for starters, take a point to create one cluster CacheUnit l_P1 = (CacheUnit)l_ListUnits[0]; l_ListUnits.RemoveAt(0); // so far, we have a one-point cluster LC_.Add(new UnitCluster(distance, l_P1)); #region Main Loop // the algorithm is inside this loop List <UnitCluster> l_ListAttainableClusters; UnitCluster l_c; foreach (CacheUnit p in l_ListUnits) { l_ListAttainableClusters = new List <UnitCluster>(); l_ListAttainableClusters = LC_.FindAll(x => x.IsPointReachable(p.PointPosition)); LC_.RemoveAll(x => x.IsPointReachable(p.PointPosition)); l_c = new UnitCluster(distance, p); // merge point's "reachable" clusters if (l_ListAttainableClusters.Count > 0) { l_c.AnnexCluster(l_ListAttainableClusters.Aggregate((c, x) => c = MergeClusters(x, c))); } LC_.Add(l_c); //Logger.DBLog.InfoFormat("Cluster Found: Total Points {0} with Centeroid {1}", l_c.ListPoints.Count, l_c.Centeroid.ToString()); l_ListAttainableClusters = null; l_c = null; } // of loop over candidate points //LC_=LC_.OrderByDescending(o => o.ListPoints.Count).ToList(); #endregion return(LC_); }