Search in sources :

Example 1 with TraversalAStar

use of org.neo4j.graphalgo.impl.path.TraversalAStar in project neo4j by neo4j.

the class TestAStar method canUseBranchState.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void canUseBranchState() throws Exception {
    // This test doesn't use the predefined finder, which only means an unnecessary instantiation
    // if such an object. And this test will be run twice (once for each finder type in data()).
    /**
         * <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>
         */
    Node nodeA = graph.makeNode("A", "x", 0d, "y", 0d);
    Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
    Node nodeC = graph.makeNode("C", "x", 7d, "y", 0d);
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("B", "C", "length", 3d);
    graph.makeEdge("A", "C", "length", 10d);
    final Map<Node, Double> seenBranchStates = new HashMap<Node, Double>();
    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;
    PathFinder<WeightedPath> traversalFinder = new TraversalAStar(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);
}
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) WeightedPath(org.neo4j.graphalgo.WeightedPath) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 2 with TraversalAStar

use of org.neo4j.graphalgo.impl.path.TraversalAStar 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)

Aggregations

HashMap (java.util.HashMap)2 WeightedPath (org.neo4j.graphalgo.WeightedPath)2 TraversalAStar (org.neo4j.graphalgo.impl.path.TraversalAStar)2 Node (org.neo4j.graphdb.Node)2 Path (org.neo4j.graphdb.Path)2 PathExpander (org.neo4j.graphdb.PathExpander)2 Relationship (org.neo4j.graphdb.Relationship)2 BranchState (org.neo4j.graphdb.traversal.BranchState)2 InitialBranchState (org.neo4j.graphdb.traversal.InitialBranchState)2 Test (org.junit.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)1 Transaction (org.neo4j.graphdb.Transaction)1