Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. Solution to problem using breadth first :
  2. Exception in thread "main" java.lang.NullPointerException
  3. at SolvingProblem.isGoal(SolvingProblem.java:24)
  4. at AbstractTreeSearch.solve(AbstractTreeSearch.java:31)
  5. at EightP.main(EightP.java:15)
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.Arrays;
  10.  
  11. public class SolvingProblem implements Problem {
  12.  
  13. State initialState = new State(State.arrayA);
  14. State GoalState = new State(State.arrayG);
  15.  
  16. @Override
  17. public Object getInitialState() {
  18. return initialState;
  19. }
  20.  
  21. @Override
  22. public boolean isGoal(Object state) {
  23. return state.equals(GoalState);
  24. }
  25.  
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28.  
  29. public abstract class AbstractTreeSearch implements Search {
  30.  
  31. Collection<Node> frontier;
  32.  
  33. public Node solve(Problem problem) {
  34.  
  35. //initialize the search tree using the initial state of problem
  36. frontier = initFrontier();
  37. frontier.addAll(expand(new Node(problem.getInitialState()), problem));
  38. //Starting frontier
  39. boolean done = false;
  40. Node solution = null;
  41. while (!done) {
  42. if (frontier.isEmpty()) {
  43. System.out.println("Blank frontier");
  44. done = true;
  45. } else {
  46. Node node = chooseLeafNode(frontier, problem);
  47. //inspecting node
  48. if (problem.isGoal(node.getState())) {
  49. System.out.println("Solution found");
  50. System.out.println();
  51. solution = node;
  52. done = true;
  53. } else {
  54. //Expanding node, frontier is..
  55. frontier.addAll(expand(node, problem));
  56.  
  57. }
  58. }
  59. }
  60. return solution;
  61. }
  62.  
  63. SolvingProblem p2 = new SolvingProblem();
  64. System.out.println("Solution to problem using breadth first : ");
  65. System.out.println(new BreadthFirstTreeSearch().solve(p2).pathToString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement