|
| 1 | +/* |
| 2 | + * Copyright 2012 Peter Karich [email protected] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package de.jetsli.graph.reader; |
| 17 | + |
| 18 | +import de.jetsli.graph.routing.AStar; |
| 19 | +import de.jetsli.graph.routing.DijkstraBidirection; |
| 20 | +import de.jetsli.graph.routing.DijkstraBidirectionRef; |
| 21 | +import de.jetsli.graph.routing.DijkstraSimple; |
| 22 | +import de.jetsli.graph.routing.Path; |
| 23 | +import de.jetsli.graph.routing.RoutingAlgorithm; |
| 24 | +import de.jetsli.graph.storage.Graph; |
| 25 | +import de.jetsli.graph.storage.Location2IDIndex; |
| 26 | +import de.jetsli.graph.storage.Location2IDQuadtree; |
| 27 | +import de.jetsli.graph.util.CmdArgs; |
| 28 | +import de.jetsli.graph.util.CoordTrig; |
| 29 | +import de.jetsli.graph.util.Helper; |
| 30 | +import java.io.File; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Peter Karich |
| 36 | + */ |
| 37 | +public class RoutingAlgorithmIntegrationTests { |
| 38 | + |
| 39 | + private final Graph unterfrankenGraph; |
| 40 | + |
| 41 | + public RoutingAlgorithmIntegrationTests(Graph graph) { |
| 42 | + this.unterfrankenGraph = graph; |
| 43 | + } |
| 44 | + |
| 45 | + public void start() { |
| 46 | + Collector testCollector = new Collector(); |
| 47 | + |
| 48 | + // |
| 49 | + List<OneRun> list = new ArrayList<OneRun>(); |
| 50 | + list.add(new OneRun(43.727687, 7.418737, 43.730729, 7.421288, 1.532, 88)); |
| 51 | + list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3.448, 136)); |
| 52 | + runAlgo(testCollector, "files/monaco.osm.gz", "target/graph-monaco", list); |
| 53 | + |
| 54 | + // |
| 55 | + list = new ArrayList<OneRun>(); |
| 56 | + list.add(new OneRun(42.56819, 1.603231, 42.571034, 1.520662, 16.3845, 636)); |
| 57 | + list.add(new OneRun(42.529176, 1.571302, 42.571034, 1.520662, 12.4408, 397)); |
| 58 | + runAlgo(testCollector, "files/andorra.osm.gz", "target/graph-andorra", list); |
| 59 | + |
| 60 | + // |
| 61 | + testUnterfranken(testCollector); |
| 62 | + |
| 63 | + if (testCollector.list.size() > 0) { |
| 64 | + System.out.println("\n-------------------------------\n"); |
| 65 | + System.out.println("FOUND " + testCollector.list.size() + " ERRORS"); |
| 66 | + for (String s : testCollector.list) { |
| 67 | + System.out.println(s); |
| 68 | + } |
| 69 | + } else |
| 70 | + System.out.println("SUCCESS!"); |
| 71 | + } |
| 72 | + |
| 73 | + private void testUnterfranken(Collector testCollector) { |
| 74 | + RoutingAlgorithm[] algos = createAlgos(unterfrankenGraph); |
| 75 | + for (RoutingAlgorithm algo : algos) { |
| 76 | + int failed = testCollector.list.size(); |
| 77 | + testCollector.assertNull(algo, 773352, 858026); |
| 78 | + testCollector.assertNull(algo, 696295, 773352); |
| 79 | + |
| 80 | + testCollector.assertDistance(algo, 424236, 794975, 115.4, 2094); |
| 81 | + testCollector.assertDistance(algo, 331738, 111807, 121.4, 2328); |
| 82 | + testCollector.assertDistance(algo, 501620, 155552, 78.0, 1126); |
| 83 | + testCollector.assertDistance(algo, 399826, 269920, 53.1, 1041); |
| 84 | + testCollector.assertDistance(algo, 665211, 246823, 35.4, 710); |
| 85 | + testCollector.assertDistance(algo, 783718, 696695, 66.3, 1283); |
| 86 | + testCollector.assertDistance(algo, 811865, 641256, 113.3, 1729); |
| 87 | + testCollector.assertDistance(algo, 513676, 22669, 168.4, 2817); |
| 88 | + testCollector.assertDistance(algo, 896965, 769132, 6.0, 131); |
| 89 | + testCollector.assertDistance(algo, 115253, 430074, 56.6, 967); |
| 90 | + System.out.println("unterfranken " + algo.getClass().getSimpleName() |
| 91 | + + ": " + (testCollector.list.size() - failed) + " failed"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + RoutingAlgorithm[] createAlgos(Graph g) { |
| 96 | + return new RoutingAlgorithm[]{new AStar(g), new DijkstraBidirectionRef(g), |
| 97 | + new DijkstraBidirection(g), new DijkstraSimple(g)}; |
| 98 | + } |
| 99 | + |
| 100 | + public static class Collector { |
| 101 | + |
| 102 | + List<String> list = new ArrayList<String>(); |
| 103 | + |
| 104 | + public Collector assertNull(RoutingAlgorithm algo, int from, int to) { |
| 105 | + Path p = algo.clear().calcShortestPath(from, to); |
| 106 | + if (p != null) |
| 107 | + list.add(algo.getClass().getSimpleName() + " returns value where null is expected. " |
| 108 | + + "from:" + from + ", to:" + to); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + public Collector assertDistance(RoutingAlgorithm algo, int from, int to, double distance, int locations) { |
| 113 | + Path p = algo.clear().calcShortestPath(from, to); |
| 114 | + if (p == null) |
| 115 | + list.add(algo.getClass().getSimpleName() + " returns no path for " |
| 116 | + + "from:" + from + ", to:" + to); |
| 117 | + else if (Math.abs(p.distance() - distance) > 1e-1) |
| 118 | + list.add(algo.getClass().getSimpleName() + " returns path not matching the expected " |
| 119 | + + "distance of " + distance + "\t Returned was " + p.distance() |
| 120 | + + "\t (expected locations " + locations + ", was " + p.locations() + ") " |
| 121 | + + "from:" + from + ", to:" + to); |
| 122 | + else if (p.locations() != locations) |
| 123 | + list.add(algo.getClass().getSimpleName() + " returns path not matching the expected " |
| 124 | + + "locations of " + locations + "\t Returned was " + p.locations() |
| 125 | + + "\t (expected distance " + distance + ", was " + p.distance() + ") " |
| 126 | + + "from:" + from + ", to:" + to); |
| 127 | + return this; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + class OneRun { |
| 132 | + |
| 133 | + double fromLat, fromLon; |
| 134 | + double toLat, toLon; |
| 135 | + double dist; |
| 136 | + int locs; |
| 137 | + |
| 138 | + public OneRun(double fromLat, double fromLon, double toLat, double toLon, double dist, int locs) { |
| 139 | + this.fromLat = fromLat; |
| 140 | + this.fromLon = fromLon; |
| 141 | + this.toLat = toLat; |
| 142 | + this.toLon = toLon; |
| 143 | + this.dist = dist; |
| 144 | + this.locs = locs; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + void runAlgo(Collector testCollector, String osmFile, String graphFile, List<OneRun> forEveryAlgo) { |
| 149 | + try { |
| 150 | + // make sure we are using the latest file format |
| 151 | + Helper.deleteDir(new File(graphFile)); |
| 152 | + Graph g = OSMReader.osm2Graph(new CmdArgs().put("osm", osmFile).put("graph", graphFile)); |
| 153 | + System.out.println(osmFile + " - all locations " + g.getNodes()); |
| 154 | + Location2IDIndex idx = new Location2IDQuadtree(g).prepareIndex(2000); |
| 155 | + RoutingAlgorithm[] algos = createAlgos(g); |
| 156 | + for (RoutingAlgorithm algo : algos) { |
| 157 | + int failed = testCollector.list.size(); |
| 158 | + |
| 159 | + for (OneRun or : forEveryAlgo) { |
| 160 | + int from = idx.findID(or.fromLat, or.fromLon); |
| 161 | + int to = idx.findID(or.toLat, or.toLon); |
| 162 | + testCollector.assertDistance(algo, from, to, or.dist, or.locs); |
| 163 | + } |
| 164 | + |
| 165 | + System.out.println(osmFile + " " + algo.getClass().getSimpleName() |
| 166 | + + ": " + (testCollector.list.size() - failed) + " failed"); |
| 167 | + } |
| 168 | + } catch (Exception ex) { |
| 169 | + throw new RuntimeException("cannot handle osm file " + osmFile, ex); |
| 170 | + } finally { |
| 171 | + Helper.deleteDir(new File(graphFile)); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments