Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. package org.educationalProject.surfacePathfinder.path;
  2.  
  3. import org.educationalProject.surfacePathfinder.Point;
  4. import org.jgrapht.graph.DefaultWeightedEdge;
  5. import org.jgrapht.graph.SimpleWeightedGraph;
  6. import org.jgrapht.util.FibonacciHeap;
  7. import org.jgrapht.util.FibonacciHeapNode;
  8.  
  9. import java.util.*;
  10.  
  11.  
  12. public class AStarPathFind {
  13.  
  14. private SimpleWeightedGraph<Point, DefaultWeightedEdge> graph;
  15. private Set<Point> settledNodes;
  16. private PriorityQueue<DistancePoint> unSettledNodes;
  17. private Map<Point, Point> predecessors;
  18. private Map<Point, Double> gScore;
  19. private Map<Point, Double> fScore;
  20. private Point source;
  21. private Point destination;
  22.  
  23. public List<Point> getShortestPath(SimpleWeightedGraph<Point, DefaultWeightedEdge> graph, Point source, Point destination) {
  24. this.init(graph, source, destination);
  25. this.findPath();
  26. return this.retrievalPath();
  27. }
  28.  
  29. private void init(SimpleWeightedGraph<Point, DefaultWeightedEdge> graph, Point source, Point destination) {
  30. this.graph = graph;
  31. this.source = source;
  32. this.destination = destination;
  33. settledNodes = new HashSet<Point>();
  34. unSettledNodes = new PriorityQueue<DistancePoint>(comparator);
  35. gScore = new HashMap<Point, Double>();
  36. fScore = new HashMap<Point, Double>();
  37. predecessors = new HashMap<Point, Point>();
  38.  
  39. gScore.put(this.source, 0.0);
  40. fScore.put(this.source, getHeuristic(this.source, this.source));
  41. unSettledNodes.add(new DistancePoint(this.source, fScore.get(this.source)));
  42. }
  43.  
  44. private void findPath() {
  45. while (unSettledNodes.size() > 0) {
  46. Point current = unSettledNodes.poll().point;
  47. if (isSettled(current))
  48. continue;
  49. if (current.equals(destination))
  50. return;
  51. settledNodes.add(current);
  52. findMinimalDistances(current);
  53. }
  54. }
  55.  
  56. private void findMinimalDistances(Point node) {
  57. List<Point> adjacentNodes = getNeighbors(node);
  58.  
  59. for (Point neighbor : adjacentNodes) {
  60. double tentativeScore = getDistance(node) + getDistance(node, neighbor);
  61. if (isSettled(neighbor))
  62. continue;
  63. if(tentativeScore > getDistance(neighbor))
  64. continue;
  65.  
  66. if (isUnSettled(neighbor)) {
  67. predecessors.remove(neighbor);
  68. gScore.remove(neighbor);
  69. fScore.remove(neighbor);
  70. }
  71. gScore.put(neighbor, tentativeScore);
  72. fScore.put(neighbor, getHeuristic(node, neighbor));
  73. unSettledNodes.add(new DistancePoint(neighbor, fScore.get(neighbor)));
  74. predecessors.put(neighbor, node);
  75. }
  76. }
  77.  
  78. private Double getHeuristic(Point current, Point next) {
  79. Double g = gScore.get(current);
  80. Double h = (Double)Math.sqrt((next.x - destination.x) * (next.x - destination.x)
  81. + (next.y - destination.y) * (next.y - destination.y)
  82. + (next.alt - destination.alt) * (next.alt - destination.alt))
  83. + Math.abs(next.alt - destination.alt);
  84.  
  85. return g + h;
  86. }
  87. private Double getDistance(Point node, Point target) {
  88. if (node.equals(target))
  89. return 0.0;
  90. DefaultWeightedEdge e = graph.getEdge(node, target);
  91. return (Double)graph.getEdgeWeight(e);
  92. }
  93. private Double getDistance(Point node) {
  94. Double g = gScore.get(node);
  95. if (g == null) {
  96. return Double.MAX_VALUE;
  97. } else {
  98. return g;
  99. }
  100. }
  101.  
  102. private List<Point> getNeighbors(Point node) {
  103. List<Point> neighbors = new ArrayList<Point>();
  104. Set<DefaultWeightedEdge> edges = graph.edgesOf(node);
  105. for (DefaultWeightedEdge edge : edges) {
  106. Point source = graph.getEdgeSource(edge);
  107. Point target = graph.getEdgeTarget(edge);
  108. if (source.equals(node) && !isSettled(target)) {
  109. neighbors.add(target);
  110. }else if (target.equals(node) && !isSettled(source)){
  111. neighbors.add(source);
  112. }
  113. }
  114. return neighbors;
  115. }
  116.  
  117. public static Comparator<DistancePoint> comparator = new Comparator<DistancePoint>() {
  118. @Override
  119. public int compare(DistancePoint a, DistancePoint b) {
  120. return (int)Math.signum(a.distance - b.distance);
  121. }
  122. };
  123.  
  124. private Point getMinimum() {
  125. Point minimum = unSettledNodes.peek().point;
  126. return minimum;
  127. }
  128.  
  129. private boolean isSettled(Point node) {
  130. return settledNodes.contains(node);
  131. }
  132. private boolean isUnSettled(Point node) {return fScore.containsKey(node); }
  133.  
  134. private List<Point> retrievalPath() {
  135. List<Point> shortestPath = new LinkedList<Point>();
  136. Point step = this.destination;
  137. if (predecessors.get(step) == null) {
  138. return null;
  139. }
  140. shortestPath.add(step);
  141. while (predecessors.get(step) != null) {
  142. step = predecessors.get(step);
  143. shortestPath.add(step);
  144. }
  145. Collections.reverse(shortestPath);
  146. return shortestPath;
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement