-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.py
411 lines (375 loc) · 16.5 KB
/
lib.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import math, random, heapq, time
from mem_top import mem_top
import logging
class Heuristics:
def _displacementHeuristic(state):
"""
----------------------------------------------------------
Description: Calculate the number of misplaced tiles in the state.
Use: puzzles = Puzzle.randomizePuzzle(Heuristics.MISPLACED, 8)
----------------------------------------------------------
Returns:
count - The number of misplaced tiles in the state.
----------------------------------------------------------
"""
count = 0
for i in range(state.boardLength):
if state.board[i] != 0 and state.board[i] != i:
count += 1
return count
def _manhattanHeuristic(state):
"""
----------------------------------------------------------
Description: Calculate the manhattan distance of the state.
Use: puzzles = Puzzle.randomizePuzzle(Heuristics.MANHATTAN, 8)
----------------------------------------------------------
Parameters:
state - The state to calculate the manhattan distance of.
Returns:
h - The manhattan distance of the state.
----------------------------------------------------------
"""
h = 0
for i in range(state.boardLength):
if state.board[i] != 0:
x1, y1 = state._get2dCoord(i)
x2, y2 = state._get2dCoord(state.board[i])
h += abs(x1 - x2) + abs(y1 - y2)
return h
def _outOfRowColoumnHeuristic(state):
"""
----------------------------------------------------------
Description: Calculate the number of tiles out of row or coloumn.
Use: puzzles = Puzzle.randomizePuzzle(Heuristics.ROWCOL, 8)
----------------------------------------------------------
Parameters:
state - The state to calculate the heuristic from
Returns:
h - The number of tiles out of row or coloumn.
----------------------------------------------------------
"""
h = 0
for i in range(state.boardLength):
if state.board[i] != 0:
x1, y1 = state._get2dCoord(i)
x2, y2 = state._get2dCoord(state.board[i])
if x1 != x2:
h += 1
if y1 != y2:
h += 1
return h
def _linearConflictHeuristic(state):
"""
----------------------------------------------------------
Description: Calculate the linear conflict heuristic of the state.
Use: puzzles = Puzzle.randomizePuzzle(Heuristics.LINEARCONFLICT, 8)
----------------------------------------------------------
Parameters:
state - The state to calculate the heuristic from
Returns:
h - The linear conflict heuristic of the state.
----------------------------------------------------------
"""
h = 0
for i in range(state.boardLength):
if state.board[i] != 0:
x1, y1 = state._get2dCoord(i)
x2, y2 = state._get2dCoord(state.board[i])
h += abs(x1 - x2) + abs(y1 - y2)
if y1 == y2: # Tile is in the same row as it's goal tile
for j in range(state.boardLength): # Check if there is a tile in the same row that gives a linear conflict
if state.board[j] != 0:
x3, y3 = state._get2dCoord(j)
x4, y4 = state._get2dCoord(state.board[j])
if y3 == y1: # on the same row
if(x3 < x1 and x4 >= x1): # tile is in the way
h += 2
if x1 == x2: # Tile is in the same coloumn as it's goal tile
for j in range(state.boardLength): # Check if there is a tile in the same coloumn that gives a linear conflict
if state.board[j] != 0:
x3, y3 = state._get2dCoord(j)
x4, y4 = state._get2dCoord(state.board[j])
if x3 == x1: # on the same col
if(y3 < y1 and y4 >= y1): # tile is in the way
h += 2
return h
def _euclideanHeuristic(state):
"""
----------------------------------------------------------
Description: Calculate the euclidean distance of the state.
Use: puzzles = Puzzle.randomizePuzzle(Heuristics.EUCLIDEAN, 8)
----------------------------------------------------------
Parameters:
state - The state to calculate the euclidean distance of.
Returns:
h - The euclidean distance of the state.
----------------------------------------------------------
"""
h = 0
for i in range(state.boardLength):
if state.board[i] != 0:
x1, y1 = state._get2dCoord(i)
x2, y2 = state._get2dCoord(state.board[i])
h += math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
return h
def strToHeuristic(str):
if (str == "DISPLACEMENT"):
return Heuristics.DISPLACEMENT
elif (str == "MANHATTAN"):
return Heuristics.MANHATTAN
elif (str == "ROWCOL"):
return Heuristics.ROWCOL
elif (str == "LINEARCONFLICT"):
return Heuristics.LINEARCONFLICT
elif (str == "EUCLIDEAN"):
return Heuristics.EUCLIDEAN
def heuristicToStr(func):
if func == Heuristics._displacementHeuristic:
return "DISPLACEMENT"
elif func == Heuristics._manhattanHeuristic:
return "MANHATTAN"
elif func == Heuristics._outOfRowColoumnHeuristic:
return "ROWCOL"
elif func == Heuristics._linearConflictHeuristic:
return "LINEARCONFLICT"
elif func == Heuristics._euclideanHeuristic:
return "EUCLIDEAN"
DISPLACEMENT = _displacementHeuristic
MANHATTAN = _manhattanHeuristic
ROWCOL = _outOfRowColoumnHeuristic
EUCLIDEAN = _euclideanHeuristic
LINEARCONFLICT = _linearConflictHeuristic
class Puzzle:
def __init__():
logging.basicConfig(filename='debug.log', encoding='utf-8', level=logging.DEBUG)
return
def randomizePuzzles(heuristic, size, numPuzzles, seed):
"""
----------------------------------------------------------
Description: Randomizes 100 puzzles of the given size.
Use: puzzles = Puzzle.randomizePuzzles(Heuristics.MANHATTAN, 8)
----------------------------------------------------------
Parameters:
heuristic - The heuristic to use for the puzzles.
size - The size of the puzzle to randomize. Must be a perfect square.
Returns:
puzzles - Array of randomized puzzles.
----------------------------------------------------------
"""
try:
assert math.sqrt(size + 1) % 1 == 0
random.seed(seed)
puzzles = []
while len(puzzles) < numPuzzles:
board = [i for i in range(size+1)]
random.shuffle(board)
s = State(board, math.sqrt(len(board)), heuristic=heuristic)
if (s.isSolvable()):
puzzles.append(s)
return puzzles
except AssertionError:
print("ERROR:\t Size must be a perfect square!")
return
def solvePuzzleArray(puzzles, outputFile, outputCSV, debug=False):
"""
----------------------------------------------------------
Description: Solves the given puzzles and writes the results to the given file.
Use: Puzzle.solvePuzzles(puzzles)
----------------------------------------------------------
Parameters:
puzzles - An array of puzzles to solve.
Yields:
count - The current number of puzzles solved.
----------------------------------------------------------
"""
count = 1
totalStats = {
'timeTaken': 0,
'numNodesExplored': 0,
'numStepsToSolution': 0,
'nodesPerSecond': 0
}
file = open("./output/" + outputFile, "w")
fStats = open("./output/" + outputCSV, "w")
fStats.write("Puzzle ID,Puzzle,Time Taken,Nodes Explored,Steps to Solution,Nodes per Second\n") # write header
for puzzle in puzzles:
file.write("-"*150 + "\n")
file.write(" Puzzle # " + str(count) + " | " + str(puzzle.board) + "\n")
file.flush()
stats = Puzzle.solvePuzzle(puzzle, debug)
# Update total stats
totalStats["numNodesExplored"] += stats["numNodesExplored"]
totalStats["timeTaken"] += stats["timeTaken"]
totalStats["nodesPerSecond"] += stats["nodesPerSecond"]
totalStats["numStepsToSolution"] += len(stats["pathToSolution"])
# Write to file
file.write("\t{:<30} ---> {:.2f}s \n".format("Time taken to complete puzzle:",stats["timeTaken"]))
file.write("\t{:<30} ---> {} \n".format("Number of expanded nodes:",str(stats["numNodesExplored"])))
file.write("\t{:<30} ---> {} \n".format("Number of steps to solution:",str(len(stats["pathToSolution"]))))
file.write("\t{:<30} ---> {} \n".format("Path to Solution:",str(stats["pathToSolution"])))
file.write("\t{:<30} ---> {:.2f} nodes/s \n".format("Nodes expanded per second:",stats["nodesPerSecond"]))
fStats.write('%s,%s,%s,%s,%s,%s\n' % (str(count),str(stats["startingBoard"]),str(stats["timeTaken"]),str(stats["numNodesExplored"]),str(len(stats["pathToSolution"])),str(stats["nodesPerSecond"])))
yield
count += 1
file.flush()
fStats.flush()
# Write total stats to file
file.write("\n" + "="*150 + "\n")
file.write(" Average Stats for {} {}-Puzzles: \n".format(len(puzzles), str(puzzles[0].boardLength-1)))
file.write("\t{:<38} ---> {:.2f}s \n".format("Average Time taken to complete puzzle:",totalStats["timeTaken"]/len(puzzles)))
file.write("\t{:<38} ---> {} \n".format("Average Number of expanded nodes:",str(totalStats["numNodesExplored"]/len(puzzles))))
file.write("\t{:<38} ---> {} \n".format("Average Number of steps to solution:",str(totalStats["numStepsToSolution"]/len(puzzles))))
file.write("\t{:<38} ---> {:.2f} nodes/s \n".format("Average Nodes expanded per second:",totalStats["nodesPerSecond"]/len(puzzles)))
file.write("="*150 + "\n")
file.close()
fStats.close()
# print average stats
print("="*150)
print(" Average Stats for {} {}-Puzzles:".format(len(puzzles), str(puzzles[0].boardLength-1)))
print("\t{:<38} ---> {:.2f}s".format("Average Time taken to complete puzzle:",totalStats["timeTaken"]/len(puzzles)))
print("\t{:<38} ---> {}".format("Average Number of expanded nodes:",str(totalStats["numNodesExplored"]/len(puzzles))))
print("\t{:<38} ---> {}".format("Average Number of steps to solution:",str(totalStats["numStepsToSolution"]/len(puzzles))))
print("\t{:<38} ---> {:.2f} nodes/s".format("Average Nodes expanded per second:",totalStats["nodesPerSecond"]/len(puzzles)))
print("="*150)
return
def solvePuzzle(s, debug=False):
"""
----------------------------------------------------------
Description: Solves the given puzzle using A*.
Use: stats = Puzzle.solvePuzzle(puzzle)
----------------------------------------------------------
Parameters:
s - Starting state to solve the puzzle from.
debug - Whether or not to print debug information.
Returns:
stats - Dictionary containing the following information:
timeTaken - The time taken to solve the puzzle.
numNodesExplored - The number of nodes explored.
pathToSolution - The path to the solution.
nodesPerSecond - The number of nodes expanded per second.
----------------------------------------------------------
"""
explored = {}
frontier = []
startingBoard = s
start = time.time()
heapq.heapify(frontier) # create heap and add initial state
heapq.heappush(frontier, s._getStateAsStr())
if debug: print('start:\t', str(s))
while len(frontier) != 0: # loop until frontier is empty
sParams = heapq.heappop(frontier) # (newState.f, newState.board, newState.g, newState.h, newState.path, newState.heuristicFunction)
# print('params:\t', sParams)
s = State.strToState(sParams[2])
exploredCost = explored.get(str(s))
if exploredCost != None:
if exploredCost <= s.g: # if the explored cost is less than the current cost, then we don't need to explore this state
continue
explored[str(s)] = s.g
if s.h == 0: # check if at the goal state
break
if debug:
if len(explored) % 100000 == 0:
print(mem_top())
print('#explored:\t', len(explored))
print('nps:\t\t {:.2f}\n'.format(len(explored) / (time.time() - start)))
for move in s.getMoves(): # add all possible moves to the frontier
heapq.heappush(frontier, move)
# pass the solution and stats back to the caller
end = time.time()
stats = {
'timeTaken': (time.time() - start),
'numNodesExplored': len(explored),
'pathToSolution': s.path[:],
'nodesPerSecond': len(explored) / ((end - start) if (end - start) != 0 else 0.01),
'startingBoard': '-'.join(str(x) for x in startingBoard.board)
}
if debug:
print('\ndone:\t', str(s))
print("\t{:<30} ---> {:.2f}s ".format("Time taken to complete puzzle:",stats["timeTaken"]))
print("\t{:<30} ---> {} ".format("Number of expanded nodes:",str(stats["numNodesExplored"])))
print("\t{:<30} ---> {} ".format("Number of steps to solution:",str(len(stats["pathToSolution"]))))
print("\t{:<30} ---> {} ".format("Path to Solution:",str(stats["pathToSolution"])))
print("\t{:<30} ---> {:.2f} nodes/s ".format("Nodes expanded per second:",stats["nodesPerSecond"]))
return stats
class State:
def __init__(self, board, width, g=0, h=None, path = [], heuristic=Heuristics.MANHATTAN):
self.board = board
self.boardLength = len(board)
self.width = width
self.heuristicFunction = heuristic
self.g = g
self.h = self.getH() if h == None else h
self.f = self.g + self.h
self.path = path
# print("INIT | board: {} | g: {} | h: {} | f: {} | path: {}".format(self.board, self.g, self.h, self.f, self.path))
def __str__(self):
return ','.join(map(str, self.board))
def __eq__(self, other):
return str(self) == str(other)
def __lt__(self, other):
return self.f < other.f
def __le__(self, other):
return self.f <= other.f
def __gt__(self, other):
return self.f > other.f
def __ge__(self, other):
return self.f >= other.f
def getH(self):
return self.heuristicFunction(self)
def _get2dCoord(self, index):
return (index % self.width, index // self.width)
def getMoves(self):
"""
----------------------------------------------------------
Description - Finds the states that are reachable from self state.
Use: State.getMoves()
----------------------------------------------------------
"""
moves = []
# print("IN getMoves:", self.board)
index = self.board.index(0)
row = index // self.width
col = index % self.width
if row > 0:
moves.append(self._swap(index, index - self.width))
if row < self.width - 1:
moves.append(self._swap(index, index + self.width))
if col > 0:
moves.append(self._swap(index, index - 1))
if col < self.width - 1:
moves.append(self._swap(index, index + 1))
return moves
def _swap(self, index1, index2):
board = self.board[:]
index1 = int(index1)
index2 = int(index2)
step = board[index2]
board[index1], board[index2] = board[index2], board[index1]
newState = State(board, self.width, self.g + 1, path = self.path + [step], heuristic=self.heuristicFunction)
#f, newState.board, newState.g, newState.h, newState.path, newState.heuristicFunction
return newState._getStateAsStr()
def _getStateAsStr(self):
return (self.f, self.h, '%s|%s|%s|%s|%s' % (State.arrayToStr(self.board), self.g, self.h, State.arrayToStr(self.path), Heuristics.heuristicToStr(self.heuristicFunction)))
def arrayToStr(board):
return ','.join(map(str, board))
def strToState(str):
boardAsStr, g, h, pathAsStr, heuristic = str.split('|') # split the string into the board, g, h, path, and heuristic
board = list(map(int, boardAsStr.split(','))) # convert the stringified board to a list of ints
path = list(pathAsStr.split(',')) if pathAsStr != '' else [] # convert the stringified path to a list of ints
# print('path =================', path)
# print("Board: ",board, "g: ",g, "h: ",h, "path: ",path, "heuristic: ",heuristic)
return State(board, math.sqrt(len(board)), float(g), float(h), path, Heuristics.strToHeuristic(heuristic))
def isSolvable(self):
inversions = 0
for i in range(self.boardLength):
for j in range(i + 1, self.boardLength):
if self.board[i] != 0 and self.board[j] != 0 and self.board[i] > self.board[j]:
inversions += 1
if self.width % 2 == 0:
row = self.board.index(0) // self.width
if row % 2 == 0:
return inversions % 2 == 0
else:
return inversions % 2 == 1
else:
return inversions % 2 == 0