Пример #1
0
        public void TestTwoEdgeInfiniteHops()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 2, 100, null);

            var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue);

            // calculate witness for weight of 200.
            var forwardWitnesses  = new EdgePath <float> [1];
            var backwardWitnesses = new EdgePath <float> [1];

            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 1000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(2, forwardWitnesses[0].Vertex);
            Assert.AreEqual(2, backwardWitnesses[0].Vertex);

            // calculate witness for weight of 50.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 50 }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);
        }
Пример #2
0
        public void TestOneEdgeOneHop()
        {
            // build graph.
            var graph = new DirectedGraph(ContractedEdgeDataSerializer.Size);

            graph.AddEdge(0, 1, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = null,
                Weight       = 100
            }));

            var witnessCalculator = new DykstraWitnessCalculator(1);

            // calculate witness for weight of 200.
            var forwardWitnesses  = new bool[1];
            var backwardWitnesses = new bool[1];

            witnessCalculator.Calculate(graph, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 200 }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(true, forwardWitnesses[0]);
            Assert.AreEqual(true, backwardWitnesses[0]);

            // calculate witness for weight of 50.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 50 }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);

            Assert.AreEqual(false, forwardWitnesses[0]);
            Assert.AreEqual(false, backwardWitnesses[0]);
        }
Пример #3
0
        public void TestUTurnHandling()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 3, 100, null, 1, null, null);
            graph.AddEdge(3, 0, 100, null, 1, null, null);
            graph.AddEdge(2, 3, 100, null, 1, null, null);
            graph.AddEdge(3, 2, 100, null, 1, null, null);

            // a path from 0->2 is impossible because it would involve a u-turn at vertex 3 resulting in an original path 0->1->3->1->2.
            // same for 2->0

            var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue);

            // calculate witness for weight of 200.
            var forwardWitnesses  = new EdgePath <float> [1];
            var backwardWitnesses = new EdgePath <float> [1];

            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { float.MaxValue }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // build graph.
            graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);
            graph.AddEdge(0, 3, 100, null, 1, null, new uint[] { 100 });
            graph.AddEdge(3, 0, 100, null, 1, new uint[] { 100 }, null);
            graph.AddEdge(2, 3, 100, null, 1, null, new uint[] { 100 });
            graph.AddEdge(3, 2, 100, null, 1, new uint[] { 100 }, null);

            // a path from 0->2 is impossible because it would involve a u-turn at vertex 3 resulting in an original path 0->...->100->3->100->...->2.
            // same for 2->0

            // calculate witness for weight of 200.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { float.MaxValue }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);
        }
Пример #4
0
        public void TestQuadrilateralOneWay()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 2, 100, true);
            graph.AddEdge(2, 0, 100, false);
            graph.AddEdge(0, 3, 10, false);
            graph.AddEdge(3, 0, 10, true);
            graph.AddEdge(1, 2, 1000, false);
            graph.AddEdge(2, 1, 1000, true);
            graph.AddEdge(1, 3, 10000, true);
            graph.AddEdge(3, 1, 10000, false);
            graph.Compress(false);

            var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue);

            // calculate witnesses for 0.
            var forwardWitnesses  = new EdgePath <float> [1];
            var backwardWitnesses = new EdgePath <float> [1];

            witnessCalculator.Calculate(graph, (i) => null, 2, new List <uint>(new uint[] { 3 }), new List <float>(new float[] { 110 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 0);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 0.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 3, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 110 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 0);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 2.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 1100 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 2);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 2.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 1, new List <uint>(new uint[] { 0 }), new List <float>(new float[] { 1100 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 2);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 1.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 3, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 11000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 1);
            Assert.AreEqual(2, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 1.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 2, new List <uint>(new uint[] { 3 }), new List <float>(new float[] { 11000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 1);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(3, backwardWitnesses[0].Vertex);

            // calculate witnesses for 3.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 10010 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 3);
            Assert.AreEqual(1, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 3.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 1, new List <uint>(new uint[] { 0 }), new List <float>(new float[] { 10010 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 3);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(0, backwardWitnesses[0].Vertex);
        }
Пример #5
0
        public void TestQuadrilateralOneWay()
        {
            // build graph.
            var graph = new DirectedGraph(ContractedEdgeDataSerializer.Size);

            graph.AddEdge(0, 2, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = true,
                Weight       = 100
            }));
            graph.AddEdge(2, 0, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = false,
                Weight       = 100
            }));
            graph.AddEdge(0, 3, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = false,
                Weight       = 10
            }));
            graph.AddEdge(3, 0, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = true,
                Weight       = 10
            }));
            graph.AddEdge(1, 2, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = false,
                Weight       = 1000
            }));
            graph.AddEdge(2, 1, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = true,
                Weight       = 1000
            }));
            graph.AddEdge(1, 3, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = true,
                Weight       = 10000
            }));
            graph.AddEdge(3, 1, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData()
            {
                ContractedId = Constants.NO_VERTEX,
                Direction    = false,
                Weight       = 10000
            }));
            graph.Compress(false);

            var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue);

            // calculate witnesses for 0.
            var forwardWitnesses  = new bool[1];
            var backwardWitnesses = new bool[1];

            witnessCalculator.Calculate(graph, 2, new List <uint>(new uint[] { 3 }), new List <float>(new float[] { 110 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 0);
            Assert.AreEqual(false, forwardWitnesses[0]);
            Assert.AreEqual(false, backwardWitnesses[0]);

            // calculate witnesses for 0.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 3, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 110 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 0);
            Assert.AreEqual(false, forwardWitnesses[0]);
            Assert.AreEqual(false, backwardWitnesses[0]);

            // calculate witnesses for 2.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 1100 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 2);
            Assert.AreEqual(false, forwardWitnesses[0]);
            Assert.AreEqual(false, backwardWitnesses[0]);

            // calculate witnesses for 2.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 1, new List <uint>(new uint[] { 0 }), new List <float>(new float[] { 1100 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 2);
            Assert.AreEqual(false, forwardWitnesses[0]);
            Assert.AreEqual(false, backwardWitnesses[0]);

            // calculate witnesses for 1.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 3, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 11000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 1);
            Assert.AreEqual(true, forwardWitnesses[0]);
            Assert.AreEqual(false, backwardWitnesses[0]);

            // calculate witnesses for 1.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 2, new List <uint>(new uint[] { 3 }), new List <float>(new float[] { 11000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 1);
            Assert.AreEqual(false, forwardWitnesses[0]);
            Assert.AreEqual(true, backwardWitnesses[0]);

            // calculate witnesses for 3.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 10010 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 3);
            Assert.AreEqual(true, forwardWitnesses[0]);
            Assert.AreEqual(false, backwardWitnesses[0]);

            // calculate witnesses for 3.
            forwardWitnesses  = new bool[1];
            backwardWitnesses = new bool[1];
            witnessCalculator.Calculate(graph, 1, new List <uint>(new uint[] { 0 }), new List <float>(new float[] { 10010 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 3);
            Assert.AreEqual(false, forwardWitnesses[0]);
            Assert.AreEqual(true, backwardWitnesses[0]);
        }
Пример #6
0
 /// <summary>
 /// Removes witnessed shortcuts.
 /// </summary>
 public static void RemoveWitnessed <T>(this Shortcuts <T> shortcuts, uint vertex, DykstraWitnessCalculator <T> witnessCalculator)
     where T : struct
 {
     witnessCalculator.Calculate(vertex, shortcuts);
 }