(Solved) : Implement Algorithm Solve Sliding Tile Puzzle Game Goal Return Instructions Solving Puzzle Q44009682 . . .
you will implement the A* algorithm to solvethe sliding tile puzzle game. Your goal is to return theinstructions for solving the puzzle and show the configurationafter each move. A majority of the code is written, I need helpcomputing 3 functions in the PuzzleState class from the source codeI provided below (see where comments “”TODO”” are). Also is thisfor Artificial Intelligence
Requirements
You are to create a program in Python 3 that performs thefollowing:
- Loads the mp1input.txt file from the current directory. Thisrepresents the starting state of the sliding puzzle. The format ofthis file is composed of 3 rows of 3 values, each value separatedby a single space. The values are the integers 0 through 8 thatrepresent the puzzle. The 0 integer represents an empty space (notile). Here is an example of the input file contents:
3 1 2
4 7 5
0 6 8 - Displays heading information to the screen:
Artificial Intelligence - Executes the A* algorithm with the Manhattan distance heuristic(as discussed in the textbook). The goal state is thisconfiguration:
0 1 2
3 4 5
6 7 8 - Shows the solution in form of the puzzle configurations aftereach move, the move number, and the action taken. This formatshould match the sample output shown on the last page.
Displays the number of states that A* had to visit in order toget to the solution
Additional Requirements
- The name of your source code file should be mp1.py. All yourcode should be within a single file.
- You can only import numpy and queue packages.
- Your code should follow good coding practices, including gooduse of whitespace and use of both inline and block comments.
- You need to use meaningful identifier names that conform tostandard naming conventions.
- At the top of each file, you need to put in a block commentwith the following information: your name, date, course name,semester, and assignment name.
- The output should exactly match the sampleoutput shown on the last page. Note that for a different inputstate, the output may be different. I will be testing on adifferent input than shown in the sample.
import numpy as npimport queueclass 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 self.fcost = self.gcost + self.hcost self.pred = predState # Predecesor state self.zeroloc = np.argwhere(self.puzzle == 0)[0] 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 “”” # load random start state onto frontier priority queuefrontier = 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 = 0while 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 guaranteed to have lower cost, so no need to updateprint(‘nNumber of states visited =’,num_states)
Expert Answer
Answer to you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for s…
OR