Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

  1. 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
  2. Displays heading information to the screen: Artificial Intelligence
  3. 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
  4. 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 on the last page.

Displays the number of states that A* had to visit in order to get to the solution

Additional Requirements

  1. The name of your source code file should be mp1.py. All your code should be within a single file.
  2. You can only import numpy and queue packages.
  3. Your code should follow good coding practices, including good use of whitespace and use of both inline and block comments.
  4. You need to use meaningful identifier names that conform to standard naming conventions.
  5. At the top of each file, you need to put in a block comment with the following information: your name, date, course name, semester, and assignment name.
  6. The output should exactly match the sample output shown on the last page. Note that for a different input state, the output may be different. I will be testing on a different input than shown in the sample.
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 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 queue 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 guaranteed 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

mple 10. Determine d dx S 0 t dt.

Answered: 1 week ago

Question

Describe how to train managers to coach employees. page 422

Answered: 1 week ago