public List <XY> RunClusterAlgo(int grxid, int gryid) { GlobalClass.program += "43. GridBaseCluster.RunClusterAlgo .. "; // var clusterPoints = new List<XY>(); // params invalid if (BaseDataset == null || BaseDataset.Count == 0 || grxid <= 0 || gryid <= 0) { return(clusterPoints); } // put points in buckets foreach (var p in BaseDataset) { // find relative val var relativeX = p.X - MinX; var relativeY = p.Y - MinY; int xid = (int)(relativeX / DeltaX); int yid = (int)(relativeY / DeltaY); // bucket id string id = GetId(xid, yid); // bucket exists, add point if (BaseBucketsLookup.ContainsKey(id)) { BaseBucketsLookup[id].Points.Add(p); } // new bucket, create and add point else { Bucket bucket = new Bucket(xid, yid); bucket.Points.Add(p); BaseBucketsLookup.Add(id, bucket); } } // calc centrod for all buckets BaseSetCentroidForAllBuckets(BaseBucketsLookup.Values); // merge if gridpoint is to close MergeClustersGrid(); return(BaseGetClusterResult()); }
void MergeClustersGridHelper(string currentKey, string[] neighborKeys) { GlobalClass.program += "41. GridBaseCluster.MergeClustersGridHelper .. "; double minDistX = DeltaX / 2.0;//heuristic, higher value gives less merging double minDistY = DeltaY / 2.0; //if clusters in grid are too close to each other, merge them double minDist = MathTool.Max(minDistX, minDistY); foreach (var neighborKey in neighborKeys) { if (!BaseBucketsLookup.ContainsKey(neighborKey)) { continue; } var neighbor = BaseBucketsLookup[neighborKey]; if (neighbor.IsUsed == false) { continue; } var current = BaseBucketsLookup[currentKey]; var dist = MathTool.Distance(current.Centroid, neighbor.Centroid); if (dist > minDist) { continue; } var centroidgroup = current.Centroid.CGroup; foreach (var p in neighbor.Points) { //update CGroup p.CGroup = centroidgroup; }//end neighP = neighbor.Points.ToArray(); current.Points.AddRange(neighbor.Points);//O(n) // recalc centroid var cp = BaseGetCentroidFromCluster(current.Points); current.Centroid = cp; neighbor.IsUsed = false; //merged, then not used anymore } }
void MergeClustersGridHelper(string currentKey, string[] neighborKeys) { double minDistX = DeltaX / 2.0; //heuristic, higher value gives less merging double minDistY = DeltaY / 2.0; //if clusters in grid are too close to each other, merge them double minDist = MathTool.Max(minDistX, minDistY); foreach (var neighborKey in neighborKeys) { if (!BaseBucketsLookup.ContainsKey(neighborKey)) { continue; } var neighbor = BaseBucketsLookup[neighborKey]; if (neighbor.IsUsed == false) { continue; } var current = BaseBucketsLookup[currentKey]; var dist = MathTool.Distance(current.Centroid, neighbor.Centroid); if (dist > minDist) { continue; } // merge var color = current.Centroid.Color; foreach (var p in neighbor.Points) { //update color p.Color = color; } current.Points.AddRange(neighbor.Points); //O(n) // recalc centroid var cp = BaseGetCentroidFromCluster(current.Points); current.Centroid = cp; neighbor.IsUsed = false; //merged, then not used anymore neighbor.Points.Clear(); //clear mem } }
public List <XY> RunClusterAlgo(int gridx, int gridy) { // params invalid if (gridx <= 0 || gridy <= 0) { throw new ApplicationException("GridCluster.RunClusterAlgo gridx or gridy is <= 0"); } // put points in buckets foreach (var p in BaseDataset) { // find relative val var relativeX = p.X - MinX; var relativeY = p.Y - MinY; int idx = (int)(relativeX / DeltaX); int idy = (int)(relativeY / DeltaY); // bucket id string id = GetId(idx, idy); // bucket exists, add point if (BaseBucketsLookup.ContainsKey(id)) { BaseBucketsLookup[id].Points.Add(p); } // new bucket, create and add point else { Bucket bucket = new Bucket(idx, idy); bucket.Points.Add(p); BaseBucketsLookup.Add(id, bucket); } } // calc centrod for all buckets BaseSetCentroidForAllBuckets(BaseBucketsLookup.Values); // merge if gridpoint is to close if (DoMergeGridIfCentroidsAreCloseToEachOther) { MergeClustersGrid(); } if (DoUpdateAllCentroidsToNearestContainingPoint) { BaseUpdateAllCentroidsToNearestContainingPoint(); } // check again // merge if gridpoint is to close if (DoMergeGridIfCentroidsAreCloseToEachOther && DoUpdateAllCentroidsToNearestContainingPoint) { MergeClustersGrid(); // and again set centroid to closest point in bucket BaseUpdateAllCentroidsToNearestContainingPoint(); } return(BaseGetClusterResult()); }