Question

Apply the A algorithm to the 8-puzzle problem to the initial tile pattern shown below to arrive at the goal node. Show expan
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;


class state {

public:
   int board[3][3];
   int g,h;

   state *came_from;

  
   state() {
       g=0;
       h=0;
       came_from = NULL;
   }
   void print() {
       for(int i=0;i<3;i++) {
           for(int j=0;j<3;j++)
               cout<<board[i][j]<<" ";
           cout<<endl;
       }
       printf("g: %d, h:%d\n",g, h);
       int eval = h - g;
       printf("heuristic value : %d \n\n", eval);
   }
  
  
   bool operator ==(state a) {
       for(int i=0;i<3;i++)
           for(int j=0;j<3;j++)
               if(this->board[i][j] != a.board[i][j])
                   return false;
       return true;
   }

  
};

int heuristic(state current,state goal) {
   int distance=0;
   for(int i=0;i<3;i++)
       for(int j=0;j<3;j++)
           if(current.board[i][j] != goal.board[i][j])
               distance++;
   return distance;
}

vector<state> output;

bool smallerH(state a,state b) {
   return a.h < b.h;
}

void swap(state &a,int i,int j,int posi,int posj) {
   int temp;
   temp = a.board[i][j];
   a.board[i][j] = a.board[posi][posj];
   a.board[posi][posj] = temp;
}

bool isInSet(state a,vector<state> b) {
   for(int i=0;i<b.size();i++)
       if(a == b[i])
           return true;

   return false;
}

void addNeighbor(state current,state goal, int newI, int newJ, int posi, int posj,
                   vector<state> &openset,vector<state> closedset) {
                  
   state newstate = current;
   swap(newstate,newI,newJ,posi,posj);
       if(!isInSet(newstate,closedset)) {

           int tentative_g_score = current.g + 1;

           if( !isInSet(newstate,openset) || tentative_g_score < newstate.g ) {

               newstate.g = tentative_g_score;
               newstate.h = newstate.g + heuristic(newstate,goal);
  
state *temp = new state();
*temp = current;
               newstate.came_from = temp;
               openset.push_back(newstate);
           }
       }
}

void neighbors(state current,state goal, vector<state> &openset,vector<state> closedset) {
   int i,j,posi,posj;
  
   //Find the position of '0'
   for(i=0;i<3;i++){
       for(j=0;j<3;j++) {
           if(current.board[i][j] == 0)
           {
               posi=i;
               posj=j;
           }
       }  
   }
   i=posi; j=posj;

   if((i-1)>=0) {
       i--;
       addNeighbor(current,goal,i,j,posi,posj,openset,closedset);
   }
  
   if((i+1)<3) {
       i++;
       addNeighbor(current,goal,i,j,posi,posj,openset,closedset);
   }

   if((j-1)>=0) {
       j--;
       addNeighbor(current,goal,i,j,posi,posj,openset,closedset);
   }
  
   if((j+1)<3) {
       j++;
       addNeighbor(current,goal,i,j,posi,posj,openset,closedset);
   }
}

void reconstruct_path(state current, vector<state> &came_from) {
state *temp = &current;
while(temp != NULL) {
came_from.push_back(*temp);
temp = temp->came_from;
}
}

bool astar(state start, state goal) {
   vector<state> closedset;
   vector<state> openset;

   state current;

   start.g = 0;
   start.h = start.g + heuristic(start,goal);

   openset.push_back(start);
  
   while(!openset.empty()) {

       sort(openset.begin(),openset.end(),smallerH);

       current = openset[0]; //select the state having lowest fscore value.
      
       if(current == goal) {
           reconstruct_path(current,output);
           return true;
       }

       openset.erase(openset.begin());
       closedset.push_back(current);

       neighbors(current,goal,openset,closedset);
   }

   return false;
}

int main() {
   state start,goal;
  
   cout<<"Enter the 3x3 starting position of the board :\n";
   for(int i=0;i<3;i++)
       for(int j=0;j<3;j++)
           cin>>start.board[i][j];

   cout<<"Enter goal board :\n";
   for(int i=0;i<3;i++)
       for(int j=0;j<3;j++)
           cin>>goal.board[i][j];

   if(astar(start,goal) == true) {
for(int i=output.size()-1;i>=0;i--) {
output[i].print();
}
cout<<"Successful in finding out the goal state. \n";
   }
   else
       cout<<"\n Not able to find the goal state. \n";
   return 0;
}

This would be the solution what you are looking for. You can find the proper code on my github as well from which this has been taken and adjusted as per your requirement.

Cheers.

Add a comment
Know the answer?
Add Answer to:
Apply the A" algorithm to the 8-puzzle problem to the initial tile pattern shown below to arrive ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is...

    you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show the configuration after each move. A majority of the code is written, I need help computing 3 functions in the PuzzleState class from the source code I provided below (see where comments ""TODO"" are). Also is this for Artificial Intelligence Requirements You are to create a program in Python 3 that performs the following:...

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • 5. If we apply binary dilation to the same large object twice using the same small...

    5. If we apply binary dilation to the same large object twice using the same small structuring element, the effect, if any, of the second dilation on the object is that the object: (a) is unchanged (b) is completely removed (c) becomes larger (d) becomes smaller (e) does not change 6. Which of the following is/are true? (a) Dijkstra's algorithm can be used to find shortest paths in a network (b) Dijkstra's algorithm is a method to find straight lines...

  • Problem 1 (10 points) Estimated time: 6-8 mins. For the truss shown below I. Identify all...

    Problem 1 (10 points) Estimated time: 6-8 mins. For the truss shown below I. Identify all zero force members II.Which of the following statement(s) is/are true? Circle all that apply. (a) Force in member CG is in tension and has a magnitude of 5 kN. (b) The magnitude and sense (T/C) of the internal force in member CH must be equal to that in member CF. (c) The magnitude and sense (T/C) of the internal force in member GH must...

  • Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in...

    Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...

  • USE THE DATA LISTED IN THE EXCEL BELOW PLEASE THANK YOU !! [5] Q2. Problem 3.14....

    USE THE DATA LISTED IN THE EXCEL BELOW PLEASE THANK YOU !! [5] Q2. Problem 3.14. Refer to Plastic hardness Problem 1.22. Use the data modified for this problem from Datasets for Assignment_3. (a) Perform the F test to determine whether or not there is lack of fit of a linear regression function; use a = .01. State the alternatives, decision rule, and conclusion. (b) Is there any advantage of having an equal number of replications at each of the...

  • Based on the document below, 1. Describe the hypothesis Chaudhuri et al ids attempting to evaluate;...

    Based on the document below, 1. Describe the hypothesis Chaudhuri et al ids attempting to evaluate; in other words, what is the goal of this paper? Why is he writing it? 2. Does the data presented in the paper support the hypothesis stated in the introduction? Explain. 3.According to Chaudhuri, what is the potential role of thew alkaline phosphatase in the cleanup of industrial waste. CHAUDHURI et al: KINETIC BEHAVIOUR OF CALF INTESTINAL ALP WITH PNPP 8.5, 9, 9.5, 10,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT