Uploaded by Richa Dhakal

Lab4 Sliding Puzzle with A star

advertisement
Lab 4: 15 Puzzle with A*
Grade: 30 pts
Solve a given 4x4 sliding puzzle (Goal: _123456789ABCDEF) by using A* Searching algorithm.
LastName_FirstInitial_U1_L4.py
Step 1: Modify your 8-puzzle methods to work for 15-puzzle.
Step 2: Complete inversion_count function.
Decide if the slide puzzle is solvable or not by counting inversions.
If N(height) is odd like 8 puzzle, then puzzle instance is solvable if :
number of inversions is even in the input state.
If N is even like 15 puzzle, puzzle instance is solvable if :
the blank is on an even row counting from the bottom (second-last, fourth-last, etc.) and
number of inversions is even.
the blank is on an odd row counting from the bottom (last, third-last, fifth-last, etc.) and
number of inversions is odd.
Step 3: Research possible heuristic functions you can use for this lab and define one or more heuristic
function(s). You may use multiple heuristic depends on situations.
•
•
•
•
•
Number of mis-placed tiles
Manhattan distance
Linear conflict
Gaschnig’s
…
def dist_heuristic(start, goal, size):
Step 4: Complete A* algorithm by implementing:
def swap(node, i, j):
def generate_children(node, size=4):
def display_path(path_list, size=4):
def a_star(start, goal, heuristic=dist_heuristic, size = 4):
frontier = HeapPriorityQueue()
# Hint: check ‘path’(closed set) before add a new node to frontier
Sample outputs:
Case 1:
Type initial State: 152349678_ABCDEF
1523
1523
1_23
_123
4967
4_67
4567
4567
8_AB
89AB
89AB
89AB
CDEF
CDEF
CDEF
CDEF
The shortest path length is : 4
Duration: 0.0
Case 2:
Type initial State: 2_63514B897ACDEF
2_63
_263
5263
5263
5263
514B
514B
_14B
1_4B
14_B
897A
897A
897A
897A
897A
CDEF
CDEF
CDEF
CDEF
CDEF
5263
147B
89_A
CDEF
……
……
……
……
_123
4567
89AB
CDEF
8943
C2_6
A71F
DB5E
……
……
……
……
_123
4567
89AB
CDEF
The shortest path length is : 16
Duration: 0.005014657974243164
Case 3:
Type initial state: 8936C_24A71FDB5E
8936
8936
8936
893_
89_3
C_24
C2_4
C24_
C246
C246
A71F
A71F
A71F
A71F
A71F
DB5E
DB5E
DB5E
DB5E
DB5E
The shortest path length is : 37
Duration: 0.27825474739074707
Case 4:
Type initial state: 8293AC4671FEDB5_
……
The shortest path length is : 39
Duration: 0.7709157466888428
Download