Skip to content

Commit

Permalink
braking change: rename to always use traversal key, fixes #1549
Browse files Browse the repository at this point in the history
  • Loading branch information
karussell committed Mar 20, 2019
1 parent 35b58dd commit 7e66c44
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 48 deletions.
1 change: 1 addition & 0 deletions core/files/changelog.txt
@@ -1,4 +1,5 @@
0.12 0.12
renamed public TraversalMode.createTraversalId => TraversalMode.createTraversalKey, protected AbstractBidirAlgo.getTraversalId => AbstractBidirAlgo.getTraversalKey
access refactoring #1436 that moves AccessValue into SpatialRule.Access access refactoring #1436 that moves AccessValue into SpatialRule.Access
refactoring of EncodingManager to use builder pattern. Migration should be simple. Replace new EncodingManager with EncodingManager.create refactoring of EncodingManager to use builder pattern. Migration should be simple. Replace new EncodingManager with EncodingManager.create
The methods GraphHopper.setEnableInstructions/setPreferredLanguage is now in EncodingManager.Builder The methods GraphHopper.setEnableInstructions/setPreferredLanguage is now in EncodingManager.Builder
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/com/graphhopper/routing/AStar.java
Expand Up @@ -103,15 +103,15 @@ private Path runAlgo() {
if (Double.isInfinite(alreadyVisitedWeight)) if (Double.isInfinite(alreadyVisitedWeight))
continue; continue;


int traversalId = traversalMode.createTraversalId(iter, false); int traversalKey = traversalMode.createTraversalKey(iter, false);
AStarEntry ase = fromMap.get(traversalId); AStarEntry ase = fromMap.get(traversalKey);
if (ase == null || ase.weightOfVisitedPath > alreadyVisitedWeight) { if (ase == null || ase.weightOfVisitedPath > alreadyVisitedWeight) {
int neighborNode = iter.getAdjNode(); int neighborNode = iter.getAdjNode();
currWeightToGoal = weightApprox.approximate(neighborNode); currWeightToGoal = weightApprox.approximate(neighborNode);
estimationFullWeight = alreadyVisitedWeight + currWeightToGoal; estimationFullWeight = alreadyVisitedWeight + currWeightToGoal;
if (ase == null) { if (ase == null) {
ase = new AStarEntry(iter.getEdge(), neighborNode, estimationFullWeight, alreadyVisitedWeight); ase = new AStarEntry(iter.getEdge(), neighborNode, estimationFullWeight, alreadyVisitedWeight);
fromMap.put(traversalId, ase); fromMap.put(traversalKey, ase);
} else { } else {
// assert (ase.weight > 0.9999999 * estimationFullWeight) : "Inconsistent distance estimate. It is expected weight >= estimationFullWeight but was " // assert (ase.weight > 0.9999999 * estimationFullWeight) : "Inconsistent distance estimate. It is expected weight >= estimationFullWeight but was "
// + ase.weight + " < " + estimationFullWeight + " (" + ase.weight / estimationFullWeight + "), and weightOfVisitedPath:" // + ase.weight + " < " + estimationFullWeight + " (" + ase.weight / estimationFullWeight + "), and weightOfVisitedPath:"
Expand All @@ -126,7 +126,7 @@ private Path runAlgo() {
ase.parent = currEdge; ase.parent = currEdge;
prioQueueOpenSet.add(ase); prioQueueOpenSet.add(ase);


updateBestPath(iter, ase, traversalId); updateBestPath(iter, ase, traversalKey);
} }
} }


Expand Down Expand Up @@ -157,7 +157,7 @@ public int getVisitedNodes() {
return visitedCount; return visitedCount;
} }


protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry bestSPTEntry, int traversalId) { protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry bestSPTEntry, int traversalKey) {
} }


