Skip to content

Commit b7289c5

Browse files
author
Peter
committed
fixed minor bug in optimize (maxEdges); moved the two fast integration tests into unit tests
1 parent f1065b2 commit b7289c5

File tree

4 files changed

+135
-79
lines changed

4 files changed

+135
-79
lines changed

core/src/main/java/de/jetsli/graph/reader/OSMReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void main(String[] strs) throws Exception {
6262
// return lat > 49.3 && lat < 50 && lon > 10.8 && lon < 11.6;
6363
}
6464
};
65-
//osm2Graph(osmReader, args);
65+
osm2Graph(osmReader, args);
6666
RoutingAlgorithmIntegrationTests tests = new RoutingAlgorithmIntegrationTests(osmReader.getGraph());
6767
if (args.getBool("test", false)) {
6868
tests.start();

core/src/main/java/de/jetsli/graph/reader/RoutingAlgorithmIntegrationTests.java

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
import de.jetsli.graph.storage.Graph;
2525
import de.jetsli.graph.storage.Location2IDIndex;
2626
import de.jetsli.graph.storage.Location2IDQuadtree;
27-
import de.jetsli.graph.util.CmdArgs;
28-
import de.jetsli.graph.util.Helper;
2927
import de.jetsli.graph.util.StopWatch;
30-
import java.io.File;
3128
import java.util.ArrayList;
3229
import java.util.List;
3330
import java.util.Random;
@@ -46,34 +43,7 @@ public RoutingAlgorithmIntegrationTests(Graph graph) {
4643
}
4744

4845
public void start() {
49-
Collector testCollector = new Collector();
50-
51-
//
52-
List<OneRun> list = new ArrayList<OneRun>();
53-
list.add(new OneRun(43.727687, 7.418737, 43.730729, 7.421288, 1.532, 88));
54-
list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3.448, 136));
55-
runAlgo(testCollector, "files/monaco.osm.gz", "target/graph-monaco", list);
56-
57-
//
58-
list = new ArrayList<OneRun>();
59-
list.add(new OneRun(42.56819, 1.603231, 42.571034, 1.520662, 16.378, 636));
60-
list.add(new OneRun(42.529176, 1.571302, 42.571034, 1.520662, 12.429, 397));
61-
runAlgo(testCollector, "files/andorra.osm.gz", "target/graph-andorra", list);
62-
63-
//
64-
testUnterfranken(testCollector);
65-
66-
if (testCollector.list.size() > 0) {
67-
System.out.println("\n-------------------------------\n");
68-
System.out.println("FOUND " + testCollector.list.size() + " ERRORS");
69-
for (String s : testCollector.list) {
70-
System.out.println(s);
71-
}
72-
} else
73-
System.out.println("SUCCESS!");
74-
}
75-
76-
private void testUnterfranken(Collector testCollector) {
46+
TestAlgoCollector testCollector = new TestAlgoCollector();
7747
RoutingAlgorithm[] algos = createAlgos(unterfrankenGraph);
7848
for (RoutingAlgorithm algo : algos) {
7949
int failed = testCollector.list.size();
@@ -95,26 +65,32 @@ private void testUnterfranken(Collector testCollector) {
9565
System.out.println("unterfranken " + algo.getClass().getSimpleName()
9666
+ ": " + (testCollector.list.size() - failed) + " failed");
9767
}
68+
69+
if (testCollector.list.size() > 0) {
70+
System.out.println("\n-------------------------------\n");
71+
System.out.println(testCollector);
72+
} else
73+
System.out.println("SUCCESS!");
9874
}
9975

100-
RoutingAlgorithm[] createAlgos(Graph g) {
76+
public static RoutingAlgorithm[] createAlgos(Graph g) {
10177
return new RoutingAlgorithm[]{new AStar(g), new DijkstraBidirectionRef(g),
10278
new DijkstraBidirection(g), new DijkstraSimple(g)};
10379
}
10480

105-
public static class Collector {
81+
public static class TestAlgoCollector {
10682

107-
List<String> list = new ArrayList<String>();
83+
public List<String> list = new ArrayList<String>();
10884

109-
public Collector assertNull(RoutingAlgorithm algo, int from, int to) {
85+
public TestAlgoCollector assertNull(RoutingAlgorithm algo, int from, int to) {
11086
Path p = algo.clear().calcShortestPath(from, to);
11187
if (p != null)
11288
list.add(algo.getClass().getSimpleName() + " returns value where null is expected. "
11389
+ "from:" + from + ", to:" + to);
11490
return this;
11591
}
11692

117-
public Collector assertDistance(RoutingAlgorithm algo, int from, int to, double distance, int locations) {
93+
public TestAlgoCollector assertDistance(RoutingAlgorithm algo, int from, int to, double distance, int locations) {
11894
Path p = algo.clear().calcShortestPath(from, to);
11995
if (p == null) {
12096
list.add(algo.getClass().getSimpleName() + " returns no path for "
@@ -135,49 +111,15 @@ public Collector assertDistance(RoutingAlgorithm algo, int from, int to, double
135111
+ "from:" + from + ", to:" + to);
136112
return this;
137113
}
138-
}
139-
140-
class OneRun {
141-
142-
double fromLat, fromLon;
143-
double toLat, toLon;
144-
double dist;
145-
int locs;
146-
147-
public OneRun(double fromLat, double fromLon, double toLat, double toLon, double dist, int locs) {
148-
this.fromLat = fromLat;
149-
this.fromLon = fromLon;
150-
this.toLat = toLat;
151-
this.toLon = toLon;
152-
this.dist = dist;
153-
this.locs = locs;
154-
}
155-
}
156114

157-
void runAlgo(Collector testCollector, String osmFile, String graphFile, List<OneRun> forEveryAlgo) {
158-
try {
159-
// make sure we are using the latest file format
160-
Helper.deleteDir(new File(graphFile));
161-
Graph g = OSMReader.osm2Graph(new CmdArgs().put("osm", osmFile).put("graph", graphFile));
162-
System.out.println(osmFile + " - all locations " + g.getNodes());
163-
Location2IDIndex idx = new Location2IDQuadtree(g).prepareIndex(2000);
164-
RoutingAlgorithm[] algos = createAlgos(g);
165-
for (RoutingAlgorithm algo : algos) {
166-
int failed = testCollector.list.size();
167-
168-
for (OneRun or : forEveryAlgo) {
169-
int from = idx.findID(or.fromLat, or.fromLon);
170-
int to = idx.findID(or.toLat, or.toLon);
171-
testCollector.assertDistance(algo, from, to, or.dist, or.locs);
172-
}
173-
174-
System.out.println(osmFile + " " + algo.getClass().getSimpleName()
175-
+ ": " + (testCollector.list.size() - failed) + " failed");
115+
@Override
116+
public String toString() {
117+
String str = "";
118+
str += "FOUND " + list.size() + " ERRORS\n";
119+
for (String s : list) {
120+
str += s + "\n";
176121
}
177-
} catch (Exception ex) {
178-
throw new RuntimeException("cannot handle osm file " + osmFile, ex);
179-
} finally {
180-
Helper.deleteDir(new File(graphFile));
122+
return str;
181123
}
182124
}
183125
private Logger logger = LoggerFactory.getLogger(getClass());

core/src/main/java/de/jetsli/graph/storage/MemoryGraphSafe.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import de.jetsli.graph.coll.MyOpenBitSet;
2121
import de.jetsli.graph.util.EdgeIdIterator;
2222
import de.jetsli.graph.util.Helper;
23+
import gnu.trove.impl.hash.TIntHash;
2324
import gnu.trove.map.hash.TIntIntHashMap;
25+
import gnu.trove.set.hash.TIntHashSet;
2426
import java.io.File;
2527
import java.io.IOException;
2628
import java.util.Arrays;
@@ -580,7 +582,8 @@ void inPlaceDelete(int deleted) {
580582
// rewrite the edges of nodes connected to moved nodes
581583
// go through all edges and pick the necessary ... <- this is easier to implement then
582584
// a more efficient breadth-first search
583-
int maxEdges = getMaxEdges();
585+
int maxEdges = getMaxEdges() * LEN_EDGE;
586+
TIntHashSet hash = new TIntHashSet();
584587
for (int edgePointer = 0; edgePointer < maxEdges;) {
585588
edgePointer += LEN_EDGE;
586589
// nodeId could be wrong - see tests
@@ -589,6 +592,8 @@ void inPlaceDelete(int deleted) {
589592
if (!toUpdatedSet.contains(nodeA) && !toUpdatedSet.contains(nodeB))
590593
continue;
591594

595+
hash.add(nodeA);
596+
hash.add(nodeB);
592597
// now overwrite exiting edge with new node ids
593598
// also flags and links could have changed due to different node order
594599
int updatedA = oldToNewIndexMap.get(nodeA);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.routing;
17+
18+
import de.jetsli.graph.reader.OSMReader;
19+
import de.jetsli.graph.reader.RoutingAlgorithmIntegrationTests;
20+
import de.jetsli.graph.storage.Graph;
21+
import de.jetsli.graph.storage.Location2IDIndex;
22+
import de.jetsli.graph.storage.Location2IDQuadtree;
23+
import de.jetsli.graph.util.CmdArgs;
24+
import de.jetsli.graph.util.Helper;
25+
import java.io.File;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import static org.junit.Assert.*;
31+
32+
/**
33+
* @author Peter Karich
34+
*/
35+
public class RoutingAlgorithmRealTest {
36+
37+
RoutingAlgorithmIntegrationTests.TestAlgoCollector testCollector;
38+
39+
@Before
40+
public void setUp() {
41+
testCollector = new RoutingAlgorithmIntegrationTests.TestAlgoCollector();
42+
}
43+
44+
@Test
45+
public void testMonaco() {
46+
List<OneRun> list = new ArrayList<OneRun>();
47+
list.add(new OneRun(43.727687, 7.418737, 43.730729, 7.421288, 1.532, 88));
48+
list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3.448, 136));
49+
runAlgo(testCollector, "files/monaco.osm.gz", "target/graph-monaco", list);
50+
51+
assertEquals(testCollector.toString(), 0, testCollector.list.size());
52+
}
53+
54+
@Test
55+
public void testAndorra() {
56+
List<OneRun> list = new ArrayList<OneRun>();
57+
list = new ArrayList<OneRun>();
58+
list.add(new OneRun(42.56819, 1.603231, 42.571034, 1.520662, 16.378, 636));
59+
list.add(new OneRun(42.529176, 1.571302, 42.571034, 1.520662, 12.429, 397));
60+
runAlgo(testCollector, "files/andorra.osm.gz", "target/graph-andorra", list);
61+
62+
assertEquals(testCollector.toString(), 0, testCollector.list.size());
63+
}
64+
65+
void runAlgo(RoutingAlgorithmIntegrationTests.TestAlgoCollector testCollector, String osmFile,
66+
String graphFile, List<OneRun> forEveryAlgo) {
67+
try {
68+
// make sure we are using the latest file format
69+
Helper.deleteDir(new File(graphFile));
70+
Graph g = OSMReader.osm2Graph(new CmdArgs().put("osm", osmFile).put("graph", graphFile));
71+
// System.out.println(osmFile + " - all locations " + g.getNodes());
72+
Location2IDIndex idx = new Location2IDQuadtree(g).prepareIndex(2000);
73+
RoutingAlgorithm[] algos = RoutingAlgorithmIntegrationTests.createAlgos(g);
74+
for (RoutingAlgorithm algo : algos) {
75+
int failed = testCollector.list.size();
76+
77+
for (OneRun or : forEveryAlgo) {
78+
int from = idx.findID(or.fromLat, or.fromLon);
79+
int to = idx.findID(or.toLat, or.toLon);
80+
testCollector.assertDistance(algo, from, to, or.dist, or.locs);
81+
}
82+
83+
// System.out.println(osmFile + " " + algo.getClass().getSimpleName()
84+
// + ": " + (testCollector.list.size() - failed) + " failed");
85+
}
86+
} catch (Exception ex) {
87+
throw new RuntimeException("cannot handle osm file " + osmFile, ex);
88+
} finally {
89+
Helper.deleteDir(new File(graphFile));
90+
}
91+
}
92+
93+
class OneRun {
94+
95+
double fromLat, fromLon;
96+
double toLat, toLon;
97+
double dist;
98+
int locs;
99+
100+
public OneRun(double fromLat, double fromLon, double toLat, double toLon, double dist, int locs) {
101+
this.fromLat = fromLat;
102+
this.fromLon = fromLon;
103+
this.toLat = toLat;
104+
this.toLon = toLon;
105+
this.dist = dist;
106+
this.locs = locs;
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)