using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Xml; using HeuristicLab.Algorithms.GraphRouting; using HeuristicLab.Algorithms.GraphRouting.Interfaces; using HeuristicLab.Problems.RoutePlanning.Interfaces; using HeuristicLab.Problems.RoutePlanning.RoutingGraph; using HeuristicLab.Problems.RoutePlanning.Data.Osm; // For testing openstreetmap data is used // http://www.openstreetmap.org // Map data © OpenStreetMap contributors, CC BY-SA namespace HeuristicLab.Problems.RoutePlanning.Test { class Program { static void Main(string[] args) { string file1 = @"..\..\OsmTestFiles\test.osm"; string file2 = @"..\..\OsmTestFiles\testNode1.osm"; string file3 = @"..\..\OsmTestFiles\testWay1.osm"; string file4 = @"..\..\OsmTestFiles\testRelation1.osm"; string file5 = @"C:\dev\osmfiles\test_mid.osm"; string file6 = @"C:\dev\osmfiles\oberosterreich.highway.osm"; string file7 = @"C:\dev\osmfiles\austria.highway.osm"; string file8 = @"C:\dev\osmfiles\vorarlberg.highway.osm"; string fileNYArcs = @"C:\dev\DIMACSfiles\USA-road-t.NY.gr.gz"; string fileNYCoords = @"C:\dev\DIMACSfiles\USA-road-d.NY.co.gz"; //IDataSource ds1 = TestLoad(typeof(OsmDataSource), file6); //IDataSource ds2 = TestLoad(typeof(DIMACSDataSource), fileNYCoords, fileNYArcs); //IGraph graph = TestGetRoutingGraph(ds1); //ExecuteBenchmark(graph, typeof(AStarAlgorithmV3), 262144, 20); //IRouter router = new AStarAlgorithmV3(graph); //long[] r = router.Calculate(266733493, 986825165); //long[] r = router.Calculate(998717680, 986825165); //TestRouter(new DijkstraAlgorithmV2(graph), 529102170, 1001363194, true); // inz //TestRouter(new DijkstraAlgorithmV2(graph), 529102170, 31372732, false); // inz - hgb //TestRouter(new BidrectionalDijkstraAlgorithmV2(graph), 529102170, 31372732, false); // inz - hgb //TestRouter(new AStarAlgorithmV3(graph), 529102170, 31372732, false); // inz - hgb //TestRouter(new AStarAlgorithmV3(graph), 346151602, 33196510, false); // bregenz (bahnhofstr) - wien (stepansplatz) //TestRouter(new AStarAlgorithmV3(graph), 32044987, 261576106, false); //vorarblberg //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(AStarAlgorithmV3), 529102170, 1001363194, false, false); // inz TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(AStarAlgorithmV3), 529102170, 31372732, false, true); // inz - hgb //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(DijkstraAlgorithmV2), 529102170, 31372732, false, true); // inz - hgb //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(BidrectionalDijkstraAlgorithmV2), 529102170, 31372732, false, true); // inz - hgb //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(AStarAlgorithmV3), 529102170, 31372732, false, true); // inz - hgb //TestLoadAndRouter(typeof(OsmDataSource), file7, typeof(AStarAlgorithmV3), 346151602, 33196510, false, true); // bregenz (bahnhofstr) - wien (stepansplatz) //ICostCalculator costCalc = new EarthDistanceCostCalculator(); //IDataSource ds = new OsmDataSource(file7, costCalc); //IGraph graph = ds.GetRoutingGraph(); //IRouter router = new AStarAlgorithmV3(graph, costCalc); //Console.Write("Calculate "); //var sw = Stopwatch.StartNew(); //long[] result = router.Calculate(346151602, 33196510); //Console.WriteLine("done."); //Console.WriteLine("Execution Time: {0}", sw.Elapsed); System.Console.Read(); } private static long[] TestRouter(IRouter router, long sourceNodeId, long targetNodeId, bool showResult) { Console.WriteLine("Test Router BEGIN ---------------------------------"); Console.WriteLine("Type: " + router.GetType()); Console.Write("Calculate route ... "); var sw = Stopwatch.StartNew(); long[] result = router.Calculate(sourceNodeId, targetNodeId); sw.Stop(); Console.WriteLine("done."); Console.WriteLine("Execution Time: {0}", sw.Elapsed); Console.WriteLine(); if (showResult) { Console.WriteLine("Result: "); foreach (long i in result) System.Console.Write(i + "; "); Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("==================================================="); Console.WriteLine(); return result; } private static IDataSource TestLoad(Type dsType, params string[] paths) { FileInfo file = new FileInfo(paths[0]); Console.WriteLine("Test Load Data BEGIN ------------------------------"); Console.WriteLine("File name: {0}", file.Name); Console.WriteLine(); Console.Write("Loading data ... "); var sw = Stopwatch.StartNew(); IDataSource ds = (IDataSource)Activator.CreateInstance(dsType, paths); sw.Stop(); Console.WriteLine("done."); Console.WriteLine("Time: {0}", sw.Elapsed); Console.WriteLine(); Console.WriteLine("==================================================="); Console.WriteLine(); return ds; } private static void TestLoadAndRouter(Type dsType, string filepath, Type routerType, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) { FileInfo file = new FileInfo(filepath); Console.WriteLine("Test Load and Route BEGIN -------------------------"); Console.WriteLine("File name: {0}", file.Name); Console.WriteLine(); Console.Write("Loading data ... "); var sw = Stopwatch.StartNew(); IDataSource ds = (IDataSource)Activator.CreateInstance(dsType, filepath); sw.Stop(); Console.WriteLine("done."); Console.WriteLine("Loading Time: {0}", sw.Elapsed); Console.WriteLine(); IGraph graph = ds.GetRoutingGraph(); IRouter router = (IRouter)Activator.CreateInstance(routerType, graph); Console.Write("Calculate route ... "); sw = Stopwatch.StartNew(); long[] result = router.Calculate(sourceNodeId, targetNodeId); sw.Stop(); Console.WriteLine("done."); Console.WriteLine("Execution Time: {0}", sw.Elapsed); Console.WriteLine(); if (showResult) { Console.WriteLine("Result: "); foreach (long i in result) System.Console.Write(i + "; "); Console.WriteLine(); Console.WriteLine(); } if (writeResultFile) { string dt = DateTime.Now.ToString("yyyyMMddhhmmss_"); string fn = Path.GetFileNameWithoutExtension(file.Name); string resultFile = dt + fn + "_" + sourceNodeId + "-" + targetNodeId + ".gpx"; Console.WriteLine("Result file: {0}", resultFile); WriteGPXFile(graph, result, resultFile); } Console.WriteLine("==================================================="); Console.WriteLine(); } private static void WriteGPXFile(IGraph graph, long[] route, string file) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); using (XmlWriter writer = XmlWriter.Create(file, settings)) { writer.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/0"); writer.WriteAttributeString("version", "1.1"); writer.WriteAttributeString("creator", "cloudia"); foreach (long nodeId in route) { Vertex node = graph.GetVertex(nodeId); writer.WriteStartElement("wpt"); writer.WriteAttributeString("lat", XmlConvert.ToString(node.Latitude)); writer.WriteAttributeString("lon", XmlConvert.ToString(node.Logitude)); writer.WriteEndElement(); // wpt } writer.WriteEndElement(); // gpx } } private static IGraph TestGetRoutingGraph(IDataSource ds) { Console.WriteLine("Test GetRoutingGraph BEGIN ------------------------"); Console.WriteLine(); Console.Write("Construct graph ... "); var sw = Stopwatch.StartNew(); IGraph graph = ds.GetRoutingGraph(); sw.Stop(); Console.WriteLine("done."); Console.WriteLine("Time: {0}", sw.Elapsed); Console.WriteLine(); Console.WriteLine("==================================================="); Console.WriteLine(); return graph; } private static List> GenerateSourceTargetPairs(IGraph graph, int locality, int noOfPairs) { List> benchmarkPairs = new List>(); Random r = new Random(); DijkstraAlgorithmV2 dijkstra = new DijkstraAlgorithmV2(graph); int vertexCount = graph.GetVertices().Count; for (int i = 0; i < noOfPairs; i++) { long startId = r.Next(); Vertex s = graph.GetVertex(startId); while (s == null) { startId = r.Next(); s = graph.GetVertex(startId); } long targetId = dijkstra.GetNodeIdWithRank(s.Id, locality); Tuple t = new Tuple(startId, targetId); benchmarkPairs.Add(t); } return benchmarkPairs; } private static void ExecuteBenchmark(IGraph graph, Type routerType, int locality, int noOfQueries) { Console.WriteLine("Benchmark Routing Algrithm BEGIN ------------------"); Stopwatch sw; IRouter router = (IRouter)Activator.CreateInstance(routerType, graph); Console.Write("Generate queries ... "); List> queries = GenerateSourceTargetPairs(graph, locality, noOfQueries); Console.WriteLine("done."); Console.Write("Execute benchmark ... "); List times = new List(noOfQueries); foreach (Tuple pair in queries) { sw = Stopwatch.StartNew(); long[] result = router.Calculate(pair.Item1, pair.Item2); sw.Stop(); times.Add(sw.Elapsed); } Console.WriteLine("done.\n"); Console.WriteLine("Results:"); PrintStatistics(times); Console.WriteLine("==================================================="); } private static void PrintStatistics(List times) { long averageTicks = Convert.ToInt64(times.Average(timeSpan => timeSpan.Ticks)); TimeSpan avg = new TimeSpan(averageTicks); TimeSpan min = times.Min(); TimeSpan max = times.Max(); TimeSpan total = times.Aggregate(TimeSpan.Zero, (sum, value) => sum.Add(value)); Console.WriteLine("Min:\t" + min); Console.WriteLine("Max:\t" + max); Console.WriteLine("Avg:\t" + avg); Console.WriteLine("Total:\t" + total); Console.WriteLine("Count:\t" + times.Count); } } }