コード例 #1
0
 /// <summary>
 /// Cluster the data set into groups of similar instances.
 /// </summary>
 /// <param name="matrix">Input matrix</param>
 /// <param name="clusteringMethod">Algorithm to use for clustering</param>
 /// <param name="distanceMethod">Distance measure used to compare instances</param>
 /// <param name="clusters">Number of desired clusters</param>
 /// <returns>Results of the cluster analysis</returns>
 public static IClusteringResults Cluster(this InsightMatrix matrix, ClusteringMethod clusteringMethod,
     DistanceMethod comparisonMethod, int clusters)
 {
     switch (clusteringMethod)
     {
         case ClusteringMethod.KMeans:
             return new KMeansClustering().Cluster(matrix, comparisonMethod, clusters);
         default:
             return new MetaKMeansClustering().Cluster(matrix, comparisonMethod, clusters);
     }
 }
コード例 #2
0
 /// <summary>
 /// Calculates the distance between two vectors.
 /// </summary>
 /// <param name="u">1st vector</param>
 /// <param name="v">2nd vector</param>
 /// <param name="distanceMethod">Algorithm to use for the distance calculation</param>
 /// <returns>Distance between the two vectors</returns>
 public static double DistanceFrom(this InsightVector u, InsightVector v, DistanceMethod distanceMethod)
 {
     switch (distanceMethod)
     {
         case DistanceMethod.EuclideanDistance:
             return new EuclideanDistance().CalculateDistance(u, v);
         case DistanceMethod.HammingDistance:
             return new HammingDistance().CalculateDistance(u, v);
         default:
             return new ManhattanDistance().CalculateDistance(u, v);
     }
 }
コード例 #3
0
        /// <summary>
        /// Gets the concrete type of Calculation strategy being used
        /// </summary>
        /// <param name="distanceMethod"></param>
        /// <returns>
        /// The type of Calculation strategy being used
        /// </returns>
        public IDistanceCalculationStrategy GetDistanceCalculationStrategy(DistanceMethod distanceMethod)
        {
            switch (distanceMethod)
            {
            case DistanceMethod.GeodesicCurve:
                return((IDistanceCalculationStrategy)_serviceProvider.GetService(typeof(GeodesicCurveDistanceCalculationStrategy)));

            case DistanceMethod.Pythagoras:
                var result = (IDistanceCalculationStrategy)_serviceProvider.GetService(typeof(PythagorasDistanceCalculationStrategy));
                return(result);

            default:
                throw new ArgumentOutOfRangeException(nameof(distanceMethod), distanceMethod, null);
            }
        }
コード例 #4
0
        public ActionResult <DistanceResponse> Get([EnumDataType(typeof(DistanceMethod))] DistanceMethod distanceMethod, [EnumDataType(typeof(MeasureUnit))] MeasureUnit measureUnit,
                                                   [FromQuery][Required][Range(-90, 90)] double point1Latitude, [FromQuery][Required][Range(-180, 180)] double point1Longitude,
                                                   [FromQuery][Required][Range(-90, 90)] double point2Latitude, [FromQuery][Required][Range(-180, 180)] double point2Longitude)
        {
            //Validates the model
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Initialises the distance point values.
            var point1 = new DistancePoint(point1Latitude, point1Longitude);
            var point2 = new DistancePoint(point2Latitude, point2Longitude);


            //Gets the type of distance calculation method service from the factory
            var distanceCalculationMethod = _distanceCalculatorFactory.GetDistanceCalculationStrategy(distanceMethod);

            //Returns the response
            return(new ActionResult <DistanceResponse>(new DistanceResponse
            {
                Distance = distanceCalculationMethod.CalculateDistance(point1, point2, measureUnit)
            }));
        }
