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