-
Notifications
You must be signed in to change notification settings - Fork 0
/
disjoint_heuristic_database_creator.py
94 lines (77 loc) · 2.8 KB
/
disjoint_heuristic_database_creator.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
from search import *
from fifteenpuzzle import *
import pickle
# import time
def patternHash(state):
numbers = state.getOneDimentionalState()
idx = 0
for i in range(16):
if numbers[i] in [1,5,6,9,10,13]:#[6,7,8,11,12]:#[1,2,3,4,5]:#[1,5,6,9,10,13]:#[2,3,4]:#[1,5,6,9,10,13]:
val = numbers[i]
idx = idx << 4 | val
idx = idx << 4 | i
return idx
def blankHash(state):
numbers = state.getOneDimentionalState()
idx = 0
for i in range(16):
if numbers[i] in [1,5,6,9,10,13,0]: #[6,7,8,11,12,0]:#[1,2,3,4,5,0]:#[1,5,6,9,10,13, 0]:#[2,3,4,0]: #[1,5,6,9,10,13, 0]:
val = numbers[i]
idx = idx << 4 | val
idx = idx << 4 | i
return idx
class LightNode:
"""A node with no parent. """
def __init__(self, data):
self.data = data # tuple(state, cumulativeCost)
def createDB(problem):
# startTime = time.time()
fringe = Queue()
closedSet = set()
db = {}
itr = 1
fringe.push( LightNode((problem.getStartState(), 0)) )
while len(db) < 5765760: #524160: #5765760:
if fringe.isEmpty():
print('break')
break
node = fringe.pop()
state, currCost = node.data
patternHashValue = patternHash(state)
itr += 1
if itr % 10000 == 0:
print(currCost)
print(itr)
print(len(db))
if patternHashValue not in db:
db[patternHashValue] = currCost
successors = problem.getSuccessors(state)
for successor in successors:
sucState, _, _ = successor
sucBlankHash = blankHash(sucState)
if sucBlankHash not in closedSet:
closedSet.add(sucBlankHash)
if patternHash(sucState) != patternHashValue:
sucCost = currCost + 1
else:
sucCost = currCost
successorNode = LightNode((sucState, sucCost))
fringe.push(successorNode)
# endTime = time.time()
# db['exeTime'] = endTime - startTime
dbfile = open('DB_15691013', 'ab')
pickle.dump(db, dbfile)
dbfile.close()
numbers = [1, -1, -1, -1, 5, 6, 0, -1, 9, 10, -1, -1, 13, -1, -1, -1]
#[1, 2, 3, 4, 5, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1]
#[1, -1, -1, -1, 5, 6, 0, -1, 9, 10, -1, -1, 13, -1, -1, -1]
#[-1, -1, -1, -1, -1, -1, -1, -1, 9, 10, 0, -1, 13, 14, 15, -1]
#[-1, -1, -1, -1, -1, 6, 7, 8, 0, -1, 11, 12, -1, -1, -1, -1]
#[1, 2, 3, 4, 5, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1]
#[0, 2, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
puzzle = FifteenPuzzleState(numbers)
print(puzzle)
print(patternHash(puzzle))
print(blankHash(puzzle))
problem = FifteenPuzzleSearchProblem(puzzle)
createDB(problem)