Sharing of my labs carried out during the TDDC17 course at Linköping University.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomGraphSearch.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package searchCustom;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import searchShared.NodeQueue;
  5. import searchShared.Problem;
  6. import searchShared.SearchObject;
  7. import searchShared.SearchNode;
  8. import world.GridPos;
  9. public class CustomGraphSearch implements SearchObject {
  10. private HashSet<SearchNode> explored;
  11. private NodeQueue frontier;
  12. protected ArrayList<SearchNode> path;
  13. private boolean insertFront;
  14. /**
  15. * The constructor tells graph search whether it should insert nodes to front or back of the frontier
  16. */
  17. public CustomGraphSearch(boolean bInsertFront) {
  18. insertFront = bInsertFront;
  19. }
  20. /**
  21. * Implements "graph search", which is the foundation of many search algorithms
  22. */
  23. public ArrayList<SearchNode> search(Problem problem) {
  24. // The frontier is a queue of expanded SearchNodes not processed yet
  25. frontier = new NodeQueue();
  26. // The explored set is a set of nodes that have been processed
  27. explored = new HashSet<SearchNode>();
  28. // The start state is given
  29. GridPos startState = (GridPos) problem.getInitialState();
  30. // Initialise the frontier with the start state
  31. frontier.addNodeToFront(new SearchNode(startState));
  32. // Path will be empty until we find the goal.
  33. path = new ArrayList<SearchNode>();
  34. /*
  35. * Note on how java and this program work:
  36. * -ArrayList are just resizable array. "<>" contains the type of the listed objects
  37. * -Structures :
  38. * while (boolean) {}
  39. * while (a > 0) {}
  40. * for (int i = 0; i < 5; i++){} --> 5 iterations
  41. * for (String i : StringList){} --> Iterations on all the objects in the list
  42. * -Class used here:
  43. * -SearchNode --> Equivalent of the "Node" class in the Python program. Represent a node.
  44. * -Problem --> Class containing lot of informations on the problem
  45. * like the reachable states from a position, the start, the end
  46. * -GridPos --> represent a position (with x and y coordinates)
  47. */
  48. // While we have something to explore
  49. while (frontier.size() > 0) {
  50. // Get the first node on the list
  51. SearchNode Node = frontier.removeFirst();
  52. // We check if it is our destination
  53. if (problem.isGoalState(Node.getState())) {
  54. return path = Node.getPathFromRoot();
  55. } // if not, we continue
  56. // Get the list of children of the current node
  57. ArrayList<GridPos> childPosition = problem.getReachableStatesFrom(Node.getState());
  58. // For every child in the list
  59. for (GridPos position : childPosition) {
  60. // Creating a new node
  61. SearchNode childNode = new SearchNode(position, Node);
  62. // If we have not explored the node
  63. if (explored.contains(childNode) == false) {
  64. // Adding the child node to the frontier
  65. // Front --> DFS (LIFO queue)
  66. // Back --> BFS (FIFO queue)
  67. if (insertFront) {
  68. frontier.addNodeToFront(childNode);
  69. }
  70. else {
  71. frontier.addNodeToBack(childNode);
  72. }
  73. // We add the node to explored node
  74. explored.add(childNode);
  75. }
  76. }
  77. }
  78. /* Some hints:
  79. * -Read early part of chapter 3 in the book!
  80. * -You are free to change anything how you wish as long as the program runs, but some structure is given to help you.
  81. * -You can Google for "javadoc <class>" if you are uncertain of what you can do with a particular Java type.
  82. *
  83. * -SearchNodes are the nodes of the search tree and contains the relevant problem state, in this case x,y position (GridPos) of the agent
  84. * --You can create a new search node from a state by: SearchNode childNode = new SearchNode(childState, currentNode);
  85. * --You can also extract the state by .getState() method
  86. * --All search structures use search nodes, but the problem object only speaks in state, so you may need to convert between them
  87. *
  88. * -The frontier is a queue of search nodes, open this class to find out what you can do with it!
  89. *
  90. * -If you are unfamiliar with Java, the "HashSet<SearchNode>" used for the explored set means a set of SearchNode objects.
  91. * --You can add nodes to the explored set, or check if it contains a node!
  92. *
  93. * -To get the child states (adjacent grid positions that are not walls) of a particular search node, do: ArrayList<GridPos> childStates = p.getReachableStatesFrom(currentState);
  94. *
  95. * -Depending on the addNodesToFront boolean variable, you may need to do something with the frontier... (see book)
  96. *
  97. * -You can check if you have reached the goal with p.isGoalState(NodeState)
  98. *
  99. * When the goal is found, the path to be returned can be found by: path = node.getPathFromRoot();
  100. */
  101. /* Note: Returning an empty path signals that no path exists */
  102. return path;
  103. }
  104. /*
  105. * Functions below are just getters used externally by the program
  106. */
  107. public ArrayList<SearchNode> getPath() {
  108. return path;
  109. }
  110. public ArrayList<SearchNode> getFrontierNodes() {
  111. return new ArrayList<SearchNode>(frontier.toList());
  112. }
  113. public ArrayList<SearchNode> getExploredNodes() {
  114. return new ArrayList<SearchNode>(explored);
  115. }
  116. public ArrayList<SearchNode> getAllExpandedNodes() {
  117. ArrayList<SearchNode> allNodes = new ArrayList<SearchNode>();
  118. allNodes.addAll(getFrontierNodes());
  119. allNodes.addAll(getExploredNodes());
  120. return allNodes;
  121. }
  122. }