public void TestPriorityCalculation1NoWitnesses() { var graph = new MemoryDirectedGraph <CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue); var priorityCalculator = new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator); int newEdges, removedEdges, contracted, depth; priorityCalculator.Calculate(vertex1, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(0, newEdges); Assert.AreEqual(1, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); priorityCalculator.Calculate(vertex2, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(0, newEdges); Assert.AreEqual(1, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); priorityCalculator.Calculate(vertex3, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(2, newEdges); Assert.AreEqual(2, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); }
/// <summary> /// Creates a router. /// </summary> /// <param name="interpreter"></param> /// <param name="manifestResourceName"></param> /// <returns></returns> protected override Router CreateRouter(IOsmRoutingInterpreter interpreter, string manifestResourceName) { TagsTableCollectionIndex tagsIndex = new TagsTableCollectionIndex(); // do the data processing. var data = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( data, interpreter, tagsIndex, Vehicle.Car); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestResourceName)); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); // do the pre-processing part. var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(data, new EdgeDifference(data, witnessCalculator), witnessCalculator); preProcessor.Start(); //IBasicRouter<LiveEdge> basicRouter = new DykstraRoutingLive(memoryData.TagsIndex); return(Router.CreateCHFrom(data, new CHRouter(), interpreter)); }
/// <summary> /// Executes the CH contractions while verifying each step. /// </summary> /// <param name="stream"></param> public void DoTestCHEdgeDifferenceVerifiedContraction(Stream stream) { _interpreter = new OsmRoutingInterpreter(); var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. _data = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( _data, _interpreter, tagsIndex, Vehicle.Car); var dataProcessorSource = new XmlOsmStreamSource(stream); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); // do the pre-processing part. var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(_data, new EdgeDifference(_data, witnessCalculator), witnessCalculator); preProcessor.OnBeforeContractionEvent += new CHPreProcessor.VertexDelegate(pre_processor_OnBeforeContractionEvent); preProcessor.OnAfterContractionEvent += new CHPreProcessor.VertexDelegate(pre_processor_OnAfterContractionEvent); preProcessor.Start(); }
public void TestVerifiedContraction1NoWitnesses() { var graph = new MemoryDirectedGraph <CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); preProcessor.Contract(3); // there should be no edge from 1->3 and from 2->3. Assert.IsFalse(graph.ContainsEdges(1, 3)); Assert.IsFalse(graph.ContainsEdges(2, 3)); var router = new CHRouter(); // expected: (1)-10s-(3)-10s-(2) (20s in total). var path = router.Calculate(graph, 1, 2); Assert.IsNotNull(path); Assert.AreEqual(20, path.Weight); var pathArray = path.ToArrayWithWeight(); Assert.AreEqual(3, pathArray.Length); float latitude, longitude; Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); // expected: (2)-10s-(3)-10s-(1) (20s in total). path = router.Calculate(graph, 2, 1); Assert.IsNotNull(path); Assert.AreEqual(20, path.Weight); pathArray = path.ToArrayWithWeight(); Assert.AreEqual(3, pathArray.Length); Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); }
/// <summary> /// Builds the edge difference. /// </summary> private EdgeDifference BuildEdgeDifference(IOsmRoutingInterpreter interpreter) { DynamicGraphRouterDataSource <CHEdgeData> data = this.BuildData(interpreter); // do the pre-processing part. INodeWitnessCalculator witnessCalculator = new DykstraWitnessCalculator(); return(new EdgeDifference( data, witnessCalculator)); }
/// <summary> /// Builds the edge difference. /// </summary> private CHPreProcessor BuildCHPreProcessor(IOsmRoutingInterpreter interpreter) { DynamicGraphRouterDataSource <CHEdgeData> data = this.BuildData(interpreter); // do the pre-processing part. INodeWitnessCalculator witnessCalculator = new DykstraWitnessCalculator(); var edgeDifference = new EdgeDifference( data, witnessCalculator); var preProcessor = new CHPreProcessor( data, edgeDifference, witnessCalculator); return(preProcessor); }
public void TestTwoOnewayEdgeInfiniteHops() { // build graph. var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize); graph.AddEdge(0, 1, 100, true); graph.AddEdge(1, 2, 100, true); graph.AddEdge(0, 2, 300, true); 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(Constants.NO_VERTEX, 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); // build graph. graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize); graph.AddEdge(1, 0, 100, true); graph.AddEdge(2, 1, 100, true); graph.AddEdge(2, 0, 100, true); // calculate witness for weight of 200. forwardWitnesses = new EdgePath <float> [1]; backwardWitnesses = new EdgePath <float> [1]; witnessCalculator.Calculate(graph, (i) => null, 2, new List <uint>(new uint[] { 0 }), new List <float>(new float[] { 1000 }), ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue); Assert.AreEqual(0, forwardWitnesses[0].Vertex); Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex); // calculate witness for weight of 50. forwardWitnesses = new EdgePath <float> [1]; backwardWitnesses = new EdgePath <float> [1]; witnessCalculator.Calculate(graph, (i) => null, 2, new List <uint>(new uint[] { 0 }), 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); }
/// <summary> /// Creates a new hierarchy builder. /// </summary> public HierarchyBuilder(DirectedMetaGraph graph, DykstraWitnessCalculator <T> witnessCalculator, WeightHandler <T> weightHandler) { weightHandler.CheckCanUse(graph); _graph = graph; _witnessCalculator = witnessCalculator; _weightHandler = weightHandler; _vertexInfo = new VertexInfo <T>(); _depth = new Dictionary <long, int>(); _contractionCount = new Dictionary <uint, int>(); this.DifferenceFactor = 5; this.DepthFactor = 5; this.ContractedFactor = 8; }
public void TestCHContractionTest2() { // // (-1/1)-------->(-3/2)------->(-5/3) // // build the data. DynamicGraphRouterDataSource <CHEdgeData> data = this.BuildData( Assembly.GetExecutingAssembly().GetManifestResourceStream( "OsmSharp.Test.Unittests.Routing.CH.Contraction.contraction_test2.osm")); // build the witness calculator. INodeWitnessCalculator witness_calculator = new DykstraWitnessCalculator(); // test the ordering operators. SparseOrdering sparse_ordering = new SparseOrdering( data); Assert.AreEqual(-1, sparse_ordering.Calculate(2)); EdgeDifference edge_difference_ordering = new EdgeDifference( data, witness_calculator); Assert.AreEqual(0, edge_difference_ordering.Calculate(2)); // do the actual contraction. CHPreProcessor pre_processor = new CHPreProcessor( data, edge_difference_ordering, witness_calculator); pre_processor.Contract(2); // check the neighbours of each vertex. HashSet <uint> neighbours = this.BuildNeighboursSet(data.GetArcs(2)); Assert.IsTrue(neighbours.Contains(1)); Assert.IsTrue(neighbours.Contains(3)); neighbours = this.BuildNeighboursSet(data.GetArcs(1)); Assert.IsTrue(neighbours.Contains(3)); Assert.IsFalse(neighbours.Contains(2)); neighbours = this.BuildNeighboursSet(data.GetArcs(3)); Assert.IsTrue(neighbours.Contains(1)); Assert.IsFalse(neighbours.Contains(2)); }
public void TestWithSimpleRestriction() { // 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) => { return(new uint[][] { new uint[] { 0, 1, 2 } }); }, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 1000 }), ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue); Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex); Assert.AreEqual(2, backwardWitnesses[0].Vertex); }
/// <summary> /// Flushes all data. /// </summary> public override void Flush() { base.Flush(); // compress the graph. INodeWitnessCalculator witnessCalculator = new DykstraWitnessCalculator(); var edgeDifference = new EdgeDifference( _graph, witnessCalculator); var preProcessor = new CHPreProcessor( _graph, edgeDifference, witnessCalculator); preProcessor.Start(); // create serializer. var routingSerializer = new CHEdgeDataDataSourceSerializer(true); routingSerializer.Serialize(_graphStream, _graph); _graphStream.Flush(); }
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); }
/// <summary> /// Preprocesses the data from the given OsmStreamReader and converts it directly to a routable data source. /// </summary> /// <param name="reader"></param> /// <param name="tagsIndex"></param> /// <param name="interpreter"></param> /// <param name="vehicle"></param> /// <returns></returns> public static DynamicGraphRouterDataSource <CHEdgeData> Preprocess(OsmStreamSource reader, ITagsCollectionIndex tagsIndex, IOsmRoutingInterpreter interpreter, Vehicle vehicle) { // pull in the data. var graph = new DynamicGraphRouterDataSource <CHEdgeData>(new MemoryDirectedGraph <CHEdgeData>(), tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( graph, interpreter, tagsIndex, vehicle); targetData.RegisterSource(reader); targetData.Pull(); // compress the graph. var witnessCalculator = new DykstraWitnessCalculator(); var edgeDifference = new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator); var preProcessor = new CHPreProcessor(graph, edgeDifference, witnessCalculator); preProcessor.Start(); return(graph); }
/// <summary> /// Flushes all data. /// </summary> public override void Flush() { base.Flush(); // compress the graph. INodeWitnessCalculator witnessCalculator = new DykstraWitnessCalculator(); var edgeDifference = new EdgeDifferenceContractedSearchSpace( _graph, witnessCalculator); var preProcessor = new CHPreProcessor( _graph, edgeDifference, witnessCalculator); preProcessor.Start(); // create tags. TagsCollectionBase metaTags = new TagsCollection(); // create serializer. var routingSerializer = new CHEdgeDataDataSourceSerializer(); routingSerializer.Serialize(_graphStream, _graph, metaTags); _graphStream.Flush(); }
public void TestTwoEdgeInfiniteHops() { // build graph. var graph = new DirectedGraph(ContractedEdgeDataSerializer.Size); graph.AddEdge(0, 1, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData() { ContractedId = Constants.NO_VERTEX, Direction = null, Weight = 100 })); graph.AddEdge(1, 2, ContractedEdgeDataSerializer.SerializeMeta(new ContractedEdgeData() { ContractedId = Constants.NO_VERTEX, Direction = null, Weight = 100 })); var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue); // 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[] { 2 }), new List <float>(new float[] { 1000 }), 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[] { 2 }), new List <float>(new float[] { 50 }), ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue); Assert.AreEqual(false, forwardWitnesses[0]); Assert.AreEqual(false, backwardWitnesses[0]); }
/// <summary> /// Returns a new router. /// </summary> /// <param name="interpreter"></param> /// <param name="embeddedName"></param> /// <param name="contract"></param> /// <returns></returns> public override Router BuildRouter(IOsmRoutingInterpreter interpreter, string embeddedName, bool contract) { if (_data == null) { _data = new Dictionary <string, DynamicGraphRouterDataSource <CHEdgeData> >(); } DynamicGraphRouterDataSource <CHEdgeData> data = null; if (!_data.TryGetValue(embeddedName, out data)) { var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. data = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( data, interpreter, tagsIndex, Vehicle.Car); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format( "OsmSharp.Test.Unittests.{0}", embeddedName))); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); if (contract) { // do the pre-processing part. var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(data, new EdgeDifference(data, witnessCalculator), witnessCalculator); preProcessor.Start(); } _data[embeddedName] = data; } return(Router.CreateCHFrom(data, new CHRouter(), interpreter)); }
/// <summary> /// Preprocesses the data from the given OsmStreamReader and converts it directly to a routable data source. /// </summary> /// <param name="reader"></param> /// <param name="tagsIndex"></param> /// <param name="interpreter"></param> /// <param name="vehicle"></param> /// <param name="keepDirectNeighbours"></param> /// <returns></returns> public static DynamicGraphRouterDataSource <CHEdgeData> Preprocess(OsmStreamSource reader, ITagsCollectionIndex tagsIndex, IOsmRoutingInterpreter interpreter, Vehicle vehicle, bool keepDirectNeighbours) { // pull in the data. var dynamicGraphRouterDataSource = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( dynamicGraphRouterDataSource, interpreter, tagsIndex, vehicle); targetData.RegisterSource(reader); targetData.Pull(); // compress the graph. INodeWitnessCalculator witnessCalculator = new DykstraWitnessCalculator(); var edgeDifference = new EdgeDifference( dynamicGraphRouterDataSource, witnessCalculator); var preProcessor = new CHPreProcessor( dynamicGraphRouterDataSource, edgeDifference, witnessCalculator, keepDirectNeighbours); preProcessor.Start(); return(dynamicGraphRouterDataSource); }
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]); }
public void TestCHContractionTest1() { // // (-1/1)---------(-3/2)--------(-5/3) // // build the data. var data = this.BuildData( Assembly.GetExecutingAssembly().GetManifestResourceStream( "OsmSharp.Test.Unittests.Routing.CH.Contraction.contraction_test1.osm")); // build the witness calculator. var witnessCalculator = new DykstraWitnessCalculator(); // test the ordering operators. var sparseOrdering = new SparseOrdering( data); Assert.AreEqual(-1, sparseOrdering.Calculate(2)); var edgeDifferenceOrdering = new EdgeDifference( data, witnessCalculator); Assert.AreEqual(2, edgeDifferenceOrdering.Calculate(2)); // do the actual contraction. var preProcessor = new CHPreProcessor( data, edgeDifferenceOrdering, witnessCalculator); preProcessor.Contract(2); // check the neighbours of each vertex. var neighbours = data.GetEdges(2); Assert.AreEqual(2, neighbours.Length); Assert.IsTrue(neighbours.Any((x) => { return(x.Key == 1 && x.Value.ToHigher); })); Assert.IsTrue(neighbours.Any((x) => { return(x.Key == 3 && x.Value.ToHigher); })); neighbours = data.GetEdges(1); Assert.AreEqual(2, neighbours.Length); Assert.IsTrue(neighbours.Any((x) => { return(x.Key == 2 && x.Value.ToLower); })); Assert.IsTrue(neighbours.Any((x) => { return(x.Key == 3 && !x.Value.ToHigher && !x.Value.ToLower); })); neighbours = data.GetEdges(3); Assert.AreEqual(2, neighbours.Length); Assert.IsTrue(neighbours.Any((x) => { return(x.Key == 2 && x.Value.ToLower); })); Assert.IsTrue(neighbours.Any((x) => { return(x.Key == 1 && !x.Value.ToHigher && !x.Value.ToLower); })); }
public void TestVerifiedContraction3TinyOneWay() { var graph = new MemoryDirectedGraph <CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); var vertex4 = graph.AddVertex(4, 0); var vertex5 = graph.AddVertex(5, 0); graph.AddEdge(vertex1, vertex4, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex4, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex1, vertex2, new CHEdgeData(1, true, true, false, 10)); // oneway forward graph.AddEdge(vertex2, vertex1, new CHEdgeData(1, false, false, true, 10)); // oneway backward. graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex5, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex5, vertex2, new CHEdgeData(1, false, true, true, 10)); Assert.IsFalse(graph.ContainsEdges(vertex1, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex2, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex3, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex4, vertex1)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex1, vertex2)); Assert.IsFalse(graph.ContainsEdges(vertex2, vertex2)); Assert.IsTrue(graph.ContainsEdges(vertex3, vertex2)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex2)); Assert.IsTrue(graph.ContainsEdges(vertex5, vertex2)); Assert.IsTrue(graph.ContainsEdges(vertex1, vertex3)); Assert.IsTrue(graph.ContainsEdges(vertex2, vertex3)); Assert.IsFalse(graph.ContainsEdges(vertex3, vertex3)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex3)); Assert.IsTrue(graph.ContainsEdges(vertex1, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex2, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex3, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex1, vertex5)); Assert.IsTrue(graph.ContainsEdges(vertex2, vertex5)); Assert.IsFalse(graph.ContainsEdges(vertex3, vertex5)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex5)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex5)); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); preProcessor.Start(); var router = new CHRouter(); // expected: (4)-10s-(1)-10s-(2)-10s-(3) (30s in total). var path = router.Calculate(graph, 4, 5); Assert.IsNotNull(path); Assert.AreEqual(30, path.Weight); var pathArray = path.ToArrayWithWeight(); Assert.AreEqual(4, pathArray.Length); float latitude, longitude; Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(4, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); Assert.AreEqual(30, pathArray[3].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[3].Item1, out latitude, out longitude)); Assert.AreEqual(5, latitude); // expected: (5)-10s-(2)-10s-(3)-10s-(1)-10s-(4) (40s in total). path = router.Calculate(graph, 5, 4); Assert.IsNotNull(path); Assert.AreEqual(40, path.Weight); pathArray = path.ToArrayWithWeight(); Assert.AreEqual(5, pathArray.Length); Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(5, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(30, pathArray[3].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[3].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); Assert.AreEqual(40, pathArray[4].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[4].Item1, out latitude, out longitude)); Assert.AreEqual(4, latitude); }
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); }
/// <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); }