-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_star.py
73 lines (66 loc) · 2.71 KB
/
a_star.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
import heapq
import heuristics
import utilities
def searchSPI(p):
unvisited = []
heapq.heappush(unvisited, (p.cost_so_far+heuristics.sum_of_permutation_inversion(p), p))
visited = []
while unvisited:
current = heapq.heappop(unvisited)[1]
# with open('puzzleA_starSPITrace.txt', 'a') as f:
# f.write("{} {} cost = {}\n".format(current.move, current.config, current.cost_so_far))
if utilities.goal(current):
return current
if utilities.wasVisited(current, visited):
continue
else:
children = utilities.generateChildren(current)
#Remove visited children
children = utilities.unvisitedChildren(children, visited)
for c in children:
heapq.heappush(unvisited, (c.cost_so_far+heuristics.sum_of_permutation_inversion(c), c))
visited.append(current)
#input("Press Enter to continue")
return None
def searchMD(p):
unvisited = []
heapq.heappush(unvisited, (p.cost_so_far+heuristics.manhattan_distance(p), p))
visited = []
while unvisited:
current = heapq.heappop(unvisited)[1]
# with open('puzzleA_starMDTrace.txt', 'a') as f:
# f.write("{} {} cost = {}\n".format(current.move, current.config, current.cost_so_far))
if utilities.goal(current):
return current
if utilities.wasVisited(current, visited):
continue
else:
children = utilities.generateChildren(current)
#Remove visited children
children = utilities.unvisitedChildren(children, visited)
for c in children:
heapq.heappush(unvisited, (c.cost_so_far+heuristics.manhattan_distance(c), c))
visited.append(current)
#input("Press Enter to continue")
return None
def searchHD(p):
unvisited = []
heapq.heappush(unvisited, (p.cost_so_far+heuristics.hamming_distance(p), p))
visited = []
while unvisited:
current = heapq.heappop(unvisited)[1]
# with open('puzzleA_starMDTrace.txt', 'a') as f:
# f.write("{} {} cost = {}\n".format(current.move, current.config, current.cost_so_far))
if utilities.goal(current):
return current
if utilities.wasVisited(current, visited):
continue
else:
children = utilities.generateChildren(current)
#Remove visited children
children = utilities.unvisitedChildren(children, visited)
for c in children:
heapq.heappush(unvisited, (c.cost_so_far+heuristics.hamming_distance(c), c))
visited.append(current)
#input("Press Enter to continue")
return None