Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Solution to problem using breadth first :
- Exception in thread "main" java.lang.NullPointerException
- at SolvingProblem.isGoal(SolvingProblem.java:24)
- at AbstractTreeSearch.solve(AbstractTreeSearch.java:31)
- at EightP.main(EightP.java:15)
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Arrays;
- public class SolvingProblem implements Problem {
- State initialState = new State(State.arrayA);
- State GoalState = new State(State.arrayG);
- @Override
- public Object getInitialState() {
- return initialState;
- }
- @Override
- public boolean isGoal(Object state) {
- return state.equals(GoalState);
- }
- import java.util.ArrayList;
- import java.util.Collection;
- public abstract class AbstractTreeSearch implements Search {
- Collection<Node> frontier;
- public Node solve(Problem problem) {
- //initialize the search tree using the initial state of problem
- frontier = initFrontier();
- frontier.addAll(expand(new Node(problem.getInitialState()), problem));
- //Starting frontier
- boolean done = false;
- Node solution = null;
- while (!done) {
- if (frontier.isEmpty()) {
- System.out.println("Blank frontier");
- done = true;
- } else {
- Node node = chooseLeafNode(frontier, problem);
- //inspecting node
- if (problem.isGoal(node.getState())) {
- System.out.println("Solution found");
- System.out.println();
- solution = node;
- done = true;
- } else {
- //Expanding node, frontier is..
- frontier.addAll(expand(node, problem));
- }
- }
- }
- return solution;
- }
- SolvingProblem p2 = new SolvingProblem();
- System.out.println("Solution to problem using breadth first : ");
- System.out.println(new BreadthFirstTreeSearch().solve(p2).pathToString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement