HW2: Search

Out 9/14, Due 9/26 by 11:59pm EST

Problem 3.8 from the second edition of the textbook, which is included below. Submit a PDF file to the homework 2 repository with your answer to this question.

Programming A*: Write a Python program that implements the A* algorithm for the N-puzzle. Below are things that you must do so that your program can be run and tested:

  • Name your executable np.py
  • The program must accept two command line arguments in this order:
    • An integer value of n, which is the number of non-blank tiles in the puzzle. Note that the puzzle is a square of sqrt(n+1) tiles per side.
    • A string which is to be used as a random seed to create the initial puzzle state.
    You're program should include the following code:
    		  import random
    		  import sys
    		  n = int(sys.argv[1])
    		  random.seed(sys.argv[2])
    		  state = list(range(n)) + [' ']
    		  random.shuffle(state)
    		
    The state variable will contain the values for the tiles when reading from top left to bottom right.
  • The goal state has the integers from 1 to n in numeric order from top left to bottom right, with the bottom right square containing the blank.
  • When your program is run it will use the A* algorithm to find the shortest solution (in terms of number of moves) to get from the start state to the goal state.
  • When the program terminates it must print either FAILURE if no path exists or it must print the number of moves required and the sequence of game states that starts with the start state and ends with the goal state.
  • Use the Manhattan distance heurstic as described in the text for this puzzle.
As I said in class, there are many implementations of A* in Python for this problem. You may look at them but do not include any copied code. Write it yourself! Submit your Python code to the homework 2 repository. Also submit a file named OUTPUT.txt that has the output for the largest value of n for which you were able to run your program. to run.