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