Question
Introduction implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show
Introduction
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.
Requirements
You are to create a program in Python 3 that performs the following:
- Loads the mp1input.txt file from the current directory. This represents the starting state of the sliding puzzle. The format of this file is composed of 3 rows of 3 values, each value separated by a single space. The values are the integers 0 through 8 that represent the puzzle. The 0 integer represents an empty space (no tile). Here is an example of the input file contents: 3 1 2 4 7 5 0 6 8
- Executes the A* algorithm with the Manhattan distance heuristic (as discussed in the textbook). The goal state is this configuration: 0 1 2 3 4 5 6 7 8
- Shows the solution in form of the puzzle configurations after each move, the move number, and the action taken. This format should match the sample output shown
Sample Program Output 1
Artificial Intelligence
START
[[3 1 2]
[4 7 5]
[0 6 8]]
Move 1 ACTION: right
[[3 1 2]
[4 7 5]
[6 0 8]]
Move 2 ACTION: up
[[3 1 2]
[4 0 5]
[6 7 8]]
Move 3 ACTION: left
[[3 1 2]
[0 4 5]
[6 7 8]]
Move 4 ACTION: up
[[0 1 2]
[3 4 5]
[6 7 8]]
Number of states visited = 10
import numpy as np import queue
class PuzzleState(): SOLVED_PUZZLE = np.arange(9).reshape((3, 3))
def __init__(self,conf,g,predState): self.puzzle = conf # Configuration of the state self.gcost = g # Path cost self._compute_heuristic_cost() # Set heuristic cost will set the hcost self.fcost = self.gcost + self.hcost self.pred = predState # Predecesor state self.zeroloc = np.argwhere(self.puzzle == 0)[0] #knows where the zero is self.action_from_pred = None def __hash__(self): return tuple(self.puzzle.ravel()).__hash__() def _compute_heuristic_cost(self): """ TODO """
def is_goal(self): return np.array_equal(PuzzleState.SOLVED_PUZZLE,self.puzzle) def __eq__(self, other): return np.array_equal(self.puzzle, other.puzzle) def __lt__(self, other): return self.fcost < other.fcost def __str__(self): return np.str(self.puzzle) move = 0 def show_path(self): if self.pred is not None: self.pred.show_path() if PuzzleState.move==0: print('START') else: print('Move',PuzzleState.move, 'ACTION:', self.action_from_pred) PuzzleState.move = PuzzleState.move + 1 print(self) def can_move(self, direction): """ TODO """
def gen_next_state(self, direction): """ TODO """
frontier = queue.PriorityQueue() a = np.loadtxt('mp1input.txt', dtype=np.int32) start_state = PuzzleState(a,0,None)
frontier.put(start_state)
closed_set = set()
num_states = 0 while not frontier.empty(): # choose state at front of priority queue next_state = frontier.get() # if goal then quit and return path if next_state.is_goal(): next_state.show_path() break # Add state chosen for expansion to closed_set closed_set.add(next_state) num_states = num_states + 1 # Expand state (up to 4 moves possible) possible_moves = ['up','down','left','right'] for move in possible_moves: if next_state.can_move(move): neighbor = next_state.gen_next_state(move) if neighbor in closed_set: continue if neighbor not in frontier.queue: frontier.put(neighbor) # If it's already in the frontier, it's gauranteed to have lower cost, so no need to update
print(' Number of states visited =',num_states)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started