Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added astar

  • Loading branch information...
commit 8e13f0b353deee5c3a29aea399ff46a6fd7143e2 1 parent f7e8ac1
@karussell authored
Showing with 285 additions and 76 deletions.
  1. +6 −5 core/src/main/java/de/jetsli/graph/reader/MMapGraphStorage.java
  2. +48 −0 core/src/main/java/de/jetsli/graph/reader/MemoryGraphStorage.java
  3. +9 −7 core/src/main/java/de/jetsli/graph/reader/OSMReader.java
  4. +80 −2 core/src/main/java/de/jetsli/graph/routing/AStar.java
  5. +14 −14 core/src/main/java/de/jetsli/graph/routing/DijkstraBidirection.java
  6. +8 −4 core/src/main/java/de/jetsli/graph/routing/DijkstraShortestOf2ToPub.java
  7. +5 −1 core/src/main/java/de/jetsli/graph/routing/DijkstraSimple.java
  8. +5 −0 core/src/main/java/de/jetsli/graph/routing/DijkstraWhichToOne.java
  9. +2 −0  core/src/main/java/de/jetsli/graph/routing/RoutingAlgorithm.java
  10. +1 −1  core/src/main/java/de/jetsli/graph/storage/MemoryGraph.java
  11. +9 −1 core/src/main/java/de/jetsli/graph/util/CalcDistance.java
  12. +40 −0 core/src/test/java/de/jetsli/graph/routing/AStarTest.java
  13. +31 −29 .../test/java/de/jetsli/graph/routing/{AbstractDijkstraTester.java → AbstractRoutingAlgorithmTester.java}
  14. +3 −6 core/src/test/java/de/jetsli/graph/routing/DijkstraBidirectionTest.java
  15. +2 −2 core/src/test/java/de/jetsli/graph/routing/DijkstraSimpleTest.java
  16. +2 −2 core/src/test/java/de/jetsli/graph/routing/DijkstraTwoDriversTest.java
  17. +2 −2 core/src/test/java/de/jetsli/graph/routing/DijkstraWhichToOneTest.java
  18. +18 −0 core/src/test/java/de/jetsli/graph/util/CalcDistanceTest.java
