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
}
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());
}
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();
}
}
Aggregations