public void TestWithinOneEdge() { // build graph. var routerDb = new RouterDb(); routerDb.AddSupportedProfile(MockProfile.CarMock()); routerDb.Network.AddVertex(0, 0, 0); routerDb.Network.AddVertex(1, 0, 0); routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.AddContracted(MockProfile.CarMock()); // run algorithm. var algorithm = new ManyToManyBidirectionalDykstra(new Router(routerDb), MockProfile.CarMock(), new RouterPoint[] { new RouterPoint(0, 0, 0, ushort.MaxValue / 10) }, new RouterPoint[] { new RouterPoint(1, 1, 0, ushort.MaxValue / 10 * 9) }); algorithm.Run(); Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); Assert.IsNotNull(algorithm.Weights); Assert.AreEqual(1, algorithm.Weights.Length); Assert.AreEqual(1, algorithm.Weights[0].Length); Assert.AreEqual(MockProfile.CarMock().Factor(null).Value * 80, algorithm.Weights[0][0], 0.01); }
public Result <float[][]> TryCalculateWeight(Profile profile, RouterPoint[] sources, RouterPoint[] targets, ISet <int> invalidSources, ISet <int> invalidTargets) { if (!this._db.Supports(profile)) { return(new Result <float[][]>("Routing profile is not supported.", (Func <string, Exception>)(message => new Exception(message)))); } Func <ushort, Factor> getFactor = this.GetGetFactor(profile); DirectedMetaGraph contracted; float[][] weights; if (this._db.TryGetContracted(profile, out contracted)) { ManyToManyBidirectionalDykstra bidirectionalDykstra = new ManyToManyBidirectionalDykstra(this._db, profile, sources, targets); bidirectionalDykstra.Run(); if (!bidirectionalDykstra.HasSucceeded) { return(new Result <float[][]>(bidirectionalDykstra.ErrorMessage, (Func <string, Exception>)(message => (Exception) new RouteNotFoundException(message)))); } weights = bidirectionalDykstra.Weights; } else { ManyToMany manyToMany = new ManyToMany(this._db, getFactor, sources, targets, float.MaxValue); manyToMany.Run(); if (!manyToMany.HasSucceeded) { return(new Result <float[][]>(manyToMany.ErrorMessage, (Func <string, Exception>)(message => (Exception) new RouteNotFoundException(message)))); } weights = manyToMany.Weights; } int[] numArray = new int[targets.Length]; for (int index1 = 0; index1 < weights.Length; ++index1) { int num = 0; for (int index2 = 0; index2 < weights[index1].Length; ++index2) { if (index2 != index1 && (double)weights[index1][index2] == 3.40282346638529E+38) { ++num; ++numArray[index2]; if (numArray[index2] > (sources.Length - 1) / 2) { invalidTargets.Add(index2); } } } if (num > (targets.Length - 1) / 2) { invalidSources.Add(index1); } } return(new Result <float[][]>(weights)); }
public void TestPentagon() { var routerDb = new RouterDb(); routerDb.AddSupportedProfile(MockProfile.CarMock()); routerDb.Network.AddVertex(0, 0, 0); routerDb.Network.AddVertex(1, 1, 1); routerDb.Network.AddVertex(2, 2, 2); routerDb.Network.AddVertex(3, 3, 3); routerDb.Network.AddVertex(4, 4, 4); routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(1, 2, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(2, 3, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(3, 4, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(4, 0, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); // build graph. var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size, ContractedEdgeDataSerializer.MetaSize); graph.AddEdge(0, 1, 100 * MockProfile.CarMock().Factor(null).Value, null, Constants.NO_VERTEX); graph.AddEdge(0, 4, 100 * MockProfile.CarMock().Factor(null).Value, null, Constants.NO_VERTEX); graph.AddEdge(2, 1, 100 * MockProfile.CarMock().Factor(null).Value, null, Constants.NO_VERTEX); graph.AddEdge(2, 3, 100 * MockProfile.CarMock().Factor(null).Value, null, Constants.NO_VERTEX); graph.AddEdge(3, 1, 200 * MockProfile.CarMock().Factor(null).Value, null, 2); graph.AddEdge(4, 1, 200 * MockProfile.CarMock().Factor(null).Value, null, 0); graph.AddEdge(4, 3, 100 * MockProfile.CarMock().Factor(null).Value, null, Constants.NO_VERTEX); routerDb.AddContracted(MockProfile.CarMock(), new ContractedDb(graph)); // create algorithm and run. var algorithm = new ManyToManyBidirectionalDykstra(new Router(routerDb), MockProfile.CarMock(), new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2), routerDb.Network.CreateRouterPointForVertex(3), routerDb.Network.CreateRouterPointForVertex(4) }, new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2), routerDb.Network.CreateRouterPointForVertex(3), routerDb.Network.CreateRouterPointForVertex(4) }); algorithm.Run(); // check results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); Assert.IsNotNull(algorithm.Weights); Assert.AreEqual(5, algorithm.Weights.Length); Assert.AreEqual(5, algorithm.Weights[0].Length); Assert.AreEqual(5, algorithm.Weights[1].Length); Assert.AreEqual(5, algorithm.Weights[2].Length); Assert.AreEqual(5, algorithm.Weights[3].Length); Assert.AreEqual(5, algorithm.Weights[4].Length); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][0], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][1], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][2], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][3], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][4], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][0], 0.1); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][1], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][2], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][3], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][4], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][0], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][1], 0.1); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][2], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][3], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][4], 0.1); }
public void TestTwoEdgesLeftMiddleHighest() { // build graph. var oneway = MockProfile.CarMock(t => new Speed() { Value = MockProfile.CarMock().Speed(null).Value, Direction = 1 }); var routerDb = new RouterDb(); routerDb.AddSupportedProfile(oneway); routerDb.Network.AddVertex(0, 0, 0); routerDb.Network.AddVertex(1, 1, 1); routerDb.Network.AddVertex(2, 2, 2); routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(1, 2, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); // build graph. var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size, ContractedEdgeDataSerializer.MetaSize); graph.AddEdge(1, 0, 100 * MockProfile.CarMock().Factor(null).Value, false, Constants.NO_VERTEX); graph.AddEdge(2, 1, 100 * MockProfile.CarMock().Factor(null).Value, false, Constants.NO_VERTEX); routerDb.AddContracted(MockProfile.CarMock(), new ContractedDb(graph)); // create algorithm and run. var algorithm = new ManyToManyBidirectionalDykstra(new Router(routerDb), oneway, new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2) }, new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2) }); algorithm.Run(); // check results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); Assert.IsNotNull(algorithm.Weights); Assert.AreEqual(3, algorithm.Weights.Length); Assert.AreEqual(3, algorithm.Weights[0].Length); Assert.AreEqual(3, algorithm.Weights[1].Length); Assert.AreEqual(3, algorithm.Weights[2].Length); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][0], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][1], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][2], 0.1); Assert.AreEqual(float.MaxValue, algorithm.Weights[1][0]); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][1], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][2], 0.1); Assert.AreEqual(float.MaxValue, algorithm.Weights[2][0]); Assert.AreEqual(float.MaxValue, algorithm.Weights[2][1]); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][2], 0.1); }
public void TestThreeEdges() { // build graph. var routerDb = new RouterDb(); routerDb.AddSupportedProfile(MockProfile.CarMock()); routerDb.Network.AddVertex(0, 0, 0); routerDb.Network.AddVertex(1, 1, 1); routerDb.Network.AddVertex(2, 2, 2); routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(1, 2, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(2, 0, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.AddContracted(MockProfile.CarMock()); // run algorithm (0, 1, 2)->(0, 1, 2). var algorithm = new ManyToManyBidirectionalDykstra(new Router(routerDb), MockProfile.CarMock(), new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2) }, new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2) }); algorithm.Run(); Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); var weights = algorithm.Weights; Assert.IsNotNull(weights); Assert.AreEqual(3, weights.Length); Assert.AreEqual(3, weights[0].Length); Assert.AreEqual(0, weights[0][0], 0.001); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, weights[0][1], 0.01); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, weights[0][2], 0.01); Assert.AreEqual(3, weights[1].Length); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, weights[1][0], 0.01); Assert.AreEqual(0, weights[1][1], 0.001); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, weights[1][2], 0.01); Assert.AreEqual(3, weights[2].Length); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, weights[2][0], 0.01); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, weights[2][1], 0.01); Assert.AreEqual(0, weights[2][2], 0.001); }
public void TestTwoEdgesRightHighest() { // build graph. var routerDb = new RouterDb(); routerDb.AddSupportedProfile(MockProfile.CarMock()); routerDb.Network.AddVertex(0, 0, 0); routerDb.Network.AddVertex(1, 1, 1); routerDb.Network.AddVertex(2, 2, 2); routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); routerDb.Network.AddEdge(1, 2, new Itinero.Data.Network.Edges.EdgeData() { Distance = 100, Profile = 0, MetaId = 0 }); // build graph. var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize); graph.AddEdge(0, 1, 100 * MockProfile.CarMock().Factor(null).Value, null); graph.AddEdge(1, 2, 100 * MockProfile.CarMock().Factor(null).Value, null); routerDb.AddContracted(MockProfile.CarMock(), new ContractedDb(graph)); // create algorithm and run. var algorithm = new ManyToManyBidirectionalDykstra(new Router(routerDb), MockProfile.CarMock(), new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2) }, new RouterPoint[] { routerDb.Network.CreateRouterPointForVertex(0), routerDb.Network.CreateRouterPointForVertex(1), routerDb.Network.CreateRouterPointForVertex(2) }); algorithm.Run(); // check results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); Assert.IsNotNull(algorithm.Weights); Assert.AreEqual(3, algorithm.Weights.Length); Assert.AreEqual(3, algorithm.Weights[0].Length); Assert.AreEqual(3, algorithm.Weights[1].Length); Assert.AreEqual(3, algorithm.Weights[2].Length); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][0], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][1], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[0][2], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][0], 0.1); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][1], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[1][2], 0.1); Assert.AreEqual(200 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][0], 0.1); Assert.AreEqual(100 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][1], 0.1); Assert.AreEqual(000 * MockProfile.CarMock().Factor(null).Value, algorithm.Weights[2][2], 0.1); }