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); }
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 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); }
/// <summary> /// Run fast and slow version with same test data and display the running time /// </summary> static void Run() { // Config, test data showing ability to use negative positions var rect = new Rectangle { XMin = -200, XMax = 200, YMin = -100, YMax = 100, MaxDistance = 20, }; rect.Validate(); var conf = new KnnConfiguration { K = 100, SameTypeOnly = false, MaxDistance = null }; // 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()); //algo.SetAlgorithmStrategy(new KdTreeStrategy(points.Data)); // 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); }