-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
276 lines (225 loc) · 10.9 KB
/
search.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
from __future__ import annotations
import copy
import heapq as hq
import itertools
from typing import Callable, DefaultDict, Generic, Iterable, Iterator, TypeVar
N = TypeVar('N');
T = TypeVar('T');
P = TypeVar('P')
class DStarQueue(Generic[T,P]):
def __init__(self,start:list[tuple[P,T]]=[]):
self._queue = start;
hq.heapify(self._queue);
def topKey(self):
hq.heapify(self._queue)
# print(self._queue)
if len(self._queue) > 0:
return self._queue[0][0]
else:
# print('empty queue!')
return (float('inf'), float('inf'))
def push(self,item:T,priority:P):
hq.heappush(self._queue,(priority,item));
def pop(self):
return hq.heappop(self._queue);
def __bool__(self):
return len(self._queue) > 0;
def __iter__(self)->Iterator[tuple[P,T]]:
return iter(self._queue);
def __contains__(self,item:T):
return any([x for x in self if x[1] is item]);
def pushOrUpdate(self,item:T,priority:P):
for index,(_,i) in enumerate(self):
if i == item:
self._queue[index] = (priority,item);
hq.heapify(self._queue);
return;
self.push(item,priority);
def removeItem(self,item:T)->bool:
for (p,i) in self:
if i == item:
self._queue.remove((p,i));
return True;
return False;
class DStarSearcher(Generic[N]):
def __init__(self,heuristic:Callable[[N,N],float],start:N,goal:N,pred:Callable[[N],Iterable[tuple[N,float]]],succ:Callable[[N],Iterable[tuple[N,float]]],checkpoint:dict|None=None):
self.start = start;
self.goal = goal;
self.h = heuristic;
self.pred = pred;
self.succ = succ;
self.km = 0;
if checkpoint is None:
self.g = DefaultDict[N,float](lambda: float('inf'));
self.rhs = DefaultDict[N,float](lambda: float('inf'));
self.rhs[goal] = 0;
self.U = DStarQueue[N,tuple[float,float]]();
self.U.push(self.goal,(self.h(self.start,self.goal),0));
else:
self.load_checkpoint(checkpoint);
def get_checkpoint_data(self):
return {'g':dict(self.g),'rhs':dict(self.rhs),'U':self.U};
def load_checkpoint(self,checkpoint):
self.g = DefaultDict[N,float](lambda:float('inf'),checkpoint['g']);
self.rhs = DefaultDict[N,float](lambda:float('inf'),checkpoint['rhs']);
self.U = checkpoint['U'];
def calculateKey(self,s:N): #somewhat equivalent to A*'s f_score
t = min(self.g[s],self.rhs[s]) #the minimum of the two interpretations of the distance to the end
return (self.km+self.h(self.start,s)+t,t) #(total path length, path to end [for tiebreaker])
def updateVertex(self,u:N):
if (self.g[u] != self.rhs[u]):
self.U.pushOrUpdate(u,self.calculateKey(u));
else:
self.U.removeItem(u);
def computeShortestPath(self):
#while [start is not on the best path] or [start is locally inconsistent]
while self.U.topKey() < self.calculateKey(self.start) or self.rhs[self.start] > self.g[self.start]:
# print('path changed');
k_old,u = self.U.pop();
k_new = self.calculateKey(u);
# print(u)
if k_old < k_new:
# print('pushed - preds not checked')
self.U.pushOrUpdate(u,k_new);
elif self.g[u] > self.rhs[u]:
self.g[u] = self.rhs[u];
self.U.removeItem(u);
for s,c in self.pred(u):
if (s == self.start):
print('start reached -- clause 1');
print('g,rhs:',self.g[s],self.rhs[s]);
self.rhs[s] = min(self.rhs[s],c+self.g[u]);
if (s == self.start):
print('g,rhs:',self.g[s],self.rhs[s]);
self.updateVertex(s);
if (s == self.start):
print('g,rhs:',self.g[s],self.rhs[s]);
else:
g_old = self.g[u];
self.g[u] = float('inf');
for s,c in itertools.chain(self.pred(u),[(u,0),]):
if (s == self.start):
print('start reached -- clause 2')
print('g,rhs:',self.g[s],self.rhs[s]);
if self.rhs[s] == c + g_old and s != self.goal:
self.rhs[s] = min([c+self.g[sp] for sp,c in self.succ(s)]);
if (s == self.start):
print('start - clause 3 triggered');
print('g,rhs:',self.g[s],self.rhs[s]);
self.updateVertex(s);
if (s == self.start):
print('g,rhs:',self.g[s],self.rhs[s]);
# print(self.g[self.start],self.rhs[self.start]);
def search(self,
move_func:Callable[[DStarSearcher],N], #callback for moving
scan_func:Callable[[],list[tuple[N,N,float,float]]] #start,end,old,new
):
self.computeShortestPath();
last = self.start;
while self.start != self.goal:
next_node = move_func(self); #step forward
changed_costs = scan_func();
self.start = next_node;
if any(changed_costs):
self.km += self.h(last,self.start);
last = self.start;
for (u,v,c_old,c) in changed_costs:
if (u != self.goal):
if (c_old > c):
self.rhs[u] = min(self.rhs[u],c+self.g[v]);
elif self.rhs[u] == c_old + self.g[v]:
self.rhs[u] = min([cp+self.g[sp] for sp,cp in self.succ(u)]);
self.updateVertex(u);
self.computeShortestPath();
#TODO: Consider implementing D* reset https://www.researchgate.net/publication/316945804_D_Lite_with_Reset_Improved_Version_of_D_Lite_for_Complex_Environment
def step_search(self,changed_costs:list[tuple[N,N,float,float]]=[]): #start,end,old,new
if any(changed_costs):
for (u,v,c_old,c) in changed_costs:
if (u != self.goal):
if (c_old > c):
self.rhs[u] = min(self.rhs[u],c+self.g[v]);
elif self.rhs[u] == c_old + self.g[v]:
self.rhs[u] = min([cp+self.g[sp] for sp,cp in self.succ(u)]);
self.updateVertex(u);
self.computeShortestPath();
def load_start(self,start:N):
self.old_start = self.start;
self.start = start;
# self.km = self.h(self.old_start,self.start);
def revert_start(self):
self.start = self.old_start;
# self.km = 0;
def explore_from_position(self,pos:N): #note: if pos has already been explored with no updates, this should do little to nothing
self.load_start(pos);
self.computeShortestPath();
self.revert_start();
def return_shortest_path(self,start=None):
if start:
self.load_start(start);
self.computeShortestPath();
current = self.start;
path = [current];
while current != self.goal:
current,_ = min([(s,c) for s,c in self.succ(current)],key=lambda e: self.g[e[0]] + e[1]);
path.append(current);
if start:
self.revert_start();
return path;
def update_costs(self,updates:list[tuple[N,N,float,float]]):
for (u,v,c_old,c) in updates:
if (u != self.goal):
if (c_old > c):
self.rhs[u] = min(self.rhs[u],c+self.g[v]);
elif self.rhs[u] == c_old + self.g[v]:
self.rhs[u] = min([cp+self.g[sp] for sp,cp in self.succ(u)]);
self.updateVertex(u);
self.computeShortestPath();
class LevelSearcher(Generic[N,T]):
def __init__(self,start:N,is_goal:Callable[[N],bool],heuristic:Callable[[N],float],node_key:Callable[[N],T],succ:Callable[[N],Iterable[N]],cost:Callable[[N,N],float],frustration_multiplier:float,checkpoint:dict|None=None):
self.start = start;
self.goal = is_goal;
self.c = cost;
self.h = heuristic;
self.succ = succ;
self.node_key = node_key;
self.frustration_weight = frustration_multiplier;
self.cameFrom = {};
def sort_key(edge:tuple[N|None,N]):
prev,node = edge;
if (prev == None):
raise Exception("attempting to sort initial edge, bad juju");
return min(self.g[self.node_key(prev)].values()) + self.c(prev,node) + self.h(node);
self.sort_key = sort_key;
if checkpoint is None:
self.g = DefaultDict[T,dict[N|None,float]](lambda: {None:float('inf')});
self.g[self.node_key(self.start)][None] = 0;
self.frustration:dict[N,int] = DefaultDict(lambda:0);
self.open_edges:list[tuple[N|None,N]] = [(None,start)]; #priority list of (prev,current) sorted by the sum of: a) the g score to the previous node, b) the estimated cost (self.c) from previous to current, and c) the heuristic from current
self.completed_edges:list[tuple[N|None,N]] = [];
self.complete_edge((None,start));
else:
self.load_checkpoint(checkpoint);
def sorted_edges(self)->list[tuple[N|None,N]]:
self.open_edges.sort(key=self.sort_key);
return copy.deepcopy(self.open_edges);
def complete_edge(self,edge:tuple[N|None,N]):
if edge in self.open_edges:
self.open_edges.remove(edge);
self.completed_edges.append(edge);
self.open_edges += [(edge[1],succ) for succ in self.succ(edge[1])];
elif edge in self.completed_edges:
print("WARNING: attempting to complete edge that has already been completed");
else:
print(f"WARNING: attempting to complete edge {edge} not in open edges FIX SOON");
# raise Exception(f"Attempt to complete edge {edge} not in open edges or previously completed")
def update_scores(self,scores:Iterable[tuple[N,float]]):
for node,score in scores:
self.frustration[node] += 1;
self.g[self.node_key(node)][node] = score + self.frustration[node]*self.frustration_weight;
def get_checkpoint_data(self):
return {'g':dict(self.g),'open':self.open_edges,'complete':self.completed_edges,'frustration':dict(self.frustration)};
def load_checkpoint(self,checkpoint:dict):
self.g = DefaultDict[T,dict[N|None,float]](lambda: {None:float('inf')},checkpoint['g']);
self.open_edges = checkpoint['open'];
self.completed_edges = checkpoint['complete'] if 'complete' in checkpoint else [];
self.frustration = DefaultDict[N,int](lambda:0,checkpoint['frustration'] if 'frustration' in checkpoint else {});