Search in sources :

Example 31 with GraphBuilder

use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.

the class EdgeBasedTarjanSCCTest method biggerGraph.

@Test
public void biggerGraph() {
    GraphHopperStorage g = new GraphBuilder(em).create();
    // this graph has two bigger components (nodes 0, 1, 3 and the others). Still there are some (directed) edges
    // that do not belong to these components but rather represent isolated single-edge components
    // 0 - 1 < 2 - 4 > 5
    // |   |       |
    // |    \< 6 - 7
    // 3        \- 8
    // edge-keys 0,1
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(1));
    // edge-keys 2,3
    GHUtility.setSpeed(60, true, false, encoder, g.edge(2, 1).setDistance(1));
    // edge-keys 4,5
    GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 3).setDistance(1));
    // edge-keys 6,7
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 4).setDistance(1));
    // edge-keys 8,9
    GHUtility.setSpeed(60, true, false, encoder, g.edge(6, 2).setDistance(1));
    // edge-keys 10,11
    GHUtility.setSpeed(60, true, false, encoder, g.edge(4, 5).setDistance(1));
    // edge-keys 12,13
    GHUtility.setSpeed(60, true, true, encoder, g.edge(5, 7).setDistance(1));
    // edge-keys 14,15
    GHUtility.setSpeed(60, true, true, encoder, g.edge(6, 7).setDistance(1));
    // edge-keys 16,17
    GHUtility.setSpeed(60, true, true, encoder, g.edge(6, 8).setDistance(1));
    ConnectedComponents result = EdgeBasedTarjanSCC.findComponentsRecursive(g, fwdAccessFilter, false);
    assertEquals(18, result.getEdgeKeys());
    assertEquals(6, result.getTotalComponents());
    assertEquals(2, result.getComponents().size());
    assertEquals(result.getComponents().get(1), result.getBiggestComponent());
    assertEquals(IntArrayList.from(1, 5, 4, 0), result.getComponents().get(0));
    assertEquals(IntArrayList.from(7, 8, 13, 14, 17, 16, 15, 12, 10, 6), result.getComponents().get(1));
    assertEquals(4, result.getSingleEdgeComponents().cardinality());
    for (IntCursor c : IntArrayList.from(9, 2, 3, 11)) {
        assertTrue(result.getSingleEdgeComponents().get(c.value));
    }
}
Also used : ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 32 with GraphBuilder

use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworksTest method createSubnetworkTestStorageWithOneWays.

private GraphHopperStorage createSubnetworkTestStorageWithOneWays(EncodingManager em, FlagEncoder encoder) {
    if (em.fetchEdgeEncoders().size() > 1)
        fail("Warning: This method only sets access/speed for a single encoder, but the given encoding manager has multiple encoders");
    GraphHopperStorage g = new GraphBuilder(em).create();
    // 0 - 1 - 2 - 3 - 4 <- 5 - 6
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 2).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 3).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(3, 4).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, g.edge(5, 4).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(5, 6).setDistance(1));
    // 7 -> 8 - 9 - 10
    GHUtility.setSpeed(60, true, false, encoder, g.edge(7, 8).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(8, 9).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(9, 10).setDistance(1));
    return g;
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage)

Example 33 with GraphBuilder

use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworksTest method createSubnetworkTestStorage.

private static GraphHopperStorage createSubnetworkTestStorage(EncodingManager encodingManager) {
    GraphHopperStorage g = new GraphBuilder(encodingManager).create();
    // 5 - 6
    // | /
    // 4
    // | <- (no access flags unless we change it)
    // 0 - 1 - 3 - 7 - 8
    // |       |
    // 2 -------
    g.edge(3, 4).setDistance(1);
    g.edge(0, 1).setDistance(1);
    g.edge(1, 3).setDistance(1);
    g.edge(0, 2).setDistance(1);
    g.edge(2, 3).setDistance(1);
    g.edge(3, 7).setDistance(1);
    g.edge(7, 8).setDistance(1);
    g.edge(4, 5).setDistance(1);
    g.edge(5, 6).setDistance(1);
    g.edge(4, 6).setDistance(1);
    // set access for all encoders
    AllEdgesIterator iter = g.getAllEdges();
    while (iter.next()) {
        // edge 3-4 gets no speed/access by default
        if (iter.getEdge() == 0)
            continue;
        for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) {
            iter.set(encoder.getAverageSpeedEnc(), 10);
            iter.set(encoder.getAccessEnc(), true, true);
        }
    }
    return g;
}
Also used : AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage)

