-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_Algorithms.py
280 lines (219 loc) · 7.99 KB
/
search_Algorithms.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
from collections import defaultdict
from collections import deque
from heapq import heappop, heappush
from typing import List,Dict,Tuple,Any
graph=defaultdict(list)
heuristics=defaultdict(list)
weights=defaultdict(list)
graph={
'S':['A','B'],
'A':['S','B','D'],
'B':['S','A','C'],
'C':['B','E'],
'D':['A','G'],
'E':['C'],
'G':['D']
}
heuristics = {
'S': 8,
'A': 6,
'B': 6,
'C': 4,
'D': 4,
'E': 2,
'G': 0
}
weights={
('S','A'):3,
('S','B'):5,
('A','B'):4,
('B','C'):3,
('A','D'):3,
('C','E'):6,
('D','G'):5
}
and_nodes={
'B'
}
oracle_path=['S','A','D','G']
oracle_cost=11
start_node='S'
goal_node='G'
w=2
MAX,MIN=1000,-1000
def bms(graph:Dict[str,List],start:str,goal:str)->List[str]:
#Keeps track of all possible paths
all_paths=[]
current_path=[start]
def find_all_paths(current_path:List[str])->None:
current_node=current_path[-1]
if current_node==goal:
all_paths.append(current_path.copy())
return
for neighbour in graph.get(current_node,[]):
if neighbour not in current_path:
current_path.append(neighbour)
find_all_paths(current_path)
current_path.pop() #helps search all possible paths (backtracking)
find_all_paths(current_path)
# #shortest path
# if all_paths:
# shortest_path=min(all_paths,key=len) #min(length of list in all_paths)
# return shortest_path
# else:
# return None
print(all_paths)
def bfs(graph:Dict[str,List],start:str,goal:str)->List[str]:
queue=deque([[start]])
while queue:
current_path=queue.popleft()
current_node=current_path[-1]
if current_node==goal:
return current_path
for neighbour in graph.get(current_node,[]):
if neighbour not in current_path:
new_path=current_path+[neighbour]
queue.append(new_path)
return None
def dfs(graph:Dict[str,List],start:str,goal:str)-> List[str]:
ds=deque([[start]])
#stack=[[start]]
while ds:
current_path=ds.pop() #popleft bfs
current_node=current_path[-1]
if current_node==goal:
return current_path
for neighbour in graph.get(current_node,[]):
if neighbour not in current_path:
new_path=current_path+[neighbour]
ds.append(new_path)
return None
def hill_climb(graph:Dict[str,List],start:str,goal:str,heuristic:Dict[str,int])->List[str]:
current_node=start
path=[current_node]
# print("Path:",path)
while current_node!=goal:
neighbours=graph.get(current_node,[])
# print("Curr:",current_node)
# print("Neighbours:",neighbours)
if not neighbours:
return None
next_node=min(neighbours,key= lambda x:heuristic.get(x,float('inf')))
# print("Next Node:",next_node)
if heuristic.get(next_node,float('inf')) >= heuristic.get(current_node,[]):
break
current_node=next_node
# print("Curr after",current_node)
path.append(current_node)
# print("Path after:",path)
return path
def beam(graph:Dict[str,List],start:str,goal:str,heuristics:Dict[str,int],w:int)->List[str]:
current_nodes=[(heuristics[start],[start])]
while current_nodes:
new_nodes=[]
for h, current_path in sorted(current_nodes)[:w]:
current_node=current_path[-1]
# print("Current Node:",current_node)
if current_node==goal:
return current_path
for neighbour in graph.get(current_node):
if neighbour not in current_path:
new_path=current_path+[neighbour]
new_h=sum(heuristics.get(node,0)for node in new_path)-heuristics.get(start,0)
heappush(new_nodes,(new_h,new_path))
current_nodes=new_nodes
# print("Current Node:",current_nodes)
return None
def a_star(graph:Dict[str,List],start:str,goal:str,heuristic:Dict[str,int],weights:Dict[Tuple[str],int])->List[str]:
pq=[(heuristic[start],0,[start])]
while pq:
a,cost,current_path=heappop(pq)
current_node=current_path[-1]
visited=set()
if current_node==goal:
return current_path
if current_node in visited:
continue
visited.add(current_node)
for neighbour in graph.get(current_node,[]):
if neighbour not in current_path:
new_path=current_path+[neighbour]
new_cost=cost+weights.get((current_node,neighbour),0)
new_h=new_cost+heuristic.get(neighbour)
heappush(pq,(new_h,new_cost,new_path))
return None
def bb(graph:Dict[str,List],start:str,goal:str,weights:Dict[Tuple[str],int])->List[str]:
pq=[(0,[start])]
while pq:
cost,current_path=heappop(pq)
current_node=current_path[-1]
if current_node==goal:
return current_path
for neighbour in graph.get(current_node,[]):
if neighbour not in current_path:
new_path=current_path+[neighbour]
new_cost=cost+weights.get((current_node,neighbour),0)
heappush(pq,(new_cost,new_path))
return None
def best_first(graph:Dict[str,List],start:str,goal:str,heuristic:Dict[str,int],weights:Dict[Tuple[str],int])->List[str]:
pq=[(heuristic[start],[start])]
visited=set()
while pq:
cost,current_path=heappop(pq)
current_node=current_path[-1]
if current_node==goal:
return current_path
if current_node in visited:
continue
visited.add(current_node)
for neighbour in graph.get(current_node,[]):
if neighbour not in current_path:
new_path=current_path+[neighbour]
new_cost=heuristic.get(neighbour)
heappush(pq,(new_cost,new_path))
return None
def oracle_search_with_h(graph:Dict[str,List],start:str,goal:str,weights:Dict[Tuple[str],int],oracle_path,oracle_cost)->List[str]:
pq=[(0,[start])]
best_path=oracle_path
min_cost=oracle_cost
while pq:
cost,current_path=heappop(pq)
current_node=current_path[-1]
if current_node==goal:
if cost< min_cost:
best_path,min_cost=current_path,cost
continue
for neighbour in graph.get(current_node,[]):
if neighbour not in current_path:
new_path=current_path+[neighbour]
new_cost=cost+weights.get((current_node,neighbour),1)
if new_cost < min_cost:
heappush(pq,(new_cost,new_path))
return best_path
def ao_star(graph: Dict[str, List[str]], start: str, goal: str, weights: Dict[Tuple[str, str], int], and_nodes: Set[str]) -> List[str]:
def calculate_cost(node: str, visited: Set[str]) -> Tuple[float, List[str]]:
if node == goal:
return 0, [node]
if node in visited:
return float('inf'), []
visited.add(node)
if node in and_nodes:
total_cost = 0
total_path = [node]
for neighbor in graph.get(node, []):
cost, path = calculate_cost(neighbor, visited.copy())
total_cost += cost + weights.get((node, neighbor), 1)
total_path.extend(path)
return total_cost, total_path
else:
min_cost = float('inf')
best_path = []
for neighbor in graph.get(node, []):
cost, path = calculate_cost(neighbor, visited.copy())
total_cost = cost + weights.get((node, neighbor), 1)
if total_cost < min_cost:
min_cost = total_cost
best_path = [node] + path
return min_cost, best_path
_, path = calculate_cost(start, set())
return path if path else []