private ArrayList<FakeNode> getNeighbours(long id) { ArrayList<FakeNode> retNodes = new ArrayList<>(); ArrayList<Segment> neighbours = this.segments .stream() .filter( (Segment s) -> { return s.mOrigin == id; }) .collect(Collectors.toCollection(ArrayList<Segment>::new)); neighbours.addAll( this.fakeSegments .stream() .filter( (Segment s) -> { return s.mOrigin == id; }) .collect(Collectors.toCollection(ArrayList<Segment>::new))); FakeNode node; for (Segment s : neighbours) { if (fakeNodes.containsKey(s.mDestination)) { node = fakeNodes.get(s.mDestination); } else { node = new FakeNode(nodes.get(s.mDestination)); fakeNodes.put(s.mDestination, node); } ArrayList<CostFunction> functions = costs .stream() .filter( (CostFunction c) -> { return c.getSegmentId() == s.mId; }) .collect(Collectors.toCollection(ArrayList<CostFunction>::new)); functions.addAll( fakeCosts .stream() .filter( (CostFunction c) -> { return c.getSegmentId() == s.mId; }) .collect(Collectors.toCollection(ArrayList<CostFunction>::new))); node.node.cost = functions.get(0); retNodes.add(fakeNodes.get(s.mDestination)); } return retNodes; }
public Route start(DateTime time) { this.startTime = time; ArrayList<FakeNode> neighbouringNodes; hScore(startNode); openSet.add(startNode); while (!openSet.isEmpty()) { currentNode = openSet.poll(); neighbouringNodes = getNeighbours(currentNode.id); for (FakeNode neighbour : neighbouringNodes) { if (neighbour.visited == true) { continue; } else if (openSet.contains(neighbour)) { if (neighbour.g_scores > gScore(neighbour)) { neighbour.cameFrom = currentNode; calcScores(neighbour); } continue; } else { neighbour.cameFrom = currentNode; } if (endNode == neighbour) { calcScores(neighbour); return reconstructPath(); } calcScores(neighbour); openSet.add(neighbour); } currentNode.visited = true; } return null; }
private void hScore(FakeNode node) { node.h_scores = ((Haversine.haversineFromNodes(node.node, endNode.node) / 1000) / 120.0d) * 3600; }
private void calcScores(FakeNode node) { node.g_scores = gScore(node); hScore(node); node.f_score = node.g_scores + node.h_scores; }