Using Python
1. Using Python, write the following helper functions:
a. generate_random_8puzzle() - to return a random solvable 8-puzzle problem.
b. check_solvable(state) - to return whether the generated 8-puzzle is solvable or not.
Hint: For the 8-puzzle problem, an inversion is defined as any pair of tiles i and j where i < j, but i appears after j in row-major order. Consider the following two different 8-puzzle problem instances. If an 8-puzzle instance has an odd number of inversions, then it is not solvable. If it has an even number of inversions, it is solvable.
1 2 1 4 5 2 8 7 8 6
Order of tiles: 1 2 3 4 5 6 8 7
Inversions = 1 (8-7) - unsolvable
1 3 4 2 5 7 8 6
Order of tiles: 1 3 4 2 5 7 8 6
Inversions = 4 (3-2, 4-2, 7-6, 8-6) - solvable
c. display_8puzzle(state) - to print a state of a puzzle in the following format:
*
1 3 2
5 8 6
4
7
2. Write an A* search algorithm to solve 8-puzzle problems using the following heuristic functions:
a. The misplaced tile heuristic
b. The Manhattan distance heuristic
c. The maximum of the misplaced tile heuristic and the Manhattan distance heuristic
3. Generate five random solvable 8-puzzle instances and solve them using all three methods implemented in the previous question. For each instance:
a. Record the total run time (in seconds) of each method
b. Record the cost to reach the solution (i.e., number of tiles moved)
Summarize your results in a table and state the best-performing algorithm with a suitable reason.
NOTE: The goal state of the 8-puzzle should be in the order 1 2 3 4 5 6 7 8 9. However, the blank tile can be placed anywhere on the board. The following are a few acceptable goal states:
Z
J
/ 8
7
6