コード例 #5
0
 /// <summary>
 /// Cluster the data set into groups of similar instances.
 /// </summary>
 /// <param name="matrix">Input matrix</param>
 /// <param name="comparisonMethod">Distance measure used to compare instances</param>
 /// <param name="clusters">Number of desired clusters</param>
 /// <param name="iterations">Number of times to run the algorithm</param>
 /// <returns>Result set that includes cluster centroids, cluster assignments, and total distortion</returns>
 public IClusteringResults Cluster(InsightMatrix matrix, DistanceMethod comparisonMethod, int clusters, int iterations)
 {
     // The iterations parameter doesn't have an effect on the base K-means algorithm
     return PerformKMeansClustering(matrix, comparisonMethod, clusters);
 }
コード例 #6
0
        /// <summary>
        /// Performs the meta K-Means clustering algorithm on the data set using the provided parameters.
        /// </summary>
        /// <param name="matrix">Input matrix</param>
        /// <param name="similarityMethod">Similarity measure used to compare instances</param>
        /// <param name="distanceMethod">Distance measure used to compare instances</param>
        /// <param name="clusters">Number of desired clusters</param>
        /// <returns>Result set that includes cluster centroids, cluster assignments, and total distortion</returns>
        private IClusteringResults PerformMetaKMeansClustering(InsightMatrix matrix, 
            DistanceMethod? distanceMethod, int? clusters, int? iterations)
        {
            if (distanceMethod == null)
            {
                // Default to sum of squared error (equivalent to Euclidean distance)
                distanceMethod = DistanceMethod.EuclideanDistance;
            }

            if (clusters == null)
            {
                // Need to add some type of intelligent way to figure out a good number
                // of clusters to use based on an analysis of the data
                clusters = 3;
            }

            if (iterations == null)
            {
                // Default to 10 iterations
                iterations = 10;
            }

            // Use the first iteration to initialize the results
            var bestResults = new KMeansClustering().Cluster(matrix, distanceMethod.Value, clusters.Value);

            for (int i = 1; i < iterations; i++)
            {
                var results = new KMeansClustering().Cluster(matrix, distanceMethod.Value, clusters.Value);

                if (results.Distortion < bestResults.Distortion)
                {
                    bestResults = results;
                }
            }

            return bestResults;
        }
コード例 #7
0
 /// <summary>
 /// Cluster the data set into groups of similar instances.
 /// </summary>
 /// <param name="matrix">Input matrix</param>
 /// <param name="comparisonMethod">Distance measure used to compare instances</param>
 /// <param name="clusters">Number of desired clusters</param>
 /// <param name="iterations">Number of times to run the algorithm</param>
 /// <returns>Result set that includes cluster centroids, cluster assignments, and total distortion</returns>
 public IClusteringResults Cluster(InsightMatrix matrix, DistanceMethod comparisonMethod, int clusters, int iterations)
 {
     return PerformMetaKMeansClustering(matrix, comparisonMethod, clusters, iterations);
 }
コード例 #8
0
 /// <summary>
 /// Cluster the data set into groups of similar instances.
 /// </summary>
 /// <param name="matrix">Input matrix</param>
 /// <param name="clusteringMethod">Algorithm to use for clustering</param>
 /// <param name="distanceMethod">Distance measure used to compare instances</param>
 /// <param name="clusters">Number of desired clusters</param>
 /// <param name="iterations">Number of times to run the algorithm</param>
 /// <returns>Results of the cluster analysis</returns>
 public static IClusteringResults Cluster(this InsightMatrix matrix, ClusteringMethod clusteringMethod,
     DistanceMethod comparisonMethod, int clusters, int iterations)
 {
     return new MetaKMeansClustering().Cluster(matrix, comparisonMethod, clusters, iterations);
 }
