Beispiel #1
0
        public void TestMultipleLevelHiearchy5()
        {
            // build graph.
            var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size,
                                              ContractedEdgeDataSerializer.MetaSize);

            graph.AddEdge(0, 2, 30, null, 1);
            graph.AddEdge(1, 0, 10, null, Constants.NO_VERTEX);
            graph.AddEdge(1, 2, 20, null, Constants.NO_VERTEX);
            graph.AddEdge(2, 4, 70, null, 3);
            graph.AddEdge(3, 2, 30, null, Constants.NO_VERTEX);
            graph.AddEdge(3, 4, 40, null, 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>(4) });

            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);
            Assert.AreEqual(4, 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.TryGetForwardVisit(2, out visit));
            Assert.AreEqual(30, visit.Weight);
            Assert.AreEqual(2, visit.Vertex);
            Assert.IsNotNull(visit.From);
            Assert.AreEqual(0, visit.From.Vertex);
            Assert.IsTrue(algorithm.TryGetForwardVisit(4, out visit));
            Assert.AreEqual(100, visit.Weight);
            Assert.AreEqual(4, visit.Vertex);
            Assert.IsNotNull(visit.From);
            Assert.AreEqual(2, visit.From.Vertex);

            Assert.IsTrue(algorithm.TryGetBackwardVisit(4, out visit));
            Assert.AreEqual(0, visit.Weight);
            Assert.AreEqual(4, visit.Vertex);
            Assert.AreEqual(null, visit.From);

            Assert.AreEqual(new List <uint>(new uint[] { 0, 1, 2, 3, 4 }), algorithm.GetPath());
        }
Beispiel #2
0
        /// <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.");
        }
Beispiel #3
0
        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());
        }
Beispiel #4
0
        public void TestShorterEdgeOpposite()
        {
            // build graph.
            var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size,
                                              ContractedEdgeDataSerializer.MetaSize);

            graph.AddEdge(1, 0, 100, false, Constants.NO_VERTEX);
            graph.AddEdge(1, 0, 10, true, 3);
            graph.AddEdge(3, 0, 5, true, Constants.NO_VERTEX);
            graph.AddEdge(3, 1, 5, false, Constants.NO_VERTEX);
            graph.AddEdge(1, 2, 10, null, Constants.NO_VERTEX);
            graph.AddEdge(0, 2, 110, true, 1);
            graph.AddEdge(0, 2, 20, false, 1);

            // 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(2, algorithm.Best);

            Assert.AreEqual(new List <uint>(new uint[] { 0, 1, 2 }), algorithm.GetPath());

            // create algorithm and run.
            algorithm = new Itinero.Algorithms.Contracted.BidirectionalDykstra(graph,
                                                                               new EdgePath <float>[] { new EdgePath <float>(2) }, new EdgePath <float>[] { new EdgePath <float>(0) });
            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);
            Assert.AreEqual(2, algorithm.Best);

            Assert.AreEqual(new List <uint>(new uint[] { 2, 1, 3, 0 }), algorithm.GetPath());
        }