|
| 1 | +package main.java.util.algorithms; |
| 2 | + |
| 3 | + |
| 4 | +import main.java.GraphPanel; |
| 5 | +import org.jgrapht.graph.DefaultWeightedEdge; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.PriorityQueue; |
| 11 | + |
| 12 | +/** |
| 13 | + * Implements an A* search algorithm to find a shortest path from a |
| 14 | + * {@link GraphPanel#sourceNode} to a {@link GraphPanel#targetNode}. |
| 15 | + * |
| 16 | + * @author Ryan Albertson |
| 17 | + */ |
| 18 | +public class A_Star extends Algorithm { |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructor. |
| 23 | + * @param gPanel {@link GraphPanel}. |
| 24 | + */ |
| 25 | + public A_Star(GraphPanel gPanel) { |
| 26 | + |
| 27 | + super(gPanel); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + public void runAlgorithm(Integer node) { |
| 32 | + |
| 33 | + // Init distance to reach each node through the search |
| 34 | + double[] distanceTo = new double[gPanel.nodeCount]; |
| 35 | + Arrays.fill(distanceTo, Double.POSITIVE_INFINITY); |
| 36 | + distanceTo[node] = 0.0; |
| 37 | + |
| 38 | + // Init estimated remaining distance to target node from each node |
| 39 | + double[] distanceAfter = new double[gPanel.nodeCount]; |
| 40 | + double targetX = gPanel.nodeCoords.get(gPanel.targetNode)[0]; |
| 41 | + double targetY = gPanel.nodeCoords.get(gPanel.targetNode)[1]; |
| 42 | + for (int i = 0; i < gPanel.nodeCount; i++) { |
| 43 | + double iX = gPanel.nodeCoords.get(i)[0]; |
| 44 | + double iY = gPanel.nodeCoords.get(i)[1]; |
| 45 | + double dist = Math.sqrt(Math.pow(Math.abs(iY - targetY), 2) + |
| 46 | + Math.pow(Math.abs(iX - targetX), 2)); |
| 47 | + distanceAfter[i] = dist; |
| 48 | + } |
| 49 | + |
| 50 | + // Node priority considers the sum: distance[i] + distanceAfter[i] |
| 51 | + Double[] prio = new Double[gPanel.nodeCount]; |
| 52 | + for (int i = 0; i < gPanel.nodeCount; i++) prio[i] = Double.MAX_VALUE; |
| 53 | + |
| 54 | + boolean[] isExplored = new boolean[gPanel.nodeCount]; |
| 55 | + |
| 56 | + // Order of nodes to be visited |
| 57 | + PriorityQueue<Integer> nodesQueue = new PriorityQueue<>((node1, node2) -> |
| 58 | + prio[node1].compareTo(prio[node2])); |
| 59 | + nodesQueue.add(node); |
| 60 | + |
| 61 | + // Store nodes & edges from previous iterations of loop. For animation purposes |
| 62 | + Map<Integer, Integer> prevNode = new HashMap<>(); |
| 63 | + Map<Integer, DefaultWeightedEdge> edgeTo = new HashMap<>(); |
| 64 | + |
| 65 | + while (!nodesQueue.isEmpty()) { |
| 66 | + Integer currentNode = nodesQueue.poll(); |
| 67 | + isExplored[currentNode] = true; |
| 68 | + DefaultWeightedEdge edgeToCurrentNode = edgeTo.get(currentNode); |
| 69 | + if (null != edgeToCurrentNode) gPanel.visitedEdges.add(edgeToCurrentNode); |
| 70 | + Integer prev = prevNode.get(currentNode); |
| 71 | + if (null != prev) gPanel.path[currentNode] = prev; |
| 72 | + |
| 73 | + for (DefaultWeightedEdge edgeToAdjNode : gPanel.graph.edgesOf(currentNode)) { |
| 74 | + if (gPanel.visitedEdges.contains(edgeToAdjNode)) continue; |
| 75 | + Integer adjNode = gPanel.graph.getEdgeTarget(edgeToAdjNode); |
| 76 | + // Account for directed edges |
| 77 | + if (adjNode.equals(currentNode)) { |
| 78 | + adjNode = gPanel.graph.getEdgeSource(edgeToAdjNode); |
| 79 | + } |
| 80 | + if (isExplored[adjNode]) continue; |
| 81 | + // Update adjNode's distance if this path is shorter than current |
| 82 | + double currentDist = distanceTo[adjNode]; |
| 83 | + double newDist = distanceTo[currentNode] + |
| 84 | + gPanel.graph.getEdgeWeight(edgeToAdjNode); |
| 85 | + if (newDist < currentDist) { |
| 86 | + distanceTo[adjNode] = newDist; |
| 87 | + } |
| 88 | + // Update total distance if this estimated path is shorter |
| 89 | + double totalDist = distanceTo[adjNode] + distanceAfter[adjNode]; |
| 90 | + if (totalDist < prio[adjNode]) { |
| 91 | + prio[adjNode] = totalDist; |
| 92 | + nodesQueue.add(adjNode); |
| 93 | + prevNode.put(adjNode, currentNode); |
| 94 | + edgeTo.put(adjNode, edgeToAdjNode); |
| 95 | + } |
| 96 | + } |
| 97 | + // Check if user has stopped or paused algorithm |
| 98 | + animate(); |
| 99 | + if (isStopped()) return; |
| 100 | + // Stop algorithm if a path to the target has been found |
| 101 | + if (currentNode.equals(gPanel.targetNode)) break; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + public void runAlgorithm() { |
| 107 | + |
| 108 | + // This signature isn't needed for this algorithm. |
| 109 | + } |
| 110 | +} |
0 commit comments