Skip to content

Commit

Permalink
Clean up PrepareContractionHierarchiesTest
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Dec 4, 2019
1 parent 88bbcc5 commit c13ea6b
Showing 1 changed file with 26 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public class PrepareContractionHierarchiesTest {
private final Weighting weighting = new ShortestWeighting(carEncoder);
private final CHProfile chProfile = CHProfile.nodeBased(weighting);
private final TraversalMode tMode = TraversalMode.NODE_BASED;
private Directory dir;
private GraphHopperStorage g;
private CHGraph lg;

// 0-1-.....-9-10
// | ^ \
// | | |
// 17-16-...-11<-/
public static void initDirected2(Graph g) {
private static void initDirected2(Graph g) {
g.edge(0, 1, 1, true);
g.edge(1, 2, 1, true);
g.edge(2, 3, 1, true);
Expand All @@ -77,29 +78,8 @@ public static void initDirected2(Graph g) {
g.edge(17, 0, 1, true);
}

// 8
// |
// 6->0->1->3->7
// | |
// | v
//10<-2---4<---5
// 9
public static void initDirected1(Graph g) {
g.edge(0, 8, 1, true);
g.edge(0, 1, 1, false);
g.edge(1, 3, 1, false);
g.edge(3, 7, 1, false);
g.edge(3, 5, 1, false);
g.edge(5, 4, 1, false);
g.edge(4, 2, 1, true);
g.edge(2, 9, 1, false);
g.edge(2, 10, 1, false);
g.edge(2, 6, 1, true);
g.edge(6, 0, 1, false);
}

// prepare-routing.svg
public static Graph initShortcutsGraph(Graph g) {
private static void initShortcutsGraph(Graph g) {
g.edge(0, 1, 1, true);
g.edge(0, 2, 1, true);
g.edge(1, 2, 1, true);
Expand All @@ -122,20 +102,9 @@ public static Graph initShortcutsGraph(Graph g) {
g.edge(13, 16, 1, true);
g.edge(15, 16, 2, true);
g.edge(14, 16, 1, true);
return g;
}

GraphHopperStorage createGHStorage() {
return createGHStorage(chProfile);
}

GraphHopperStorage createGHStorage(CHProfile p) {
return new GraphBuilder(encodingManager).setCHProfiles(p).create();
}

GraphHopperStorage createExampleGraph() {
GraphHopperStorage g = createGHStorage();

private static void initExampleGraph(Graph g) {
//5-1-----2
// \ __/|
// 0 |
Expand All @@ -149,26 +118,32 @@ GraphHopperStorage createExampleGraph() {
g.edge(2, 3, 1, true);
g.edge(4, 3, 2, true);
g.edge(5, 1, 2, true);
return g;
}

@Before
public void setUp() {
dir = new GHDirectory("", DAType.RAM_INT);
g = createGHStorage();
lg = g.getCHGraph();
}

private GraphHopperStorage createGHStorage() {
return createGHStorage(chProfile);
}

private GraphHopperStorage createGHStorage(CHProfile p) {
return new GraphBuilder(encodingManager).setCHProfiles(p).create();
}

@Test
public void testReturnsCorrectWeighting() {
GraphHopperStorage g = createGHStorage();
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
prepare.doWork();
assertSame(weighting, prepare.getWeighting());
}

@Test
public void testAddShortcuts() {
GraphHopperStorage g = createExampleGraph();
CHGraph lg = g.getCHGraph();
initExampleGraph(g);
int old = lg.getEdges();
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
prepare.doWork();
Expand All @@ -177,27 +152,23 @@ public void testAddShortcuts() {

@Test
public void testMoreComplexGraph() {
GraphHopperStorage g = createGHStorage();
initShortcutsGraph(g);
int oldCount = g.getEdges();
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
prepare.doWork();
CHGraph lg = g.getCHGraph();
assertEquals(oldCount, g.getEdges());
assertEquals(oldCount + 7, lg.getEdges());
}

@Test
public void testDirectedGraph() {
GraphHopperStorage g = createGHStorage();
g.edge(5, 4, 3, false);
g.edge(4, 5, 10, false);
g.edge(2, 4, 1, false);
g.edge(5, 2, 1, false);
g.edge(3, 5, 1, false);
g.edge(4, 3, 1, false);
g.freeze();
CHGraph lg = g.getCHGraph();
int oldCount = lg.getEdges();
assertEquals(6, oldCount);
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
Expand All @@ -213,13 +184,11 @@ public void testDirectedGraph() {

@Test
public void testDirectedGraph2() {
GraphHopperStorage g = createGHStorage();
initDirected2(g);
int oldCount = g.getEdges();
assertEquals(19, oldCount);
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
prepare.doWork();
CHGraph lg = g.getCHGraph();
// PrepareTowerNodesShortcutsTest.printEdges(g);
assertEquals(oldCount, g.getEdges());
assertEquals(oldCount, GHUtility.count(g.getAllEdges()));
Expand All @@ -235,7 +204,7 @@ public void testDirectedGraph2() {
assertEquals(IntArrayList.from(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), p.calcNodes());
}

void initRoundaboutGraph(Graph g) {
private void initRoundaboutGraph(Graph g) {
// roundabout:
//16-0-9-10--11 12<-13
// \ \ / \
Expand Down Expand Up @@ -288,12 +257,10 @@ void initRoundaboutGraph(Graph g) {

@Test
public void testRoundaboutUnpacking() {
GraphHopperStorage g = createGHStorage();
initRoundaboutGraph(g);
int oldCount = g.getEdges();
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
prepare.doWork();
CHGraph lg = g.getCHGraph();
assertEquals(oldCount, g.getEdges());
assertEquals(oldCount, lg.getOriginalEdges());
assertEquals(oldCount + 23, lg.getEdges());
Expand Down Expand Up @@ -352,8 +319,6 @@ private void initUnpackingGraph(GraphHopperStorage g, CHGraph lg, Weighting w) {

@Test
public void testUnpackingOrder() {
GraphHopperStorage g = createGHStorage();
CHGraph lg = g.getCHGraph();
initUnpackingGraph(g, lg, weighting);
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
// do not call prepare.doWork() here
Expand All @@ -365,8 +330,6 @@ public void testUnpackingOrder() {

@Test
public void testUnpackingOrder_Fastest() {
GraphHopperStorage g = createGHStorage();
CHGraph lg = g.getCHGraph();
Weighting w = new FastestWeighting(carEncoder);
initUnpackingGraph(g, lg, w);

Expand All @@ -380,7 +343,6 @@ public void testUnpackingOrder_Fastest() {

@Test
public void testDisconnects() {
final GraphHopperStorage g = createGHStorage();
// 4
// v
// 0
Expand Down Expand Up @@ -413,7 +375,6 @@ public int getNumNodes() {
}
});
prepare.doWork();
CHGraph lg = g.getCHGraph();
CHEdgeExplorer explorer = lg.createEdgeExplorer();
// shortcuts (and edges) leading to or coming from lower level nodes should be disconnected
// so far we are only disconnecting shortcuts however, see comments in CHGraphImpl.
Expand Down Expand Up @@ -443,7 +404,8 @@ public void testStallOnDemandViaVirtuaNode_issue1574() {
// use fastest weighting in this test to be able to fine-tune some weights via the speed (see below)
Weighting fastestWeighting = new FastestWeighting(carEncoder);
CHProfile chProfile = CHProfile.nodeBased(fastestWeighting);
final GraphHopperStorage g = createGHStorage(chProfile);
g = createGHStorage(chProfile);
lg = g.getCHGraph();
// the following graph reproduces the issue. note that we will use the node ids as ch levels, so there will
// be a shortcut 3->2 visible at node 2 and another one 3->4 visible at node 3.
// we will fine-tune the edge-speeds such that without the fix node 4 will be stalled and node 5 will not get
Expand Down Expand Up @@ -499,7 +461,6 @@ public int getNumNodes() {
return g.getNodes();
}
}).doWork();
CHGraph lg = g.getCHGraph(chProfile);
assertEquals("there should be exactly two (bidirectional) shortcuts (2-3) and (3-4)", 2, lg.getEdges() - lg.getOriginalEdges());

// insert virtual node and edges
Expand Down Expand Up @@ -541,7 +502,6 @@ private EdgeIteratorState getEdge(Graph graph, int from, int to, boolean incomin

@Test
public void testCircleBug() {
GraphHopperStorage g = createGHStorage();
// /--1
// -0--/
// |
Expand All @@ -561,8 +521,6 @@ public void testBug178() {
// 0-1->-2--3--4
// \-<-/
//
GraphHopperStorage g = createGHStorage();
CHGraph lg = g.getCHGraph();
g.edge(1, 2, 1, false);
g.edge(2, 1, 1, false);

Expand Down Expand Up @@ -629,7 +587,7 @@ public void testMultiplePreparationsIdenticalView() {
CHProfile bikeProfile = CHProfile.nodeBased(new ShortestWeighting(tmpBikeEncoder));

List<CHProfile> profiles = Arrays.asList(carProfile, bikeProfile);
GraphHopperStorage ghStorage = new GraphHopperStorage(profiles, dir, tmpEncodingManager, false).create(1000);
GraphHopperStorage ghStorage = new GraphBuilder(tmpEncodingManager).setCHProfiles(profiles).create();
initShortcutsGraph(ghStorage);

ghStorage.freeze();
Expand All @@ -648,11 +606,11 @@ public void testMultiplePreparationsDifferentView() {
CHProfile carProfile = CHProfile.nodeBased(new FastestWeighting(tmpCarEncoder));
CHProfile bikeProfile = CHProfile.nodeBased(new FastestWeighting(tmpBikeEncoder));

List<CHProfile> profiles = Arrays.asList(carProfile, bikeProfile);
GraphHopperStorage ghStorage = new GraphHopperStorage(profiles, dir, tmpEncodingManager, false).create(1000);
GraphHopperStorage ghStorage = new GraphBuilder(tmpEncodingManager).setCHProfiles(carProfile, bikeProfile).create();
initShortcutsGraph(ghStorage);
EdgeIteratorState edge = GHUtility.getEdge(ghStorage, 9, 14).
set(tmpBikeEncoder.getAccessEnc(), false).setReverse(tmpBikeEncoder.getAccessEnc(), false);
GHUtility.getEdge(ghStorage, 9, 14).
set(tmpBikeEncoder.getAccessEnc(), false).
setReverse(tmpBikeEncoder.getAccessEnc(), false);

ghStorage.freeze();

Expand All @@ -668,9 +626,7 @@ public void testReusingNodeOrdering() {
EncodingManager em = EncodingManager.create(carFlagEncoder, motorCycleEncoder);
CHProfile carProfile = CHProfile.nodeBased(new FastestWeighting(carFlagEncoder));
CHProfile motorCycleProfile = CHProfile.nodeBased(new FastestWeighting(motorCycleEncoder));
Directory dir = new RAMDirectory();
GraphHopperStorage ghStorage = new GraphHopperStorage(Arrays.asList(carProfile, motorCycleProfile), dir, em, false);
ghStorage.create(1000);
GraphHopperStorage ghStorage = new GraphBuilder(em).setCHProfiles(carProfile, motorCycleProfile).create();

int numNodes = 5_000;
int numQueries = 100;
Expand Down Expand Up @@ -711,7 +667,7 @@ public void testReusingNodeOrdering() {
assertTrue("reusing node ordering should speed up ch contraction", timeMotorCycle < 0.5 * timeCar);
}

void checkPath(GraphHopperStorage g, CHProfile p, int expShortcuts, double expDistance, IntIndexedContainer expNodes) {
private void checkPath(GraphHopperStorage g, CHProfile p, int expShortcuts, double expDistance, IntIndexedContainer expNodes) {
CHGraph lg = g.getCHGraph(p);
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g, p);
prepare.doWork();
Expand Down

0 comments on commit c13ea6b

Please sign in to comment.