-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
346 lines (316 loc) · 12.1 KB
/
search.py
File metadata and controls
346 lines (316 loc) · 12.1 KB
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
from warnings import warn
from sys import stdin
from time import perf_counter as tt
from time import sleep
from collections import deque, namedtuple, defaultdict
from bisect import bisect
from copy import copy
from math import sqrt
from itertools import repeat, chain
from heap import MinHeap, MaxHeap
class Search:
def __init__(self, start_node, child_func, end_node, cost):
self.start_node = start_node
self.child_nodes = child_func
self.end_node = end_node
self.cost = cost
def run(self):
return True
def timeit(self, start_node=None):
"""Benchmark the search performance. Wrap over self.run().
"""
self.found = 0
self.step = 0
self.path = []
t0 = tt()
result = self.run(start_node)
t1 = tt()
self.timelog = timelog = t1-t0
print(self)
return result, timelog
def __str__(self):
return (f"Search time = {self.timelog} s\n"
f"Step = {self.step}\n"
f"Found = {self.found}\n"
f"Path len = {str(len(self.path)) + str(self.path) if len(self.path)<30 else len(self.path)}")
class IDSearch(Search):
def timeit(self, start_node=None, limit=1_000_000):
"""Benchmark the search performance. Wrap over self.run().
Iterative Depth Search
"""
self.found = 0
self.step = 0
self.path = []
cap = 0
t0 = tt()
while not self.found and cap < limit:
result = self.run(start_node, cap=cap)
cap += 4
t1 = tt()
self.timelog = timelog = t1-t0
print(self)
return result, timelog
class BreadthFirstSearch(Search):
def run(self, start_node=None, depth=0, cap=1_000_000, step=0, found=0):
memory = set()
end_node = self.end_node
childs = self.child_nodes
path = [start_node or self.start_node]
stack = set([start_node or self.start_node])
t0 = tt()
while not found and (-1<step<=cap or -1<depth<=cap) :
if tt()-t0 > 3:
print(f"Step, Depth = {step}, {depth}")
t0 = tt()
diff = 0
if end_node in stack:
diff += 1
stack = list(stack)
path.append(end_node)
found = 1
break
else:
diff += len(stack)
memory.update(stack)
stack = set(chain.from_iterable(childs(x) for x in stack)) - memory
if not diff:
break
step += diff
depth += 1
del stack
self.step, self.path, self.found = self.step+step, path, bool(found)
return True
class DepthFirstSearch(IDSearch):
def run(self, start_node=None, depth=0, cap=1, step=0, found=0):
childs = self.child_nodes
memory = set()
memorise = memory.add
start_node, end_node = start_node or self.start_node, self.end_node
path = [start_node or self.start_node]
stack = defaultdict(deque)
stack[depth] += [start_node or self.start_node]
t0 = tt()
while not found and (-1<depth<=cap):
if tt()-t0 > 3:
print(f"Step, Depth = {step}, {depth}")
t0 = tt()
search_space = stack[depth]
while len(search_space):
if depth == cap:
if end_node in search_space:
step += len(search_space)
found = True
break
else:
path.pop()
depth -= 1
break
node = search_space.popleft()
if node in memory:
continue
memorise(node)
step += 1
path.append(node)
if node == end_node:
found = True
break
depth += 1
stack[depth] += list(set(childs(node)) - memory)
break
else:
path.pop()
depth -= 1
del stack
self.path, self.step, self.found = path, self.step+step, found
return True
class BidirectionSearch(Search):
def run(self, start_node=None, depth=0, cap=1_000_000, step=0, found=0):
childs = self.child_nodes
head, tail = start_node or self.start_node, self.end_node
head_pathfinder, tail_pathfinder = {head:head}, {tail:tail}
head_space, tail_space = set([head]), set([tail])
head_memory, tail_memory = set([head]), set([tail])
intersect = set()
diff = 2
t0 = tt()
while not found and -1<step<=cap:
if tt()-t0 > 3:
print(f"Step, Depth = {step}, {depth}")
t0 = tt()
intersect = head_space.intersection(tail_space)
if intersect:
found = True
break
heads = dict(chain.from_iterable(
((v,k) for v in childs(k) if v not in head_memory)
for k in head_space
))
head_pathfinder.update(heads)
heads = set(heads.keys())
head_space.update(heads)
tails = dict(chain.from_iterable(
((v,k) for v in childs(k) if v not in tail_memory)
for k in tail_space
))
tail_pathfinder.update(tails)
tails = set(tails.keys())
tail_space.update(tails)
head_memory.update(heads)
tail_memory.update(tails)
diff1, diff2 = len(tails), len(heads)
step += diff1 + diff2
if not diff1 or not diff2:
break
depth += 1
step = len(head_space) + len(tail_space)
del head_space
del tail_space
path = []
if found:
print(intersect)
middle = list(intersect)[0]
p1, p2 = middle, head_pathfinder[middle]
path = deque([middle])
while p1 != p2:
path.appendleft(p2)
p1, p2 = p2, head_pathfinder[p2]
p1, p2 = middle, tail_pathfinder[middle]
while p1 != p2:
path.append(p2)
p1, p2 = p2, tail_pathfinder[p2]
path = list(path)
self.path, self.step, self.found = path, self.step+step, bool(found)
return True
class HeuristicSearch(Search):
def run(self, start_node=None, depth=0, cap=1_000_000, step=0, found=0):
memory = set()
memorise = memory.add
end_node = self.end_node
childs = self.child_nodes
path = [start_node or self.start_node]
stack = MinHeap(start_node or self.start_node)
t0 = tt()
while not found and len(stack) and (-1<step<=cap or -1<depth<=cap) :
if tt()-t0 > 3:
print(f"Step, Depth = {step}, {depth}")
t0 = tt()
step += 1
node = stack.popleft()
memorise(node)
if node == end_node:
found = 1
path.append(node)
break
stack += set(childs(node)) - memory
depth += 1
del stack
self.step, self.path, self.found = self.step+step, path, bool(found)
return True
class ForeseeSearch(Search):
def run(self, start_node=None, depth=0, cap=1_000_000, step=0, found=0, foresee_step=16):
memory = set()
memorise = memory.add
end_node = self.end_node
childs = self.child_nodes
self.path = path = [start_node or self.start_node]
stack = MinHeap(start_node or self.start_node)
t0 = tt()
while not found and len(stack) and (-1<step<=cap or -1<depth<=cap) :
if tt()-t0 > 3:
print(f"Step, Depth = {step}, {depth}")
t0 = tt()
step += 1
try:
parent = stack.popleft()
except IndexError:
found = 0
break
memorise(parent)
if parent == end_node:
found = 1
path.append(parent)
break
new_nodes = set(childs(parent)) - memory
backtrack = {child:parent for child in new_nodes}
working_memory = memory | new_nodes
count = 1
foreseeing = MinHeap()
foreseeing += new_nodes
while (not found) and new_nodes and count < foresee_step:
all_new_childs = set()
for new_node in new_nodes:
new_childs = set(childs(new_node)) - working_memory
working_memory |= new_childs
all_new_childs |= new_childs
backtrack.update({child:new_node for child in new_childs})
if end_node in new_childs:
found = 1
break
new_nodes = all_new_childs
foreseeing += all_new_childs
count += 1
# Walk to the node of lowest cost
if len(foreseeing):
destination = foreseeing.popleft()
if destination <= parent:
backpath = [destination]
_from, _to = backtrack[destination], destination
while _from != parent:
# Add nodes in the middle back into the stack
# Since we don't know if they will lead us to the end,
# we haven't explore them yet.
stack.append(_to)
backpath.append(_from)
_from, _to = backtrack[_from], _from
backpath = backpath[::-1]
step += len(backpath)
path += backpath
if found:
break
depth += count
del stack
self.step, self.path, self.found = self.step+step, path, bool(found)
return True
class IDASearch(IDSearch):
def run(self, start_node=None, depth=0, cap=1_000_000, step=0, found=0):
childs = self.child_nodes
memory = set()
memorise = memory.add
start_node, end_node = start_node or self.start_node, self.end_node
path = [start_node or self.start_node]
stack = defaultdict(MinHeap)
stack[depth] += [start_node or self.start_node]
t0 = tt()
while not found and (-1<depth<=cap):
if tt()-t0 > 3:
print(f"Step, Depth = {step}, {depth}")
t0 = tt()
search_space = stack[depth]
while len(search_space):
if depth == cap:
if end_node in search_space:
step += len(search_space)
found = True
break
else:
path.pop()
depth -= 1
break
node = search_space.popleft()
if node in memory:
continue
memorise(node)
step += 1
path.append(node)
if node == end_node:
found = True
break
depth += 1
stack[depth] += set(childs(node)) - memory
break
else:
path.pop()
depth -= 1
del stack
self.path, self.step, self.found = path, self.step+step, found
return True