コード例 #9
0
ファイル: Voronoi.cs プロジェクト: socdream/CoreImaging
        public static byte[] Generate(int width, int height, int blockSize, List <Tuple <byte, byte, byte> > colors, DistanceMethod method = DistanceMethod.Euclidean, int seed = -1)
        {
            var data = new byte[width * height * 3];

            var countWidth  = width / blockSize;
            var countHeight = height / blockSize;

            // generate points
            var points = new List <Tuple <int, int> >();

            if (seed == -1)
            {
                seed = (int)DateTime.Now.Ticks;
            }
            var rng = new Random();

            for (int i = 0; i < countWidth; i++)
            {
                for (int j = 0; j < countHeight; j++)
                {
                    var x = i * blockSize + rng.Next(blockSize);
                    var y = j * blockSize + rng.Next(blockSize);

                    points.Add(new Tuple <int, int>(x, y));
                }
            }

            // paint pixels
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    var color = GetColor(i, j, points, colors, method);

                    data[(j * width + i) * 3]     = color.Item1;
                    data[(j * width + i) * 3 + 1] = color.Item2;
                    data[(j * width + i) * 3 + 2] = color.Item3;
                }
            }

            return(data);
        }
コード例 #10
0
 public VoronoiNoiseGenerator(int seed, DistanceMethod distanceMethod)
 {
     Seed            = seed;
     _distanceMethod = distanceMethod;
     _useDistance    = distanceMethod != DistanceMethod.None;
 }
コード例 #11
0
 // Start is called before the first frame update
 void Start()
 {
     distanceMethod = method;
     FillMap();
 }
コード例 #12
0
ファイル: Voronoi.cs プロジェクト: socdream/CoreImaging
        private static Tuple <byte, byte, byte> GetColor(int x, int y, List <Tuple <int, int> > points, List <Tuple <byte, byte, byte> > colors, DistanceMethod method = DistanceMethod.Euclidean)
        {
            var closestDistance = int.MaxValue;
            var selected        = -1;
            int i = 0;

            for (i = 0; i < points.Count; i++)
            {
                var x2       = (points[i].Item1 - x) * (points[i].Item1 - x);
                var y2       = (points[i].Item2 - y) * (points[i].Item2 - y);
                var distance = x2 + y2;

                if (closestDistance > distance)
                {
                    closestDistance = distance;
                    selected        = i;
                }
            }

            return(colors[selected % colors.Count]);
        }
コード例 #13
0
 public Problem023()
 {
     distanceMethod = GetHeuristicDistance;
 }
コード例 #14
0
 public AStarGraphNode(DistanceMethod func, int distanceFromRoot, Couple <int> couple) : base(func, distanceFromRoot, couple)
 {
 }
コード例 #15
0
 public AStarGraphNode(DistanceMethod func, int distanceFromRoot, int x, int y) : base(func, distanceFromRoot, x, y)
 {
 }
コード例 #16
0
 public AStarTreeNode(DistanceMethod func, int distanceFromRoot, Couple <int> coordinates)
 {
     DistanceFunction = func;
     DistanceFromRoot = distanceFromRoot;
     Coorditates      = coordinates;
 }
コード例 #17
0
 public AStarTreeNode(DistanceMethod func, int distanceFromRoot, int x, int y) : this(func, distanceFromRoot, new Couple <int>(x, y))
 {
 }
