Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1894_RoutePlanning/HeuristicLab.Problems.RoutePlanning.Test/Program.cs @ 17785

Last change on this file since 17785 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
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.IO;
6using System.Linq;
7using System.Xml;
8using HeuristicLab.Algorithms.GraphRouting;
9using HeuristicLab.Algorithms.GraphRouting.Interfaces;
10using HeuristicLab.Problems.RoutePlanning.Interfaces;
11using HeuristicLab.Problems.RoutePlanning.RoutingGraph;
12using HeuristicLab.Problems.RoutePlanning.Data.Osm;
13
14// For testing openstreetmap data is used
15// http://www.openstreetmap.org
16// Map data © OpenStreetMap contributors, CC BY-SA
17
18namespace HeuristicLab.Problems.RoutePlanning.Test {
19  class Program {
20    static void Main(string[] args) {
21      string file1 = @"..\..\OsmTestFiles\test.osm";
22      string file2 = @"..\..\OsmTestFiles\testNode1.osm";
23      string file3 = @"..\..\OsmTestFiles\testWay1.osm";
24      string file4 = @"..\..\OsmTestFiles\testRelation1.osm";
25      string file5 = @"C:\dev\osmfiles\test_mid.osm";
26      string file6 = @"C:\dev\osmfiles\oberosterreich.highway.osm";
27      string file7 = @"C:\dev\osmfiles\austria.highway.osm";
28      string file8 = @"C:\dev\osmfiles\vorarlberg.highway.osm";
29
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
33      //IDataSource ds1 = TestLoad(typeof(OsmDataSource), file6);
34      //IDataSource ds2 = TestLoad(typeof(DIMACSDataSource), fileNYCoords, fileNYArcs);
35
36      //IGraph graph = TestGetRoutingGraph(ds1);
37      //ExecuteBenchmark(graph, typeof(AStarAlgorithmV3), 262144, 20);
38
39      //IRouter router = new AStarAlgorithmV3(graph);
40      //long[] r = router.Calculate(266733493, 986825165);
41      //long[] r = router.Calculate(998717680, 986825165);
42
43      //TestRouter(new DijkstraAlgorithmV2(graph), 529102170, 1001363194, true); // inz
44
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
48
49      //TestRouter(new AStarAlgorithmV3(graph), 346151602, 33196510, false); // bregenz (bahnhofstr) - wien (stepansplatz)
50
51      //TestRouter(new AStarAlgorithmV3(graph), 32044987, 261576106, false);    //vorarblberg
52
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
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)
59
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
71      System.Console.Read();
72    }
73
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
96    private static IDataSource TestLoad(Type dsType, params string[] paths) {
97      FileInfo file = new FileInfo(paths[0]);
98      Console.WriteLine("Test Load Data BEGIN ------------------------------");
99      Console.WriteLine("File name: {0}", file.Name);
100      Console.WriteLine();
101      Console.Write("Loading data ... ");
102      var sw = Stopwatch.StartNew();
103      IDataSource ds = (IDataSource)Activator.CreateInstance(dsType, paths);
104      sw.Stop();
105      Console.WriteLine("done.");
106      Console.WriteLine("Time: {0}", sw.Elapsed);
107      Console.WriteLine();
108      Console.WriteLine("===================================================");
109      Console.WriteLine();
110      return ds;
111    }
112
113    private static void TestLoadAndRouter(Type dsType, string filepath, Type routerType, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) {
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();
120      IDataSource ds = (IDataSource)Activator.CreateInstance(dsType, filepath);
121      sw.Stop();
122      Console.WriteLine("done.");
123      Console.WriteLine("Loading Time: {0}", sw.Elapsed);
124      Console.WriteLine();
125      IGraph graph = ds.GetRoutingGraph();
126      IRouter router = (IRouter)Activator.CreateInstance(routerType, graph);
127      Console.Write("Calculate route ... ");
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);
133      Console.WriteLine();
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      }
147      Console.WriteLine("===================================================");
148      Console.WriteLine();
149    }
150
151    private static void WriteGPXFile(IGraph graph, long[] route, string file) {
152      XmlWriterSettings settings = new XmlWriterSettings();
153      settings.Indent = true;
154      settings.IndentChars = ("   ");
155
156      using (XmlWriter writer = XmlWriter.Create(file, settings)) {
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) {
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
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    }
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    }
248  }
249}
Note: See TracBrowser for help on using the repository browser.