public static class AStarEntry extends SPTEntry { public static class AStarEntry extends SPTEntry {
Expand Down
Expand Up @@ -216,14 +216,14 @@ private void fillEdges(SPTEntry currEdge, PriorityQueue<SPTEntry> prioQueue,
continue; continue;


final int origEdgeId = getOrigEdgeId(iter, reverse); final int origEdgeId = getOrigEdgeId(iter, reverse);
final int traversalId = getTraversalId(iter, origEdgeId, reverse); final int traversalKey = getTraversalKey(iter, origEdgeId, reverse);
final double weight = calcWeight(iter, currEdge, reverse); final double weight = calcWeight(iter, currEdge, reverse);
if (Double.isInfinite(weight)) if (Double.isInfinite(weight))
continue; continue;
SPTEntry entry = bestWeightMap.get(traversalId); SPTEntry entry = bestWeightMap.get(traversalKey);
if (entry == null) { if (entry == null) {
entry = createEntry(iter, origEdgeId, weight, currEdge, reverse); entry = createEntry(iter, origEdgeId, weight, currEdge, reverse);
bestWeightMap.put(traversalId, entry); bestWeightMap.put(traversalKey, entry);
prioQueue.add(entry); prioQueue.add(entry);
} else if (entry.getWeightOfVisitedPath() > weight) { } else if (entry.getWeightOfVisitedPath() > weight) {
prioQueue.remove(entry); prioQueue.remove(entry);
Expand All @@ -233,12 +233,12 @@ private void fillEdges(SPTEntry currEdge, PriorityQueue<SPTEntry> prioQueue,
continue; continue;


if (updateBestPath) if (updateBestPath)
updateBestPath(iter, entry, traversalId, reverse); updateBestPath(iter, entry, traversalKey, reverse);
} }
} }


protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int traversalId, boolean reverse) { protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int traversalKey, boolean reverse) {
SPTEntry entryOther = bestWeightMapOther.get(traversalId); SPTEntry entryOther = bestWeightMapOther.get(traversalKey);
if (entryOther == null) if (entryOther == null)
return; return;


Expand Down Expand Up @@ -283,8 +283,8 @@ protected int getIncomingEdge(SPTEntry entry) {
return entry.edge; return entry.edge;
} }


protected int getTraversalId(EdgeIteratorState edge, int origEdgeId, boolean reverse) { protected int getTraversalKey(EdgeIteratorState edge, int origEdgeId, boolean reverse) {
return traversalMode.createTraversalId(edge, reverse); return traversalMode.createTraversalKey(edge, reverse);
} }


protected double calcWeight(EdgeIteratorState iter, SPTEntry currEdge, boolean reverse) { protected double calcWeight(EdgeIteratorState iter, SPTEntry currEdge, boolean reverse) {
Expand Down
Expand Up @@ -78,7 +78,7 @@ public boolean finished() {
} }


@Override @Override
protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int traversalId, boolean reverse) { protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int traversalKey, boolean reverse) {
// special case where the fwd/bwd search runs directly into the opposite node, for example if the highest level // special case where the fwd/bwd search runs directly into the opposite node, for example if the highest level
// node of the shortest path matches the source or target. in this case one of the searches does not contribute // node of the shortest path matches the source or target. in this case one of the searches does not contribute
// anything to the shortest path. // anything to the shortest path.
Expand Down Expand Up @@ -142,7 +142,7 @@ protected int getIncomingEdge(SPTEntry entry) {
} }


@Override @Override
protected int getTraversalId(EdgeIteratorState edge, int origEdgeId, boolean reverse) { protected int getTraversalKey(EdgeIteratorState edge, int origEdgeId, boolean reverse) {
int baseNode = graph.getOtherNode(origEdgeId, edge.getAdjNode()); int baseNode = graph.getOtherNode(origEdgeId, edge.getAdjNode());
return GHUtility.createEdgeKey(baseNode, edge.getAdjNode(), origEdgeId, reverse); return GHUtility.createEdgeKey(baseNode, edge.getAdjNode(), origEdgeId, reverse);
} }
Expand Down
26 changes: 12 additions & 14 deletions core/src/main/java/com/graphhopper/routing/AlternativeRoute.java
Expand Up @@ -17,12 +17,10 @@
*/ */
package com.graphhopper.routing; package com.graphhopper.routing;


import com.carrotsearch.hppc.IntObjectMap;
import com.carrotsearch.hppc.IntSet; import com.carrotsearch.hppc.IntSet;
import com.carrotsearch.hppc.predicates.IntObjectPredicate; import com.carrotsearch.hppc.predicates.IntObjectPredicate;
import com.graphhopper.coll.GHIntHashSet; import com.graphhopper.coll.GHIntHashSet;
import com.graphhopper.coll.GHIntObjectHashMap; import com.graphhopper.coll.GHIntObjectHashMap;
import com.graphhopper.routing.AStar.AStarEntry;
import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.util.TraversalMode;
import com.graphhopper.routing.weighting.WeightApproximator; import com.graphhopper.routing.weighting.WeightApproximator;
import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.routing.weighting.Weighting;
Expand Down Expand Up @@ -302,8 +300,8 @@ public List<AlternativeInfo> calcAlternatives(final int maxPaths,
final double maxShareFactor, final double shareInfluence, final double maxShareFactor, final double shareInfluence,
final double minPlateauFactor, final double plateauInfluence) { final double minPlateauFactor, final double plateauInfluence) {
final double maxWeight = maxWeightFactor * bestPath.getWeight(); final double maxWeight = maxWeightFactor * bestPath.getWeight();
final GHIntObjectHashMap<IntSet> traversalIDMap = new GHIntObjectHashMap<>(); final GHIntObjectHashMap<IntSet> traversalKeyMap = new GHIntObjectHashMap<>();
final AtomicInteger startTID = addToMap(traversalIDMap, bestPath); final AtomicInteger startTID = addToMap(traversalKeyMap, bestPath);


// find all 'good' alternatives from forward-SPT matching the backward-SPT and optimize by // find all 'good' alternatives from forward-SPT matching the backward-SPT and optimize by
// small total weight (1), small share and big plateau (3a+b) and do these expensive calculations // small total weight (1), small share and big plateau (3a+b) and do these expensive calculations
Expand All @@ -323,8 +321,8 @@ public List<AlternativeInfo> calcAlternatives(final int maxPaths,


bestWeightMapFrom.forEach(new IntObjectPredicate<SPTEntry>() { bestWeightMapFrom.forEach(new IntObjectPredicate<SPTEntry>() {
@Override @Override
public boolean apply(final int traversalId, final SPTEntry fromSPTEntry) { public boolean apply(final int traversalKey, final SPTEntry fromSPTEntry) {
SPTEntry toSPTEntry = bestWeightMapTo.get(traversalId); SPTEntry toSPTEntry = bestWeightMapTo.get(traversalKey);
if (toSPTEntry == null) if (toSPTEntry == null)
return true; return true;


Expand Down Expand Up @@ -359,9 +357,9 @@ public boolean apply(final int traversalId, final SPTEntry fromSPTEntry) {
// e.g. when starting point has two edges and one is part of the best path the other edge is path of an alternative // e.g. when starting point has two edges and one is part of the best path the other edge is path of an alternative
assert traversalMode.isEdgeBased(); assert traversalMode.isEdgeBased();
} else { } else {
int nextToTraversalId = traversalMode.createTraversalId(tmpFromEntry.adjNode, int nextToTraversalKey = traversalMode.createTraversalKey(tmpFromEntry.adjNode,
tmpFromEntry.parent.adjNode, tmpFromEntry.edge, true); tmpFromEntry.parent.adjNode, tmpFromEntry.edge, true);
SPTEntry tmpNextToSPTEntry = bestWeightMapTo.get(nextToTraversalId); SPTEntry tmpNextToSPTEntry = bestWeightMapTo.get(nextToTraversalKey);
if (tmpNextToSPTEntry == null) if (tmpNextToSPTEntry == null)
return true; return true;


Expand All @@ -387,10 +385,10 @@ public boolean apply(final int traversalId, final SPTEntry fromSPTEntry) {
SPTEntry prevToSPTEntry = toSPTEntry; SPTEntry prevToSPTEntry = toSPTEntry;
// List<Integer> plateauEdges = new ArrayList<Integer>(); // List<Integer> plateauEdges = new ArrayList<Integer>();
while (prevToSPTEntry.parent != null) { while (prevToSPTEntry.parent != null) {
int nextFromTraversalId = traversalMode.createTraversalId(prevToSPTEntry.adjNode, prevToSPTEntry.parent.adjNode, int nextFromTraversalKey = traversalMode.createTraversalKey(prevToSPTEntry.adjNode, prevToSPTEntry.parent.adjNode,
prevToSPTEntry.edge, false); prevToSPTEntry.edge, false);


SPTEntry nextFromSPTEntry = bestWeightMapFrom.get(nextFromTraversalId); SPTEntry nextFromSPTEntry = bestWeightMapFrom.get(nextFromTraversalKey);
// end of a plateau // end of a plateau
if (nextFromSPTEntry == null) if (nextFromSPTEntry == null)
break; break;
Expand Down Expand Up @@ -430,7 +428,7 @@ public boolean apply(final int traversalId, final SPTEntry fromSPTEntry) {


// for now do not add alternatives to set, if we do we need to remove then on alternatives.clear too (see below) // for now do not add alternatives to set, if we do we need to remove then on alternatives.clear too (see below)
// AtomicInteger tid = addToMap(traversalIDMap, path); // AtomicInteger tid = addToMap(traversalIDMap, path);
// int tid = traversalMode.createTraversalId(path.calcEdges().get(0), false); // int tid = traversalMode.createTraversalKey(path.calcEdges().get(0), false);
alternatives.add(new AlternativeInfo(sortBy, path, fromEE, toEE, shareWeight, altNames)); alternatives.add(new AlternativeInfo(sortBy, path, fromEE, toEE, shareWeight, altNames));


Collections.sort(alternatives, ALT_COMPARATOR); Collections.sort(alternatives, ALT_COMPARATOR);
Expand All @@ -451,7 +449,7 @@ public boolean apply(final int traversalId, final SPTEntry fromSPTEntry) {
SPTEntry getFirstShareEE(SPTEntry startEE, boolean reverse) { SPTEntry getFirstShareEE(SPTEntry startEE, boolean reverse) {
while (startEE.parent != null) { while (startEE.parent != null) {
// TODO we could make use of traversal ID directly if stored in SPTEntry // TODO we could make use of traversal ID directly if stored in SPTEntry
int tid = traversalMode.createTraversalId(startEE.adjNode, startEE.parent.adjNode, startEE.edge, reverse); int tid = traversalMode.createTraversalKey(startEE.adjNode, startEE.parent.adjNode, startEE.edge, reverse);
if (isAlreadyExisting(tid)) if (isAlreadyExisting(tid))
return startEE; return startEE;


Expand All @@ -467,7 +465,7 @@ SPTEntry getFirstShareEE(SPTEntry startEE, boolean reverse) {
*/ */
boolean isAlreadyExisting(final int tid) { boolean isAlreadyExisting(final int tid) {
final AtomicBoolean exists = new AtomicBoolean(false); final AtomicBoolean exists = new AtomicBoolean(false);
traversalIDMap.forEach(new IntObjectPredicate<IntSet>() { traversalKeyMap.forEach(new IntObjectPredicate<IntSet>() {
@Override @Override
public boolean apply(int key, IntSet set) { public boolean apply(int key, IntSet set) {
if (set.contains(tid)) { if (set.contains(tid)) {
Expand Down Expand Up @@ -526,7 +524,7 @@ AtomicInteger addToMap(GHIntObjectHashMap<IntSet> map, Path path) {
IntSet set = new GHIntHashSet(); IntSet set = new GHIntHashSet();
final AtomicInteger startTID = new AtomicInteger(-1); final AtomicInteger startTID = new AtomicInteger(-1);
for (EdgeIteratorState iterState : path.calcEdges()) { for (EdgeIteratorState iterState : path.calcEdges()) {
int tid = traversalMode.createTraversalId(iterState, false); int tid = traversalMode.createTraversalKey(iterState, false);
set.add(tid); set.add(tid);
if (startTID.get() < 0) { if (startTID.get() < 0) {
// for node based traversal we need to explicitely add base node as starting node and to list // for node based traversal we need to explicitely add base node as starting node and to list
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/com/graphhopper/routing/Dijkstra.java
Expand Up @@ -80,16 +80,16 @@ protected void runAlgo() {
if (!accept(iter, currEdge.edge)) if (!accept(iter, currEdge.edge))
continue; continue;


int traversalId = traversalMode.createTraversalId(iter, false); int traversalKey = traversalMode.createTraversalKey(iter, false);
double tmpWeight = weighting.calcWeight(iter, false, currEdge.edge) + currEdge.weight; double tmpWeight = weighting.calcWeight(iter, false, currEdge.edge) + currEdge.weight;
if (Double.isInfinite(tmpWeight)) if (Double.isInfinite(tmpWeight))
continue; continue;


SPTEntry nEdge = fromMap.get(traversalId); SPTEntry nEdge = fromMap.get(traversalKey);
if (nEdge == null) { if (nEdge == null) {
nEdge = new SPTEntry(iter.getEdge(), iter.getAdjNode(), tmpWeight); nEdge = new SPTEntry(iter.getEdge(), iter.getAdjNode(), tmpWeight);
nEdge.parent = currEdge; nEdge.parent = currEdge;
fromMap.put(traversalId, nEdge); fromMap.put(traversalKey, nEdge);
fromHeap.add(nEdge); fromHeap.add(nEdge);
} else if (nEdge.weight > tmpWeight) { } else if (nEdge.weight > tmpWeight) {
fromHeap.remove(nEdge); fromHeap.remove(nEdge);
Expand All @@ -100,7 +100,7 @@ protected void runAlgo() {
} else } else
continue; continue;


updateBestPath(iter, nEdge, traversalId); updateBestPath(iter, nEdge, traversalKey);
} }


if (fromHeap.isEmpty()) if (fromHeap.isEmpty())
Expand Down Expand Up @@ -131,7 +131,7 @@ public int getVisitedNodes() {
return visitedNodes; return visitedNodes;
} }


protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry bestSPTEntry, int traversalId) { protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry bestSPTEntry, int traversalKey) {
} }


@Override @Override
Expand Down
Expand Up @@ -18,7 +18,6 @@
package com.graphhopper.routing; package com.graphhopper.routing;


import com.carrotsearch.hppc.IntObjectMap; import com.carrotsearch.hppc.IntObjectMap;
import com.graphhopper.routing.util.TraversalMode;
import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.Graph; import com.graphhopper.storage.Graph;
import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.SPTEntry;
Expand Down Expand Up @@ -63,8 +62,8 @@ private boolean entryIsStallable(SPTEntry entry, IntObjectMap<SPTEntry> bestWeig
// reached via a suboptimal path. We do this regardless of the CH level of the adjacent nodes. // reached via a suboptimal path. We do this regardless of the CH level of the adjacent nodes.
EdgeIterator iter = edgeExplorer.setBaseNode(entry.adjNode); EdgeIterator iter = edgeExplorer.setBaseNode(entry.adjNode);
while (iter.next()) { while (iter.next()) {
int traversalId = traversalMode.createTraversalId(iter, reverse); int traversalKey = traversalMode.createTraversalKey(iter, reverse);
SPTEntry adjNode = bestWeightMap.get(traversalId); SPTEntry adjNode = bestWeightMap.get(traversalKey);
if (adjNode != null && if (adjNode != null &&
adjNode.weight + weighting.calcWeight(iter, !reverse, getIncomingEdge(entry)) < entry.weight) { adjNode.weight + weighting.calcWeight(iter, !reverse, getIncomingEdge(entry)) < entry.weight) {
return true; return true;
Expand Down
Expand Up @@ -90,7 +90,7 @@ public static TraversalMode fromString(String name) {
* backward searches in bidirectional algorithms. * backward searches in bidirectional algorithms.
* @return the identifier to access the shortest path tree * @return the identifier to access the shortest path tree
*/ */
public final int createTraversalId(EdgeIteratorState iterState, boolean reverse) { public final int createTraversalKey(EdgeIteratorState iterState, boolean reverse) {
if (edgeBased) { if (edgeBased) {
if (noOfStates == 1) if (noOfStates == 1)
return iterState.getEdge(); return iterState.getEdge();
Expand All @@ -102,9 +102,9 @@ public final int createTraversalId(EdgeIteratorState iterState, boolean reverse)
} }


/** /**
* If you have an EdgeIteratorState the other createTraversalId is preferred! * If you have an EdgeIteratorState the other createTraversalKey is preferred!
*/ */
public final int createTraversalId(int baseNode, int adjNode, int edgeId, boolean reverse) { public final int createTraversalKey(int baseNode, int adjNode, int edgeId, boolean reverse) {
if (edgeBased) { if (edgeBased) {
if (noOfStates == 1) if (noOfStates == 1)
return edgeId; return edgeId;
Expand Down
6 changes: 3 additions & 3 deletions tools/src/main/java/com/graphhopper/ui/DebugAStarBi.java
Expand Up @@ -44,11 +44,11 @@ public void setGraphics2D(Graphics2D g2) {
} }


@Override @Override
public void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int traversalId, boolean reverse) { public void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int traversalKey, boolean reverse) {
if (g2 != null) { if (g2 != null) {
mg.plotNode(g2, traversalId, Color.YELLOW); mg.plotNode(g2, traversalKey, Color.YELLOW);
} }
super.updateBestPath(edgeState, entry, traversalId, reverse); super.updateBestPath(edgeState, entry, traversalKey, reverse);
} }


@Override @Override
Expand Down
Expand Up @@ -47,11 +47,11 @@ public void setGraphics2D(Graphics2D g2) {
} }


@Override @Override
public void updateBestPath(EdgeIteratorState es, SPTEntry entry, int traversalId, boolean reverse) { public void updateBestPath(EdgeIteratorState es, SPTEntry entry, int traversalKey, boolean reverse) {
if (g2 != null) { if (g2 != null) {
mg.plotEdge(g2, na.getLat(entry.parent.adjNode), na.getLon(entry.parent.adjNode), na.getLat(entry.adjNode), na.getLon(entry.adjNode), .8f); mg.plotEdge(g2, na.getLat(entry.parent.adjNode), na.getLon(entry.parent.adjNode), na.getLat(entry.adjNode), na.getLon(entry.adjNode), .8f);
} }
// System.out.println("new node:" + currLoc); // System.out.println("new node:" + currLoc);
super.updateBestPath(es, entry, traversalId, reverse); super.updateBestPath(es, entry, traversalKey, reverse);
} }
} }
6 changes: 3 additions & 3 deletions tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java
Expand Up @@ -118,11 +118,11 @@ public void setGraphics2D(Graphics2D g2) {
} }


@Override @Override
public void updateBestPath(EdgeIteratorState es, SPTEntry entry, int traversalId, boolean reverse) { public void updateBestPath(EdgeIteratorState es, SPTEntry entry, int traversalKey, boolean reverse) {
if (g2 != null) if (g2 != null)
mg.plotNode(g2, traversalId, Color.YELLOW, 6); mg.plotNode(g2, traversalKey, Color.YELLOW, 6);


super.updateBestPath(es, entry, traversalId, reverse); super.updateBestPath(es, entry, traversalKey, reverse);
} }
} }


Expand Down

0 comments on commit 7e66c44

Please sign in to comment.