public void TestTwoEdgesLeftMiddleHighest() { // build graph. var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size, ContractedEdgeDataSerializer.MetaSize); graph.AddEdge(1, 0, 100, false, Constants.NO_VERTEX); graph.AddEdge(2, 1, 100, false, Constants.NO_VERTEX); // create algorithm and run. var algorithm = new Itinero.Algorithms.Contracted.BidirectionalDykstra(graph, new EdgePath <float>[] { new EdgePath <float>(0) }, new EdgePath <float>[] { new EdgePath <float>(2) }); algorithm.Run(); // check results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); Assert.AreEqual(0, algorithm.Best); EdgePath <float> visit; Assert.IsTrue(algorithm.TryGetForwardVisit(0, out visit)); Assert.AreEqual(0, visit.Weight); Assert.AreEqual(0, visit.Vertex); Assert.AreEqual(null, visit.From); Assert.IsTrue(algorithm.TryGetBackwardVisit(2, out visit)); Assert.AreEqual(0, visit.Weight); Assert.AreEqual(2, visit.Vertex); Assert.AreEqual(null, visit.From); Assert.IsTrue(algorithm.TryGetBackwardVisit(1, out visit)); Assert.AreEqual(100, visit.Weight); Assert.AreEqual(1, visit.Vertex); Assert.IsNotNull(visit.From); Assert.AreEqual(2, visit.From.Vertex); Assert.IsTrue(algorithm.TryGetBackwardVisit(0, out visit)); Assert.AreEqual(200, visit.Weight); Assert.AreEqual(0, visit.Vertex); Assert.IsNotNull(visit.From); Assert.AreEqual(1, visit.From.Vertex); Assert.AreEqual(new List <uint>(new uint[] { 0, 1, 2 }), algorithm.GetPath()); }
public void TestMultipleLevelHiearchy2() { // build graph. var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size, ContractedEdgeDataSerializer.MetaSize); graph.AddEdge(1, 0, 100, null, Constants.NO_VERTEX); graph.AddEdge(1, 2, 100, null, Constants.NO_VERTEX); graph.AddEdge(2, 0, 200, null, 1); graph.AddEdge(2, 3, 100, null, Constants.NO_VERTEX); graph.AddEdge(3, 0, 300, null, 2); graph.AddEdge(3, 4, 100, null, Constants.NO_VERTEX); graph.AddEdge(4, 0, 400, null, 3); // create algorithm and run. var algorithm = new Itinero.Algorithms.Contracted.BidirectionalDykstra(graph, new EdgePath <float>[] { new EdgePath <float>(0) }, new EdgePath <float>[] { new EdgePath <float>(4) }); algorithm.Run(); // check results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); Assert.AreEqual(0, algorithm.Best); EdgePath <float> visit; Assert.IsTrue(algorithm.TryGetForwardVisit(0, out visit)); Assert.AreEqual(0, visit.Weight); Assert.AreEqual(0, visit.Vertex); Assert.AreEqual(null, visit.From); Assert.IsTrue(algorithm.TryGetBackwardVisit(4, out visit)); Assert.AreEqual(0, visit.Weight); Assert.AreEqual(4, visit.Vertex); Assert.IsTrue(algorithm.TryGetBackwardVisit(0, out visit)); Assert.AreEqual(400, visit.Weight); Assert.AreEqual(0, visit.Vertex); Assert.IsNotNull(visit.From); Assert.AreEqual(4, visit.From.Vertex); Assert.AreEqual(new List <uint>(new uint[] { 0, 1, 2, 3, 4 }), algorithm.GetPath()); }
/// <summary> /// Returns the path. /// </summary> /// <returns></returns> public static List <uint> GetPath <T>(this BidirectionalDykstra <T> dykstra) where T : struct { dykstra.CheckHasRunAndHasSucceeded(); EdgePath <T> fromSource; EdgePath <T> toTarget; if (dykstra.TryGetForwardVisit(dykstra.Best, out fromSource) && dykstra.TryGetBackwardVisit(dykstra.Best, out toTarget)) { var vertices = new List <uint>(); // add vertices from source. vertices.Add(fromSource.Vertex); while (fromSource.From != null) { if (fromSource.From.Vertex != Constants.NO_VERTEX) { // this should be the end of the path. if (fromSource.Edge == Constants.NO_EDGE) { // only expand when there is no edge id. dykstra.Graph.ExpandEdge(fromSource.From.Vertex, fromSource.Vertex, vertices, false, true); } } vertices.Add(fromSource.From.Vertex); fromSource = fromSource.From; } vertices.Reverse(); // and add vertices to target. while (toTarget.From != null) { if (toTarget.From.Vertex != Constants.NO_VERTEX) { // this should be the end of the path. if (toTarget.Edge == Constants.NO_EDGE) { // only expand when there is no edge id. dykstra.Graph.ExpandEdge(toTarget.From.Vertex, toTarget.Vertex, vertices, false, false); } } vertices.Add(toTarget.From.Vertex); toTarget = toTarget.From; } return(vertices); } throw new InvalidOperationException("No path could be found to/from source/target."); }