1

I'm trying to solve the 8-puzzle problem using the heuristic search. I'm using a 3*3 matrix to represent a possibility. The code is not complete, but when I try to add the explored element to the explored set(which is an ArrayList), it only updates the current element in explored set instead of adding one more element to the end. When I try to print all the elements in the explored set, there is always only one element (updated each iteration). I'm wondering what is wrong with my code. Thank you!!

public static void printexplored(ArrayList<int[][]> explored){
        //System.out.println("the size of the explored set is " + explored.size());
        System.out.println("the explored set is...");
        while(explored.isEmpty() == false){
            int[][] temp = explored.remove(0);
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    System.out.print(temp[i][j]);
                }
                System.out.println();
            }
            System.out.println();
        }
    } 


public static boolean heuristicSearch(int initialState[][]){
        Queue<int[][]> frontier = new LinkedList<int[][]>();
        frontier.add(initialState);
        ArrayList<int[][]> explored = new ArrayList<int[][]>();
        int f_score = 0;
        //int count = 0;

        while(frontier.isEmpty() == false){

            int[][] temporaryState = new int[3][3]; 

            temporaryState = frontier.remove();
            int indexX = blankIndexX(temporaryState);
            int indexY = blankIndexY(temporaryState);

            explored.add(temporaryState);

            printexplored(explored);
  • 1
    Your code is incomplete; there are no end braces for either the last while loop or the method containing it. – arcy Feb 25 '19 at 17:44
  • 2
    Your print() method removes all the items in the explored list one-by-one. Ordinarily, a print() method would not modify the objects referenced by its arguments. You could instead *iterate* through the explored list. See `List.iterator()`. – Andy Thomas Feb 25 '19 at 17:48

2 Answers2

1

Your code is incomplete but one thing which immediately stands out is that you are adding and removing element to the explored list at the same time. See comments below:

public static void printexplored(ArrayList<int[][]> explored){
        //System.out.println("the size of the explored set is " + explored.size());
        System.out.println("the explored set is...");
        while(explored.isEmpty() == false){

//---->YOU REMOVED THE ELEMENT WHICH WAS ADDED EARLIER HERE:

            int[][] temp = explored.remove(0);
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    System.out.print(temp[i][j]);
                }
                System.out.println();
            }
            System.out.println();
        }
    } 


public static boolean heuristicSearch(int initialState[][]){
        Queue<int[][]> frontier = new LinkedList<int[][]>();
        frontier.add(initialState);
        ArrayList<int[][]> explored = new ArrayList<int[][]>();
        int f_score = 0;
        //int count = 0;

        while(frontier.isEmpty() == false){

            int[][] temporaryState = new int[3][3]; 

            temporaryState = frontier.remove();
            int indexX = blankIndexX(temporaryState);
            int indexY = blankIndexY(temporaryState);

    //---->YOU ARE ADDING AN ELEMENT HERE BUT REMOVING IT LATER IN THE THE 
    //printexplored METHOD:

            explored.add(temporaryState);

            printexplored(explored);
Stacky
  • 503
  • 2
  • 6
  • 23
  • This answer could be improved by showing a fix -- in this case, a non-destructive method to iterate over a List. – Andy Thomas Feb 25 '19 at 17:52
1

In your printing method, you're removing the element from the list.
To fix, replace the following lines in printexplored() method:

        while(explored.isEmpty() == false){
            int[][] temp = explored.remove(0);

for this one:

        for (int i = 0; i < explored.size(); i++) {
            int[][] temp = explored.get( i );
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
André Paris
  • 139
  • 6