use of org.neo4j.graphalgo.WeightedPath in project graphdb by neo4j-attic.
the class StopAfterWeightIterator method fetchNextOrNull.
@Override
protected WeightedPath fetchNextOrNull() {
if (!paths.hasNext()) {
return null;
}
WeightedPath path = new WeightedPathImpl(costEvaluator, paths.next());
if (foundWeight != null && path.weight() > foundWeight) {
return null;
}
foundWeight = path.weight();
return path;
}
use of org.neo4j.graphalgo.WeightedPath 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.WeightedPath in project graphdb by neo4j-attic.
the class ExperimentalAStar method findAllPaths.
public Iterable<WeightedPath> findAllPaths(Node start, final Node end) {
Predicate<Path> filter = new Predicate<Path>() {
public boolean accept(Path position) {
return position.endNode().equals(end);
}
};
final Traverser traverser = traversalDescription.order(new SelectorFactory(end)).filter(filter).traverse(start);
return new Iterable<WeightedPath>() {
public Iterator<WeightedPath> iterator() {
return new StopAfterWeightIterator(traverser.iterator(), costEvaluator);
}
};
}
Aggregations