Advertisement
Guest User

LabCat

a guest
May 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.PriorityQueue;
  6. import java.util.Scanner;
  7.  
  8.  
  9. class StateComparator implements Comparator<State> {
  10.  
  11. enum Algorithm {
  12. BestFirst, AStar
  13. }
  14.  
  15. private Algorithm algorithm;
  16. private State solutionState;
  17.  
  18. /**
  19. * Create a new StateComparator which compares two different states given
  20. * the wanted algorithm and solution state.
  21. * @param algorithm BestFirst or AStar
  22. * @param solutionState desired solution state
  23. */
  24. public StateComparator(Algorithm algorithm, State solutionState) {
  25. this.algorithm = algorithm;
  26. this.solutionState = solutionState;
  27. }
  28.  
  29. private int computeF(State state) {
  30. int result = 0;
  31. /* f(n) = g(n) + h(n) */
  32. switch(algorithm) {
  33. case BestFirst:
  34. /* g(n) = 0 */
  35. result = state.approximateDistance(solutionState);
  36. case AStar:
  37. /* g(n) = numarul de mutari din pozitia initiala */
  38. result = state.getDistance() + state.approximateDistance(solutionState);
  39. }
  40. return result;
  41. }
  42.  
  43. @Override
  44. public int compare(State state0, State state1) {
  45. return computeF(state0) - computeF(state1);
  46. }
  47. }
  48.  
  49. public class MainClass {
  50.  
  51. static State initialState;
  52. static State solutionState;
  53.  
  54. public static void readData(String filename) {
  55. int x, y;
  56. int numRows, numCols;
  57.  
  58. Scanner scanner;
  59. try {
  60. scanner = new Scanner(new File(filename));
  61.  
  62. /* read map stats */
  63.  
  64. numRows = scanner.nextInt();
  65. numCols = scanner.nextInt();
  66. State.init(numRows, numCols);
  67.  
  68. x = scanner.nextInt();
  69. y = scanner.nextInt();
  70. initialState = new State(x, y);
  71.  
  72. x = scanner.nextInt();
  73. y = scanner.nextInt();
  74. solutionState = new State(x, y);
  75.  
  76. /* read the map */
  77. for(int i = 0; i < State.numRows; i++)
  78. for(int j = 0; j < State.numCols; j++)
  79. if(scanner.nextInt() == 1)
  80. State.matrix[i][j] = true;
  81.  
  82. scanner.close();
  83. } catch (FileNotFoundException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. }
  88. private static boolean is_explored(ArrayList<State> closed , State state) {
  89. for (int i = 0; i < closed.size(); i++) {
  90. if(state.equals(closed.get(i))) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96.  
  97. public static void main(String[] args) {
  98. /* Citire harta, stare initiala si finala */
  99. readData("Puzzle.in");
  100.  
  101. Comparator<State> stateComparator = new StateComparator(StateComparator.Algorithm.AStar, solutionState);
  102. PriorityQueue<State> open = new PriorityQueue<State>(1, stateComparator);
  103.  
  104. /* Initial doar nodul de start este in curs de explorare */
  105. open.add(initialState);
  106.  
  107. /* Pentru nodurile care au fost deja expandate. */
  108. ArrayList<State> closed = new ArrayList<State>();
  109. State value;
  110.  
  111. /* TODO: A* */
  112.  
  113. while(!open.isEmpty()) {
  114. value = open.peek();
  115. open.poll();
  116. if(value.equals(solutionState)) {
  117. value.printPath();
  118. System.out.println();
  119. }
  120.  
  121. ArrayList<State> vecini = new ArrayList<State>();
  122.  
  123. if(!is_explored(closed, value)) {
  124. closed.add(value);
  125. value.expand(vecini);
  126. }
  127. for (int i = 0; i < vecini.size(); i++) {
  128. open.add(vecini.get(i));
  129. }
  130.  
  131. value.printPath();
  132. }
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement