public void TestPermutationCount() { int[] test_sequence = new int[] { 1, 2 }; PermutationEnumerable <int> enumerator = new PermutationEnumerable <int>(test_sequence); List <int[]> set = new List <int[]>(enumerator); Assert.AreEqual(2, set.Count); test_sequence = new int[] { 1, 2, 3 }; enumerator = new PermutationEnumerable <int>(test_sequence); set = new List <int[]>(enumerator); Assert.AreEqual(6, set.Count); test_sequence = new int[] { 1, 2, 3, 4 }; enumerator = new PermutationEnumerable <int>(test_sequence); set = new List <int[]>(enumerator); Assert.AreEqual(24, set.Count); test_sequence = new int[] { 1, 2, 3, 4, 5 }; enumerator = new PermutationEnumerable <int>(test_sequence); set = new List <int[]>(enumerator); Assert.AreEqual(120, set.Count); }
/// <summary> /// Solves the TSP. /// </summary> /// <param name="problem"></param> /// <returns></returns> protected override IRoute DoSolve(IProblem problem) { // initialize. List <int> solution = new List <int>(); for (int idx = 0; idx < problem.Size; idx++) { // add each customer again. solution.Add(idx); } // keep on looping until all the permutations // have been considered. PermutationEnumerable <int> enumerator = new PermutationEnumerable <int>( solution.ToArray()); int[] best = null; double bestWeight = double.MaxValue; foreach (int[] permutation in enumerator) { double weight = RouteExtensions.CalculateWeight(permutation, problem.First == problem.Last, problem); if (weight < bestWeight) { // the best weight has improved. bestWeight = weight; best = permutation; } } return(new SimpleAsymmetricRoute(best.ToList <int>(), true)); }
/// <summary> /// Solves the TSP. /// </summary> /// <param name="problem"></param> /// <returns></returns> protected override IRoute DoSolve(IProblem problem) { // initialize. List<int> solution = new List<int>(); for (int idx = 0; idx < problem.Size; idx++) { // add each customer again. solution.Add(idx); } // keep on looping until all the permutations // have been considered. PermutationEnumerable<int> enumerator = new PermutationEnumerable<int>( solution.ToArray()); int[] best = null; double bestWeight = double.MaxValue; foreach (int[] permutation in enumerator) { double weight = RouteExtensions.CalculateWeight(permutation, problem.First == problem.Last, problem); if (weight < bestWeight) { // the best weight has improved. bestWeight = weight; best = permutation; } } return new SimpleAsymmetricRoute(best.ToList<int>(), true); }
public void TestReset() { var testSequence = new int[] { 1, 2, 3 }; var enumerable = new PermutationEnumerable <int>(testSequence); var set = new List <int[]>(); var enumerator = enumerable.GetEnumerator(); while (enumerator.MoveNext()) { set.Add(enumerator.Current); } enumerator.Reset(); set.Clear(); while (enumerator.MoveNext()) { set.Add(enumerator.Current); } Assert.AreEqual(6, set.Count); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 3, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 2, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 3, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1, 3 })); }
public void TestPermutationContent() { int[] test_sequence = new int[] { 1, 2 }; PermutationEnumerable <int> enumerator = new PermutationEnumerable <int>(test_sequence); List <int[]> set = new List <int[]>(enumerator); Assert.AreEqual(2, set.Count); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1 })); test_sequence = new int[] { 1, 2, 3 }; enumerator = new PermutationEnumerable <int>(test_sequence); set = new List <int[]>(enumerator); Assert.AreEqual(6, set.Count); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 3, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 2, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 3, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1, 3 })); // 4 items tests all the crucial elements of the algorithm. (full code coverage) test_sequence = new int[] { 1, 2, 3, 4 }; enumerator = new PermutationEnumerable <int>(test_sequence); set = new List <int[]>(enumerator); Assert.AreEqual(24, set.Count); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2, 3, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 3, 2, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 1, 2, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 2, 1, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 3, 1, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1, 3, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2, 4, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 3, 4, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 1, 4, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 2, 4, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 3, 4, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1, 4, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 4, 2, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 4, 3, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 4, 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 4, 2, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 4, 3, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 4, 1, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 1, 2, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 1, 3, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 3, 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 3, 2, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 2, 3, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 2, 1, 3 })); }
/// <summary> /// Solves the given problem. /// </summary> /// <returns></returns> public sealed override Tour Solve(TSProblem problem, TSPObjective objective, out float fitness) { // initialize. var solution = new List <int>(); for (int customer = 0; customer < problem.Weights.Length; customer++) { // add each customer again. if (customer != problem.First && customer != problem.Last) { solution.Add(customer); } } if (solution.Count < 2) { // a tiny problem. // build route. var withFirst = new List <int>(solution); withFirst.Insert(0, problem.First); if (problem.Last.HasValue && problem.First != problem.Last) { // the special case of a fixed last customer. withFirst.Add(problem.Last.Value); } var route = new Tour(withFirst, problem.Last); fitness = objective.Calculate(problem, route); return(route); } // keep on looping until all the permutations // have been considered. var enumerator = new PermutationEnumerable <int>( solution.ToArray()); Tour bestSolution = null; var bestFitness = float.MaxValue; foreach (var permutation in enumerator) { // build route from permutation. var withFirst = new List <int>(permutation); withFirst.Insert(0, problem.First); if (problem.Last.HasValue && problem.First != problem.Last) { // the special case of a fixed last customer. withFirst.Add(problem.Last.Value); } var localRoute = new Tour(withFirst, problem.Last); // calculate fitness. var localFitness = objective.Calculate(problem, localRoute); if (localFitness < bestFitness) { // the best weight has improved. bestFitness = localFitness; bestSolution = localRoute; } } fitness = bestFitness; return(bestSolution); }
public void TestPermutationContent() { int[] test_sequence = new int[] { 1, 2 }; PermutationEnumerable<int> enumerator = new PermutationEnumerable<int>(test_sequence); List<int[]> set = new List<int[]>(enumerator); Assert.AreEqual(2, set.Count); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1 })); test_sequence = new int[] { 1, 2, 3 }; enumerator = new PermutationEnumerable<int>(test_sequence); set = new List<int[]>(enumerator); Assert.AreEqual(6, set.Count); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 3, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 2, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 3, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1, 3 })); // 4 items tests all the crucial elements of the algorithm. (full code coverage) test_sequence = new int[] { 1, 2, 3, 4 }; enumerator = new PermutationEnumerable<int>(test_sequence); set = new List<int[]>(enumerator); Assert.AreEqual(24, set.Count); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2, 3, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 3, 2, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 1, 2, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 2, 1, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 3, 1, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1, 3, 4 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 2, 4, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 3, 4, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 1, 4, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 2, 4, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 3, 4, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 1, 4, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 4, 2, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 1, 4, 3, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 4, 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 3, 4, 2, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 4, 3, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 2, 4, 1, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 1, 2, 3 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 1, 3, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 3, 1, 2 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 3, 2, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 2, 3, 1 })); Assert.IsTrue(this.TestPermutationContent(set, new int[] { 4, 2, 1, 3 })); }
public void TestPermutationCount() { var testSequence = new int[] { 1, 2 }; var enumerable = new PermutationEnumerable <int>(testSequence); var set = new List <int[]>(enumerable); Assert.AreEqual(2, set.Count); testSequence = new int[] { 1, 2, 3 }; enumerable = new PermutationEnumerable <int>(testSequence); set = new List <int[]>(enumerable); Assert.AreEqual(6, set.Count); testSequence = new int[] { 1, 2, 3, 4 }; enumerable = new PermutationEnumerable <int>(testSequence); set = new List <int[]>(enumerable); Assert.AreEqual(24, set.Count); testSequence = new int[] { 1, 2, 3, 4, 5 }; enumerable = new PermutationEnumerable <int>(testSequence); set = new List <int[]>(enumerable); Assert.AreEqual(120, set.Count); }
public void RoutingRegressionTest2() { OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter(); TagsTableCollectionIndex tags_index = new TagsTableCollectionIndex(); // do the data processing. DynamicGraphRouterDataSource <LiveEdge> memory_data = new DynamicGraphRouterDataSource <LiveEdge>(tags_index); LiveGraphOsmStreamTarget target_data = new LiveGraphOsmStreamTarget( memory_data, interpreter, tags_index); XmlOsmStreamSource data_processor_source = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Test.Unittests.test_network.osm")); OsmStreamFilterSort sorter = new OsmStreamFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); IBasicRouter <LiveEdge> basic_router = new DykstraRoutingLive(); Router router = Router.CreateLiveFrom(memory_data, basic_router, interpreter); // build coordinates list of resolved points. List <GeoCoordinate> testPoints = new List <GeoCoordinate>(); testPoints.Add(new GeoCoordinate(51.0582204, 3.7193524)); testPoints.Add(new GeoCoordinate(51.0582199, 3.7194002)); //testPoints.Add(new GeoCoordinate(51.0582178, 3.7195025)); testPoints.Add(new GeoCoordinate(51.0581727, 3.7195833)); testPoints.Add(new GeoCoordinate(51.0581483, 3.7195553)); //testPoints.Add(new GeoCoordinate(51.0581219, 3.7195231)); //testPoints.Add(new GeoCoordinate(51.0580965, 3.7194918)); testPoints.Add(new GeoCoordinate(51.0581883, 3.7196617)); testPoints.Add(new GeoCoordinate(51.0581628, 3.7196889)); // build a matrix of routes between all points. Route[][] referenceRoutes = new Route[testPoints.Count][]; int[] permuationArray = new int[testPoints.Count]; for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { permuationArray[fromIdx] = fromIdx; referenceRoutes[fromIdx] = new Route[testPoints.Count]; for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // create router from scratch. router = Router.CreateLiveFrom( memory_data, basic_router, interpreter); // resolve points. RouterPoint from = router.Resolve(Vehicle.Car, testPoints[fromIdx]); RouterPoint to = router.Resolve(Vehicle.Car, testPoints[toIdx]); // calculate route. referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to); } } // resolve points in some order and compare the resulting routes. // they should be identical in length except for some numerical rounding errors. PermutationEnumerable <int> enumerator = new PermutationEnumerable <int>( permuationArray); foreach (int[] permutation in enumerator) { //int[] permutation = new int[] { 5, 0, 1, 2, 4, 3, 6, 7, 8 }; //// incrementally resolve points. //// create router from scratch. //router = new Router<SimpleWeighedEdge>(memory_data, interpreter, basic_router); //// resolve in the order of the permutation. //RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length]; //resolvedPoints[permutation[0]] = router.Resolve(VehicleEnum.Car, testPoints[permutation[0]]); //for (int idx = 1; idx < permutation.Length; idx++) //{ // resolvedPoints[permutation[idx]] = router.Resolve(VehicleEnum.Car, testPoints[permutation[idx]]); // for (int fromIdx = 0; fromIdx <= idx; fromIdx++) // { // for (int toIdx = 0; toIdx <= idx; toIdx++) // { // // calculate route. // OsmSharpRoute route = router.Calculate(VehicleEnum.Car, // resolvedPoints[permutation[fromIdx]], resolvedPoints[permutation[toIdx]]); // Assert.AreEqual(referenceRoutes[permutation[fromIdx]][permutation[toIdx]].TotalDistance, // route.TotalDistance, 0.1); // } // } //} // create router from scratch. router = Router.CreateLiveFrom(memory_data, basic_router, interpreter); // resolve in the order of the permutation. RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length]; for (int idx = 0; idx < permutation.Length; idx++) { resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]); } for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // calculate route. Route route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]); Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 0.1); } } } }
public void RoutingRegressionTest3() { var interpreter = new OsmRoutingInterpreter(); var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. var memoryData = new DynamicGraphRouterDataSource<LiveEdge>(tagsIndex); var targetData = new LiveGraphOsmStreamTarget(memoryData, interpreter, tagsIndex); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Test.Unittests.test_network.osm")); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); var basicRouter = new Dykstra(); var router = Router.CreateLiveFrom(memoryData, basicRouter, interpreter); // build coordinates list of resolved points. var testPoints = new List<GeoCoordinate>(); testPoints.Add(new GeoCoordinate(51.0581719, 3.7201622)); testPoints.Add(new GeoCoordinate(51.0580439, 3.7202134)); testPoints.Add(new GeoCoordinate(51.0580573, 3.7204378)); testPoints.Add(new GeoCoordinate(51.0581862, 3.7203758)); // build a matrix of routes between all points. var referenceRoutes = new Route[testPoints.Count][]; var permuationArray = new int[testPoints.Count]; for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { permuationArray[fromIdx] = fromIdx; referenceRoutes[fromIdx] = new Route[testPoints.Count]; for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // create router from scratch. router = Router.CreateLiveFrom( memoryData, basicRouter, interpreter); // resolve points. var from = router.Resolve(Vehicle.Car, testPoints[fromIdx]); var to = router.Resolve(Vehicle.Car, testPoints[toIdx]); // calculate route. referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to); } } // resolve points in some order and compare the resulting routes. // they should be identical in length except for some numerical rounding errors. var enumerator = new PermutationEnumerable<int>( permuationArray); foreach (int[] permutation in enumerator) { // create router from scratch. router = Router.CreateLiveFrom(memoryData, basicRouter, interpreter); // resolve in the order of the permutation. var resolvedPoints = new RouterPoint[permutation.Length]; for (int idx = 0; idx < permutation.Length; idx++) { resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]); } for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // calculate route. var route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]); Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 0.1); } } } }
public void RoutingRegressionTest2() { var interpreter = new OsmRoutingInterpreter(); var tagsIndex = new TagsIndex(); // do the data processing. var memoryData = new RouterDataSource<Edge>(new Graph<Edge>(), tagsIndex); var targetData = new GraphOsmStreamTarget(memoryData, interpreter, tagsIndex); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Routing.Test.data.test_network.osm")); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); var basicRouter = new Dykstra(); var router = Router.CreateFrom(memoryData, basicRouter, interpreter); // build coordinates list of resolved points. var testPoints = new List<GeoCoordinate>(); testPoints.Add(new GeoCoordinate(51.0582204, 3.7193524)); testPoints.Add(new GeoCoordinate(51.0582199, 3.7194002)); testPoints.Add(new GeoCoordinate(51.0581727, 3.7195833)); testPoints.Add(new GeoCoordinate(51.0581483, 3.7195553)); testPoints.Add(new GeoCoordinate(51.0581883, 3.7196617)); testPoints.Add(new GeoCoordinate(51.0581628, 3.7196889)); // build a matrix of routes between all points. var referenceRoutes = new Route[testPoints.Count][]; var permuationArray = new int[testPoints.Count]; for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { permuationArray[fromIdx] = fromIdx; referenceRoutes[fromIdx] = new Route[testPoints.Count]; for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // create router from scratch. router = Router.CreateFrom( memoryData, basicRouter, interpreter); // resolve points. var from = router.Resolve(Vehicle.Car, testPoints[fromIdx]); var to = router.Resolve(Vehicle.Car, testPoints[toIdx]); // calculate route. referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to); } } // resolve points in some order and compare the resulting routes. // they should be identical in length except for some numerical rounding errors. var enumerator = new PermutationEnumerable<int>(permuationArray); foreach (int[] permutation in enumerator) { // create router from scratch. router = Router.CreateFrom(memoryData, basicRouter, interpreter); // resolve in the order of the permutation. var resolvedPoints = new RouterPoint[permutation.Length]; for (int idx = 0; idx < permutation.Length; idx++) { resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]); } for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // calculate route. var route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]); // TODO: changed the resolve accuracy to .5m. Make sure this is more accurate in the future. Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 1); } } } }
public void RoutingRegressionTest4() { OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter(); SimpleTagsIndex tags_index = new SimpleTagsIndex(); // do the data processing. DynamicGraphRouterDataSource <LiveEdge> memory_data = new DynamicGraphRouterDataSource <LiveEdge>(tags_index); LiveGraphOsmStreamWriter target_data = new LiveGraphOsmStreamWriter( memory_data, interpreter, memory_data.TagsIndex); XmlOsmStreamSource data_processor_source = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test_network.osm")); OsmStreamFilterSort sorter = new OsmStreamFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); IBasicRouter <LiveEdge> basic_router = new DykstraRoutingLive(memory_data.TagsIndex); Router router = Router.CreateLiveFrom(memory_data, basic_router, interpreter); // build coordinates list of resolved points. List <GeoCoordinate> testPoints = new List <GeoCoordinate>(); testPoints.Add(new GeoCoordinate(51.0581719, 3.7201622)); testPoints.Add(new GeoCoordinate(51.0580439, 3.7202134)); testPoints.Add(new GeoCoordinate(51.0580573, 3.7204378)); testPoints.Add(new GeoCoordinate(51.0581862, 3.7203758)); // build a matrix of routes between all points. OsmSharpRoute[][] referenceRoutes = new OsmSharpRoute[testPoints.Count][]; int[] permuationArray = new int[testPoints.Count]; for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { permuationArray[fromIdx] = fromIdx; referenceRoutes[fromIdx] = new OsmSharpRoute[testPoints.Count]; for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // create router from scratch. router = Router.CreateLiveFrom( memory_data, basic_router, interpreter); // resolve points. RouterPoint from = router.Resolve(Vehicle.Car, testPoints[fromIdx]); RouterPoint to = router.Resolve(Vehicle.Car, testPoints[toIdx]); // calculate route. referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to); } } // resolve points in some order and compare the resulting routes. // they should be identical in length except for some numerical rounding errors. PermutationEnumerable <int> enumerator = new PermutationEnumerable <int>( permuationArray); foreach (int[] permutation in enumerator) { // create router from scratch. router = Router.CreateLiveFrom(memory_data, basic_router, interpreter); // resolve in the order of the permutation. RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length]; for (int idx = 0; idx < permutation.Length; idx++) { resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]); } for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // calculate route. OsmSharpRoute route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]); Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 0.1); } } } }
public void RoutingRegressionTest2() { OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter(); SimpleTagsIndex tags_index = new SimpleTagsIndex(); // do the data processing. DynamicGraphRouterDataSource<LiveEdge> memory_data = new DynamicGraphRouterDataSource<LiveEdge>(tags_index); LiveGraphOsmStreamTarget target_data = new LiveGraphOsmStreamTarget( memory_data, interpreter, memory_data.TagsIndex); XmlOsmStreamSource data_processor_source = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Test.Unittests.test_network.osm")); OsmStreamFilterSort sorter = new OsmStreamFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); IBasicRouter<LiveEdge> basic_router = new DykstraRoutingLive(memory_data.TagsIndex); Router router = Router.CreateLiveFrom(memory_data, basic_router, interpreter); // build coordinates list of resolved points. List<GeoCoordinate> testPoints = new List<GeoCoordinate>(); testPoints.Add(new GeoCoordinate(51.0582204, 3.7193524)); testPoints.Add(new GeoCoordinate(51.0582199, 3.7194002)); //testPoints.Add(new GeoCoordinate(51.0582178, 3.7195025)); testPoints.Add(new GeoCoordinate(51.0581727, 3.7195833)); testPoints.Add(new GeoCoordinate(51.0581483, 3.7195553)); //testPoints.Add(new GeoCoordinate(51.0581219, 3.7195231)); //testPoints.Add(new GeoCoordinate(51.0580965, 3.7194918)); testPoints.Add(new GeoCoordinate(51.0581883, 3.7196617)); testPoints.Add(new GeoCoordinate(51.0581628, 3.7196889)); // build a matrix of routes between all points. Route[][] referenceRoutes = new Route[testPoints.Count][]; int[] permuationArray = new int[testPoints.Count]; for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { permuationArray[fromIdx] = fromIdx; referenceRoutes[fromIdx] = new Route[testPoints.Count]; for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // create router from scratch. router = Router.CreateLiveFrom( memory_data, basic_router, interpreter); // resolve points. RouterPoint from = router.Resolve(Vehicle.Car, testPoints[fromIdx]); RouterPoint to = router.Resolve(Vehicle.Car, testPoints[toIdx]); // calculate route. referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to); } } // resolve points in some order and compare the resulting routes. // they should be identical in length except for some numerical rounding errors. PermutationEnumerable<int> enumerator = new PermutationEnumerable<int>( permuationArray); foreach (int[] permutation in enumerator) { //int[] permutation = new int[] { 5, 0, 1, 2, 4, 3, 6, 7, 8 }; //// incrementally resolve points. //// create router from scratch. //router = new Router<SimpleWeighedEdge>(memory_data, interpreter, basic_router); //// resolve in the order of the permutation. //RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length]; //resolvedPoints[permutation[0]] = router.Resolve(VehicleEnum.Car, testPoints[permutation[0]]); //for (int idx = 1; idx < permutation.Length; idx++) //{ // resolvedPoints[permutation[idx]] = router.Resolve(VehicleEnum.Car, testPoints[permutation[idx]]); // for (int fromIdx = 0; fromIdx <= idx; fromIdx++) // { // for (int toIdx = 0; toIdx <= idx; toIdx++) // { // // calculate route. // OsmSharpRoute route = router.Calculate(VehicleEnum.Car, // resolvedPoints[permutation[fromIdx]], resolvedPoints[permutation[toIdx]]); // Assert.AreEqual(referenceRoutes[permutation[fromIdx]][permutation[toIdx]].TotalDistance, // route.TotalDistance, 0.1); // } // } //} // create router from scratch. router = Router.CreateLiveFrom(memory_data, basic_router, interpreter); // resolve in the order of the permutation. RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length]; for (int idx = 0; idx < permutation.Length; idx++) { resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]); } for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // calculate route. Route route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]); Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 0.1); } } } }
public void TestPermutationCount() { int[] test_sequence = new int[] { 1, 2 }; PermutationEnumerable<int> enumerator = new PermutationEnumerable<int>(test_sequence); List<int[]> set = new List<int[]>(enumerator); Assert.AreEqual(2, set.Count); test_sequence = new int[] { 1, 2, 3 }; enumerator = new PermutationEnumerable<int>(test_sequence); set = new List<int[]>(enumerator); Assert.AreEqual(6, set.Count); test_sequence = new int[] { 1, 2, 3, 4 }; enumerator = new PermutationEnumerable<int>(test_sequence); set = new List<int[]>(enumerator); Assert.AreEqual(24, set.Count); test_sequence = new int[] { 1, 2, 3, 4, 5 }; enumerator = new PermutationEnumerable<int>(test_sequence); set = new List<int[]>(enumerator); Assert.AreEqual(120, set.Count); }
public void RoutingRegressionTest2() { var interpreter = new OsmRoutingInterpreter(); var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. var memoryData = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex); var targetData = new LiveGraphOsmStreamTarget(memoryData, interpreter, tagsIndex); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Test.Unittests.test_network.osm")); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); var basicRouter = new DykstraRoutingLive(); var router = Router.CreateLiveFrom(memoryData, basicRouter, interpreter); // build coordinates list of resolved points. var testPoints = new List <GeoCoordinate>(); testPoints.Add(new GeoCoordinate(51.0582204, 3.7193524)); testPoints.Add(new GeoCoordinate(51.0582199, 3.7194002)); testPoints.Add(new GeoCoordinate(51.0581727, 3.7195833)); testPoints.Add(new GeoCoordinate(51.0581483, 3.7195553)); testPoints.Add(new GeoCoordinate(51.0581883, 3.7196617)); testPoints.Add(new GeoCoordinate(51.0581628, 3.7196889)); // build a matrix of routes between all points. var referenceRoutes = new Route[testPoints.Count][]; var permuationArray = new int[testPoints.Count]; for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { permuationArray[fromIdx] = fromIdx; referenceRoutes[fromIdx] = new Route[testPoints.Count]; for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // create router from scratch. router = Router.CreateLiveFrom( memoryData, basicRouter, interpreter); // resolve points. var from = router.Resolve(Vehicle.Car, testPoints[fromIdx]); var to = router.Resolve(Vehicle.Car, testPoints[toIdx]); // calculate route. referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to); } } // resolve points in some order and compare the resulting routes. // they should be identical in length except for some numerical rounding errors. var enumerator = new PermutationEnumerable <int>(permuationArray); foreach (int[] permutation in enumerator) { // create router from scratch. router = Router.CreateLiveFrom(memoryData, basicRouter, interpreter); // resolve in the order of the permutation. var resolvedPoints = new RouterPoint[permutation.Length]; for (int idx = 0; idx < permutation.Length; idx++) { resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]); } for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++) { for (int toIdx = 0; toIdx < testPoints.Count; toIdx++) { // calculate route. var route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]); // TODO: changed the resolve accuracy to .5m. Make sure this is more accurate in the future. Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 1); } } } }