Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1894

  • renamed old Vertex<T> to OsmVertex<T>
  • added new Vertex class
File size: 7.7 KB
Line 
1
2using System;
3using System.Diagnostics;
4using System.IO;
5using System.Xml;
6using HeuristicLab.Algorithms.GraphRouting;
7using HeuristicLab.Problems.RoutePlanning.Graph;
8using HeuristicLab.Problems.RoutePlanning.Osm;
9using HeuristicLab.Problems.RoutePlanning.Osm.Data;
10namespace HeuristicLab.Problems.RoutePlanning.Test {
11  class Program {
12    static void Main(string[] args) {
13      // For testing openstreetmap data is used
14      // http://www.openstreetmap.org
15      // Map data © OpenStreetMap contributors, CC BY-SA
16
17      string file1 = @"..\..\OsmTestFiles\test.osm";
18      string file2 = @"..\..\OsmTestFiles\testNode1.osm";
19      string file3 = @"..\..\OsmTestFiles\testWay1.osm";
20      string file4 = @"..\..\OsmTestFiles\testRelation1.osm";
21      string file5 = @"..\..\OsmTestFiles\test_mid.osm";
22      string file6 = @"C:\dev\osmfiles\oberosterreich.highway.osm";
23
24      IDataSource ds = TestLoad(file6);
25      OsmGraph graph = new OsmGraph(ds);
26      //TestRouter(new DijkstraAlgorithm(graph), 529102170, 31372732, false); // inz - hgb
27      //TestRouter(new AStarAlgorithm(graph), 529102170, 31372732, false); // inz - hgb
28      TestRouter(new AStarAlgorithmV2(graph), 529102170, 31372732, false); // inz - hgb
29      //TestRouter(new BidrectionalDijkstraAlgorithm(graph), 529102170, 31372732, false); // inz - hgb
30
31      //TestLoadAndRouter(file6, typeof(DijkstraAlgorithm), 529102170, 31372732, false, false); // inz - hgb
32      //TestLoadAndRouter(file6, typeof(AStarAlgorithm), 529102170, 31372732, false, false); // inz - hgb
33      //TestLoadAndRouter(file6, typeof(AStarAlgorithmV2), 529102170, 31372732, false, true); // inz - hgb
34
35      //TestLoadAndRouter(file6, typeof(DijkstraAlgorithm), 529102170, 1001363194, true, false);
36      //TestLoadAndRouter(file6, typeof(AStarAlgorithm), 529102170, 1001363194, true, false);
37      //TestLoadAndRouter(file6, typeof(AStarAlgorithmV2), 529102170, 1001363194, true, false);
38      //TestLoadAndRouter(file6, typeof(BidrectionalDijkstraAlgorithm), 529102170, 1001363194, true, false);
39
40      //TestLoadAndRouter(file5, typeof(DijkstraAlgorithm), 529102170, 1001363194, true, false);
41      //TestLoadAndRouter(file5, typeof(BidrectionalDijkstraAlgorithm), 529102170, 1001363194, true, true);
42      //TestLoadAndRouter(file6, typeof(BidrectionalDijkstraAlgorithm), 529102170, 31372732, false, true); // inz - hgb
43
44      System.Console.Read();
45    }
46
47    private static long[] TestRouter(IRouter router, long sourceNodeId, long targetNodeId, bool showResult) {
48      Console.WriteLine("Test Router BEGIN ---------------------------------");
49      Console.WriteLine("Type: " + router.GetType());
50
51      Console.Write("Calculate route ... ");
52      var sw = Stopwatch.StartNew();
53      long[] result = router.Calculate(sourceNodeId, targetNodeId);
54      sw.Stop();
55      Console.WriteLine("done.");
56      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
57      Console.WriteLine();
58      if (showResult) {
59        Console.WriteLine("Result: ");
60        foreach (long i in result) System.Console.Write(i + "; ");
61        Console.WriteLine();
62        Console.WriteLine();
63      }
64      Console.WriteLine("===================================================");
65      Console.WriteLine();
66      return result;
67    }
68
69    private static IDataSource TestLoad(string filepath) {
70      FileInfo file = new FileInfo(filepath);
71      Console.WriteLine("Test Load Data BEGIN ------------------------------");
72      Console.WriteLine("File name: {0}", file.Name);
73      Console.WriteLine();
74      Console.Write("Loading data ... ");
75      var sw = Stopwatch.StartNew();
76      XmlDataSource xmlDs = new XmlDataSource(filepath);
77      sw.Stop();
78      Console.WriteLine("done.");
79      Console.WriteLine("Time: {0}", sw.Elapsed);
80      Console.WriteLine();
81      Console.WriteLine("===================================================");
82      Console.WriteLine();
83      return xmlDs;
84    }
85
86    private static void TestLoadAndRouter(string filepath, Type routerType, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) {
87      FileInfo file = new FileInfo(filepath);
88      Console.WriteLine("Test Load and Route BEGIN -------------------------");
89      Console.WriteLine("File name: {0}", file.Name);
90      Console.WriteLine();
91      Console.Write("Loading data ... ");
92      var sw = Stopwatch.StartNew();
93      XmlDataSource xmlDs = new XmlDataSource(filepath);
94      sw.Stop();
95      Console.WriteLine("done.");
96      Console.WriteLine("Loading Time: {0}", sw.Elapsed);
97      Console.WriteLine();
98      OsmGraph graph = new OsmGraph(xmlDs);
99      IRouter router = (IRouter)Activator.CreateInstance(routerType, graph);
100      Console.Write("Calculate route ... ");
101      sw = Stopwatch.StartNew();
102      long[] result = router.Calculate(sourceNodeId, targetNodeId);
103      sw.Stop();
104      Console.WriteLine("done.");
105      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
106      Console.WriteLine();
107      if (showResult) {
108        Console.WriteLine("Result: ");
109        foreach (long i in result) System.Console.Write(i + "; ");
110        Console.WriteLine();
111        Console.WriteLine();
112      }
113      if (writeResultFile) {
114        string dt = DateTime.Now.ToString("yyyyMMddhhmmss_");
115        string fn = Path.GetFileNameWithoutExtension(file.Name);
116        string resultFile = dt + fn + "_" + sourceNodeId + "-" + targetNodeId + ".gpx";
117        Console.WriteLine("Result file: {0}", resultFile);
118        WriteGPXFile(graph, result, resultFile);
119      }
120      Console.WriteLine("===================================================");
121      Console.WriteLine();
122    }
123
124    private static void WriteGPXFile(OsmGraph graph, long[] route, string file) {
125      XmlWriterSettings settings = new XmlWriterSettings();
126      settings.Indent = true;
127      settings.IndentChars = ("   ");
128
129      using (XmlWriter writer = XmlWriter.Create(file, settings)) {
130        writer.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/0");
131        writer.WriteAttributeString("version", "1.1");
132        writer.WriteAttributeString("creator", "cloudia");
133
134        foreach (long nodeId in route) {
135          OsmVertex<Node> node = graph.GetVertex(nodeId);
136          writer.WriteStartElement("wpt");
137          writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
138          writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
139          writer.WriteEndElement(); // wpt
140        }
141
142        //writer.WriteStartElement("rte");
143        //foreach (long nodeId in route) {
144        //  Vertex<Node> node = graph.GetVertex(nodeId);
145        //  writer.WriteStartElement("rtept");
146        //  writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
147        //  writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
148        //  writer.WriteEndElement(); // rtept
149        //}
150        //writer.WriteEndElement(); // rte
151
152        //writer.WriteStartElement("trk");
153        //writer.WriteElementString("name", "RoutePath");
154        ////writer.WriteEndElement(); // name
155        //writer.WriteStartElement("trkseq");
156        //foreach (long nodeId in route) {
157        //  Vertex<Node> node = graph.GetVertex(nodeId);
158        //  writer.WriteStartElement("trkpt");
159        //  writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
160        //  writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
161        //  writer.WriteEndElement(); // trkpt
162        //}
163        //writer.WriteEndElement(); // trkseq
164        //writer.WriteEndElement(); // trk
165
166        writer.WriteEndElement(); // gpx
167      }
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.