Advertisement
salmancreation

A* Search Algo

Mar 20th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. package serach;
  2.  
  3. import java.util.PriorityQueue;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import java.util.List;
  7. import java.util.Comparator;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10.  
  11.  
  12. public class asearch {
  13.  
  14.  
  15.  
  16. //h scores is the stright-line distance from the current city to Bucharest
  17. public static void main(String[] args){
  18.  
  19. //initialize the graph base on the Romania map
  20. Node n1 = new Node("Arad",366);
  21. Node n2 = new Node("Zerind",374);
  22. Node n3 = new Node("Oradea",380);
  23. Node n4 = new Node("Sibiu",253);
  24. Node n5 = new Node("Fagaras",178);
  25. Node n6 = new Node("Rimnicu Vilcea",193);
  26. Node n7 = new Node("Pitesti",98);
  27. Node n8 = new Node("Timisoara",329);
  28. Node n9 = new Node("Lugoj",244);
  29. Node n10 = new Node("Mehadia",241);
  30. Node n11 = new Node("Drobeta",242);
  31. Node n12 = new Node("Craiova",160);
  32. Node n13 = new Node("Bucharest",0);
  33. Node n14 = new Node("Giurgiu",77);
  34.  
  35. //initialize the edges
  36.  
  37. //Arad
  38. n1.adjacencies = new Edge[]{
  39. new Edge(n2,75),
  40. new Edge(n4,140),
  41. new Edge(n8,118)
  42. };
  43.  
  44. //Zerind
  45. n2.adjacencies = new Edge[]{
  46. new Edge(n1,75),
  47. new Edge(n3,71)
  48. };
  49.  
  50.  
  51. //Oradea
  52. n3.adjacencies = new Edge[]{
  53. new Edge(n2,71),
  54. new Edge(n4,151)
  55. };
  56.  
  57. //Sibiu
  58. n4.adjacencies = new Edge[]{
  59. new Edge(n1,140),
  60. new Edge(n5,99),
  61. new Edge(n3,151),
  62. new Edge(n6,80),
  63. };
  64.  
  65.  
  66. //Fagaras
  67. n5.adjacencies = new Edge[]{
  68. new Edge(n4,99),
  69.  
  70. //178
  71. new Edge(n13,211)
  72. };
  73.  
  74. //Rimnicu Vilcea
  75. n6.adjacencies = new Edge[]{
  76. new Edge(n4,80),
  77. new Edge(n7,97),
  78. new Edge(n12,146)
  79. };
  80.  
  81. //Pitesti
  82. n7.adjacencies = new Edge[]{
  83. new Edge(n6,97),
  84. new Edge(n13,101),
  85. new Edge(n12,138)
  86. };
  87.  
  88. //Timisoara
  89. n8.adjacencies = new Edge[]{
  90. new Edge(n1,118),
  91. new Edge(n9,111)
  92. };
  93.  
  94. //Lugoj
  95. n9.adjacencies = new Edge[]{
  96. new Edge(n8,111),
  97. new Edge(n10,70)
  98. };
  99.  
  100. //Mehadia
  101. n10.adjacencies = new Edge[]{
  102. new Edge(n9,70),
  103. new Edge(n11,75)
  104. };
  105.  
  106. //Drobeta
  107. n11.adjacencies = new Edge[]{
  108. new Edge(n10,75),
  109. new Edge(n12,120)
  110. };
  111.  
  112. //Craiova
  113. n12.adjacencies = new Edge[]{
  114. new Edge(n11,120),
  115. new Edge(n6,146),
  116. new Edge(n7,138)
  117. };
  118.  
  119. //Bucharest
  120. n13.adjacencies = new Edge[]{
  121. new Edge(n7,101),
  122. new Edge(n14,90),
  123. new Edge(n5,211)
  124. };
  125.  
  126. //Giurgiu
  127. n14.adjacencies = new Edge[]{
  128. new Edge(n13,90)
  129. };
  130.  
  131. AstarSearch(n1,n13);
  132.  
  133. List<Node> path = printPath(n13);
  134.  
  135. System.out.println("Path: " + path);
  136.  
  137.  
  138. }
  139.  
  140. public static List<Node> printPath(Node target){
  141. List<Node> path = new ArrayList<Node>();
  142.  
  143. for(Node node = target; node!=null; node = node.parent){
  144. path.add(node);
  145. }
  146.  
  147. Collections.reverse(path);
  148.  
  149. return path;
  150. }
  151.  
  152. public static void AstarSearch(Node source, Node goal){
  153.  
  154. Set<Node> explored = new HashSet<Node>();
  155.  
  156. PriorityQueue<Node> queue = new PriorityQueue<Node>(20,
  157. new Comparator<Node>(){
  158. //override compare method
  159. public int compare(Node i, Node j){
  160. if(i.f_scores > j.f_scores){
  161. return 1;
  162. }
  163.  
  164. else if (i.f_scores < j.f_scores){
  165. return -1;
  166. }
  167.  
  168. else{
  169. return 0;
  170. }
  171. }
  172.  
  173. }
  174. );
  175.  
  176. //cost from start
  177. source.g_scores = 0;
  178.  
  179. queue.add(source);
  180.  
  181. boolean found = false;
  182.  
  183. while((!queue.isEmpty())&&(!found)){
  184.  
  185. //the node in having the lowest f_score value
  186. Node current = queue.poll();
  187.  
  188. explored.add(current);
  189.  
  190. //goal found
  191. if(current.value.equals(goal.value)){
  192. found = true;
  193. }
  194.  
  195. //check every child of current node
  196. for(Edge e : current.adjacencies){
  197. Node child = e.target;
  198. double cost = e.cost;
  199. double temp_g_scores = current.g_scores + cost;
  200. double temp_f_scores = temp_g_scores + child.h_scores;
  201.  
  202.  
  203. /*if child node has been evaluated and
  204. the newer f_score is higher, skip*/
  205.  
  206. if((explored.contains(child)) &&
  207. (temp_f_scores >= child.f_scores)){
  208. continue;
  209. }
  210.  
  211. /*else if child node is not in queue or
  212. newer f_score is lower*/
  213.  
  214. else if((!queue.contains(child)) ||
  215. (temp_f_scores < child.f_scores)){
  216.  
  217. child.parent = current;
  218. child.g_scores = temp_g_scores;
  219. child.f_scores = temp_f_scores;
  220.  
  221. if(queue.contains(child)){
  222. queue.remove(child);
  223. }
  224.  
  225. queue.add(child);
  226.  
  227. }
  228.  
  229. }
  230.  
  231. }
  232.  
  233. }
  234.  
  235. }
  236.  
  237. class Node{
  238.  
  239. public final String value;
  240. public double g_scores;
  241. public final double h_scores;
  242. public double f_scores = 0;
  243. public Edge[] adjacencies;
  244. public Node parent;
  245.  
  246. public Node(String val, double hVal){
  247. value = val;
  248. h_scores = hVal;
  249. }
  250.  
  251. public String toString(){
  252. return value;
  253. }
  254.  
  255. }
  256.  
  257. class Edge{
  258. public final double cost;
  259. public final Node target;
  260.  
  261. public Edge(Node targetNode, double costVal){
  262. target = targetNode;
  263. cost = costVal;
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement