// 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; }
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); }
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); }