forked from sbochkar/distributed_coverage_planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdubins_cost.py
130 lines (90 loc) · 2.6 KB
/
dubins_cost.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
import dubins
from math import sqrt
from shapely.geometry import LineString
from shapely.geometry import Polygon
from shapely.geometry import LinearRing
def compute_costs(P, mapping, radius, orig_poly):
"""
Compute dubins costs between path segments which could be either
a line or a point.
TODO: Collision checking!!
"""
MAX_COST = 1000000
num_nodes = len(mapping)
r = radius
cost = [[0 for i in range(num_nodes)] for i in range(num_nodes)]
print("Size: %d nodes."%num_nodes)
# Populate the cost matrix
for i in range(num_nodes):
# print(i)
for j in range(num_nodes):
q0 = mapping[i][0].get_exit_info(mapping[i][1])
q1 = mapping[j][0].get_entrance_info(mapping[j][1])
# Check for collisions
x0 = q0[0]; y0 = q0[1]
x1 = q1[0]; y1 = q1[1]
#if has_collision(P, [(x0, y0), (x1, y1)]):
if has_collision(orig_poly, [(x0, y0), (x1, y1)]):
length = 100*dubins.path_length(q0, q1, r)
length += 1000000
else:
length = 100*dubins.path_length(q0, q1, r)
cost[i][j] = length
# Generate a cluster information list
cluster_list = []
node_list = []
counter = 0
for i in range(num_nodes):
segment, direction_id = mapping[i]
if direction_id == 0:
if node_list:
cluster_list.append(node_list)
counter += 1
node_list = []
node_list.append(i)
cluster_list.append(node_list)
return cost, cluster_list
def compute_tsp_costs(P, tsp_mapping, radius):
"""
Compute direction free costs"
"""
MAX_COST = 1000000
num_nodes = len(tsp_mapping)
r = radius
cost = [[0 for i in range(num_nodes)] for i in range(num_nodes)]
print("Size: %d nodes."%num_nodes)
# Populate the cost matrix
for i in range(num_nodes):
print(i)
for j in range(num_nodes):
q0 = tsp_mapping[i][0].get_exit_info(tsp_mapping[i][1])
q1 = tsp_mapping[j][0].get_entrance_info(tsp_mapping[j][1])
# Check for collisions
x0 = q0[0]; y0 = q0[1]
x1 = q1[0]; y1 = q1[1]
if has_collision(P, [(x0, y0), (x1, y1)]):
length = 1000000
else:
length = sqrt((x1-x0)**2+(y1-y2)**2)
cost[i][j] = length
# Generate a cluster information list
cluster_list = []
for i in range(num_nodes):
cluster_list.append([i])
return cost, cluster_list
def has_collision(P, edge):
exterior = LinearRing(P[0])
holes = P[1]
segment = LineString(edge)
if exterior.intersects(segment): return True
for hole in holes:
interior = LinearRing(hole)
if interior.intersects(segment): return True
return False
if __name__ == '__main__':
if __package__ is None:
import os, sys
sys.path.insert(0, os.path.abspath("../.."))
from pkg.discritizers import classes
else:
import classes