コード例 #18
0
ファイル: BinaryHashLayer.cs プロジェクト: yuduanopen/MyCaffe
        private List <PoolItemCollection> run(int nNum, BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            List <PoolItemCollection> rgrgPool = new List <PoolItemCollection>();

            // This happens on the test or run net so we need to see if the cache that is shared (or loaded) is ready to use.
            if (!m_bIsFull)
            {
                float[] rgIsFull = convertF(m_colBlobs[2].mutable_cpu_data);
                if (rgIsFull[0] == 1 && rgIsFull[1] == 1)
                {
                    m_bIsFull = true;
                    m_log.WriteLine("The Binary Hash Cache is ready to use.");
                }
            }

            if (!m_bIsFull)
            {
                return(null);
            }

            float[] rgOutput = convertF(colTop[0].mutable_cpu_data);
            bool    bUpdate  = false;

            // Normalize the input to range [0,1].
            normalize(colBottom[1], m_blobNormalized, false);
            normalize(colBottom[2], m_blobNormalized, true);

            // Find the distance between each input and each element of cache #1
            // and then from cache #2.
            for (int i = 0; i < nNum; i++)
            {
                //-----------------------------------------
                //  Build the pool using the Rough pass
                //-----------------------------------------
                PoolItemCollection rgPool1 = new alpha.PoolItemCollection();

                int[,] rgOffsets1 = new int[m_nLabelCount * m_param.binary_hash_param.cache_depth, 2];
                for (int j = 0; j < m_nLabelCount; j++)
                {
                    for (int k = 0; k < m_param.binary_hash_param.cache_depth; k++)
                    {
                        int nIdx1 = j * m_param.binary_hash_param.cache_depth + k;
                        rgOffsets1[nIdx1, 0] = i * m_nLayer2Dim;
                        rgOffsets1[nIdx1, 1] = j * m_nLayer2Dim * m_param.binary_hash_param.cache_depth + k * m_nLayer2Dim;
                    }
                }

                DistanceMethod distMethod1 = (m_param.binary_hash_param.dist_calc_pass1 == BinaryHashParameter.DISTANCE_TYPE.EUCLIDEAN) ? DistanceMethod.EUCLIDEAN : DistanceMethod.HAMMING;
                double[]       rgDist1     = m_cuda.calculate_batch_distances(distMethod1, m_dfBinaryThreshold1, m_nLayer2Dim, m_blobNormalized.gpu_data, m_colBlobs[0].gpu_data, m_blobWork.mutable_gpu_data, rgOffsets1);

                for (int j = 0; j < m_nLabelCount; j++)
                {
                    for (int k = 0; k < m_param.binary_hash_param.cache_depth; k++)
                    {
                        int nIdx1 = j * m_param.binary_hash_param.cache_depth + k;
                        rgPool1.Add(new PoolItem(j, k, rgDist1[nIdx1]));
                    }
                }

                rgPool1.Sort();


                //-----------------------------------------
                //  Fine tuned pass
                //
                //  Find the 'pool_size' number of
                //  minimum distances.
                //-----------------------------------------
                PoolItemCollection rgPool2 = new PoolItemCollection();

                int[,] rgOffsets2 = new int[m_param.binary_hash_param.pool_size, 2];
                for (int k = 0; k < m_param.binary_hash_param.pool_size; k++)
                {
                    PoolItem poolItem = rgPool1[k];
                    rgOffsets2[k, 0] = i * m_nLayer3Dim;
                    rgOffsets2[k, 1] = poolItem.Label * m_nLayer3Dim * m_param.binary_hash_param.cache_depth + poolItem.IndexIntoClass * m_nLayer3Dim;
                }

                DistanceMethod distMethod2 = (m_param.binary_hash_param.dist_calc_pass2 == BinaryHashParameter.DISTANCE_TYPE.EUCLIDEAN) ? DistanceMethod.EUCLIDEAN : DistanceMethod.HAMMING;
                double[]       rgDist2     = m_cuda.calculate_batch_distances(distMethod2, m_dfBinaryThreshold2, m_nLayer3Dim, m_blobNormalized.gpu_diff, m_colBlobs[1].gpu_data, m_blobWork.mutable_gpu_data, rgOffsets2);

                for (int k = 0; k < m_param.binary_hash_param.pool_size; k++)
                {
                    PoolItem poolItem = rgPool1[k];
                    rgPool2.Add(new PoolItem(poolItem.Label, poolItem.IndexIntoClass, rgDist2[k]));
                }

                rgPool2.Sort();


                //-----------------------------------------
                //  Select the label from the 'top_k'
                //  minimum items of the fine-tuned pass.
                //-----------------------------------------

                int nNewLabel        = rgPool2.SelectNewLabel(m_param.binary_hash_param.selection_method, (int)m_param.binary_hash_param.top_k, m_phase);
                int nIdx             = i * m_nLabelCount;
                int nPredictionLabel = getLabel(rgOutput, nIdx, m_nLabelCount);

                // If the new label is different from the previously predicted label, replace it.
                if (nNewLabel != nPredictionLabel)
                {
                    setLabel(rgOutput, nIdx, m_nLabelCount, nNewLabel);
                    bUpdate = true;
                }

                rgrgPool.Add(rgPool2);
            }

            if (bUpdate)
            {
                colTop[0].mutable_cpu_data = convert(rgOutput);
            }

            return(rgrgPool);
        }
コード例 #19
0
        /// <summary>
        /// Performs the K-Means clustering algorithm on the data set using the provided parameters.
        /// </summary>
        /// <param name="matrix">Input matrix</param>
        /// <param name="similarityMethod">Similarity measure used to compare instances</param>
        /// <param name="distanceMethod">Distance measure used to compare instances</param>
        /// <param name="clusters">Number of desired clusters</param>
        /// <returns>Result set that includes cluster centroids, cluster assignments, and total distortion</returns>
        private IClusteringResults PerformKMeansClustering(InsightMatrix matrix, 
            DistanceMethod? distanceMethod, int? clusters)
        {
            if (distanceMethod == null)
            {
                // Default to sum of squared error (equivalent to Euclidean distance)
                distanceMethod = DistanceMethod.EuclideanDistance;
            }

            if (clusters == null)
            {
                // Need to add some type of intelligent way to figure out a good number
                // of clusters to use based on an analysis of the data
                clusters = 3;
            }

            var assignments = new InsightVector(matrix.RowCount);
            var centroids = new InsightMatrix(clusters.Value, matrix.ColumnCount);
            var random = new Random();
            double distortion = -1;

            // Initialize means via random selection
            for (int i = 0; i < clusters; i++)
            {
                var samples = new List<int>();
                int sample = random.Next(0, matrix.RowCount - 1);

                // Make sure we don't use the same instance more than once
                while (samples.Exists(x => x == sample))
                {
                    sample = random.Next(0, matrix.RowCount - 1);
                }

                samples.Add(sample);
                centroids.SetRow(i, matrix.Row(sample));
            }

            // Keep going until convergence point is reached
            while (true)
            {
                // Re-initialize the distortion (total error)
                distortion = 0;

                // Assign each point to the nearest mean
                for (int i = 0; i < matrix.RowCount; i++)
                {
                    // Compute the proximity to each centroid to find the closest match
                    double closestProximity = -1;
                    for (int j = 0; j < clusters; j++)
                    {
                        double proximity = matrix.Row(i).DistanceFrom(centroids.Row(j), distanceMethod.Value);

                        if (j == 0)
                        {
                            closestProximity = proximity;
                            assignments[i] = j;
                        }
                        else if (proximity < closestProximity)
                        {
                            closestProximity = proximity;
                            assignments[i] = j;
                        }
                    }

                    // Add the proximity value to the total distortion for this solution
                    distortion += closestProximity;
                }

                // Calculate the new means for each centroid
                var newCentroids = new InsightMatrix(clusters.Value, matrix.ColumnCount);
                bool converged = true;

                for (int i = 0; i < clusters; i++)
                {
                    int instanceCount = assignments.Where(x => x == i).Count();

                    // Compute the means for each instance assigned to the current cluster
                    for (int j = 0; j < newCentroids.ColumnCount; j++)
                    {
                        double sum = 0;
                        for (int k = 0; k < matrix.RowCount; k++)
                        {
                            if (assignments[k] == i) sum += matrix[k, j];
                        }

                        if (instanceCount > 0)
                            newCentroids[i, j] = Math.Round(sum / instanceCount, 2);
                        else
                            newCentroids[i, j] = centroids[i, j];

                        if (newCentroids[i, j] != centroids[i, j])
                            converged = false;
                    }

                    centroids.SetRow(i, newCentroids.Row(i));
                }

                // If the new centroid means did not change then we've reached the final result
                if (converged) break;
            }

            return new ClusteringResults(centroids, assignments, distortion);
        }