// Read about this at github.com/kunukn/single-detect
        static void SetKnnAlgo(IPoints points)
        {
            IPointsKnn dataset = new PointsKnn();

            dataset.Data.AddRange(points.Data.Select(i => i as IPKnn));
            var rect = new SingleDetectLibrary.Code.Data.Rectangle
            {
                XMin        = -190,
                XMax        = 190,
                YMin        = -100,
                YMax        = 100,
                MaxDistance = 20,
            };

            rect.Validate();

            // Naive stratey works with all points on Earth.
            // Grid strategy runs much faster and can be used as approx algo
            // but only works on certain local areas, not wrapped world. e.g. from lon -90 to lon 90,
            // e.g. Europe only areas or US only areas etc. but not New Zealand due to being near lon 180.
            // All points must be within rect boundary
            IKnnAlgorithm algo = new KnnAlgorithm(dataset, rect, StrategyType.Naive);

            Data = algo;
        }
예제 #2
0
        static void Run()
        {
            // Config
            var rect = new Rectangle
            {
                XMin = -200,
                XMax = 200,
                YMin = -100,
                YMax = 100,
                MaxDistance = 20,
            };
            rect.Validate();

            var conf = new KnnConfiguration { K = 100 };

            // Random points
            IPoints points = new Points();
            var rand = new Random();
            for (var i = 0; i < 500000; i++)
            {
                var x = rect.XMin + rand.NextDouble() * rect.Width;
                var y = rect.YMin + rand.NextDouble() * rect.Height;
                points.Data.Add(new P
                {
                    X = x,
                    Y = y,
                });
            }
            points.Round(3);

            // Init algo
            IAlgorithm algo =
                new Algorithm(points, rect, StrategyType.Grid);

            // Use algo

            var origin = new P { X = 0, Y = 0 };
            var duration = algo.UpdateKnn(origin, conf);

            // Print result
            WL(string.Format("{0} msec. {1}:", algo.Strategy.Name, duration));
            WL("K Nearest Neighbors:");
            WL(string.Format("Origin: {0}", origin));
            WL(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum()));
            algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(WL);

            // Update strategy
            algo.SetAlgorithmStrategy(new NaiveStrategy());

            // Use algo
            duration = algo.UpdateKnn(origin, conf);

            // Print result
            WL(string.Format("\n{0} msec. {1}:", algo.Strategy.Name, duration));
            WL("K Nearest Neighbors:");
            WL(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum()));
            algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(WL);
        }
예제 #3
0
        static void Run()
        {
            // Config
            var rect = new Rectangle
                           {
                               XMin = -300, XMax = 300,
                               YMin = -200, YMax = 200,
                               MaxDistance = 10,
                           };
            rect.Validate();

            // Random points
            IPoints points = new Points();
            var rand = new Random();
            for (var i = 0; i < 5000; i++)
            {
                var x = rect.XMin + rand.NextDouble() * rect.Width;
                var y = rect.YMin + rand.NextDouble() * rect.Height;
                points.Data.Add(new P
                {
                    X = x,
                    Y = y,
                });
            }
            points.Round(3);

            // Init algo
            IAlgorithm algo =
                new Algorithm(points, rect, StrategyType.Grid);

            // Use algo
            var duration = algo.UpdateSingles();

            // Print result
            WL(string.Format("{0} msec. {1}:", algo.Strategy.Name, duration));
            WL("Singles:\n");
            algo.Singles.OrderBy(i => i.Uid).ToList().ForEach(WL);

            // Update strategy
            algo.SetAlgorithmStrategy(new NaiveStrategy());

            // Use algo
            duration = algo.UpdateSingles();

            // Print result
            WL(string.Format("\n{0} msec. {1}:", algo.Strategy.Name, duration));
            WL("Singles:\n");
            algo.Singles.OrderBy(i => i.Uid).ToList().ForEach(WL);
        }
        static void Knn()
        {
            IPoints points = Dataset.LoadDataset(@"c:\temp\points.csv");

            // Used for testing K nearest neighbor
            IPointsKnn dataset = new PointsKnn();
            dataset.Data.AddRange(points.Data.Select(i => i as IPKnn));

            // Used for testing K nearest neighbor
            var rect = new SingleDetectLibrary.Code.Data.Rectangle
            {
                XMin = -180,
                XMax = 180,
                YMin = -90,
                YMax = 90,
                MaxDistance = 20,
            };
            rect.Validate();
            const int k = 3;

            IAlgorithm algo = new Algorithm(dataset, rect, StrategyType.Grid);

            var origin = new SingleDetectLibrary.Code.Data.P { X = 0, Y = 0 };            
            algo.UpdateIndex(origin);

            var duration = algo.UpdateKnn(origin, new KnnConfiguration{K = k});

            // Print result
            CW(string.Format("{0} msec. {1}:", algo.Strategy.Name, duration));
            CW("K Nearest Neighbors:");
            CW(string.Format("Origin: {0}", origin));
            CW(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum()));
            algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(CW);


            // Update strategy
            algo.SetAlgorithmStrategy(new NaiveStrategy());

            // Use algo
            duration = algo.UpdateKnn(origin, new KnnConfiguration{K = k});

            // Print result
            CW(string.Format("\n{0} msec. {1}:", algo.Strategy.Name, duration));
            CW("K Nearest Neighbors:");
            CW(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum()));
            algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(CW);
        }
 // Read about this at github.com/kunukn/single-detect
 static void SetKnnAlgo(IPoints points)
 {            
     IPointsKnn dataset = new PointsKnn();
     dataset.Data.AddRange(points.Data.Select(i => i as IPKnn));            
     var rect = new SingleDetectLibrary.Code.Data.Rectangle
     {
         XMin = -190,
         XMax = 190,
         YMin = -100,
         YMax = 100,
         MaxDistance = 20,
     };
     rect.Validate();
     
     // Naive stratey works with all points on Earth.
     // Grid strategy runs much faster and can be used as approx algo 
     // but only works on certain local areas, not wrapped world. e.g. from lon -90 to lon 90, 
     // e.g. Europe only areas or US only areas etc. but not New Zealand due to being near lon 180.
     // All points must be within rect boundary
     IKnnAlgorithm algo = new KnnAlgorithm(dataset, rect, StrategyType.Naive);          
     Data = algo;
 }