-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprm_basic.py
More file actions
40 lines (33 loc) · 1.42 KB
/
Copy pathprm_basic.py
File metadata and controls
40 lines (33 loc) · 1.42 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
"""Standard PRM — baseline planner (mirrors the original algorithm)."""
import networkx as nx
from prm_base import PRMBase
class BasicPRM(PRMBase):
name = "BasicPRM"
def _add_vertex(self, v, dmax):
self.G.add_node(v)
for u in list(self.G.nodes):
if u != v and self.dist(u, v) < dmax:
if self.edge_free(u, v):
self.G.add_edge(u, v, weight=self.dist(u, v))
def construct(self, N, dmax, verbose=False):
for k in range(N):
self._add_vertex(self.random_sample(), dmax)
if verbose and (k + 1) % 100 == 0:
print(f" [{self.name}] {k+1}/{N}")
return self.G
def find(self, start, goal, dmax, max_retries=5, extra=100, **kw):
if start not in self.G:
self._add_vertex(start, dmax)
if goal not in self.G:
self._add_vertex(goal, dmax)
for attempt in range(max_retries):
try:
path = nx.astar_path(self.G, start, goal,
heuristic=self.dist, weight='weight')
cost = nx.astar_path_length(self.G, start, goal,
heuristic=self.dist, weight='weight')
return path, cost
except nx.NetworkXNoPath:
for _ in range(extra):
self._add_vertex(self.random_sample(), dmax)
return None, float('inf')