-
Notifications
You must be signed in to change notification settings - Fork 0
/
8puzzle.py
173 lines (157 loc) · 5.35 KB
/
8puzzle.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
try:
import Queue
except ImportError:
import queue as Queue
### Part 1: define search state
class EightPuzzleState:
def __init__(self, numberList):
self.cells = []
self.cells = numberList[:]
for index in range(9):
if (self.cells[index] == 0):
self.blankLocation = index
def goalState(self):
if (self.cells == [0, 1, 2, 3, 4, 5, 6, 7, 8]):
return True
else:
return False
def nextActions(self):
actions = []
index = self.blankLocation
if (index%3!=2):
actions.append('right')
if (index%3!=0):
actions.append('left')
if (index>2):
actions.append('up')
if (index<6):
actions.append('down')
return actions
def nextState(self, move):
index = self.blankLocation
newState = EightPuzzleState([0,0,0,0,0,0,0,0,0])
newState.cells = self.cells[:]
if move == 'up':
newState.cells[index] = self.cells[index-3]
newState.cells[index-3] = self.cells[index]
newState.blankLocation = index-3
if move == 'down':
newState.cells[index] = self.cells[index+3]
newState.cells[index+3] = self.cells[index]
newState.blankLocation = index+3
if move == 'right':
newState.cells[index] = self.cells[index+1]
newState.cells[index+1] = self.cells[index]
newState.blankLocation = index+1
if move == 'left':
newState.cells[index] = self.cells[index-1]
newState.cells[index-1] = self.cells[index]
newState.blankLocation = index-1
return newState
def __str__(self):
lines = []
lines.append('---------')
for n in [0, 3, 6]:
newLine = '|'
for cell in self.cells[n:n+3]:
if cell == 0:
newLine += ' '
else:
newLine += cell.__str__()
newLine += '|'
newLine = ' '.join(newLine)
lines.append(newLine)
lines.append('---------')
lines = '\n'.join(lines)
return lines
def __eq__(self, other):
return (self.cells == other.cells)
def __hash__(self):
return hash(str(self.cells))
### Part 2: define seacrh problem
class searchProblem:
def __init__(self, state):
self.state = state
def currentState(self):
return self.state
def nextStates(self, state):
next = []
for action in state.nextActions():
next.append((state.nextState(action), action, 1))
return next
def theEnd(self, state):
return state.goalState()
### Part 3: define seacrh method
def missTile(state, problem=None):
heur = 0
for index in range(9):
if state.cells[index] != index:
heur += 1
return heur
def algoVisit(problem, algo='ucs', heuristic=missTile):
"""
'bfs' = breadth first search
'dfs' = depth first search
'ucs' = uniform cost search
'greedy' = greedy search
'astar' = A star search
"""
if algo == 'dfs':
fringeQueue = Queue.LifoQueue()
elif algo == 'bfs':
fringeQueue = Queue.Queue()
else:
fringeQueue = Queue.PriorityQueue()
visited = set([])
path = ()
cost = 0
heur = 0
node = problem.currentState()
fringeQueue.put( (heur, (node, path, cost)) )
while not fringeQueue.empty():
# print(len(visited))
(heur, (node, path, cost)) = fringeQueue.get()
if problem.theEnd(node):
return path, visited
break
if node not in visited:
successors = problem.nextStates(node)
visited.add(node)
else:
successors = []
for successor in successors:
if successor[0] not in visited:
if algo == 'astar':
heur = cost+successor[2] + 1*heuristic(successor[0],problem)
elif algo == 'greedy':
heur = heuristic(successor[0],problem)
else: # 'ucs'
heur = cost+successor[2]
fringeQueue.put( (heur, (successor[0], path+(successor[1],), cost+successor[2])) )
### Part 4: main program
if __name__ == '__main__':
algo = 'astar' # 'bfs', 'dfs', 'ucs', 'greedy', 'astar'
numberList = [1, 7, 8, 2, 3, 4, 5, 6, 0]
# [1, 0, 2, 3, 4, 5, 6, 7, 8] # bfs: 2
# [1, 2, 5, 3, 4, 8, 6, 7, 0] # bfs: 16
# [0, 3, 1, 6, 8, 2, 7, 5, 4] # bfs: 16
# [4, 3, 2, 7, 0, 5, 1, 6, 8] # bfs: 781
# [1, 2, 5, 7, 6, 8, 0, 4, 3] # bfs: 3541
# [5, 1, 3, 4, 0, 2, 6, 7, 8] # bfs: 3842
# bfs: 134450, 24
# ucs: 134127, 24
# greedy: 314, 66
state = EightPuzzleState(numberList)
problem = searchProblem(state)
path, visited = algoVisit(problem, algo, )
print('========================================')
print('Need %d moves to solve this puzzle by %s: %s' %(len(path), algo, str(path)))
print('========================================')
print(state)
i = 1
for action in path:
state = state.nextState(action)
print('move#%d: %s' % (i, action))
print(state)
i += 1
print('Total number of nodes visited by %s: %d ' %(algo, len(visited)))