Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8497 was 8481, checked in by spimming, 12 years ago

#1894:

  • adapted AStar and Dijkstra algorithms for new graph representation
  • test program modified
File size: 13.5 KB
Line 
1
2using System;
3using System.Diagnostics;
4using System.IO;
5using System.Xml;
6using HeuristicLab.Algorithms.GraphRouting;
7using HeuristicLab.Problems.RoutePlanning.DIMACS;
8using HeuristicLab.Problems.RoutePlanning.Graph;
9using HeuristicLab.Problems.RoutePlanning.Osm;
10using HeuristicLab.Problems.RoutePlanning.Osm.Data;
11namespace HeuristicLab.Problems.RoutePlanning.Test {
12  class Program {
13    static void Main(string[] args) {
14      // For testing openstreetmap data is used
15      // http://www.openstreetmap.org
16      // Map data © OpenStreetMap contributors, CC BY-SA
17
18      string file1 = @"..\..\OsmTestFiles\test.osm";
19      string file2 = @"..\..\OsmTestFiles\testNode1.osm";
20      string file3 = @"..\..\OsmTestFiles\testWay1.osm";
21      string file4 = @"..\..\OsmTestFiles\testRelation1.osm";
22      string file5 = @"..\..\OsmTestFiles\test_mid.osm";
23      string file6 = @"C:\dev\osmfiles\oberosterreich.highway.osm";
24      string file7 = @"C:\dev\osmfiles\austria.highway.osm";
25
26      string fileNYArcs = @"C:\dev\DIMACSfiles\USA-road-t.NY.gr.gz";
27      string fileNYCoords = @"C:\dev\DIMACSfiles\USA-road-d.NY.co.gz";
28
29      //IDataSource ds = TestLoad(file5);
30      //OsmGraph graph = new OsmGraph(ds);
31      //IGraph graphNew = TestGetRoutingGraph(ds);
32
33      IDataSource ds = new DIMACSDataSource(fileNYCoords, fileNYArcs);
34
35      //TestRouter(new DijkstraAlgorithm(graph), 529102170, 1001363194, true);
36      //TestRouter(new DijkstraAlgorithmV2(graphNew), 529102170, 1001363194, true);
37      //TestRouter(new DijkstraAlgorithm(graph), 529102170, 31372732, false); // inz - hgb
38      //TestRouter(new DijkstraAlgorithmV2(graphNew), 529102170, 31372732, false); // inz - hgb
39      //TestRouter(new BidrectionalDijkstraAlgorithm(graph), 529102170, 31372732, false); // inz - hgb
40      //TestRouter(new BidrectionalDijkstraAlgorithmV2(graphNew), 529102170, 31372732, false); // inz - hgb
41      //TestRouter(new AStarAlgorithm(graph), 529102170, 31372732, false); // inz - hgb
42      //TestRouter(new AStarAlgorithmV2(graph), 529102170, 31372732, false); // inz - hgb
43      //TestRouter(new AStarAlgorithmV3(graphNew), 529102170, 31372732, false); // inz - hgb
44
45      //TestRouter(new AStarAlgorithmV2(graph), 346151602, 33196510, false); // bregenz (bahnhofstr) - wien (stepansplatz)
46      //TestRouter(new AStarAlgorithmV3(graphNew), 346151602, 33196510, false); // bregenz (bahnhofstr) - wien (stepansplatz)
47
48      //TestLoadAndRouter(file6, typeof(DijkstraAlgorithm), 529102170, 31372732, false, false); // inz - hgb
49      //TestLoadAndRouter(file6, typeof(AStarAlgorithm), 529102170, 31372732, false, false); // inz - hgb
50      //TestLoadAndRouter(file6, typeof(AStarAlgorithmV2), 529102170, 31372732, false, true); // inz - hgb
51
52      //TestLoadAndRouter(file6, typeof(DijkstraAlgorithm), 529102170, 1001363194, true, false);
53      //TestLoadAndRouter(file6, typeof(DijkstraAlgorithm), 529102170, 1001363194, true, false);
54      //TestLoadAndRouter(file6, typeof(AStarAlgorithm), 529102170, 1001363194, true, false);
55      //TestLoadAndRouter(file6, typeof(AStarAlgorithmV2), 529102170, 1001363194, true, false);
56      //TestLoadAndRouter(file6, typeof(BidrectionalDijkstraAlgorithm), 529102170, 1001363194, true, false);
57
58      //TestLoadAndRouter(file5, typeof(DijkstraAlgorithm), 529102170, 1001363194, true, false);
59      //TestLoadAndRouter(file5, typeof(BidrectionalDijkstraAlgorithm), 529102170, 1001363194, true, true);
60      //TestLoadAndRouter(file6, typeof(BidrectionalDijkstraAlgorithm), 529102170, 31372732, false, true); // inz - hgb
61
62      //TestLoadAndRouterNew(file6, typeof(DijkstraAlgorithmV2), 529102170, 31372732, false, true);
63      //TestLoadAndRouterNew(file6, typeof(BidrectionalDijkstraAlgorithmV2), 529102170, 31372732, false, true);
64      //TestLoadAndRouterNew(file6, typeof(AStarAlgorithmV3), 529102170, 31372732, false, true);
65
66      System.Console.Read();
67    }
68
69    private static long[] TestRouter(IRouter router, long sourceNodeId, long targetNodeId, bool showResult) {
70      Console.WriteLine("Test Router BEGIN ---------------------------------");
71      Console.WriteLine("Type: " + router.GetType());
72
73      Console.Write("Calculate route ... ");
74      var sw = Stopwatch.StartNew();
75      long[] result = router.Calculate(sourceNodeId, targetNodeId);
76      sw.Stop();
77      Console.WriteLine("done.");
78      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
79      Console.WriteLine();
80      if (showResult) {
81        Console.WriteLine("Result: ");
82        foreach (long i in result) System.Console.Write(i + "; ");
83        Console.WriteLine();
84        Console.WriteLine();
85      }
86      Console.WriteLine("===================================================");
87      Console.WriteLine();
88      return result;
89    }
90
91    private static IDataSource TestLoad(string filepath) {
92      FileInfo file = new FileInfo(filepath);
93      Console.WriteLine("Test Load Data BEGIN ------------------------------");
94      Console.WriteLine("File name: {0}", file.Name);
95      Console.WriteLine();
96      Console.Write("Loading data ... ");
97      var sw = Stopwatch.StartNew();
98      XmlDataSource xmlDs = new XmlDataSource(filepath);
99      sw.Stop();
100      Console.WriteLine("done.");
101      Console.WriteLine("Time: {0}", sw.Elapsed);
102      Console.WriteLine();
103      Console.WriteLine("===================================================");
104      Console.WriteLine();
105      return xmlDs;
106    }
107
108    private static void TestLoadAndRouter(string filepath, Type routerType, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) {
109      FileInfo file = new FileInfo(filepath);
110      Console.WriteLine("Test Load and Route BEGIN -------------------------");
111      Console.WriteLine("File name: {0}", file.Name);
112      Console.WriteLine();
113      Console.Write("Loading data ... ");
114      var sw = Stopwatch.StartNew();
115      XmlDataSource xmlDs = new XmlDataSource(filepath);
116      sw.Stop();
117      Console.WriteLine("done.");
118      Console.WriteLine("Loading Time: {0}", sw.Elapsed);
119      Console.WriteLine();
120      OsmGraph graph = new OsmGraph(xmlDs);
121      IRouter router = (IRouter)Activator.CreateInstance(routerType, graph);
122      Console.Write("Calculate route ... ");
123      sw = Stopwatch.StartNew();
124      long[] result = router.Calculate(sourceNodeId, targetNodeId);
125      sw.Stop();
126      Console.WriteLine("done.");
127      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
128      Console.WriteLine();
129      if (showResult) {
130        Console.WriteLine("Result: ");
131        foreach (long i in result) System.Console.Write(i + "; ");
132        Console.WriteLine();
133        Console.WriteLine();
134      }
135      if (writeResultFile) {
136        string dt = DateTime.Now.ToString("yyyyMMddhhmmss_");
137        string fn = Path.GetFileNameWithoutExtension(file.Name);
138        string resultFile = dt + fn + "_" + sourceNodeId + "-" + targetNodeId + ".gpx";
139        Console.WriteLine("Result file: {0}", resultFile);
140        WriteGPXFile(graph, result, resultFile);
141      }
142      Console.WriteLine("===================================================");
143      Console.WriteLine();
144    }
145
146    private static void TestLoadAndRouterNew(string filepath, Type routerType, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) {
147      FileInfo file = new FileInfo(filepath);
148      Console.WriteLine("Test Load and Route BEGIN -------------------------");
149      Console.WriteLine("File name: {0}", file.Name);
150      Console.WriteLine();
151      Console.Write("Loading data ... ");
152      var sw = Stopwatch.StartNew();
153      XmlDataSource xmlDs = new XmlDataSource(filepath);
154      sw.Stop();
155      Console.WriteLine("done.");
156      Console.WriteLine("Loading Time: {0}", sw.Elapsed);
157      Console.WriteLine();
158      IGraph graph = xmlDs.GetRoutingGraph();
159      IRouter router = (IRouter)Activator.CreateInstance(routerType, graph);
160      Console.Write("Calculate route ... ");
161      sw = Stopwatch.StartNew();
162      long[] result = router.Calculate(sourceNodeId, targetNodeId);
163      sw.Stop();
164      Console.WriteLine("done.");
165      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
166      Console.WriteLine();
167      if (showResult) {
168        Console.WriteLine("Result: ");
169        foreach (long i in result) System.Console.Write(i + "; ");
170        Console.WriteLine();
171        Console.WriteLine();
172      }
173      if (writeResultFile) {
174        string dt = DateTime.Now.ToString("yyyyMMddhhmmss_");
175        string fn = Path.GetFileNameWithoutExtension(file.Name);
176        string resultFile = dt + fn + "_" + sourceNodeId + "-" + targetNodeId + ".gpx";
177        Console.WriteLine("Result file: {0}", resultFile);
178        WriteGPXFileNew(graph, result, resultFile);
179      }
180      Console.WriteLine("===================================================");
181      Console.WriteLine();
182    }
183
184    private static void WriteGPXFile(OsmGraph graph, long[] route, string file) {
185      XmlWriterSettings settings = new XmlWriterSettings();
186      settings.Indent = true;
187      settings.IndentChars = ("   ");
188
189      using (XmlWriter writer = XmlWriter.Create(file, settings)) {
190        writer.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/0");
191        writer.WriteAttributeString("version", "1.1");
192        writer.WriteAttributeString("creator", "cloudia");
193
194        foreach (long nodeId in route) {
195          OsmVertex<Node> node = graph.GetVertex(nodeId);
196          writer.WriteStartElement("wpt");
197          writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
198          writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
199          writer.WriteEndElement(); // wpt
200        }
201
202        //writer.WriteStartElement("rte");
203        //foreach (long nodeId in route) {
204        //  Vertex<Node> node = graph.GetVertex(nodeId);
205        //  writer.WriteStartElement("rtept");
206        //  writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
207        //  writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
208        //  writer.WriteEndElement(); // rtept
209        //}
210        //writer.WriteEndElement(); // rte
211
212        //writer.WriteStartElement("trk");
213        //writer.WriteElementString("name", "RoutePath");
214        ////writer.WriteEndElement(); // name
215        //writer.WriteStartElement("trkseq");
216        //foreach (long nodeId in route) {
217        //  Vertex<Node> node = graph.GetVertex(nodeId);
218        //  writer.WriteStartElement("trkpt");
219        //  writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
220        //  writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
221        //  writer.WriteEndElement(); // trkpt
222        //}
223        //writer.WriteEndElement(); // trkseq
224        //writer.WriteEndElement(); // trk
225
226        writer.WriteEndElement(); // gpx
227      }
228    }
229
230    private static void WriteGPXFileNew(IGraph graph, long[] route, string file) {
231      XmlWriterSettings settings = new XmlWriterSettings();
232      settings.Indent = true;
233      settings.IndentChars = ("   ");
234
235      using (XmlWriter writer = XmlWriter.Create(file, settings)) {
236        writer.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/0");
237        writer.WriteAttributeString("version", "1.1");
238        writer.WriteAttributeString("creator", "cloudia");
239
240        foreach (long nodeId in route) {
241          Vertex node = graph.GetVertex(nodeId);
242          writer.WriteStartElement("wpt");
243          writer.WriteAttributeString("lat", XmlConvert.ToString(node.Latitude));
244          writer.WriteAttributeString("lon", XmlConvert.ToString(node.Logitude));
245          writer.WriteEndElement(); // wpt
246        }
247
248        //writer.WriteStartElement("rte");
249        //foreach (long nodeId in route) {
250        //  Vertex<Node> node = graph.GetVertex(nodeId);
251        //  writer.WriteStartElement("rtept");
252        //  writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
253        //  writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
254        //  writer.WriteEndElement(); // rtept
255        //}
256        //writer.WriteEndElement(); // rte
257
258        //writer.WriteStartElement("trk");
259        //writer.WriteElementString("name", "RoutePath");
260        ////writer.WriteEndElement(); // name
261        //writer.WriteStartElement("trkseq");
262        //foreach (long nodeId in route) {
263        //  Vertex<Node> node = graph.GetVertex(nodeId);
264        //  writer.WriteStartElement("trkpt");
265        //  writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
266        //  writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
267        //  writer.WriteEndElement(); // trkpt
268        //}
269        //writer.WriteEndElement(); // trkseq
270        //writer.WriteEndElement(); // trk
271
272        writer.WriteEndElement(); // gpx
273      }
274    }
275
276    private static IGraph TestGetRoutingGraph(IDataSource ds) {
277      Console.WriteLine("Test GetRoutingGraph BEGIN ------------------------");
278      Console.WriteLine();
279      Console.Write("Construct graph ... ");
280      var sw = Stopwatch.StartNew();
281      IGraph graph = ds.GetRoutingGraph();
282      sw.Stop();
283      Console.WriteLine("done.");
284      Console.WriteLine("Time: {0}", sw.Elapsed);
285      Console.WriteLine();
286      Console.WriteLine("===================================================");
287      Console.WriteLine();
288      return graph;
289    }
290  }
291}
Note: See TracBrowser for help on using the repository browser.