View
11 core/src/main/java/de/jetsli/graph/reader/MMapGraphStorage.java
@@ -18,21 +18,20 @@
import de.jetsli.graph.util.CalcDistance;
import de.jetsli.graph.storage.Graph;
import de.jetsli.graph.storage.MMapGraph;
-import de.jetsli.graph.util.StopWatch;
+import de.jetsli.graph.storage.MemoryGraph;
import gnu.trove.map.hash.TIntIntHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- *
* @author Peter Karich, info@jetsli.de
*/
public class MMapGraphStorage implements Storage {
- private static final int FILLED = -2;
+ protected static final int FILLED = -2;
private final Logger logger = LoggerFactory.getLogger(getClass());
private MMapGraph g;
- private TIntIntHashMap osmIdToIndexMap;
+ protected TIntIntHashMap osmIdToIndexMap;
private final String file;
public MMapGraphStorage(String file, int expectedNodes) {
@@ -48,7 +47,9 @@ public boolean loadExisting() {
@Override
public void createNew() {
- g = new MMapGraph(file, osmIdToIndexMap.size());
+ if (g != null)
+ g.close();
+ g = new MMapGraph(null, osmIdToIndexMap.size());
// createNew(*true*) to avoid slow down for mmap files (and RAM bottlenecks)
// but still write to disc at the end!
g.createNew(true);
View
48 core/src/main/java/de/jetsli/graph/reader/MemoryGraphStorage.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2012 Peter Karich info@jetsli.de
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package de.jetsli.graph.reader;
+
+import de.jetsli.graph.storage.MemoryGraph;
+
+/**
+ * @author Peter Karich, info@jetsli.de
+ */
+public class MemoryGraphStorage extends MMapGraphStorage {
+
+ MemoryGraph g;
+
+ public MemoryGraphStorage(int expectedNodes) {
+ super(null, expectedNodes);
+ }
+
+ @Override
+ public boolean loadExisting() {
+ return false;
+ }
+
+ @Override
+ public void createNew() {
+ g = new MemoryGraph(osmIdToIndexMap.size());
+ }
+
+ @Override public void stats() {
+ }
+
+ @Override
+ public void flush() {
+ osmIdToIndexMap = null;
+ }
+}
View
16 core/src/main/java/de/jetsli/graph/reader/OSMReader.java
@@ -15,11 +15,10 @@
*/
package de.jetsli.graph.reader;
-import de.jetsli.graph.coll.MyBitSet;
-import de.jetsli.graph.coll.MyOpenBitSet;
+import de.jetsli.graph.routing.AStar;
import de.jetsli.graph.util.CalcDistance;
-import de.jetsli.graph.routing.DijkstraBidirection;
import de.jetsli.graph.routing.Path;
+import de.jetsli.graph.routing.RoutingAlgorithm;
import de.jetsli.graph.storage.Graph;
import de.jetsli.graph.storage.Location2IDIndex;
import de.jetsli.graph.storage.Location2IDQuadtree;
@@ -29,7 +28,6 @@
import java.util.*;
import java.util.List;
import java.util.Map.Entry;
-import java.util.concurrent.atomic.AtomicInteger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
@@ -120,7 +118,9 @@ public void doDijkstra(int runs) throws Exception {
Location2IDIndex index = new Location2IDQuadtree(g).prepareIndex(20000);
double minLat = 49.484186, minLon = 8.974228;
double maxLat = 50.541363, maxLon = 10.880356;
- DijkstraBidirection dijkstra = new DijkstraBidirection(g);
+// RoutingAlgorithm algo = new DijkstraBidirection(g);
+// RoutingAlgorithm algo = new DijkstraSimple(g);
+ RoutingAlgorithm algo = new AStar(g);
Random rand = new Random(123);
StopWatch sw = new StopWatch();
for (int i = 0; i < runs; i++) {
@@ -136,8 +136,9 @@ public void doDijkstra(int runs) throws Exception {
continue;
}
+ algo.clear();
sw.start();
- Path p = dijkstra.clear().calcShortestPath(from, to);
+ Path p = algo.calcShortestPath(from, to);
sw.stop();
if (p == null) {
logger.warn("no route found for i=" + i + " !?" + " graph-from " + from + ", graph-to " + to);
@@ -149,7 +150,8 @@ public void doDijkstra(int runs) throws Exception {
}
public OSMReader(String file, int size) {
- storage = new MMapGraphStorage(file, expectedLocs = size);
+ // storage = new MMapGraphStorage(file, expectedLocs = size);
+ storage = new MemoryGraphStorage(expectedLocs = size);
}
public boolean loadExisting() {
View
82 core/src/main/java/de/jetsli/graph/routing/AStar.java
@@ -13,12 +13,90 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package de.jetsli.graph.routing;
+import de.jetsli.graph.storage.DistEntry;
+import de.jetsli.graph.storage.Edge;
+import de.jetsli.graph.storage.Graph;
+import de.jetsli.graph.util.CalcDistance;
+import gnu.trove.map.TIntObjectMap;
+import gnu.trove.map.hash.TIntObjectHashMap;
+import gnu.trove.set.hash.TIntHashSet;
+import java.util.PriorityQueue;
+
/**
* @author Peter Karich
*/
-public class AStar {
+public class AStar implements RoutingAlgorithm {
+
+ private Graph graph;
+ private CalcDistance dist = new CalcDistance();
+
+ public AStar(Graph g) {
+ this.graph = g;
+ }
+
+ @Override public Path calcShortestPath(int from, int to) {
+ Edge fromEntry = new Edge(from, 0);
+ Edge curr = fromEntry;
+ TIntHashSet visited = new TIntHashSet();
+ TIntObjectMap<Edge> map = new TIntObjectHashMap<Edge>();
+ PriorityQueue<Edge> heap = new PriorityQueue<Edge>();
+
+
+ while (true) {
+ int currVertex = curr.node;
+ double lat = graph.getLatitude(currVertex);
+ double lon = graph.getLongitude(currVertex);
+
+ for (DistEntry entry : graph.getOutgoing(currVertex)) {
+ int tmpV = entry.node;
+ if (visited.contains(tmpV))
+ continue;
+
+ // possibilities:
+ // 1. we could use landmarks and less expensive triangular formular
+ // which satisfies the h(x) requirement instead of this expensive real calculation
+ // 2. use less expensive calc distance
+ // (e.g. normed dist ... hmh but then entry.distance of edges needs to be normed too!)
+ double tmpLat = graph.getLatitude(tmpV);
+ double tmpLon = graph.getLongitude(tmpV);
+ double distToGoal = dist.approxDistKm(lat, lon, tmpLat, tmpLon);
+ double latestDist = entry.distance + curr.distance + distToGoal;
+ Edge de = map.get(tmpV);
+ if (de == null) {
+ de = new Edge(tmpV, latestDist);
+ de.prevEntry = curr;
+ map.put(tmpV, de);
+ heap.add(de);
+ } else if (de.distance > latestDist) {
+ heap.remove(de);
+ de.distance = latestDist;
+ de.prevEntry = curr;
+ heap.add(de);
+ }
+ }
+ if (to == currVertex)
+ break;
+
+ visited.add(currVertex);
+ curr = heap.poll();
+ if (curr == null)
+ return null;
+ }
+
+ // extract path from shortest-path-tree
+ Path path = new Path();
+ while (curr.node != from) {
+ path.add(curr);
+ curr = curr.prevEntry;
+ }
+ path.add(fromEntry);
+ path.reverseOrder();
+ return path;
+ }
+ @Override public RoutingAlgorithm clear() {
+ return this;
+ }
}
View
28 core/src/main/java/de/jetsli/graph/routing/DijkstraBidirection.java
@@ -62,6 +62,7 @@ public DijkstraBidirection(Graph graph) {
clear();
}
+ @Override
public RoutingAlgorithm clear() {
alreadyRun = false;
visitedFrom.clear();
@@ -104,7 +105,6 @@ public DijkstraBidirection initTo(int to) {
initFrom(from);
initTo(to);
- // identical
Path p = checkIndenticalFromAndTo();
if (p != null)
return p;
@@ -125,18 +125,18 @@ public DijkstraBidirection initTo(int to) {
}
public Path getShortest() {
- Path g = shortest.extract();
- if (g == null)
+ Path p = shortest.extract();
+ if (p == null)
return null;
- if (g.getFromLoc() != from) {
+ if (p.getFromLoc() != from) {
// move distance adjustment to reverseOrder?
- double tmpDist = g.distance();
- g.reverseOrder();
- g.setDistance(tmpDist);
+ double tmpDist = p.distance();
+ p.reverseOrder();
+ p.setDistance(tmpDist);
}
- return g;
+ return p;
}
// http://www.cs.princeton.edu/courses/archive/spr06/cos423/Handouts/EPP%20shortest%20path%20algorithms.pdf
@@ -156,16 +156,16 @@ public void fillEdges(Edge curr, MyBitSet visitedMain, PriorityQueue<Edge> prioQ
int currVertexFrom = curr.node;
for (DistEntry entry : graph.getOutgoing(currVertexFrom)) {
- int tmpV = entry.node;
- if (visitedMain.contains(tmpV))
+ int currentLinkedNode = entry.node;
+ if (visitedMain.contains(currentLinkedNode))
continue;
double tmp = entry.distance + curr.distance;
- Edge de = shortestDistMap.get(tmpV);
+ Edge de = shortestDistMap.get(currentLinkedNode);
if (de == null) {
- de = new Edge(tmpV, tmp);
+ de = new Edge(currentLinkedNode, tmp);
de.prevEntry = curr;
- shortestDistMap.put(tmpV, de);
+ shortestDistMap.put(currentLinkedNode, de);
prioQueue.add(de);
} else if (de.distance > tmp) {
// use fibonacci? see http://stackoverflow.com/q/6273833/194609
@@ -176,7 +176,7 @@ public void fillEdges(Edge curr, MyBitSet visitedMain, PriorityQueue<Edge> prioQ
prioQueue.add(de);
}
- updateShortest(de, tmpV);
+ updateShortest(de, currentLinkedNode);
} // for
}
View
12 core/src/main/java/de/jetsli/graph/routing/DijkstraShortestOf2ToPub.java
@@ -27,9 +27,8 @@
import java.util.PriorityQueue;
/**
- * Public transport represents a collection of Locations. Then there are two points P1 and P1 and
- * it is the aim to find the shortest path from P1 to one of the public transport points (M) and to
- * P2.
+ * Public transport represents a collection of Locations. Then there are two points P1 and P1 and it
+ * is the aim to find the shortest path from P1 to one of the public transport points (M) and to P2.
*
* <br/> Usage: A driver can carry the passenger from P1 to a public transport point (M) and going
* back to his own destination P2 and comparing this with the detour of taking the passenger
@@ -156,7 +155,7 @@ public boolean checkFinishCondition() {
return Math.min(currFrom.distance, currTo.distance) >= shortest.distance;
}
- public void fillEdges(Edge curr, MyBitSet visitedMain,
+ public void fillEdges(Edge curr, MyBitSet visitedMain,
PriorityQueue<Edge> prioQueue, TIntObjectMap<Edge> shortestDistMap) {
int currVertexFrom = curr.node;
@@ -206,4 +205,9 @@ public void updateShortest(Edge shortestDE, int currLoc) {
setTo(to);
return calcShortestPath();
}
+
+ @Override
+ public RoutingAlgorithm clear() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
}
View
6 core/src/main/java/de/jetsli/graph/routing/DijkstraSimple.java
@@ -70,7 +70,7 @@ public DijkstraSimple(Graph graph) {
visited.add(currVertex);
curr = heap.poll();
if (curr == null)
- throw new IllegalStateException("No path found");
+ return null;
}
// extract path from shortest-path-tree
@@ -83,4 +83,8 @@ public DijkstraSimple(Graph graph) {
path.reverseOrder();
return path;
}
+
+ @Override public RoutingAlgorithm clear() {
+ return this;
+ }
}
View
5 core/src/main/java/de/jetsli/graph/routing/DijkstraWhichToOne.java
@@ -177,4 +177,9 @@ public void fillEdges(PathWrapper shortest, Edge curr, MyBitSet visitedMain,
setDestination(to);
return calcShortestPath();
}
+
+ @Override
+ public RoutingAlgorithm clear() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
}
View
2  core/src/main/java/de/jetsli/graph/routing/RoutingAlgorithm.java
@@ -23,4 +23,6 @@
public interface RoutingAlgorithm {
Path calcShortestPath(int from, int to);
+
+ RoutingAlgorithm clear();
}
View
2  core/src/main/java/de/jetsli/graph/storage/MemoryGraph.java
@@ -37,7 +37,7 @@
private MyBitSet deletedNodes;
public MemoryGraph() {
- this(1000);
+ this(100);
}
public MemoryGraph(int capacity) {
View
10 core/src/main/java/de/jetsli/graph/util/CalcDistance.java
@@ -53,6 +53,14 @@ public double calcDistKm(double fromLat, double fromLon, double toLat, double to
return R * 2 * Math.asin(Math.sqrt(normedDist));
}
+ public double approxDistKm(double fromLat, double fromLon, double toLat, double toLon) {
+ double dLat = Math.toRadians(toLat - fromLat);
+ double dLon = Math.toRadians(toLon - fromLon);
+ double left = Math.cos(Math.toRadians((fromLat + toLat) / 2)) * dLon;
+ double d = dLat * dLat + left * left;
+ return R * Math.sqrt(d);
+ }
+
public double denormalizeDist(double normedDist) {
return R * 2 * Math.asin(Math.sqrt(normedDist));
}
@@ -61,7 +69,7 @@ public double normalizeDist(double dist) {
double tmp = Math.sin(dist / 2 / R);
return tmp * tmp;
}
-
+
public final double calcNormalizedDist(double fromLat, double fromLon, double toLat, double toLon) {
double dLat = Math.toRadians(toLat - fromLat);
double dLon = Math.toRadians(toLon - fromLon);
View
40 core/src/test/java/de/jetsli/graph/routing/AStarTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2012 Peter Karich info@jetsli.de
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package de.jetsli.graph.routing;
+
+import de.jetsli.graph.storage.Graph;
+
+/**
+ *
+ * @author Peter Karich
+ */
+public class AStarTest extends AbstractRoutingAlgorithmTester {
+
+ @Override
+ public void testBidirectional() {
+ // TODO add lat+lon to test case
+ }
+
+ @Override
+ public void testBidirectional2() {
+ // TODO add lat+lon to test case
+ }
+
+ @Override
+ public RoutingAlgorithm createAlgo(Graph g) {
+ return new AStar(g);
+ }
+}
View
60 ...graph/routing/AbstractDijkstraTester.java → ...uting/AbstractRoutingAlgorithmTester.java
@@ -15,8 +15,6 @@
*/
package de.jetsli.graph.routing;
-import de.jetsli.graph.routing.RoutingAlgorithm;
-import de.jetsli.graph.routing.Path;
import de.jetsli.graph.reader.PrinctonReader;
import de.jetsli.graph.storage.MemoryGraph;
import de.jetsli.graph.storage.Graph;
@@ -31,7 +29,7 @@
*
* @author Peter Karich, info@jetsli.de
*/
-public abstract class AbstractDijkstraTester {
+public abstract class AbstractRoutingAlgorithmTester {
static Graph matrixGraph;
@@ -40,12 +38,12 @@
}
int from;
int to;
-
- public abstract RoutingAlgorithm createDijkstra(Graph g);
+
+ public abstract RoutingAlgorithm createAlgo(Graph g);
@Test public void testCalcShortestPath() {
- Graph graph = createGraph();
- Path p = createDijkstra(graph).calcShortestPath(from, to);
+ Graph graph = createTestGraph();
+ Path p = createAlgo(graph).calcShortestPath(from, to);
assertEquals(p.toString(), 13, p.distance(), 1e-6);
assertEquals(p.toString(), 5, p.locations());
}
@@ -58,8 +56,8 @@
// |\/ \ \|
// a-b--c-d
//
- protected Graph createGraph() {
- Graph graph = new MemoryGraph(8);
+ protected Graph createTestGraph() {
+ Graph graph = createGraph(8);
from = 0;
to = 7;
@@ -87,28 +85,28 @@ protected Graph createGraph() {
}
@Test public void testWikipediaShortestPath() {
- Graph graph = createWikipediaGraph();
- Path p = createDijkstra(graph).calcShortestPath(0, 4);
+ Graph graph = createWikipediaTestGraph();
+ Path p = createAlgo(graph).calcShortestPath(0, 4);
assertEquals(p.toString(), 20, p.distance(), 1e-6);
assertEquals(p.toString(), 4, p.locations());
}
@Test public void testCalcIfNoWay() {
- Graph graph = createGraph();
- Path p = createDijkstra(graph).calcShortestPath(0, 0);
+ Graph graph = createTestGraph();
+ Path p = createAlgo(graph).calcShortestPath(0, 0);
assertEquals(p.toString(), 0, p.distance(), 1e-6);
assertEquals(p.toString(), 1, p.locations());
}
@Test public void testCalcIf1EdgeAway() {
- Graph graph = createGraph();
- Path p = createDijkstra(graph).calcShortestPath(1, 2);
+ Graph graph = createTestGraph();
+ Path p = createAlgo(graph).calcShortestPath(1, 2);
assertEquals(p.toString(), 2, p.distance(), 1e-6);
assertEquals(p.toString(), 2, p.locations());
}
- protected Graph createWikipediaGraph() {
- Graph graph = new MemoryGraph(6);
+ protected Graph createWikipediaTestGraph() {
+ Graph graph = createGraph(6);
graph.edge(0, 1, 7, true);
graph.edge(0, 2, 9, true);
@@ -142,7 +140,7 @@ protected Graph createWikipediaGraph() {
// \ / /
// 8-7-6-/
@Test public void testBidirectional() {
- Graph graph = new MemoryGraph(6);
+ Graph graph = createGraph(6);
from = 0;
to = 4;
@@ -157,7 +155,7 @@ protected Graph createWikipediaGraph() {
graph.edge(3, 8, 20, true);
graph.edge(8, 6, 20, true);
- Path p = createDijkstra(graph).calcShortestPath(from, to);
+ Path p = createAlgo(graph).calcShortestPath(from, to);
assertEquals(p.toString(), 51, p.distance(), 1e-6);
assertEquals(p.toString(), 6, p.locations());
}
@@ -168,7 +166,7 @@ protected Graph createWikipediaGraph() {
// \ / /
// 8-7-6-/
@Test public void testBidirectional2() {
- Graph graph = new MemoryGraph(6);
+ Graph graph = createGraph(6);
from = 0;
to = 4;
@@ -183,36 +181,36 @@ protected Graph createWikipediaGraph() {
graph.edge(3, 8, 20, true);
graph.edge(8, 6, 20, true);
- Path p = createDijkstra(graph).calcShortestPath(from, to);
+ Path p = createAlgo(graph).calcShortestPath(from, to);
assertEquals(p.toString(), 40, p.distance(), 1e-6);
assertEquals(p.toString(), 5, p.locations());
}
@Test public void testRekeyBugOfIntBinHeap() {
// using DijkstraSimple + IntBinHeap then rekey loops endlessly
- Path p = createDijkstra(matrixGraph).calcShortestPath(36, 91);
+ Path p = createAlgo(matrixGraph).calcShortestPath(36, 91);
assertEquals(12, p.locations());
assertEquals(66f, p.distance(), 1e-3);
}
@Test
public void testBug1() {
- assertEquals(17, createDijkstra(matrixGraph).calcShortestPath(34, 36).distance(), 1e-5);
+ assertEquals(17, createAlgo(matrixGraph).calcShortestPath(34, 36).distance(), 1e-5);
}
@Test
public void testCannotCalculateSP() {
- Graph g = new MemoryGraph();
+ Graph g = createGraph(10);
g.edge(0, 1, 1, false);
g.edge(1, 2, 1, false);
- Path p = createDijkstra(g).calcShortestPath(0, 2);
+ Path p = createAlgo(g).calcShortestPath(0, 2);
assertEquals(p.toString(), 3, p.locations());
}
@Test
public void testDirectedGraphBug1() {
- Graph g = new MemoryGraph(5);
+ Graph g = createGraph(5);
g.edge(0, 1, 3, false);
g.edge(1, 2, 3, false);
@@ -220,7 +218,7 @@ public void testDirectedGraphBug1() {
g.edge(3, 4, 3, false);
g.edge(4, 2, 1, false);
- Path p = createDijkstra(g).calcShortestPath(0, 2);
+ Path p = createAlgo(g).calcShortestPath(0, 2);
assertEquals(p.toString(), 3, p.locations());
}
@@ -230,7 +228,7 @@ public void testDirectedGraphBug1() {
String name = getClass().getSimpleName();
Random rand = new Random(0);
- Graph graph = new MemoryGraph();
+ Graph graph = createGraph(0);
String bigFile = "10000EWD.txt.gz";
// String bigFile = "largeEWD.txt.gz";
@@ -240,7 +238,7 @@ public void testDirectedGraphBug1() {
int index1 = Math.abs(rand.nextInt(graph.getLocations()));
int index2 = Math.abs(rand.nextInt(graph.getLocations()));
// constructing the graph could be expensive like for CH
- RoutingAlgorithm d = createDijkstra(graph);
+ RoutingAlgorithm d = createAlgo(graph);
if (i >= noJvmWarming)
sw.start();
@@ -295,4 +293,8 @@ private static Graph createMatrixAlikeGraph() {
return tmp;
}
+
+ Graph createGraph(int size) {
+ return new MemoryGraph(size);
+ }
}
View
9 core/src/test/java/de/jetsli/graph/routing/DijkstraBidirectionTest.java
@@ -15,9 +15,6 @@
*/
package de.jetsli.graph.routing;
-import de.jetsli.graph.routing.DijkstraBidirection;
-import de.jetsli.graph.routing.RoutingAlgorithm;
-import de.jetsli.graph.routing.Path;
import de.jetsli.graph.storage.MemoryGraph;
import de.jetsli.graph.storage.Graph;
import org.junit.Test;
@@ -27,15 +24,15 @@
*
* @author Peter Karich, info@jetsli.de
*/
-public class DijkstraBidirectionTest extends AbstractDijkstraTester {
+public class DijkstraBidirectionTest extends AbstractRoutingAlgorithmTester {
- @Override public RoutingAlgorithm createDijkstra(Graph g) {
+ @Override public RoutingAlgorithm createAlgo(Graph g) {
return new DijkstraBidirection(g);
}
@Test
public void testAddSkipNodes() {
- Graph g = createWikipediaGraph();
+ Graph g = createWikipediaTestGraph();
Path p = new DijkstraBidirection(g).calcShortestPath(0, 4);
assertEquals(p.toString(), 20, p.distance(), 1e-6);
assertTrue(p.toString(), p.contains(5));
View
4 core/src/test/java/de/jetsli/graph/routing/DijkstraSimpleTest.java
@@ -23,9 +23,9 @@
*
* @author Peter Karich, info@jetsli.de
*/
-public class DijkstraSimpleTest extends AbstractDijkstraTester {
+public class DijkstraSimpleTest extends AbstractRoutingAlgorithmTester {
- @Override public RoutingAlgorithm createDijkstra(Graph g) {
+ @Override public RoutingAlgorithm createAlgo(Graph g) {
return new DijkstraSimple(g);
}
View
4 core/src/test/java/de/jetsli/graph/routing/DijkstraTwoDriversTest.java
@@ -31,7 +31,7 @@
public class DijkstraTwoDriversTest {
@Test public void testFindMeetingPointWhenNotCrossing() {
- Graph g = AbstractDijkstraTester.matrixGraph;
+ Graph g = AbstractRoutingAlgorithmTester.matrixGraph;
DijkstraTwoDrivers d = new DijkstraTwoDrivers(g);
d.setDriverA(12, 36);
d.setDriverB(30, 45);
@@ -59,7 +59,7 @@
}
@Test public void testFindMeetingPointWhenCrossing() {
- Graph g = AbstractDijkstraTester.matrixGraph;
+ Graph g = AbstractRoutingAlgorithmTester.matrixGraph;
DijkstraTwoDrivers d = new DijkstraTwoDrivers(g);
d.setDriverA(12, 36);
d.setDriverB(30, 15);
View
4 core/src/test/java/de/jetsli/graph/routing/DijkstraWhichToOneTest.java
@@ -27,11 +27,11 @@
*
* @author Peter Karich, info@jetsli.de
*/
-public class DijkstraWhichToOneTest extends AbstractDijkstraTester {
+public class DijkstraWhichToOneTest extends AbstractRoutingAlgorithmTester {
public static int[] pubTransportPath = new int[]{20, 21, 31, 41, 51, 52, 42, 43, 53, 63, 62, 72, 73, 74, 75};
- @Override public RoutingAlgorithm createDijkstra(Graph g) {
+ @Override public RoutingAlgorithm createAlgo(Graph g) {
return new DijkstraWhichToOne(g);
}
View
18 core/src/test/java/de/jetsli/graph/util/CalcDistanceTest.java
@@ -65,13 +65,31 @@ public void testDistance() {
double res = 15.051;
assertEquals(res, dist.calcDistKm(lat, lon, lat - 0.1, lon + 0.1), 1e-3);
assertEquals(dist.normalizeDist(res), dist.calcNormalizedDist(lat, lon, lat - 0.1, lon + 0.1), 1e-3);
+ assertEquals(res, dist.approxDistKm(lat, lon, lat - 0.1, lon + 0.1), 1e-3);
+
+ res = 15.046;
+ assertEquals(res, dist.calcDistKm(lat, lon, lat + 0.1, lon - 0.1), 1e-3);
+ assertEquals(dist.normalizeDist(res), dist.calcNormalizedDist(lat, lon, lat + 0.1, lon - 0.1), 1e-3);
+ assertEquals(res, dist.approxDistKm(lat, lon, lat + 0.1, lon - 0.1), 1e-3);
res = 150.748;
assertEquals(res, dist.calcDistKm(lat, lon, lat - 1, lon + 1), 1e-3);
assertEquals(dist.normalizeDist(res), dist.calcNormalizedDist(lat, lon, lat - 1, lon + 1), 1e-3);
+ assertEquals(res, dist.approxDistKm(lat, lon, lat - 1, lon + 1), 1e-2);
+ res = 150.211;
+ assertEquals(res, dist.calcDistKm(lat, lon, lat + 1, lon - 1), 1e-3);
+ assertEquals(dist.normalizeDist(res), dist.calcNormalizedDist(lat, lon, lat + 1, lon - 1), 1e-3);
+ assertEquals(res, dist.approxDistKm(lat, lon, lat + 1, lon - 1), 1e-2);
+
res = 1527.919;
assertEquals(res, dist.calcDistKm(lat, lon, lat - 10, lon + 10), 1e-3);
assertEquals(dist.normalizeDist(res), dist.calcNormalizedDist(lat, lon, lat - 10, lon + 10), 1e-3);
+ assertEquals(res, dist.approxDistKm(lat, lon, lat - 10, lon + 10), 10);
+
+ res = 1474.016;
+ assertEquals(res, dist.calcDistKm(lat, lon, lat + 10, lon - 10), 1e-3);
+ assertEquals(dist.normalizeDist(res), dist.calcNormalizedDist(lat, lon, lat + 10, lon - 10), 1e-3);
+ assertEquals(res, dist.approxDistKm(lat, lon, lat + 10, lon - 10), 10);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.