Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.93 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4. * Example.
  5. */
  6. public class PathFinder extends AStar<PathFinder.Node>
  7. {
  8. private int[][] map;
  9.  
  10. public static class Node{
  11. public int x;
  12. public int y;
  13. Node(int x, int y){
  14. this.x = x;
  15. this.y = y;
  16. }
  17. public String toString(){
  18. return "(" + x + ", " + y + ") ";
  19. }
  20. }
  21. public PathFinder(int[][] map){
  22. this.map = map;
  23. }
  24.  
  25. protected boolean isGoal(Node node){
  26. return (node.x == map[0].length - 1) && (node.y == map.length - 1);
  27. }
  28.  
  29. protected Double g(Node from, Node to){
  30.  
  31. if(from.x == to.x && from.y == to.y)
  32. return 0.0;
  33.  
  34. if(map[to.y][to.x] == 1)
  35. return 1.0;
  36.  
  37. return Double.MAX_VALUE;
  38. }
  39.  
  40. protected Double h(Node from, Node to){
  41. /* Use the Manhattan distance heuristic. */
  42. return new Double(Math.abs(map[0].length - 1 - to.x) + Math.abs(map.length - 1 - to.y));
  43. }
  44.  
  45. protected List<Node> generateSuccessors(Node node){
  46. List<Node> ret = new LinkedList<Node>();
  47. int x = node.x;
  48. int y = node.y;
  49. if(y < map.length - 1 && map[y+1][x] == 1)
  50. ret.add(new Node(x, y+1));
  51.  
  52. if(x < map[0].length - 1 && map[y][x+1] == 1)
  53. ret.add(new Node(x+1, y));
  54.  
  55. if(y > 0 && map[y-1][x] == 1)
  56. ret.add(new Node(x, y-1));
  57. if(x > 0 && map[y][x-1] == 1)
  58. ret.add(new Node(x-1, y));
  59. return ret;
  60. }
  61.  
  62. public static void main(String [] args){
  63. int [][] map = new int[][]{
  64. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  65. {0, 0, 1, 0, 0, 0, 0, 0, 1},
  66. {1, 1, 1, 1, 0, 1, 1, 0, 1},
  67. {1, 1, 1, 1, 1, 1, 1, 0, 0},
  68. {1, 0, 0, 0, 0, 1, 0, 0, 0},
  69. {1, 1, 1, 1, 0, 1, 1, 1, 1},
  70. {1, 1, 1, 1, 0, 1, 0, 0, 1},
  71. {1, 1, 1, 1, 0, 1, 0, 0, 1},
  72. {1, 1, 1, 1, 0, 1, 0, 0, 1},
  73. {1, 1, 1, 1, 0, 1, 0, 0, 0},
  74. {1, 1, 1, 1, 0, 1, 1, 1, 1},
  75. };
  76. PathFinder pf = new PathFinder(map);
  77.  
  78. System.out.println("Find a path from the top left corner to the right bottom one.");
  79.  
  80. for(int i = 0; i < map.length; i++){
  81. for(int j = 0; j < map[0].length; j++)
  82. System.out.print(map[i][j] + " ");
  83. System.out.println();
  84. }
  85.  
  86. long begin = System.currentTimeMillis();
  87.  
  88. List<Node> nodes = pf.compute(new PathFinder.Node(0, 0));
  89.  
  90. long end = System.currentTimeMillis();
  91.  
  92.  
  93. System.out.println("Time = " + (end - begin) + " ms" );
  94. System.out.println("Expanded = " + pf.getExpandedCounter());
  95. System.out.println("Cost = " + pf.getCost());
  96.  
  97. if(nodes == null)
  98. System.out.println("No path");
  99. else{
  100. System.out.print("Path = ");
  101. for(Node n : nodes)
  102. System.out.print(n);
  103. System.out.println();
  104. }
  105. }
  106.  
  107. }
  108.  
  109. import java.util.*;
  110.  
  111. /**
  112. * A* algorithm implementation using the method design pattern.
  113. *
  114. * @author Giuseppe Scrivano
  115. */
  116. public abstract class AStar<T>
  117. {
  118. private class Path implements Comparable{
  119. public T point;
  120. public Double f;
  121. public Double g;
  122. public Path parent;
  123.  
  124. /**
  125. * Default c'tor.
  126. */
  127. public Path(){
  128. parent = null;
  129. point = null;
  130. g = f = 0.0;
  131. }
  132.  
  133. /**
  134. * C'tor by copy another object.
  135. *
  136. * @param p The path object to clone.
  137. */
  138. public Path(Path p){
  139. this();
  140. parent = p;
  141. g = p.g;
  142. f = p.f;
  143. }
  144.  
  145. /**
  146. * Compare to another object using the total cost f.
  147. *
  148. * @param o The object to compare to.
  149. * @see Comparable#compareTo()
  150. * @return <code>less than 0</code> This object is smaller
  151. * than <code>0</code>;
  152. * <code>0</code> Object are the same.
  153. * <code>bigger than 0</code> This object is bigger
  154. * than o.
  155. */
  156. public int compareTo(Object o){
  157. Path p = (Path)o;
  158. return (int)(f - p.f);
  159. }
  160.  
  161. /**
  162. * Get the last point on the path.
  163. *
  164. * @return The last point visited by the path.
  165. */
  166. public T getPoint(){
  167. return point;
  168. }
  169.  
  170. /**
  171. * Set the
  172. */
  173. public void setPoint(T p){
  174. point = p;
  175. }
  176. }
  177.  
  178. /**
  179. * Check if the current node is a goal for the problem.
  180. *
  181. * @param node The node to check.
  182. * @return <code>true</code> if it is a goal, <code>false</else> otherwise.
  183. */
  184. protected abstract boolean isGoal(T node);
  185.  
  186. /**
  187. * Cost for the operation to go to <code>to</code> from
  188. * <code>from</from>.
  189. *
  190. * @param from The node we are leaving.
  191. * @param to The node we are reaching.
  192. * @return The cost of the operation.
  193. */
  194. protected abstract Double g(T from, T to);
  195.  
  196. /**
  197. * Estimated cost to reach a goal node.
  198. * An admissible heuristic never gives a cost bigger than the real
  199. * one.
  200. * <code>from</from>.
  201. *
  202. * @param from The node we are leaving.
  203. * @param to The node we are reaching.
  204. * @return The estimated cost to reach an object.
  205. */
  206. protected abstract Double h(T from, T to);
  207.  
  208.  
  209. /**
  210. * Generate the successors for a given node.
  211. *
  212. * @param node The node we want to expand.
  213. * @return A list of possible next steps.
  214. */
  215. protected abstract List<T> generateSuccessors(T node);
  216.  
  217.  
  218. private PriorityQueue<Path> paths;
  219. private HashMap<T, Double> mindists;
  220. private Double lastCost;
  221. private int expandedCounter;
  222.  
  223. /**
  224. * Check how many times a node was expanded.
  225. *
  226. * @return A counter of how many times a node was expanded.
  227. */
  228. public int getExpandedCounter(){
  229. return expandedCounter;
  230. }
  231.  
  232. /**
  233. * Default c'tor.
  234. */
  235. public AStar(){
  236. paths = new PriorityQueue<Path>();
  237. mindists = new HashMap<T, Double>();
  238. expandedCounter = 0;
  239. lastCost = 0.0;
  240. }
  241.  
  242.  
  243. /**
  244. * Total cost function to reach the node <code>to</code> from
  245. * <code>from</code>.
  246. *
  247. * The total cost is defined as: f(x) = g(x) + h(x).
  248. * @param from The node we are leaving.
  249. * @param to The node we are reaching.
  250. * @return The total cost.
  251. */
  252. protected Double f(Path p, T from, T to){
  253. Double g = g(from, to) + ((p.parent != null) ? p.parent.g : 0.0);
  254. Double h = h(from, to);
  255.  
  256. p.g = g;
  257. p.f = g + h;
  258.  
  259. return p.f;
  260. }
  261.  
  262. /**
  263. * Expand a path.
  264. *
  265. * @param path The path to expand.
  266. */
  267. private void expand(Path path){
  268. T p = path.getPoint();
  269. Double min = mindists.get(path.getPoint());
  270.  
  271. /*
  272. * If a better path passing for this point already exists then
  273. * don't expand it.
  274. */
  275. if(min == null || min.doubleValue() > path.f.doubleValue())
  276. mindists.put(path.getPoint(), path.f);
  277. else
  278. return;
  279.  
  280. List<T> successors = generateSuccessors(p);
  281.  
  282. for(T t : successors){
  283. Path newPath = new Path(path);
  284. newPath.setPoint(t);
  285. f(newPath, path.getPoint(), t);
  286. paths.offer(newPath);
  287. }
  288.  
  289. expandedCounter++;
  290. }
  291.  
  292. /**
  293. * Get the cost to reach the last node in the path.
  294. *
  295. * @return The cost for the found path.
  296. */
  297. public Double getCost(){
  298. return lastCost;
  299. }
  300.  
  301.  
  302. /**
  303. * Find the shortest path to a goal starting from
  304. * <code>start</code>.
  305. *
  306. * @param start The initial node.
  307. * @return A list of nodes from the initial point to a goal,
  308. * <code>null</code> if a path doesn't exist.
  309. */
  310. public List<T> compute(T start){
  311. try{
  312. Path root = new Path();
  313. root.setPoint(start);
  314.  
  315. /* Needed if the initial point has a cost. */
  316. f(root, start, start);
  317.  
  318. expand(root);
  319.  
  320. for(;;){
  321. Path p = paths.poll();
  322.  
  323. if(p == null){
  324. lastCost = Double.MAX_VALUE;
  325. return null;
  326. }
  327.  
  328. T last = p.getPoint();
  329.  
  330. lastCost = p.g;
  331.  
  332. if(isGoal(last)){
  333. LinkedList<T> retPath = new LinkedList<T>();
  334.  
  335. for(Path i = p; i != null; i = i.parent){
  336. retPath.addFirst(i.getPoint());
  337. }
  338.  
  339. return retPath;
  340. }
  341. expand(p);
  342. }
  343. }
  344. catch(Exception e){
  345. e.printStackTrace();
  346. }
  347. return null;
  348.  
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement