-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy path8 Puzzle Solver.py
More file actions
147 lines (111 loc) · 3.31 KB
/
8 Puzzle Solver.py
File metadata and controls
147 lines (111 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
position_of = {1:(0,0), 2:(0,1), 3:(0,2),
4:(1,0), 5:(1,1), 6:(1,2),
7:(2,0), 8:(2,1)}
goal = [[1,2,3],
[4,5,6],
[7,8,0]]
class Node:
def __init__(self, data, level, f):
self.data = data
self.f = f
self.level = 0
self.N = len(data)
self.prev = None
def neighbors(self):
neighbors = []
N = self.N
moves, (x,y) = self.find_moves()
for i,j in moves:
state = self.copy(self.data)
state[x][y], state[i][j] = state[i][j], state[x][y]
neighbors.append(Node(state, self.level+1, 0))
return neighbors
def copy(self, data):
temp = []
for i in data:
t = []
for j in i:
t.append(j)
temp.append(t)
return temp
def find_blank(self, data):
N = self.N
for i in range(N):
for j in range(N):
if data[i][j] == 0:
return (i,j)
def find_moves(self):
x,y = self.find_blank(self.data)
moves = []
if (x != 0):
moves.append((x-1,y))
if (y != 0):
moves.append((x,y-1))
if (y != 2):
moves.append((x,y+1))
if (x != 2):
moves.append((x+1,y))
return moves, (x,y)
def heuristic(board, goal, heuristic_func):
count = 0
# Hamming Priority
if heuristic_func=='hamming':
for i in range(3):
for j in range(3):
if board[i][j] and board[i][j] != goal[i][j]:
count += 1
# Manhattan Distance
elif heuristic_func=='manhattan':
for i in range(3):
for j in range(3):
if board[i][j]:
x,y = position_of[board[i][j]]
manhattan = abs(x-i) + abs(y-j)
count += manhattan
return count
def getPath():
path = []
temp = curr
while temp.prev:
path.insert(0, temp.data)
temp = temp.prev
path.insert(0, temp.data)
return path
curr = None
def solve(start, heuristic_func):
global curr
openSet = []
closedSet = []
start = Node(start, 0, 0)
start.f = heuristic(start.data, goal, heuristic_func) + start.level
openSet.append(start)
while len(openSet) > 0:
low = 0
for i in range(len(openSet)):
if openSet[i].f < openSet[low].f:
low = i
curr = openSet[low]
if heuristic(curr.data, goal, heuristic_func) == 0:
break
closedSet.append(curr.data)
openSet.remove(curr)
for neighbor in curr.neighbors():
if neighbor.data not in closedSet:
# f(x) = g(x) + h(x)
h_x = heuristic(neighbor.data, goal, heuristic_func)
g_x = neighbor.level
neighbor.f = g_x + h_x
openSet.append(neighbor)
neighbor.prev = curr
return getPath()
def print_solution(board):
for step in solution:
for row in step:
print(row)
print()
print("Total Steps:",len(solution))
board = [[2,0,3],
[5,8,1],
[6,7,4]]
solution = solve(board, heuristic_func='manhattan')
print_solution(solution)