/// <summary> /// Generate random points clumped into individual, well-separated, Gaussian clusters with optional uniform noise added. /// /// </summary> /// <returns>Points that are grouped into clusters and stored in a Classification.</returns> public Classification <UnsignedPoint, string> MakeClusters() { var clusters = new Classification <UnsignedPoint, string>(); r = new FastRandom(); //var z = new ZigguratGaussianSampler(); var farthestDistanceFromClusterCenter = 0.0; var minDistance = EllipsoidalGenerator.MinimumSeparation(MaxDistanceStdDev, Dimensions); var centerGenerator = new DiffuseGenerator(Dimensions, minDistance) { // Keep the centers of the clusters away from the edge, so that points do not go out of bounds and have their coordinates truncated. Minimum = MaxDistanceStdDev, Maximum = MaxCoordinate - MaxDistanceStdDev }; var iCluster = 0; var clusterCenters = new Dictionary <string, UnsignedPoint> (); foreach (var clusterCenter in centerGenerator.Take(ClusterCount).Where(ctr => ctr != null)) { var centerPoint = new UnsignedPoint(clusterCenter); // The cluster size may be random, or come from ClusterSizes. int clusterSize; if (ClusterSizes.Length > 0) { clusterSize = ClusterSizes[iCluster % ClusterSizes.Length]; } else { clusterSize = r.Next(MinClusterSize, MaxClusterSize); } var pointGenerator = new EllipsoidalGenerator(clusterCenter, RandomDoubles(Dimensions, MinDistanceStdDev, MaxDistanceStdDev, r), Dimensions); var clusterId = iCluster.ToString(); foreach (var iPoint in Enumerable.Range(1, clusterSize)) { UnsignedPoint p; clusters.Add( p = new UnsignedPoint(pointGenerator.Generate(new int[Dimensions])), clusterId ); var distance = Math.Sqrt(centerPoint.Measure(p)); farthestDistanceFromClusterCenter = Math.Max(farthestDistanceFromClusterCenter, distance); } clusterCenters[clusterId] = centerPoint; iCluster++; } AddNoise((int)Math.Floor(clusters.NumPoints * NoisePercentage / 100), clusterCenters, clusters); Debug.WriteLine("Test data: Farthest Distance from center = {0:N2}. Minimum Distance Permitted between Clusters = {1:N2}. Max Standard Deviation = {2}", farthestDistanceFromClusterCenter, minDistance, MaxDistanceStdDev ); return(clusters); }
/// <summary> /// Create two random clusters that may be separated from one another by enough distance /// that they do not overlap, or be partly overlapping, or fully overlapping. /// /// NOTE: This type of setup is to test divisive clustering, that divides two partly mixed gaussians. /// </summary> /// <param name="overlapPercent">A number from zero to 100. /// If zero, the clusters do not overlap at all. /// If fifty, then the clusters partly overlap. /// If 100, the clusters have the same center, so are indistinguishable.</param> /// <returns>The two clusters.</returns> public Classification <UnsignedPoint, string> TwoClusters(double overlapPercent) { var clusters = new Classification <UnsignedPoint, string>(); r = new FastRandom(); var farthestDistanceFromClusterCenter = 0.0; var minDistance = EllipsoidalGenerator.MinimumSeparation(MaxDistanceStdDev, Dimensions); var centerGenerator = new DiffuseGenerator(Dimensions, minDistance) { // Keep the centers of the clusters away from the edge, so that points do not go out of bounds and have their coordinates truncated. // Keep the maximum coordinate farther away, because we will pick the second point by shifting one coordinate // in the higher direction. Minimum = MaxDistanceStdDev, Maximum = MaxCoordinate - MaxDistanceStdDev - (int)minDistance }; var iCluster = 0; var clusterCenter1 = centerGenerator.Take(1).FirstOrDefault(); var clusterCenter2 = (int[])clusterCenter1.Clone(); clusterCenter2[0] += (int)(minDistance * (100.0 - overlapPercent) / 100.0); var centers = new[] { clusterCenter1, clusterCenter2 }; foreach (var clusterCenter in centers) { var centerPoint = new UnsignedPoint(clusterCenter); var clusterSize = r.Next(MinClusterSize, MaxClusterSize); var pointGenerator = new EllipsoidalGenerator(clusterCenter, RandomDoubles(Dimensions, MinDistanceStdDev, MaxDistanceStdDev, r), Dimensions); var clusterId = iCluster.ToString(); foreach (var iPoint in Enumerable.Range(1, clusterSize)) { UnsignedPoint p; clusters.Add( p = new UnsignedPoint(pointGenerator.Generate(new int[Dimensions])), clusterId ); var distance = Math.Sqrt(centerPoint.Measure(p)); farthestDistanceFromClusterCenter = Math.Max(farthestDistanceFromClusterCenter, distance); } iCluster++; } //TODO: Go back and recluster the points. Put each point into the cluster whose centroid // it is nearest. Thus, if two clusters partly overlap, the points from one will be pushed into the other. return(clusters); }