Newer
Older
PBL / PBL / src / framework / AI / AStar.java
  1. package framework.AI;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6. * A*クラス。
  7. */
  8. public class AStar {
  9. private LinkedList<Location> locationOpenList = new LinkedList<Location>();
  10. private LinkedList<Location> locationClosedList = new LinkedList<Location>();
  11.  
  12. /**
  13. * 最短経路探査。 最短経路のリストを返すか、見つからない場合nullを返す。
  14. * start = goalの場合もnullを返す。
  15. * 戻り値にはgoalからstartの1個前まで経路をさかのぼったものが入っている。startは入っていない。
  16. */
  17. public Plan getPath(Location startLocation,
  18. Location goalLocation) {
  19. //出発地点と目的地点が同じならnullを返す
  20. if (startLocation.equals(goalLocation)) {
  21. return null;
  22. }
  23. // 初期化
  24. locationOpenList = new LinkedList<Location>();
  25. locationClosedList = new LinkedList<Location>();
  26. // 出発地点をLocationOpenListに追加する
  27. locationOpenList.add(startLocation);
  28. while (!locationOpenList.isEmpty()) {
  29. // LocationOpenListの最小スコアの地点をcurLocationとして取り出す
  30. Collections.sort(locationOpenList, new CostComparator());
  31. Location curLocation = locationOpenList.removeFirst();
  32.  
  33. // 現在の地点が目的地点ならば探査完了
  34. if (curLocation.equals(goalLocation)) {
  35. LinkedList<Location> locationPath = new LinkedList<Location>();
  36.  
  37. // 出発地点まで通った地点を辿る
  38. curLocation = goalLocation;
  39. while (!curLocation.equals(startLocation)) {
  40. locationPath.add(curLocation);
  41. curLocation = (Location) curLocation.getParent();
  42. }
  43. return new Plan(locationPath);
  44. }
  45.  
  46. // 現在の地点が目的地点でないなら、現在の地点をLocationClosedListに移す
  47. locationClosedList.add(curLocation);
  48.  
  49. // 隣接する地点を調べる
  50. ArrayList<IState> neighbors = curLocation.getSuccessors();
  51.  
  52. // 隣接する地点の数だけ、ループ
  53. for (int i = 0; i < neighbors.size(); i++) {
  54. // 通過でき、各リストに入ってないならコストをもらい、
  55. if (!locationOpenList.contains(neighbors.get(i))
  56. && !locationClosedList.contains(neighbors.get(i))) {
  57. //地点コストを求める
  58. //agent.getLocationCost((Location)neighbors.get(i), null, null, null);
  59. //パスコストを求める
  60. ((Location)neighbors.get(i)).calculatesCosts(curLocation, goalLocation);
  61.  
  62. // LocationOpenListに移す
  63. locationOpenList.add((Location) neighbors.get(i));
  64. }
  65. }
  66.  
  67. // -------------------------------------------------------------------------------------------------------------------------------------------
  68.  
  69. }
  70. return null;
  71. }
  72.  
  73. // -------------------------------------------------------------------------------------------------------------------------------------------
  74. // コストの昇順ソートのためのクラスを作ろう
  75.  
  76. /**
  77. * ソート条件式クラス。
  78. */
  79. class CostComparator implements Comparator<Location> {
  80. public int compare(Location n1, Location n2) {
  81. return n1.getScore() - n2.getScore();
  82. }
  83. }
  84.  
  85. // -------------------------------------------------------------------------------------------------------------------------------------------
  86.  
  87. }