Search in sources :

Example 26 with BasicEvaluationContext

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

the class TestAStar method betterTentativePath.

@Test
void betterTentativePath() {
    // GIVEN
    try (Transaction transaction = graphDb.beginTx()) {
        EstimateEvaluator<Double> estimator = (node, goal) -> (Double) node.getProperty("estimate");
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<WeightedPath> finder = aStar(context, allTypesAndDirections(), doubleCostEvaluator("weight", 0d), estimator);
        final Node node1 = graph.makeNode(transaction, "1", "estimate", 0.003d);
        final Node node2 = graph.makeNode(transaction, "2", "estimate", 0.002d);
        final Node node3 = graph.makeNode(transaction, "3", "estimate", 0.001d);
        final Node node4 = graph.makeNode(transaction, "4", "estimate", 0d);
        graph.makeEdge(transaction, "1", "3", "weight", 0.253d);
        graph.makeEdge(transaction, "1", "2", "weight", 0.018d);
        graph.makeEdge(transaction, "2", "4", "weight", 0.210d);
        graph.makeEdge(transaction, "2", "3", "weight", 0.180d);
        graph.makeEdge(transaction, "2", "3", "weight", 0.024d);
        graph.makeEdge(transaction, "3", "4", "weight", 0.135d);
        graph.makeEdge(transaction, "3", "4", "weight", 0.013d);
        // WHEN
        WeightedPath best14 = finder.findSinglePath(node1, node4);
        // THEN
        assertPath(best14, node1, node2, node3, node4);
        transaction.commit();
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MapUtil(org.neo4j.internal.helpers.collection.MapUtil) ResourceIterable(org.neo4j.graphdb.ResourceIterable) PathFinder(org.neo4j.graphalgo.PathFinder) OUTGOING(org.neo4j.graphdb.Direction.OUTGOING) HashMap(java.util.HashMap) Function(java.util.function.Function) Node(org.neo4j.graphdb.Node) CommonEvaluators.doubleCostEvaluator(org.neo4j.graphalgo.CommonEvaluators.doubleCostEvaluator) TraversalAStar(org.neo4j.graphalgo.impl.path.TraversalAStar) EvaluationContext(org.neo4j.graphalgo.EvaluationContext) PathExpander(org.neo4j.graphdb.PathExpander) Iterables(org.neo4j.internal.helpers.collection.Iterables) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) WeightedPath(org.neo4j.graphalgo.WeightedPath) MethodSource(org.junit.jupiter.params.provider.MethodSource) EstimateEvaluator(org.neo4j.graphalgo.EstimateEvaluator) Neo4jAlgoTestCase(common.Neo4jAlgoTestCase) Arguments(org.junit.jupiter.params.provider.Arguments) BranchState(org.neo4j.graphdb.traversal.BranchState) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Relationship(org.neo4j.graphdb.Relationship) PathExpanders.allTypesAndDirections(org.neo4j.graphdb.PathExpanders.allTypesAndDirections) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) GraphAlgoFactory.aStar(org.neo4j.graphalgo.GraphAlgoFactory.aStar) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with BasicEvaluationContext

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

the class TestAStar method allPathsToSelfReturnsZero.

