Advertisement
dermetfan

A* fails

Sep 28th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.60 KB | None | 0 0
  1. package net.dermetfan.libgdx.math;
  2.  
  3. import com.badlogic.gdx.math.Vector2;
  4. import com.badlogic.gdx.utils.Array;
  5.  
  6. public abstract class AStar {
  7.  
  8.     public static class Node {
  9.  
  10.         public Vector2 position;
  11.         public float weight, g_cost, h_heuristic, f_totalCost;
  12.         public boolean blocked;
  13.         public Node parent, neighbors[];
  14.  
  15.         public Node(float x, float y, float weight, boolean blocked) {
  16.             position = new Vector2(x, y);
  17.             this.weight = weight;
  18.             this.blocked = blocked;
  19.         }
  20.  
  21.     }
  22.  
  23.     public static Node[] findPath(Node start, Node end, Node[] nodes, Node[] path) {
  24.         // calculate heuristics by distance
  25.         for(Node node: nodes)
  26.             node.h_heuristic = node.position.dst(end.position);
  27.        
  28.         Node current = start, cheapestNeighbor;
  29.         float lowestF = Float.POSITIVE_INFINITY;
  30.         while(current != end) {
  31.             // find cheapest neighbor
  32.             for(Node neighbor : current.neighbors) {
  33.                 neighbor.f_totalCost = current.f_totalCost + neighbor.g_cost + neighbor.h_heuristic;
  34.                 if(neighbor.f_totalCost < lowestF); // TODO continue here
  35.             }
  36.         }
  37.        
  38.         return path;
  39.     }
  40.    
  41.     public static void pause() {
  42.         try {
  43.             Thread.sleep(1000);
  44.         } catch(InterruptedException e) {
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.  
  49. }
  50.  
  51. //      public static Node[] findPath(Node start, Node end, Node[] nodes) {
  52. ////            OPEN = priority queue containing START
  53. //          Array<Node> open = new Array<Node>();
  54. //          open.add(start);
  55. ////            CLOSED = empty set
  56. //          Array<Node> closed = new Array<Node>();
  57. ////            while lowest rank in OPEN is not the GOAL:
  58. //          Node current;
  59. //          while(open.contains(end, true)) {
  60. ////              current = remove lowest rank item from OPEN
  61. //            current = open.pop();
  62. ////              add current to CLOSED
  63. //            closed.add(current);
  64. ////              for neighbors of current:
  65. //            for(Node neighbor : current.neighbors) {
  66. ////                cost = g(current) + movementcost(current, neighbor)
  67. ////                if neighbor in OPEN and cost less than g(neighbor):
  68. ////                  remove neighbor from OPEN, because new path is better
  69. ////                if neighbor in CLOSED and cost less than g(neighbor): **
  70. ////                  remove neighbor from CLOSED
  71. ////                if neighbor not in OPEN and neighbor not in CLOSED:
  72. ////                  set g(neighbor) to cost
  73. ////                  add neighbor to OPEN
  74. ////                  set priority queue rank to g(neighbor) + h(neighbor)
  75. ////                  set neighbor's parent to current
  76. //            }
  77. //          }
  78. ////            reconstruct reverse path from goal to start
  79. ////            by following parent pointers
  80. //          return null;
  81. //      }
  82. //     
  83. //  }
  84. // 
  85. //}
  86. //
  87. //      public static Node[] findPath(Node start, Node end, Node[] nodes) {
  88. //          Array<Node> open = new Array<Node>(), closed = new Array<Node>();
  89. //
  90. //          Node current;
  91. //
  92. //          open.add(start);
  93. //          long startTime = TimeUtils.millis();
  94. //          while(!closed.contains(end, true) && TimeUtils.millis() - startTime < 1000) {
  95. //
  96. //              current = findLowestFNode(open);
  97. //              closed.add(current);
  98. //
  99. //              for(Node neighbor : current.neighbors) {
  100. //                  if(neighbor.blocked || closed.contains(neighbor, true))
  101. //                      continue;
  102. //                  if(!open.contains(neighbor, true))
  103. //                      open.add(neighbor);
  104. //                  neighbor.parent = current;
  105. //                  neighbor.f_totalCost = (neighbor.g_cost = neighbor.parent.f_totalCost + neighbor.weight) + (neighbor.h_heuristic = neighbor.position.dst(end.position));
  106. //                  if(open.contains(neighbor, true))
  107. //                      if(neighbor.g_cost < neighbor.parent.g_cost) {
  108. //                          neighbor.parent = current;
  109. //                          neighbor.f_totalCost = (neighbor.g_cost = neighbor.parent.f_totalCost + neighbor.weight) + neighbor.h_heuristic;
  110. //                      }
  111. //              }
  112. //
  113. //          }
  114. //         
  115. //          Array<Node> path = new Array<Node>(Node.class);
  116. //
  117. //          current = end;
  118. //          while(current != start) {
  119. //              path.add(current);
  120. //              current = current.parent;
  121. //          }
  122. //          path.add(current);
  123. //
  124. //          return path.toArray();
  125. //      }
  126. //
  127. //      public static Node FNode(Array<Node> nodes) {
  128. //          Node lowestFNode = nodes.first();
  129. //          for(Node node : nodes)
  130. //              if(node.f_totalCost < lowestFNode.f_totalCost)
  131. //                  lowestFNode = node;
  132. //          return lowestFNode;
  133. //      }
  134. //
  135. //  }
  136. //}
  137. //
  138. //  public static Node[] findPath(Node start, Node end, Node[] nodes) {
  139. //      // calculate heuristics
  140. //      for(Node node : nodes)
  141. //          node.h_heuristic = node.position.dst(end.position);
  142. //
  143. //      // find path
  144. //      int pathSize = 1;
  145. //      Node current = start, previous = start;
  146. //      while(current != end && pathSize < nodes.length) {
  147. //          current.parent = previous;
  148. //
  149. //          // calculate neighbor total costs
  150. //          for(Node neighbor : current.neighbors)
  151. //              neighbor.f_totalCost = current.f_totalCost + neighbor.weight + neighbor.h_heuristic + (neighbor.blocked ? 1000 : 0);
  152. //
  153. //          // get neighbor with lowest total cost
  154. //          Node cheapestNeighbor = current.neighbors[0];
  155. //          for(Node neighbor : current.neighbors)
  156. //              if(neighbor.f_totalCost < cheapestNeighbor.f_totalCost)
  157. //                  cheapestNeighbor = neighbor;
  158. //
  159. //          previous = current;
  160. //          current = cheapestNeighbor;
  161. //          pathSize++;
  162. //      }
  163. //
  164. //      end.parent = previous;
  165. //
  166. //      // trace back path // until current.parent is previous
  167. //      Node[] path = new Node[pathSize];
  168. //
  169. //      // current is end
  170. //      // until current is start
  171. //      int i = pathSize - 1;
  172. //      while(current != start) {
  173. //
  174. //          path[i] = current;
  175. //          current = current.parent;
  176. //
  177. //          i--;
  178. //      }
  179. //
  180. //      return path;
  181. //  }
  182. //
  183. //}
  184. //
  185. //
  186. //
  187. //
  188. //
  189. //
  190. //
  191. //
  192. //
  193. //
  194. //
  195. //
  196. //
  197. //
  198. //
  199. //
  200. //
  201. //
  202. //
  203. //
  204. //
  205. //
  206. //
  207. //
  208. //
  209. //
  210. //
  211. //
  212. //
  213. //
  214. //
  215. //
  216. //
  217. //
  218. //
  219. //
  220. //
  221. //
  222. //
  223. //
  224. //
  225. //
  226. //
  227. //
  228. //
  229. //
  230. //
  231. //
  232. //
  233. //
  234. //
  235. //
  236. //
  237. //
  238. //
  239. //
  240. //
  241. //
  242. //
  243. //
  244. //
  245. //
  246. //
  247. //
  248. //
  249. //
  250. //
  251. //
  252. //
  253. //
  254. //
  255. //
  256. //
  257. //
  258. //
  259. //
  260. //
  261. //
  262. //
  263. //
  264. //
  265. //
  266. //
  267. //
  268. //
  269. //
  270. //      public static class Node {
  271. //
  272. //          public Vector2 position;
  273. //          public Node parent;
  274. //          public Node[] neighbors;
  275. //          public float weight, heuristic, total = 0;
  276. //          public boolean blocked;
  277. //
  278. //          public Node(float x, float y, float weight, boolean blocked) {
  279. //              position = new Vector2(x, y);
  280. //              this.weight = weight;
  281. //              this.blocked = blocked;
  282. //          }
  283. //
  284. //          @Override
  285. //          public String toString() {
  286. //              return position.toString() + ", weight: " + weight + ", heuristic: " + heuristic + ", total: " + total + ", blocked: " + blocked + ", parent: " + (parent != null ? parent.position : false) /*parent.toString()*/+ ", neighbors: " + (neighbors != null);
  287. //          }
  288. //
  289. //      }
  290. //
  291. //      private static final Array<Node> closed = new Array<Node>();
  292. //
  293. //      public static Node[] findPath(Node[] nodes, Node start, Node end, Node[] output) {
  294. //          closed.clear();
  295. //
  296. //          // calculate heuristics
  297. //          for(Node node : nodes)
  298. //              node.heuristic = node.position.dst(end.position);
  299. //
  300. //          // main loop
  301. //          start.parent = start;
  302. //          int pathSize = 1;
  303. //          float lowestTotal;
  304. //          Node node = start, lowestTotalNeighbor = null;
  305. //          while(node != end) {
  306. //              node.total = node.parent.total + node.weight + node.heuristic;
  307. //              for(Node neighbor : node.neighbors) {
  308. //                  neighbor.parent = node;
  309. //                  if(closed.contains(neighbor, true))
  310. //                      continue;
  311. //                  else if(neighbor.blocked) {
  312. //                      closed.add(neighbor);
  313. //                      continue;
  314. //                  }
  315. //                  neighbor.total = neighbor.parent.total + neighbor.weight + neighbor.heuristic;
  316. //              }
  317. //              lowestTotal = Float.POSITIVE_INFINITY;
  318. //              for(Node neighbor : node.neighbors) {
  319. //                  if(closed.contains(neighbor, true))
  320. //                      continue;
  321. //                  if(neighbor.total < lowestTotal) {
  322. //                      lowestTotal = neighbor.total;
  323. //                      lowestTotalNeighbor = neighbor;
  324. //                      // TODO the problem is here, this counts all neighbors that have a lower total than the previous neighbor on to the pathSize but it should only be added the one that has the lowest total of of all neighbors
  325. //                  } else
  326. //                      closed.add(neighbor);
  327. //              }
  328. //              node = lowestTotalNeighbor;
  329. //              pathSize++;
  330. //          }
  331. //
  332. //          // find out path
  333. //          output = new Node[pathSize];
  334. //          while(node != start) {
  335. //              if(pathSize < 1) {
  336. //                  System.out.println("pathSize is " + (pathSize - 1));
  337. //                  break;
  338. //              }
  339. //              output[--pathSize] = node.parent;
  340. //              node = node.parent;
  341. //          }
  342. //
  343. //          for(int i = 0; i < output.length; i++)
  344. //              System.out.println(i + ": " + output[i]);
  345. //
  346. //          return output;
  347. //      }
  348. //  }
  349. //}
  350.  
  351. //
  352. //
  353. //
  354. //
  355. //
  356. //
  357. //
  358. //
  359. //
  360. //
  361. //
  362. //
  363. //
  364. //
  365. //
  366. //
  367. //
  368. //
  369. //
  370. //
  371. //
  372. //
  373. //
  374. //
  375. //
  376. //
  377. //
  378. //
  379. //public static Node[][] generateGridMap(float[][] weight, float blockedValue, boolean wrapX, boolean wrapY, boolean diagonals) {
  380. //Node[][] map = new Node[weight.length][];
  381. //int x, y;
  382. //
  383. //int neighbours = diagonals ? 8 : 4;
  384. //for(x = 0; x < weight.length; x++) {
  385. //  map[x] = new Node[weight[x].length];
  386. //  for(y = 0; y < weight[x].length; y++)
  387. //      map[x][y] = new Node(weight[x][y], weight[x][y] == blockedValue, neighbours);
  388. //}
  389. //
  390. //for(x = 0; x < map.length; x++)
  391. //  for(y = 0; y < map[x].length; y++) {
  392. //      map[x][y].neighbours[0] = map[x][(y - 1 + map[x].length) % map[x].length]; // above
  393. //      map[x][y].neighbours[1] = map[(x - 1 + map.length) % map.length][y]; // left
  394. //      map[x][y].neighbours[2] = map[(x + 1) % map.length][y]; // right
  395. //      map[x][y].neighbours[3] = map[x][(y + 1) % map[x].length]; // below
  396. //  }
  397. //
  398. //if(diagonals)
  399. //  for(x = 0; x < map.length; x++)
  400. //      for(y = 0; y < map[x].length; y++) {
  401. //          map[x][y].neighbours[4] = map[(x - 1 + map.length) % map.length][(y - 1 + map[x].length) % map[x].length]; // left above
  402. //          map[x][y].neighbours[5] = map[(x + 1) % map.length][(y - 1 + map[x].length) % map[x].length]; // right above
  403. //          map[x][y].neighbours[6] = map[(x - 1 + map.length) % map.length][(y + 1) % map[x].length]; // left below
  404. //          map[x][y].neighbours[7] = map[(x + 1) % map.length][(y + 1) % map[x].length]; // right below
  405. //      }
  406. //
  407. //return map;
  408. //}
  409. //
  410. //public static Vector2[] findPathOnGrid(Node[][] map, int startX, int startY, int endX, int endY, float moveCost, float diagonalMoveCost) {
  411. //open.clear();
  412. //closed.clear();
  413. //
  414. //int size = 1, x, y;
  415. //for(x = 0; x < map.length; x++) {
  416. //  size++;
  417. //  for(y = 0; y < map[x].length; y++) {
  418. //      size++;
  419. //      map[x][y].heuristic = Math.abs(endX - x) + Math.abs(endY - y);
  420. //  }
  421. //}
  422. //
  423. //open.ensureCapacity(size);
  424. //closed.ensureCapacity(size);
  425. //
  426. //x = startX;
  427. //y = startY;
  428. //do {
  429. //  closed.add(map[x][y]);
  430. //  for(int n = 0; n < map[x][y].neighbours.length; n++)
  431. //      if(!closed.contains(map[x][y].neighbours[n], true))
  432. //          if(!open.contains(map[x][y].neighbours[n], true)) {
  433. //              map[x][y].neighbours[n].parent = map[x][y];
  434. //          } else {
  435. //              map[x][y].neighbours[n].total = map[x][y].total + n > 3 ? diagonalMoveCost : moveCost;
  436. //          }
  437. //
  438. //} while(open.size > 0);
  439. //
  440. //return null;
  441. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement