-
Notifications
You must be signed in to change notification settings - Fork 62
/
__init__.py
267 lines (198 loc) 路 8.58 KB
/
__init__.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
# -*- coding: utf-8 -*-
""" generic A-Star path searching algorithm """
import logging
from abc import ABC, abstractmethod
from heapq import heapify, heappush, heappop
from typing import Type, Callable, Dict, Iterable, Union, TypeVar, Generic
from math import inf as infinity
# introduce generic type
T = TypeVar("T")
################################################################################
class SearchNode(Generic[T]):
"""Representation of a search node"""
__slots__ = ("data", "gscore", "fscore", "closed", "came_from", "in_openset")
def __init__(
self, data: T, gscore: float = infinity, fscore: float = infinity
) -> None:
self.data = data
self.gscore = gscore
self.fscore = fscore
self.closed = False
self.in_openset = False
self.came_from: Union[None, SearchNode[T]] = None
def __lt__(self, b: "SearchNode[T]") -> bool:
"""Natural order is based on the fscore value & is used by heapq operations"""
return self.fscore < b.fscore
################################################################################
class SearchNodeDict(Dict[T, SearchNode[T]]):
"""A dict that returns a new SearchNode when a key is missing"""
def __missing__(self, k) -> SearchNode[T]:
v = SearchNode(k)
self.__setitem__(k, v)
return v
################################################################################
SNType = TypeVar("SNType", bound=SearchNode)
class OpenSet(ABC, Generic[SNType]):
"""As we may have performance issues with the heapq module when an item is
re-inserted, we may use other implementations for this feature.
- By default the HeapQOpenSet class just relies on the heapq module, it does not need any external dependency.
- The SortedContainersOpenSet class uses the sortedcointainers module. As
this module is optional, it will be used only if your own project
depends on it.
"""
@abstractmethod
def push(self, item: SNType) -> None:
"""Add an item to the queue"""
raise NotImplementedError
@abstractmethod
def pop(self) -> SNType:
"""Remove and return the smallest item from the queue"""
raise NotImplementedError
@abstractmethod
def remove(self, item: SNType) -> None:
"""remove an item from the queue, ensuring that the queue is still valid afterwards"""
raise NotImplementedError
################################################################################
class HeapQOpenSet(OpenSet[SNType], Generic[SNType]):
"""just a wrapper around heapq operations"""
def __init__(self):
self.heap = []
heapify(self.heap)
def push(self, item: SNType) -> None:
"""Add an item to the queue"""
item.in_openset = True
heappush(self.heap, item)
def pop(self) -> SNType:
"""Remove and return the smallest item from the queue"""
item = heappop(self.heap)
item.in_openset = False
return item
def remove(self, item: SNType):
self.heap.remove(item)
heapify(
self.heap
) # costly operation but necessary as remove operation destroy the structure of the heap
item.in_openset = False
################################################################################
OpenSetImpl: Type[OpenSet] = HeapQOpenSet
try:
import sortedcontainers
class SortedContainersOpenSet(OpenSet[SNType], Generic[SNType]):
def __init__(self):
self.sortedlist = sortedcontainers.SortedList(key=lambda x: x.fscore)
def push(self, item: SNType) -> None:
item.in_openset = True
self.sortedlist.add(item)
def pop(self) -> SNType:
item = self.sortedlist.pop(0)
item.in_openset = False
return item
def remove(self, item: SNType):
self.sortedlist.remove(item)
item.in_openset = False
OpenSetImpl = SortedContainersOpenSet
logging.info("using sortedcontainers for heap operations")
except Exception as e:
logging.info("sortedcontainers module not loaded, using the default heapq module")
################################################################################*
class AStar(ABC, Generic[T]):
__slots__ = ()
@abstractmethod
def heuristic_cost_estimate(self, current: T, goal: T) -> float:
"""
Computes the estimated (rough) distance between a node and the goal.
The second parameter is always the goal.
This method must be implemented in a subclass.
"""
raise NotImplementedError
@abstractmethod
def distance_between(self, n1: T, n2: T) -> float:
"""
Gives the real distance between two adjacent nodes n1 and n2 (i.e n2
belongs to the list of n1's neighbors).
n2 is guaranteed to belong to the list returned by the call to neighbors(n1).
This method must be implemented in a subclass.
"""
@abstractmethod
def neighbors(self, node: T) -> Iterable[T]:
"""
For a given node, returns (or yields) the list of its neighbors.
This method must be implemented in a subclass.
"""
raise NotImplementedError
def is_goal_reached(self, current: T, goal: T) -> bool:
"""
Returns true when we can consider that 'current' is the goal.
The default implementation simply compares `current == goal`, but this
method can be overwritten in a subclass to provide more refined checks.
"""
return current == goal
def reconstruct_path(self, last: SearchNode, reversePath=False) -> Iterable[T]:
def _gen():
current = last
while current:
yield current.data
current = current.came_from
if reversePath:
return _gen()
else:
return reversed(list(_gen()))
def astar(
self, start: T, goal: T, reversePath: bool = False
) -> Union[Iterable[T], None]:
if self.is_goal_reached(start, goal):
return [start]
openSet: OpenSet[SearchNode[T]] = OpenSetImpl()
searchNodes: SearchNodeDict[T] = SearchNodeDict()
startNode = searchNodes[start] = SearchNode(
start, gscore=0.0, fscore=self.heuristic_cost_estimate(start, goal)
)
openSet.push(startNode)
while openSet:
current = openSet.pop()
if self.is_goal_reached(current.data, goal):
return self.reconstruct_path(current, reversePath)
current.closed = True
for neighbor in map(lambda n: searchNodes[n], self.neighbors(current.data)):
if neighbor.closed:
continue
tentative_gscore = current.gscore + self.distance_between(
current.data, neighbor.data
)
if tentative_gscore >= neighbor.gscore:
continue
neighbor_from_openset = neighbor.in_openset
if neighbor_from_openset:
# we have to remove the item from the heap, as its score has changed
openSet.remove(neighbor)
# update the node
neighbor.came_from = current
neighbor.gscore = tentative_gscore
neighbor.fscore = tentative_gscore + self.heuristic_cost_estimate(
neighbor.data, goal
)
openSet.push(neighbor)
return None
################################################################################
U = TypeVar("U")
def find_path(
start: U,
goal: U,
neighbors_fnct: Callable[[U], Iterable[U]],
reversePath=False,
heuristic_cost_estimate_fnct: Callable[[U, U], float] = lambda a, b: infinity,
distance_between_fnct: Callable[[U, U], float] = lambda a, b: 1.0,
is_goal_reached_fnct: Callable[[U, U], bool] = lambda a, b: a == b,
) -> Union[Iterable[U], None]:
"""A non-class version of the path finding algorithm"""
class FindPath(AStar):
def heuristic_cost_estimate(self, current: U, goal: U) -> float:
return heuristic_cost_estimate_fnct(current, goal) # type: ignore
def distance_between(self, n1: U, n2: U) -> float:
return distance_between_fnct(n1, n2)
def neighbors(self, node) -> Iterable[U]:
return neighbors_fnct(node) # type: ignore
def is_goal_reached(self, current, goal) -> bool:
return is_goal_reached_fnct(current, goal) # type: ignore
return FindPath().astar(start, goal, reversePath)
__all__ = ["AStar", "find_path"]