@ParameterizedTest
@MethodSource("params")
void allPathsToSelfReturnsZero(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
    try (Transaction transaction = graphDb.beginTx()) {
        // GIVEN
        Node start = graph.makeNode(transaction, "start", "x", 0d, "y", 0d);
        // WHEN
        var context = new BasicEvaluationContext(transaction, graphDb);
        ResourceIterable<WeightedPath> paths = Iterables.asResourceIterable(finderFactory.apply(context).findAllPaths(start, start));
        // THEN
        for (WeightedPath path : paths) {
            assertNotNull(path);
            assertEquals(start, path.startNode());
            assertEquals(start, path.endNode());
            assertEquals(0, path.length());
        }
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 28 with BasicEvaluationContext

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

the class TestAStar method canUseBranchState.

@SuppressWarnings({ "rawtypes", "unchecked" })
@ParameterizedTest
@MethodSource("params")
void canUseBranchState(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
    /**
     * <pre>
     *   012345    A - B:  2
     *  +------>y  A - B:  2
     * 0|A         B - C:  3
     * 1|          A - C:  10
     * 2| B
     * 3|
     * 4|
     * 5|
     * 6|
     * 7|C
     *  V
     *  x
     *
     * </pre>
     */
    try (Transaction transaction = graphDb.beginTx()) {
        Node nodeA = graph.makeNode(transaction, "A", "x", 0d, "y", 0d);
        Node nodeB = graph.makeNode(transaction, "B", "x", 2d, "y", 1d);
        Node nodeC = graph.makeNode(transaction, "C", "x", 7d, "y", 0d);
        graph.makeEdge(transaction, "A", "B", "length", 2d);
        graph.makeEdge(transaction, "A", "B", "length", 2d);
        graph.makeEdge(transaction, "B", "C", "length", 3d);
        graph.makeEdge(transaction, "A", "C", "length", 10d);
        final Map<Node, Double> seenBranchStates = new HashMap<>();
        PathExpander<Double> expander = new PathExpander<Double>() {

            @Override
            public Iterable<Relationship> expand(Path path, BranchState<Double> state) {
                double newState = state.getState();
                if (path.length() > 0) {
                    newState += (Double) path.lastRelationship().getProperty("length");
                    state.setState(newState);
                }
                seenBranchStates.put(path.endNode(), newState);
                return path.endNode().getRelationships(OUTGOING);
            }

            @Override
            public PathExpander<Double> reverse() {
                throw new UnsupportedOperationException();
            }
        };
        double initialStateValue = 0D;
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<WeightedPath> traversalFinder = new TraversalAStar<>(context, expander, new InitialBranchState.State(initialStateValue, initialStateValue), doubleCostEvaluator("length"), ESTIMATE_EVALUATOR);
        WeightedPath path = traversalFinder.findSinglePath(nodeA, nodeC);
        assertEquals((Double) 5.0D, (Double) path.weight());
        assertPathDef(path, "A", "B", "C");
        assertEquals(MapUtil.<Node, Double>genericMap(nodeA, 0D, nodeB, 2D), seenBranchStates);
        transaction.commit();
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) TraversalAStar(org.neo4j.graphalgo.impl.path.TraversalAStar) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) BranchState(org.neo4j.graphdb.traversal.BranchState) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 29 with BasicEvaluationContext

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

the class TestAllSimplePaths method testTripleRelationshipGraph.

@Test
void testTripleRelationshipGraph() {
    /* Layout
         *          ___
         * (a)---(b)===(c)---(d)
         */
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "a", "b");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "c", "d");
        PathFinder<Path> finder = instantiatePathFinder(new BasicEvaluationContext(transaction, graphDb), 10);
        Iterable<Path> paths = finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "d"));
        assertPaths(paths, "a,b,c,d", "a,b,c,d", "a,b,c,d");
        transaction.commit();
    }
}
Also used : Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 30 with BasicEvaluationContext

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

the class TestShortestPath method shouldMakeSureResultLimitIsRespectedForMultiPathHits.

@Test
void shouldMakeSureResultLimitIsRespectedForMultiPathHits() {
    /*       _____
         *      /     \
         *    (a)-----(b)
         *      \_____/
         */
    try (Transaction transaction = graphDb.beginTx()) {
        for (int i = 0; i < 3; i++) {
            graph.makeEdge(transaction, "a", "b");
        }
        Node a = graph.getNode(transaction, "a");
        Node b = graph.getNode(transaction, "b");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> assertEquals(1, count(finder.findAllPaths(a, b))), allTypesAndDirections(), 2, 1);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Aggregations

BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)52 Transaction (org.neo4j.graphdb.Transaction)52 Node (org.neo4j.graphdb.Node)40 Test (org.junit.jupiter.api.Test)33 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 MethodSource (org.junit.jupiter.params.provider.MethodSource)20 WeightedPath (org.neo4j.graphalgo.WeightedPath)20 Path (org.neo4j.graphdb.Path)18 ExactDepthPathFinder (org.neo4j.graphalgo.impl.path.ExactDepthPathFinder)8 Relationship (org.neo4j.graphdb.Relationship)8 HashSet (java.util.HashSet)5 PathExpander (org.neo4j.graphdb.PathExpander)5 GraphAlgoFactory.shortestPath (org.neo4j.graphalgo.GraphAlgoFactory.shortestPath)4 Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 EvaluationContext (org.neo4j.graphalgo.EvaluationContext)3 PathFinder (org.neo4j.graphalgo.PathFinder)3 OUTGOING (org.neo4j.graphdb.Direction.OUTGOING)3 Label (org.neo4j.graphdb.Label)3 PathExpanders.allTypesAndDirections (org.neo4j.graphdb.PathExpanders.allTypesAndDirections)3