-
Notifications
You must be signed in to change notification settings - Fork 0
/
A-Star-Search-explored.py
158 lines (141 loc) · 4.4 KB
/
A-Star-Search-explored.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
import heapq
class Node:
def __init__(self, label, goal_cost):
self.label = label
self.cost = 10000
self.goal_cost = goal_cost
self.save_cost = None
self.parent = []
self.children = []
def __repr__(self):
return str(dict({
"label": self.label,
"cost": self.cost,
"goal cost": self.goal_cost
}))
def __eq__(self, other):
return self.label == other.label
def __lt__(self, other):
if self.save_cost == 10000:
return self.goal_cost + self.cost < other.goal_cost + other.cost
else:
return self.cost < other.cost
def __gt__(self, other):
return self.cost > other.cost
def get_label(self):
return self.label
def neighbors(self):
return self.children + self.parent
class Tree:
def __init__(self):
self.nodes = []
self.edges = []
def add_nodes(self, data):
for node in data:
self.nodes.append(node)
def add_node(self, node):
self.nodes.append(node)
def get_index(self, node):
for i, n in enumerate(self.nodes):
if n.get_label() == node.get_label():
return i
return -1
def add_edges(self, tuple_edges):
for t in tuple_edges:
start_label = t[0]
end_label = t[1]
w = t[2]
index_start_label = self.get_index(Node(start_label, None))
index_end_label = self.get_index(Node(end_label, None))
self.nodes[index_start_label].children.append(
self.nodes[index_end_label])
self.nodes[index_end_label].parent.append(
self.nodes[index_start_label])
self.edges.append(
(self.nodes[index_start_label], self.nodes[index_end_label], t[2]))
def show_nodes(self):
return [node.get_data() for node in self.nodes]
def get_edge(self, start_node, end_node):
try:
return [edges for edges in self.edges if edges[0] == start_node
and edges[1] == end_node][0]
except:
return None
def update_cost(tree, current_node, prev_node):
if tree.get_edge(prev_node, current_node) is not None:
if current_node.cost > prev_node.cost + tree.get_edge(prev_node, current_node)[2]:
current_node.cost = prev_node.cost + \
tree.get_edge(prev_node, current_node)[2]
def a_star(tree, start, end):
frontier = [start]
heapq.heapify(frontier)
explored = []
while len(frontier) > 0:
state = heapq.heappop(frontier)
explored.append(state)
# print(state)
if state == end:
return explored
for child in state.neighbors():
update_cost(tree, child, state)
if child.get_label() not in list(set(node.get_label() for node in frontier + explored)):
heapq.heappush(frontier, child)
return False
if __name__ == "__main__":
V = [
Node("S", 12), # 0
Node("A", 9), # 1
Node("B", 8), # 2
Node("C", 7), # 3
Node("D", 6), # 4
Node("E", 5), # 5
Node("F", 4), # 6
Node("G", 10), # 7
Node("H", 10), # 8
Node("K", 3), # 9
Node("M", 9), # 10
Node("N", 10), # 11
Node("I", 6), # 12
Node("J", 0), # 13
Node("L", 0), # 14
Node("Z", 8) # 15
]
E = [
("S", "A", 5),
("B", "B", 6),
("S", "C", 5),
("A", "D", 6),
("A", "E", 7),
("B", "F", 3),
("B", "G", 4),
("C", "H", 6),
("C", "K", 4),
("D", "M", 5),
("D", "N", 8),
("E", "I", 8),
("F", "J", 4),
("F", "L", 4),
("K", "Z", 2),
]
tree = Tree()
tree.add_nodes(V)
tree.add_edges(E)
tree.nodes[0].cost = 0
print('Path from S to J:', end='\n')
resultJ = a_star(tree, tree.nodes[0], tree.nodes[13])
if resultJ:
s = 'explored: '
for i in resultJ:
s += i.label + " "
print(s)
else:
print('Path does not exist!')
print('Path from S to L:', end='\n')
resultL = a_star(tree, tree.nodes[0], tree.nodes[14])
if resultL:
s = 'explored: '
for i in resultL:
s += i.label + " "
print(s)
else:
print('Path does not exist!')