Advertisement
Guest User

Untitled

a guest
May 6th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.PriorityQueue;
  7. import java.util.Queue;
  8.  
  9. public class Searcher {
  10. private Queue<State> openSet;
  11. private List<State> closedSet;
  12.  
  13. /**
  14. * Constructor
  15. * Sets up the OpenSet and CloseSet
  16. */
  17. public Searcher(){
  18. this.openSet = new PriorityQueue<State>(10, new Comparator<State>(){
  19.  
  20. @Override
  21. public int compare(State o1, State o2){
  22. if (o1.getDistance() < o2.getDistance()){
  23. return -1;
  24. } if (o1.getDistance() > o2.getDistance()){
  25. return 1;
  26. } else {
  27. return 0;
  28. }
  29. }
  30. });
  31.  
  32. this.closedSet = new LinkedList<State>();
  33. }
  34.  
  35. /**
  36. * Perform the AStarSearch
  37. * @param goal
  38. * @param g
  39. */
  40. public void aStarSearch(Goal goal, Graph g){
  41. int totalCost = 0;
  42. int nodesExpanded = 0;
  43.  
  44. State prevTrip = null;
  45.  
  46. State start = new State("London", 0);
  47. openSet.add(start);
  48.  
  49. while (!openSet.isEmpty()){
  50. prevTrip = openSet.poll();
  51. closedSet.add(prevTrip);
  52. nodesExpanded = closedSet.size();
  53. checkNeighbour(prevTrip, g);
  54. goal.setGoalReached(checkGoalReached(goal, closedSet.get(closedSet.size()-1)));
  55.  
  56. if (goal.getGoalReached() == true){
  57. totalCost = closedSet.get(closedSet.size()-1).getDistance() + getTransferTime(g, closedSet.get(closedSet.size()-1));
  58. break;
  59. }
  60. }
  61. getPath(nodesExpanded, totalCost, closedSet.get(closedSet.size()-1));
  62. }
  63.  
  64. /**
  65. * checks whether goal has been reached
  66. * @param g
  67. * @param n
  68. * @return
  69. */
  70. public boolean checkGoalReached(Goal g, State n){
  71. boolean goalReached = true;
  72. Iterator<GoalData> itr = g.getTripData().iterator();
  73.  
  74. while (itr.hasNext()){
  75. if (stateLooper(itr.next(), n)== false){
  76. goalReached = false;
  77. break;
  78. }
  79. }
  80.  
  81. return goalReached;
  82. }
  83.  
  84. /**
  85. * loops through the state
  86. * @param g
  87. * @param n
  88. * @return
  89. */
  90. private boolean stateLooper(GoalData g, State n){
  91. boolean check = false;
  92. int i = 0;
  93. int j = 1;
  94. if (n.getPrevState() == null){
  95. return check;
  96. }
  97. while (j < n.getPrevState().size()){
  98. if (n.getPrevState().get(i).equals(g.getFrom()) && n.getPrevState().get(j).equals(g.getTo())){
  99. check = true;
  100. break;
  101. }
  102. i++;
  103. j++;
  104. }
  105. if (check == false){
  106. if (n.getCurrCity().equals(g.getTo()) && n.getPrevState().get(n.getPrevState().size()-1).equals(g.getFrom())){
  107. check = true;
  108. }
  109. }
  110. return check;
  111. }
  112.  
  113. /**
  114. * Gets Transfer Time and add it together
  115. * @param g
  116. * @param s
  117. * @return
  118. */
  119. public int getTransferTime(Graph g, State s){
  120. int transferCost = 0;
  121. int i = 1;
  122. while (i < s.getPrevState().size()){
  123. transferCost += g.getNode(s.getPrevState().get(i)).getTransferTime();
  124. i++;
  125. }
  126. return transferCost;
  127. }
  128.  
  129. /**
  130. * Loops through the edges and create states and add it to the openSet
  131. * @param curr
  132. * @param g
  133. */
  134. public void checkNeighbour(State curr, Graph g){
  135. State neighbours = null;
  136. Node currentCity = g.getNode(curr.getCurrCity());
  137. ArrayList<Edge> edges = currentCity.getEdges();
  138. int i = 0;
  139. while (i < edges.size()){
  140. neighbours = new State(edges.get(i).getTo(), edges.get(i).getWeight() + curr.getDistance() + g.getNode(curr.getCurrCity()).getTransferTime());
  141. openSet.add(neighbours);
  142. neighbours.clone(curr.getPrevState());
  143. neighbours.getPrevState().add(curr.getCurrCity());
  144. i++;
  145. }
  146. }
  147.  
  148. /**
  149. * Takes the last State and create a path out of it
  150. * @param nodesExpanded
  151. * @param cost
  152. * @param fullTrip
  153. */
  154. public void getPath(int nodesExpanded, int cost, State fullTrip){
  155. System.out.println(nodesExpanded + " nodes expanded");
  156. System.out.println("cost = " + cost);
  157. int i = 0;
  158. int j = 1;
  159.  
  160. while (j < fullTrip.getPrevState().size()){
  161. System.out.println("Trip " + fullTrip.getPrevState().get(i) + " to " + fullTrip.getPrevState().get(j));
  162. i++;
  163. j++;
  164. }
  165.  
  166. System.out.println("Trip " + fullTrip.getPrevState().get(fullTrip.getPrevState().size()-1) + " to " + fullTrip.getCurrCity());
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement