Search in sources :

Example 1 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.

the class TraversalAStar method findPaths.

private Iterable<WeightedPath> findPaths(Node start, Node end, boolean multiplePaths) {
    PathInterest interest;
    if (multiplePaths) {
        interest = stopAfterLowestWeight ? PathInterestFactory.allShortest() : PathInterestFactory.all();
    } else {
        interest = PathInterestFactory.single();
    }
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription traversalDescription = db.traversalDescription().uniqueness(Uniqueness.NONE).expand(expander, initialState);
    lastTraverser = traversalDescription.order(new SelectorFactory(end, interest)).evaluator(includeWhereEndNodeIs(end)).traverse(start);
    return new Iterable<WeightedPath>() {

        @Override
        public Iterator<WeightedPath> iterator() {
            return new WeightedPathIterator(lastTraverser.iterator(), costEvaluator, stopAfterLowestWeight);
        }
    };
}
Also used : WeightedPathIterator(org.neo4j.graphalgo.impl.util.WeightedPathIterator) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) PathInterest(org.neo4j.graphalgo.impl.util.PathInterest) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Example 2 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.

the class AStarPerformanceIT method somePerformanceTesting.

@Test
public void somePerformanceTesting() throws Exception {
    // GIVEN
    int numberOfNodes = 200000;
    GeoDataGenerator generator = new GeoDataGenerator(numberOfNodes, 5d, 1000, 1000);
    generator.generate(directory);
    // WHEN
    long[][] points = new long[][] { new long[] { 9415, 158154 }, new long[] { 89237, 192863 }, new long[] { 68072, 150484 }, new long[] { 186309, 194495 }, new long[] { 152097, 99289 }, new long[] { 92150, 161182 }, new long[] { 188446, 115873 }, new long[] { 85033, 7772 }, new long[] { 291, 86707 }, new long[] { 188345, 158468 } };
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(directory.getAbsoluteFile());
    PathFinder<WeightedPath> algo = aStar(allTypesAndDirections(), doubleCostEvaluator("weight", 0), GeoDataGenerator.estimateEvaluator());
    for (int i = 0; i < 10; i++) {
        System.out.println("----- " + i);
        for (long[] p : points) {
            try (Transaction tx = db.beginTx()) {
                Node start = db.getNodeById(p[0]);
                Node end = db.getNodeById(p[1]);
                long time = currentTimeMillis();
                WeightedPath path = algo.findSinglePath(start, end);
                time = currentTimeMillis() - time;
                System.out.println("time: " + time + ", len:" + path.length() + ", weight:" + path.weight());
                tx.success();
            }
        }
    }
    // THEN
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 3 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.

the class DijkstraIncreasingWeightTest method testForLoops.

@Test(timeout = 5000)
public void testForLoops() {
    try (Transaction tx = graphDb.beginTx()) {
        Node s = graph.makeNode("s");
        Node t = graph.makeNode("t");
        // Blob loop
        graph.makeEdge("s", "a1", "length", 1);
        graph.makeEdge("a1", "b", "length", 0);
        graph.makeEdge("b", "a1", "length", 0);
        // Self loop
        graph.makeEdge("a1", "a2", "length", 1);
        graph.makeEdge("a2", "a2", "length", 0);
        // Diamond loop
        graph.makeEdge("a2", "a3", "length", 1);
        graph.makeEdge("a3", "c1", "length", 0);
        graph.makeEdge("a3", "c2", "length", 0);
        graph.makeEdge("c1", "a4", "length", 0);
        graph.makeEdge("c1", "a4", "length", 0);
        graph.makeEdge("a4", "t", "length", 1);
        PathExpander expander = PathExpanders.allTypesAndDirections();
        Dijkstra algo = new Dijkstra(expander, CommonEvaluators.doubleCostEvaluator("length"), PathInterestFactory.all(NoneStrictMath.EPSILON));
        Iterator<WeightedPath> paths = algo.findAllPaths(s, t).iterator();
        assertTrue("Expected at least one path", paths.hasNext());
        assertTrue("Expected first path of length 6", paths.next().length() == 6);
        assertTrue("Expected at least two paths", paths.hasNext());
        assertTrue("Expected second path of length 6", paths.next().length() == 6);
        assertFalse("Expected exactly two paths", paths.hasNext());
        tx.success();
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) Dijkstra(org.neo4j.graphalgo.impl.path.Dijkstra) Test(org.junit.Test)

Example 4 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.

the class DijkstraIncreasingWeightTest method shouldReturnPathsInIncreasingOrderOfCost.

@Test
public void shouldReturnPathsInIncreasingOrderOfCost() {
    /*
         *
         *      ----- (e) - 1 - (f) ---
         *    /                         \
         *   /    ------- (a) --------   \
         *  1   /            \         \  2
         *  |  2              0         0 |
         *  | /                \         \|
         * (s) - 1 - (c) - 1 - (d) - 1 - (t)
         *   \                 /
         *    -- 1 - (b) - 1 -
         *
         */
    Node s = graph.makeNode("s");
    Node t = graph.makeNode("t");
    graph.makeEdgeChain("s,e,f", "length", 1.0);
    graph.makeEdge("f", "t", "length", 2);
    graph.makeEdge("s", "a", "length", 2);
    graph.makeEdge("a", "t", "length", 0);
    graph.makeEdge("s", "c", "length", 1);
    graph.makeEdge("c", "d", "length", 1);
    graph.makeEdge("s", "b", "length", 1);
    graph.makeEdge("b", "d", "length", 1);
    graph.makeEdge("d", "a", "length", 0);
    graph.makeEdge("d", "t", "length", 1);
    PathExpander expander = PathExpanders.allTypesAndDirections();
    Dijkstra algo = new Dijkstra(expander, CommonEvaluators.doubleCostEvaluator("length"), PathInterestFactory.all(NoneStrictMath.EPSILON));
    Iterator<WeightedPath> paths = algo.findAllPaths(s, t).iterator();
    for (int i = 1; i <= 3; i++) {
        assertTrue("Expected at least " + i + " path(s)", paths.hasNext());
        assertTrue("Expected 3 paths of cost 2", NoneStrictMath.equals(paths.next().weight(), 2));
    }
    for (int i = 1; i <= 3; i++) {
        assertTrue("Expected at least " + i + " path(s)", paths.hasNext());
        assertTrue("Expected 3 paths of cost 3", NoneStrictMath.equals(paths.next().weight(), 3));
    }
    assertTrue("Expected at least 7 paths", paths.hasNext());
    assertTrue("Expected 1 path of cost 4", NoneStrictMath.equals(paths.next().weight(), 4));
    assertFalse("Expected exactly 7 paths", paths.hasNext());
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) Dijkstra(org.neo4j.graphalgo.impl.path.Dijkstra) Test(org.junit.Test)

Example 5 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.

the class DijkstraIncreasingWeightTest method testKShortestPaths.

@Test
public void testKShortestPaths() {
    /*
         *      ----- (e) - 3 - (f) ---
         *    /                         \
         *   /    ------- (a) --------   \
         *  3   /            \         \  3
         *  |  2              0         0 |
         *  | /                \         \|
         * (s) - 1 - (c) - 1 - (d) - 1 - (t)
         *   \                 /
         *    -- 1 - (b) - 1 -
         *
         */
    Node s = graph.makeNode("s");
    Node t = graph.makeNode("t");
    graph.makeEdge("s", "a", "length", 2);
    graph.makeEdge("s", "b", "length", 1);
    graph.makeEdge("s", "c", "length", 1);
    graph.makeEdge("s", "e", "length", 3);
    graph.makeEdge("a", "t", "length", 0);
    graph.makeEdge("b", "d", "length", 1);
    graph.makeEdge("c", "d", "length", 1);
    graph.makeEdge("d", "a", "length", 0);
    graph.makeEdge("d", "t", "length", 1);
    graph.makeEdge("e", "f", "length", 3);
    graph.makeEdge("f", "t", "length", 3);
    PathExpander expander = PathExpanders.allTypesAndDirections();
    PathFinder<WeightedPath> algo = new Dijkstra(expander, CommonEvaluators.doubleCostEvaluator("length"), PathInterestFactory.numberOfShortest(NoneStrictMath.EPSILON, 6));
    Iterator<WeightedPath> paths = algo.findAllPaths(s, t).iterator();
    int count = 0;
    while (paths.hasNext()) {
        count++;
        WeightedPath path = paths.next();
        double expectedWeight;
        if (count <= 3) {
            expectedWeight = 2.0;
        } else {
            expectedWeight = 3.0;
        }
        assertTrue("Expected path number " + count + " to have weight of " + expectedWeight, NoneStrictMath.equals(path.weight(), expectedWeight));
    }
    assertTrue("Expected exactly 6 returned paths", count == 6);
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) Dijkstra(org.neo4j.graphalgo.impl.path.Dijkstra) Test(org.junit.Test)

Aggregations

WeightedPath (org.neo4j.graphalgo.WeightedPath)43 Node (org.neo4j.graphdb.Node)33 Test (org.junit.Test)30 Path (org.neo4j.graphdb.Path)14 Relationship (org.neo4j.graphdb.Relationship)14 Dijkstra (org.neo4j.graphalgo.impl.path.Dijkstra)7 PathFinder (org.neo4j.graphalgo.PathFinder)6 BestFirstSelectorFactory (org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 PathExpander (org.neo4j.graphdb.PathExpander)5 StopAfterWeightIterator (org.neo4j.graphalgo.impl.util.StopAfterWeightIterator)4 Traverser (org.neo4j.graphdb.traversal.Traverser)4 Predicate (org.neo4j.helpers.Predicate)4 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 WeightedPathImpl (org.neo4j.graphalgo.impl.util.WeightedPathImpl)3 Ignore (org.junit.Ignore)2 EstimateEvaluator (org.neo4j.graphalgo.EstimateEvaluator)2 Transaction (org.neo4j.graphdb.Transaction)2 HashMap (java.util.HashMap)1