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