Example 34 with GraphBuilder

use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.

the class TarjanSCCTest method testTarjan_issue761.

@Test
public void testTarjan_issue761() {
    GraphHopperStorage graph = new GraphBuilder(em).create();
    // 11-10-9
    // |     |
    // 0-1-2->3->4->5
    // |     |
    // 6     12
    // |     |
    // 7     13-14
    // |       \|
    // 8        15-16
    // oneway main road
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 3).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 4).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 5).setDistance(1));
    // going south from main road
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(3, 6).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(6, 7).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(7, 8).setDistance(1));
    // connects the two nodes 2 and 4
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(4, 9).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(9, 10).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(10, 11).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(11, 2).setDistance(1));
    // eastern part (only connected by a single directed edge to the rest of the graph)
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(5, 12).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(12, 13).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(13, 14).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(14, 15).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(15, 13).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(15, 16).setDistance(1));
    TarjanSCC.ConnectedComponents scc = TarjanSCC.findComponentsRecursive(graph, edgeFilter, false);
    assertEquals(2, scc.getTotalComponents());
    assertTrue(scc.getSingleNodeComponents().isEmpty());
    assertEquals(17, scc.getNodes());
    assertEquals(scc.getComponents().get(1), scc.getBiggestComponent());
    assertEquals(2, scc.getComponents().size());
    assertEquals(IntArrayList.from(14, 16, 15, 13, 12, 5), scc.getComponents().get(0));
    assertEquals(IntArrayList.from(8, 7, 6, 3, 4, 9, 10, 11, 2, 1, 0), scc.getComponents().get(1));
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 35 with GraphBuilder

use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.

the class TarjanSCCTest method testFindComponents.

@Test
public void testFindComponents() {
    GraphHopperStorage graph = new GraphBuilder(em).create();
    // big network (has two components actually, because 9->12 is a one-way)
    // ---
    // /     \
    // 4 < 1 - 2
    // |   |
    // <-- 8 - 11 - 12 < 9 - 15
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 4).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 8).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 4).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(8, 4).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(8, 11).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(12, 11).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(9, 12).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(9, 15).setDistance(1));
    // large network
    // 5 --------
    // |        |
    // 3 - 0 - 13
    // \ |
    // 7
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 13).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 3).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 7).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(3, 7).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(3, 5).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(13, 5).setDistance(1));
    // small network
    // 6 - 14 - 10
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(6, 14).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(10, 14).setDistance(1));
    TarjanSCC.ConnectedComponents scc = TarjanSCC.findComponentsRecursive(graph, edgeFilter, false);
    List<IntArrayList> components = scc.getComponents();
    assertEquals(4, components.size());
    assertEquals(IntArrayList.from(13, 5, 3, 7, 0), components.get(0));
    assertEquals(IntArrayList.from(2, 4, 12, 11, 8, 1), components.get(1));
    assertEquals(IntArrayList.from(10, 14, 6), components.get(2));
    assertEquals(IntArrayList.from(15, 9), components.get(3));
    assertEquals(16, scc.getNodes());
    assertEquals(0, scc.getSingleNodeComponents().cardinality());
    assertEquals(components.get(1), scc.getBiggestComponent());
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) IntArrayList(com.carrotsearch.hppc.IntArrayList) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Aggregations

GraphBuilder (com.graphhopper.storage.GraphBuilder)98 Test (org.junit.jupiter.api.Test)60 Graph (com.graphhopper.storage.Graph)48 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)40 NodeAccess (com.graphhopper.storage.NodeAccess)30 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)22 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)21 EncodingManager (com.graphhopper.routing.util.EncodingManager)19 Test (org.junit.Test)16 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)15 RepeatedTest (org.junit.jupiter.api.RepeatedTest)14 ConnectedComponents (com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents)12 Dijkstra (com.graphhopper.routing.Dijkstra)10 Path (com.graphhopper.routing.Path)10 ReaderWay (com.graphhopper.reader.ReaderWay)9 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)6 IntsRef (com.graphhopper.storage.IntsRef)6 Random (java.util.Random)6