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);
        }
Example #2
0
        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();
        }
Example #3
0
        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);
        }
Example #5
0
        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);
                        }
                    }
                }
            }
        }
Example #7
0
 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);
 }
Example #10
0
 public DbscanPoint(IClusterable x)
 {
     ClusterPoint = x;
     IsVisited    = false;
     ClusterId    = UNCLASSIFIED;
 }
Example #11
0
        public double Distance(IClusterable tp)
        {
            TestItem t = (TestItem)tp;

            return(Math.Sqrt(((X - t.X) * (X - t.X)) + ((Y - t.Y) * (Y - t.Y))));
        }
Example #12
0
 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));
            }
        }