Search in sources :

Example 1 with EstimateEvaluator

use of org.neo4j.graphalgo.EstimateEvaluator in project neo4j-documentation by neo4j.

the class PathFindingDocTest method astarExample.

@SuppressWarnings("unused")
@Test
public void astarExample() {
    // START SNIPPET: astarUsage
    Node nodeA = createNode("name", "A", "x", 0d, "y", 0d);
    Node nodeB = createNode("name", "B", "x", 7d, "y", 0d);
    Node nodeC = createNode("name", "C", "x", 2d, "y", 1d);
    Relationship relAB = createRelationship(nodeA, nodeC, "length", 2d);
    Relationship relBC = createRelationship(nodeC, nodeB, "length", 3d);
    Relationship relAC = createRelationship(nodeA, nodeB, "length", 10d);
    EstimateEvaluator<Double> estimateEvaluator = new EstimateEvaluator<Double>() {

        @Override
        public Double getCost(final Node node, final Node goal) {
            double dx = (Double) node.getProperty("x") - (Double) goal.getProperty("x");
            double dy = (Double) node.getProperty("y") - (Double) goal.getProperty("y");
            double result = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
            return result;
        }
    };
    PathFinder<WeightedPath> astar = GraphAlgoFactory.aStar(PathExpanders.allTypesAndDirections(), CommonEvaluators.doubleCostEvaluator("length"), estimateEvaluator);
    WeightedPath path = astar.findSinglePath(nodeA, nodeB);
// END SNIPPET: astarUsage
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) EstimateEvaluator(org.neo4j.graphalgo.EstimateEvaluator) Test(org.junit.Test)

Example 2 with EstimateEvaluator

use of org.neo4j.graphalgo.EstimateEvaluator in project graphdb by neo4j-attic.

the class PathFindingExamplesTest method astarExample.

@Test
public void astarExample() {
    // START SNIPPET: astarUsage
    Node nodeA = createNode("name", "A", "x", 0d, "y", 0d);
    Node nodeB = createNode("name", "B", "x", 7d, "y", 0d);
    Node nodeC = createNode("name", "C", "x", 2d, "y", 1d);
    Relationship relAB = createRelationship(nodeA, nodeC, "length", 2d);
    Relationship relBC = createRelationship(nodeC, nodeB, "length", 3d);
    Relationship relAC = createRelationship(nodeA, nodeB, "length", 10d);
    EstimateEvaluator<Double> estimateEvaluator = new EstimateEvaluator<Double>() {

        public Double getCost(final Node node, final Node goal) {
            double dx = (Double) node.getProperty("x") - (Double) goal.getProperty("x");
            double dy = (Double) node.getProperty("y") - (Double) goal.getProperty("y");
            double result = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
            return result;
        }
    };
    PathFinder<WeightedPath> astar = GraphAlgoFactory.aStar(Traversal.expanderForAllTypes(), CommonEvaluators.doubleCostEvaluator("length"), estimateEvaluator);
    WeightedPath path = astar.findSinglePath(nodeA, nodeB);
    // END SNIPPET: astarUsage
    System.out.println(path.weight());
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) EstimateEvaluator(org.neo4j.graphalgo.EstimateEvaluator) Test(org.junit.Test)

Example 3 with EstimateEvaluator

use of org.neo4j.graphalgo.EstimateEvaluator 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)

Aggregations

EstimateEvaluator (org.neo4j.graphalgo.EstimateEvaluator)3 WeightedPath (org.neo4j.graphalgo.WeightedPath)3 Node (org.neo4j.graphdb.Node)3 Relationship (org.neo4j.graphdb.Relationship)3 Test (org.junit.Test)2 Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 Arguments (org.junit.jupiter.params.provider.Arguments)1 Arguments.arguments (org.junit.jupiter.params.provider.Arguments.arguments)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)1 CommonEvaluators.doubleCostEvaluator (org.neo4j.graphalgo.CommonEvaluators.doubleCostEvaluator)1 EvaluationContext (org.neo4j.graphalgo.EvaluationContext)1