Ejemplo n.º 1
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]);
        }
Ejemplo n.º 2
0
        public void TestSerialize()
        {
            var edgeData = new ContractedEdgeData()
            {
                Weight       = 100f,
                Direction    = null,
                ContractedId = Constants.NO_VERTEX
            };

            var data = ContractedEdgeDataSerializer.SerializeMeta(edgeData);

            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(((uint)(100.0f * 4 * ContractedEdgeDataSerializer.PRECISION_FACTOR)), data[0]);
            Assert.AreEqual(Constants.NO_VERTEX, data[1]);

            edgeData = new ContractedEdgeData()
            {
                Weight       = ContractedEdgeDataSerializer.MAX_DISTANCE,
                Direction    = true,
                ContractedId = Constants.NO_VERTEX
            };

            data = ContractedEdgeDataSerializer.SerializeMeta(edgeData);
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(((uint)1) + (((uint)(ContractedEdgeDataSerializer.MAX_DISTANCE * 4 * ContractedEdgeDataSerializer.PRECISION_FACTOR))), data[0]);
            Assert.AreEqual(Constants.NO_VERTEX, data[1]);

            edgeData = new ContractedEdgeData()
            {
                Weight       = 100.25f,
                Direction    = null,
                ContractedId = Constants.NO_VERTEX
            };

            data = ContractedEdgeDataSerializer.SerializeMeta(edgeData);
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(((uint)(100.25f * ContractedEdgeDataSerializer.PRECISION_FACTOR) * 4), data[0]);
            Assert.AreEqual(Constants.NO_VERTEX, data[1]);

            edgeData = new ContractedEdgeData()
            {
                Weight       = 100,
                Direction    = false,
                ContractedId = Constants.NO_VERTEX
            };

            data = ContractedEdgeDataSerializer.SerializeMeta(edgeData);
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(((uint)2) + (((uint)(100.0f * 4 * ContractedEdgeDataSerializer.PRECISION_FACTOR))), data[0]);
            Assert.AreEqual(Constants.NO_VERTEX, data[1]);

            edgeData = new ContractedEdgeData()
            {
                Weight       = 100,
                Direction    = true,
                ContractedId = Constants.NO_VERTEX
            };

            data = ContractedEdgeDataSerializer.SerializeMeta(edgeData);
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(((uint)1) + (((uint)(100.0f * ContractedEdgeDataSerializer.PRECISION_FACTOR) * 4)), data[0]);
            Assert.AreEqual(Constants.NO_VERTEX, data[1]);

            edgeData = new ContractedEdgeData()
            {
                Weight       = ContractedEdgeDataSerializer.MAX_DISTANCE,
                Direction    = false,
                ContractedId = Constants.NO_VERTEX
            };

            data = ContractedEdgeDataSerializer.SerializeMeta(edgeData);
            Assert.IsNotNull(data);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(((uint)2) + (((uint)(ContractedEdgeDataSerializer.MAX_DISTANCE * 4 * ContractedEdgeDataSerializer.PRECISION_FACTOR))), data[0]);
            Assert.AreEqual(Constants.NO_VERTEX, data[1]);

            edgeData = new ContractedEdgeData()
            {
                Weight       = ContractedEdgeDataSerializer.MAX_DISTANCE + 100,
                Direction    = true,
                ContractedId = Constants.NO_VERTEX
            };
            Assert.Catch <ArgumentOutOfRangeException>(() =>
            {
                ContractedEdgeDataSerializer.SerializeMeta(edgeData);
            });
        }
Ejemplo n.º 3
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]);
        }