Question

In: Computer Science

Write a program in a language of your choice to perform a search using the A*...

Write a program in a language of your choice to perform a search using the A* algorithm for the eight puzzle problem, in which numbers may be shifted one space at a time to transform the initial state into the goal state (see p. 103 – 3rd Ed., pp. 105-106 – 2nd Ed. of the text).

2. a)

Use the start state-goal state combination given in pp. 103, Figure 3.28 (3rd Ed.), [pp. 105,

Figure 4.7 (2rd Ed.)], as (start_1, goal_1) (see page 2).

  1. b) Use a new start state, which is given in page 2, the same goal state (start_2, goal_1).

  2. c) Use a different goal state (the blank is now at the bottom right corner - see page 2),

    and the same start state as part a (start_1, goal_2).

  3. d) Use a the same start state as part b, and the same goal state as part c

    (start_2, goal_2) (see page 2).

  4. e) Also use { (start_3, goal_1), (start_3, goal_2), (start_3, goal_3), (start_1, goal_3),

    (start_2, goal_3) } pairs given in page 2.

  1. Use two different set of heuristic functions to estimate distance from the goal
    (See pp. 103 – 3rd Ed, p. 106 – 2nd Ed.)
    h1: the number of misplaced tiles
    h2: the sum of the distances of tiles from their goal positions (Manhattan distance)

  2. Write a report comparing the performance of the two heuristics on the nine different sets of (start_x, goal_x) combinations of part 2.

    Performance measure: the length of the solution path (from the start state to the goal), and the actual time it takes to find the solution (i.e., to reach the goal state).

Solutions

Expert Solution

from copy import deepcopy

class puzzle:
    def __init__ (self, starting, parent):
        self.board = starting
        self.parent = parent
        self.f = 0
        self.g = 0
        self.h = 0

def manhattan(self):
        h = 0
        for i in range(3):
            for j in range(3):
                x, y = divmod(self.board[i][j], 3)
                h += abs(x-i) + abs(y-j)
        return h


    def goal(self):
        inc = 0
        for i in range(3):
            for j in range(3):
                if self.board[i][j] != inc:
                    return False
                inc += 1
        return True

    def __eq__(self, other):
        return self.board == other.board

def move_function(curr):
    curr = curr.board
    for i in range(3):
        for j in range(3):
            if curr[i][j] == 0:
                x, y = i, j
                break
    q = []
    if x-1 >= 0:
        b = deepcopy(curr)
        b[x][y]=b[x-1][y]
        b[x-1][y]=0
        succ = puzzle(b, curr)
        q.append(succ)
    if x+1 < 3:
        b = deepcopy(curr)
        b[x][y]=b[x+1][y]
        b[x+1][y]=0
        succ = puzzle(b, curr)
        q.append(succ)
    if y-1 >= 0:
        b = deepcopy(curr)
        b[x][y]=b[x][y-1]
        b[x][y-1]=0
        succ = puzzle(b, curr)
        q.append(succ)
    if y+1 < 3:
        b = deepcopy(curr)
        b[x][y]=b[x][y+1]
        b[x][y+1]=0
        succ = puzzle(b, curr)
        q.append(succ)

    return q

def best_fvalue(openList):
    f = openList[0].f
    index = 0
    for i, item in enumerate(openList):
        if i == 0: 
            continue
        if(item.f < f):
            f = item.f
            index  = i

    return openList[index], index

def AStar(start):
    openList = []
    closedList = []
    openList.append(start)

    while openList:
        current, index = best_fvalue(openList)
        if current.goal():
            return current
        openList.pop(index)
        closedList.append(current)

        X = move_function(current)
        for move in X:
            ok = False   #checking in closedList
            for i, item in enumerate(closedList):
                if item == move:
                    ok = True
                    break
            if not ok:              #not in closed list
                newG = current.g + 1 
                present = False

                #openList includes move
                for j, item in enumerate(openList):
                    if item == move:
                        present = True
                        if newG < openList[j].g:
                            openList[j].g = newG
                            openList[j].f = openList[j].g + openList[j].h
                            openList[j].parent = current
                if not present:
                    move.g = newG
                    move.h = move.manhattan()
                    move.f = move.g + move.h
                    move.parent = current
                    openList.append(move)

    return None


#start = puzzle([[2,3,6],[0,1,8],[4,5,7]], None)
start = puzzle([[5,2,8],[4,1,7],[0,3,6]], None)
# start = puzzle([[0,1,2],[3,4,5],[6,7,8]], None)
#start = puzzle([[1,2,0],[3,4,5],[6,7,8]], None)
result = AStar(start)
noofMoves = 0

if(not result):
    print ("No solution")
else:
    print(result.board)
    t=result.parent
    while t:
        noofMoves += 1
        print(t.board)
        t=t.parent
print ("Length: " + str(noofMoves))

Related Solutions

Write the simplest program possible in your language of choice containing your 'main' and any other...
Write the simplest program possible in your language of choice containing your 'main' and any other functions you may need that will: A. Read a sequence of words up to a maximum of 64 words from std input. Before reading the input, prompt the user to enter the words. B. Check to see if there any duplicate words in the input read. C. If there are no duplicates, print out to std output the message 'No Duplicates'. D. If there...
Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
In 300 words Perform an Internet search on two vaccines of your choice. Locate the VIS...
In 300 words Perform an Internet search on two vaccines of your choice. Locate the VIS (vaccine information sheet) for each and provide information about those vaccines. For example, what do they immunize against, who should receive them, what are the risks of non-vaccination, what are the side effects, when are they contraindicated, etc?
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Using energia software Write a program for your MSP430 board to will perform the following three...
Using energia software Write a program for your MSP430 board to will perform the following three tasks: 1. Input: Take a character string as user input through the serial interface. 2. Processing: Convert the entire string to lower case, and 3. Output: Return the processed string to serial output, which will be displayed on your computer monitor. Example: If the input character string is, “Here I am”, the output should be, “here i am”. For serial input and output purpose,...
Perform an Internet search on professional associations for your particular career choice. (Accounting). List at least...
Perform an Internet search on professional associations for your particular career choice. (Accounting). List at least three associations, and discuss recruiting options listed on their websites (e.g., do they have discussion boards or job advertisements links?).
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
In the C# programming language... Write a program to perform student record manage for class IST311....
In the C# programming language... Write a program to perform student record manage for class IST311. Create student record class (Student.cs) and it should get the student information from the user and set up student record class. The program will perform recording student’s grade information and compute final grade, and then print out the student’s class information. Student class definitions: It should contain attributes as following: first name, last name, 5 labs grade, 3 grade, final grade. Its member functions...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT