private const bool TEST_ENV = true; //TODO: Remove this when we push to prod, its a quick debug bool public ClusteringController(IHostingEnvironment env, TimelessDenStream <Streamer> denStream) { _env = env; _denStream = denStream; _shrinkageClustering = new ShrinkageClustering <Streamer>(100, 100, Similarity.Cosine); }
public TimelessDenStream( Func <T, T, float> pointSimilarityFunction, Func <UntimedMicroCluster <T>, UntimedMicroCluster <T>, float> microClusterSimilarityFunction) { _pointSimilarityFunction = pointSimilarityFunction; _microClusterSimilarityFunction = microClusterSimilarityFunction; _microClusters = new List <UntimedMicroCluster <T> >(); _dataStream = new ConcurrentQueue <T>(); _dbscan = new TimelessDbScan <UntimedMicroCluster <T> >(EPS, MIN_POINTS, microClusterSimilarityFunction); _terminate = MaintainClusterMap(); }
public T[][] Cluster() { _clusteringInProgress = true; // Lock PCMC and OCMC collections _dbscan = new DbScan <CoreMicroCluster <T> >(50, 2, CurrentTime, _microClusterSimilarityFunction); var pcmcClusters = _dbscan.Cluster(PotentialCoreMicroClusters); var clusters = new List <T[]>(); foreach (var pcmcCluster in pcmcClusters) { var cluster = new List <T>(); foreach (var pcmc in pcmcCluster) { cluster.AddRange(pcmc.Points); } clusters.Add(cluster.ToArray()); } _clusteringInProgress = false; // Unlock PCMC and OCMC collections return(clusters.ToArray()); }
public double CalculateDistance(IClusterable element, IClusterable otherElement) { if (null == element) { throw new ArgumentNullException(nameof(element)); } if (null == otherElement) { throw new ArgumentNullException(nameof(otherElement)); } if (element.Dimension != otherElement.Dimension) { throw new ArgumentException(@"The elements must have the same dimension!"); } var squareDistance = 0.0; for (uint dimension = 0; dimension < element.Dimension; dimension++) { squareDistance += (element.GetValue(dimension) - otherElement.GetValue(dimension)) * (element.GetValue(dimension) - otherElement.GetValue(dimension)); } return Math.Sqrt(squareDistance); }
public T[][] Cluster() { if (_userTerminated) { throw new InvalidOperationException( "Cluster maintenance has been terminated by the user. " + "Cannot cluster without maintenance."); } // Lock micro cluster map during clustering, force merge the remaining points _clusteringInProgress = true; while (!_dataStream.IsEmpty) { var successfulDequeue = _dataStream.TryDequeue(out var p); if (!successfulDequeue) { continue; } // Merge the dataPoint into the micro cluster map Merge(p); } try { // Check if there is less than 10 streamers - if so, just return them if (MicroClusters.Count == 1 && MicroClusters.First().Points.Count < 10) { return(new[] { MicroClusters.First().Points.ToArray() }); } _dbscan = new TimelessDbScan <UntimedMicroCluster <T> >(50, 2, _microClusterSimilarityFunction); var pcmcClusters = _dbscan.Cluster(_microClusters); var clusters = new List <T[]>(); foreach (var pcmcCluster in pcmcClusters) { var cluster = new List <T>(); foreach (var pcmc in pcmcCluster) { cluster.AddRange(pcmc.Points); } clusters.Add(cluster.ToArray()); } _clusteringInProgress = false; // Unlock cluster map return(clusters.ToArray()); } catch (Exception e) { // Throw to return to client as a 400 throw e; } finally { _clusteringInProgress = false; } }
private static void assignCluster(IEnumerable <IClusterable> clusterableMapLayerObjects, IClusterable clusterableMapObject) { //TODO: find faster way to remove reference from other objects clusterableMapObject.IsCluster = true; //all its children are clustered foreach (var clusteredElement in clusterableMapObject.ClusteredElements) { clusteredElement.IsClustered = true; clusteredElement.ClusteredElements.Clear(); } //remove all allocated elements form remaining list foreach (var otherClusterableObject in clusterableMapLayerObjects) { if (otherClusterableObject.ClusteredElements.Count > 0 && !otherClusterableObject.IsCluster && !otherClusterableObject.IsClustered) { //remove cluster if it exist if (otherClusterableObject.ClusteredElements.Contains(clusterableMapObject)) { otherClusterableObject.ClusteredElements.Remove(clusterableMapObject); } //remove all clustered foreach (var clusteredElement in clusterableMapObject.ClusteredElements) { if (otherClusterableObject.ClusteredElements.Contains(clusteredElement)) { otherClusterableObject.ClusteredElements.Remove(clusteredElement); } } } } }
public StatefulDenStream(TimelessDenStream <Streamer> denStream) { _denStream = denStream; _shrinkageClustering = new ShrinkageClustering <Streamer>(100, 100, Similarity.Cosine); }
private static void assignCluster(IEnumerable<IClusterable> clusterableMapLayerObjects, IClusterable clusterableMapObject) { //TODO: find faster way to remove reference from other objects clusterableMapObject.IsCluster = true; //all its children are clustered foreach (var clusteredElement in clusterableMapObject.ClusteredElements) { clusteredElement.IsClustered = true; clusteredElement.ClusteredElements.Clear(); } //remove all allocated elements form remaining list foreach (var otherClusterableObject in clusterableMapLayerObjects) { if (otherClusterableObject.ClusteredElements.Count > 0 && !otherClusterableObject.IsCluster && !otherClusterableObject.IsClustered) { //remove cluster if it exist if (otherClusterableObject.ClusteredElements.Contains(clusterableMapObject)) { otherClusterableObject.ClusteredElements.Remove(clusterableMapObject); } //remove all clustered foreach (var clusteredElement in clusterableMapObject.ClusteredElements) { if (otherClusterableObject.ClusteredElements.Contains(clusteredElement)) { otherClusterableObject.ClusteredElements.Remove(clusteredElement); } } } } }
/// <summary> /// Adds an element to this cluster. /// </summary> /// <param name="element">The element which should be added.</param> internal void Add(IClusterable element) { _elements.Add(element); }
public DbscanPoint(IClusterable x) { ClusterPoint = x; IsVisited = false; ClusterId = UNCLASSIFIED; }
public double Distance(IClusterable tp) { TestItem t = (TestItem)tp; return(Math.Sqrt(((X - t.X) * (X - t.X)) + ((Y - t.Y) * (Y - t.Y)))); }
private IEnumerable <DbscanPoint> neighbor(IClusterable point) { return(_dataset.Where(x => point.Distance(x.ClusterPoint) <= Epsilon)); }
/// <summary> /// Assign one element to a cluster using the nearest center of all clusters. /// </summary> /// <param name="element">The element which should be assigned.</param> /// <param name="clusters">The clusters having a valid center.</param> private void AssignElementToCluster(IClusterable element, IEnumerable<SimpleCluster> clusters) { double minimumDistance = double.MaxValue; SimpleCluster nearestCluster = null; foreach (var cluster in clusters) { var distance = _distanceCalculator.CalculateDistance(element, cluster.Center); if (distance < minimumDistance) { minimumDistance = distance; nearestCluster = cluster; } } if (null != nearestCluster) { nearestCluster.Add(element); } else { throw new ArgumentException(nameof(clusters)); } }