Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning.Test/Program.cs @ 10728

Last change on this file since 10728 was 8520, checked in by spimming, 12 years ago

#1894:

  • extended datasource interface to get routing graph for a specific vehicle type
  • use ICostCalculator to calculate edge weights
  • moved common enums in own file
  • removed method to estimate cost from graph; use ICostCalculator
File size: 10.7 KB
RevLine 
[8293]1
[8314]2using System;
[8509]3using System.Collections.Generic;
[8316]4using System.Diagnostics;
5using System.IO;
[8509]6using System.Linq;
[8315]7using System.Xml;
[8314]8using HeuristicLab.Algorithms.GraphRouting;
[8516]9using HeuristicLab.Algorithms.GraphRouting.Interfaces;
10using HeuristicLab.Problems.RoutePlanning.Interfaces;
11using HeuristicLab.Problems.RoutePlanning.RoutingGraph;
[8520]12using HeuristicLab.Problems.RoutePlanning.Data.Osm;
[8516]13
14// For testing openstreetmap data is used
15// http://www.openstreetmap.org
16// Map data © OpenStreetMap contributors, CC BY-SA
17
[8293]18namespace HeuristicLab.Problems.RoutePlanning.Test {
19  class Program {
20    static void Main(string[] args) {
[8316]21      string file1 = @"..\..\OsmTestFiles\test.osm";
22      string file2 = @"..\..\OsmTestFiles\testNode1.osm";
23      string file3 = @"..\..\OsmTestFiles\testWay1.osm";
24      string file4 = @"..\..\OsmTestFiles\testRelation1.osm";
[8516]25      string file5 = @"C:\dev\osmfiles\test_mid.osm";
[8316]26      string file6 = @"C:\dev\osmfiles\oberosterreich.highway.osm";
[8462]27      string file7 = @"C:\dev\osmfiles\austria.highway.osm";
[8504]28      string file8 = @"C:\dev\osmfiles\vorarlberg.highway.osm";
[8314]29
[8481]30      string fileNYArcs = @"C:\dev\DIMACSfiles\USA-road-t.NY.gr.gz";
31      string fileNYCoords = @"C:\dev\DIMACSfiles\USA-road-d.NY.co.gz";
32
[8520]33      //IDataSource ds1 = TestLoad(typeof(OsmDataSource), file6);
34      //IDataSource ds2 = TestLoad(typeof(DIMACSDataSource), fileNYCoords, fileNYArcs);
[8509]35
[8516]36      //IGraph graph = TestGetRoutingGraph(ds1);
37      //ExecuteBenchmark(graph, typeof(AStarAlgorithmV3), 262144, 20);
[8481]38
[8516]39      //IRouter router = new AStarAlgorithmV3(graph);
[8514]40      //long[] r = router.Calculate(266733493, 986825165);
[8516]41      //long[] r = router.Calculate(998717680, 986825165);
[8514]42
[8516]43      //TestRouter(new DijkstraAlgorithmV2(graph), 529102170, 1001363194, true); // inz
[8481]44
[8516]45      //TestRouter(new DijkstraAlgorithmV2(graph), 529102170, 31372732, false); // inz - hgb
46      //TestRouter(new BidrectionalDijkstraAlgorithmV2(graph), 529102170, 31372732, false); // inz - hgb
47      //TestRouter(new AStarAlgorithmV3(graph), 529102170, 31372732, false); // inz - hgb
[8408]48
[8516]49      //TestRouter(new AStarAlgorithmV3(graph), 346151602, 33196510, false); // bregenz (bahnhofstr) - wien (stepansplatz)
[8462]50
[8516]51      //TestRouter(new AStarAlgorithmV3(graph), 32044987, 261576106, false);    //vorarblberg
[8504]52
[8520]53      //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(AStarAlgorithmV3), 529102170, 1001363194, false, false); // inz
54      TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(AStarAlgorithmV3), 529102170, 31372732, false, true); // inz - hgb
[8516]55      //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(DijkstraAlgorithmV2), 529102170, 31372732, false, true); // inz - hgb
56      //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(BidrectionalDijkstraAlgorithmV2), 529102170, 31372732, false, true); // inz - hgb
57      //TestLoadAndRouter(typeof(OsmDataSource), file6, typeof(AStarAlgorithmV3), 529102170, 31372732, false, true); // inz - hgb
58      //TestLoadAndRouter(typeof(OsmDataSource), file7, typeof(AStarAlgorithmV3), 346151602, 33196510, false, true); // bregenz (bahnhofstr) - wien (stepansplatz)
[8408]59
[8520]60      //ICostCalculator costCalc = new EarthDistanceCostCalculator();
61      //IDataSource ds = new OsmDataSource(file7, costCalc);
62      //IGraph graph = ds.GetRoutingGraph();
63      //IRouter router = new AStarAlgorithmV3(graph, costCalc);
64      //Console.Write("Calculate ");
65      //var sw = Stopwatch.StartNew();
66      //long[] result = router.Calculate(346151602, 33196510);
67      //Console.WriteLine("done.");
68      //Console.WriteLine("Execution Time: {0}", sw.Elapsed);
69
70
[8314]71      System.Console.Read();
72    }
73
[8408]74    private static long[] TestRouter(IRouter router, long sourceNodeId, long targetNodeId, bool showResult) {
75      Console.WriteLine("Test Router BEGIN ---------------------------------");
76      Console.WriteLine("Type: " + router.GetType());
77
78      Console.Write("Calculate route ... ");
79      var sw = Stopwatch.StartNew();
80      long[] result = router.Calculate(sourceNodeId, targetNodeId);
81      sw.Stop();
82      Console.WriteLine("done.");
83      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
84      Console.WriteLine();
85      if (showResult) {
86        Console.WriteLine("Result: ");
87        foreach (long i in result) System.Console.Write(i + "; ");
88        Console.WriteLine();
89        Console.WriteLine();
90      }
91      Console.WriteLine("===================================================");
92      Console.WriteLine();
93      return result;
94    }
95
[8504]96    private static IDataSource TestLoad(Type dsType, params string[] paths) {
97      FileInfo file = new FileInfo(paths[0]);
[8408]98      Console.WriteLine("Test Load Data BEGIN ------------------------------");
[8316]99      Console.WriteLine("File name: {0}", file.Name);
100      Console.WriteLine();
[8314]101      Console.Write("Loading data ... ");
[8316]102      var sw = Stopwatch.StartNew();
[8504]103      IDataSource ds = (IDataSource)Activator.CreateInstance(dsType, paths);
[8316]104      sw.Stop();
[8314]105      Console.WriteLine("done.");
[8408]106      Console.WriteLine("Time: {0}", sw.Elapsed);
107      Console.WriteLine();
108      Console.WriteLine("===================================================");
109      Console.WriteLine();
[8504]110      return ds;
[8408]111    }
112
[8516]113    private static void TestLoadAndRouter(Type dsType, string filepath, Type routerType, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) {
[8408]114      FileInfo file = new FileInfo(filepath);
115      Console.WriteLine("Test Load and Route BEGIN -------------------------");
116      Console.WriteLine("File name: {0}", file.Name);
117      Console.WriteLine();
118      Console.Write("Loading data ... ");
119      var sw = Stopwatch.StartNew();
[8516]120      IDataSource ds = (IDataSource)Activator.CreateInstance(dsType, filepath);
[8408]121      sw.Stop();
122      Console.WriteLine("done.");
[8316]123      Console.WriteLine("Loading Time: {0}", sw.Elapsed);
124      Console.WriteLine();
[8516]125      IGraph graph = ds.GetRoutingGraph();
[8408]126      IRouter router = (IRouter)Activator.CreateInstance(routerType, graph);
[8314]127      Console.Write("Calculate route ... ");
[8316]128      sw = Stopwatch.StartNew();
129      long[] result = router.Calculate(sourceNodeId, targetNodeId);
130      sw.Stop();
131      Console.WriteLine("done.");
132      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
[8314]133      Console.WriteLine();
[8316]134      if (showResult) {
135        Console.WriteLine("Result: ");
136        foreach (long i in result) System.Console.Write(i + "; ");
137        Console.WriteLine();
138        Console.WriteLine();
139      }
140      if (writeResultFile) {
141        string dt = DateTime.Now.ToString("yyyyMMddhhmmss_");
142        string fn = Path.GetFileNameWithoutExtension(file.Name);
143        string resultFile = dt + fn + "_" + sourceNodeId + "-" + targetNodeId + ".gpx";
144        Console.WriteLine("Result file: {0}", resultFile);
145        WriteGPXFile(graph, result, resultFile);
146      }
[8408]147      Console.WriteLine("===================================================");
[8316]148      Console.WriteLine();
[8293]149    }
[8315]150
[8516]151    private static void WriteGPXFile(IGraph graph, long[] route, string file) {
[8315]152      XmlWriterSettings settings = new XmlWriterSettings();
153      settings.Indent = true;
154      settings.IndentChars = ("   ");
155
[8316]156      using (XmlWriter writer = XmlWriter.Create(file, settings)) {
[8315]157        writer.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/0");
158        writer.WriteAttributeString("version", "1.1");
159        writer.WriteAttributeString("creator", "cloudia");
160
161        foreach (long nodeId in route) {
[8481]162          Vertex node = graph.GetVertex(nodeId);
163          writer.WriteStartElement("wpt");
164          writer.WriteAttributeString("lat", XmlConvert.ToString(node.Latitude));
165          writer.WriteAttributeString("lon", XmlConvert.ToString(node.Logitude));
166          writer.WriteEndElement(); // wpt
167        }
168
169        writer.WriteEndElement(); // gpx
170      }
171    }
172
[8462]173    private static IGraph TestGetRoutingGraph(IDataSource ds) {
174      Console.WriteLine("Test GetRoutingGraph BEGIN ------------------------");
175      Console.WriteLine();
176      Console.Write("Construct graph ... ");
177      var sw = Stopwatch.StartNew();
178      IGraph graph = ds.GetRoutingGraph();
179      sw.Stop();
180      Console.WriteLine("done.");
181      Console.WriteLine("Time: {0}", sw.Elapsed);
182      Console.WriteLine();
183      Console.WriteLine("===================================================");
184      Console.WriteLine();
185      return graph;
186    }
[8509]187
188    private static List<Tuple<long, long>> GenerateSourceTargetPairs(IGraph graph, int locality, int noOfPairs) {
189      List<Tuple<long, long>> benchmarkPairs = new List<Tuple<long, long>>();
190      Random r = new Random();
191      DijkstraAlgorithmV2 dijkstra = new DijkstraAlgorithmV2(graph);
192      int vertexCount = graph.GetVertices().Count;
193
194      for (int i = 0; i < noOfPairs; i++) {
195        long startId = r.Next();
196        Vertex s = graph.GetVertex(startId);
197
198        while (s == null) {
199          startId = r.Next();
200          s = graph.GetVertex(startId);
201        }
202        long targetId = dijkstra.GetNodeIdWithRank(s.Id, locality);
203        Tuple<long, long> t = new Tuple<long, long>(startId, targetId);
204        benchmarkPairs.Add(t);
205      }
206      return benchmarkPairs;
207    }
208
209    private static void ExecuteBenchmark(IGraph graph, Type routerType, int locality, int noOfQueries) {
210      Console.WriteLine("Benchmark Routing Algrithm BEGIN ------------------");
211      Stopwatch sw;
212      IRouter router = (IRouter)Activator.CreateInstance(routerType, graph);
213
214      Console.Write("Generate queries ... ");
215      List<Tuple<long, long>> queries = GenerateSourceTargetPairs(graph, locality, noOfQueries);
216      Console.WriteLine("done.");
217
218      Console.Write("Execute benchmark ... ");
219      List<TimeSpan> times = new List<TimeSpan>(noOfQueries);
220      foreach (Tuple<long, long> pair in queries) {
221        sw = Stopwatch.StartNew();
222        long[] result = router.Calculate(pair.Item1, pair.Item2);
223        sw.Stop();
224        times.Add(sw.Elapsed);
225      }
226      Console.WriteLine("done.\n");
227
228      Console.WriteLine("Results:");
229      PrintStatistics(times);
230
231      Console.WriteLine("===================================================");
232    }
233
234    private static void PrintStatistics(List<TimeSpan> times) {
235      long averageTicks = Convert.ToInt64(times.Average(timeSpan => timeSpan.Ticks));
236      TimeSpan avg = new TimeSpan(averageTicks);
237      TimeSpan min = times.Min();
238      TimeSpan max = times.Max();
239      TimeSpan total = times.Aggregate(TimeSpan.Zero, (sum, value) => sum.Add(value));
240
241      Console.WriteLine("Min:\t" + min);
242      Console.WriteLine("Max:\t" + max);
243      Console.WriteLine("Avg:\t" + avg);
244      Console.WriteLine("Total:\t" + total);
245      Console.WriteLine("Count:\t" + times.Count);
246
247    }
[8293]248  }
249}
Note: See TracBrowser for help on using the repository browser.