-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptimization_comparison.py
executable file
·151 lines (125 loc) · 5.02 KB
/
optimization_comparison.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
from voronoi.voronoi import GeneralizedVoronoi
from voronoi.geometry import *
from voronoi.astar import Astar
from voronoi.image import PolygonDetector
import matplotlib.pyplot as plt
from datetime import datetime
def print_result(file, start, end, result, show=True):
print('|----------------------------------------------------------|')
print('| ' + str(file + ' - ' + str(start) + ' > ' + str(end)).ljust(56) + ' |')
print('|----------------------------------------------------------|')
print('| Fig | epsilon | seconds | vertices | distance | obstacle |')
print('|-----|---------|---------|----------|----------|----------|')
for i in range(len(result)):
ele = result[i]
print('| ' + str(i+1).rjust(3) + ' | '
+ '{:1.4f}'.format(ele[0]) + ' | '
+ str(ele[1].seconds).rjust(3) + "."
+ str(int(ele[1].microseconds / 1000)).zfill(3) + ' | '
+ str(ele[2]).rjust(8) + ' | '
+ '{:1.6f}'.format(ele[3]) + ' | '
+ '{:1.6f}'.format(ele[4]) + ' | ')
print('|----------------------------------------------------------|')
if show:
plt.show()
def image_detect(path, start, end, show=True):
# boundary
b1 = Line([[0.0, 0.0], [1.0, 0.0]])
b2 = Line([[1.0, 0.0], [1.0, 1.0]])
b3 = Line([[1.0, 1.0], [0.0, 1.0]])
b4 = Line([[0.0, 0.0], [0.0, 1.0]])
# polygon detector
pd = PolygonDetector(path, [214, 255])
polygons = pd.run(bound=[1.0, 1.0])
if show:
pd.generate_plot()
pd.show()
# voronoi
vor = GeneralizedVoronoi()
vor.add_polygons(polygons)
vor.add_boundaries([b1, b2, b3, b4])
vor.add_points([start, end])
return vor
def voronoi(epsilon, vor, start, end, result, show=True):
GeneralizedVoronoi.rdp_epsilon = epsilon
start_t = datetime.now()
# voronoi
vor_result = vor.run_optimized()
# astar
astar = Astar(vor_result, start, end)
astar_result = astar.run()
end_t = datetime.now()
if show:
astar.generate_plot()
vertices_count = len(vor_result.vertices)
# distance
dist = total_distance(astar_result)
dist_obt = min_distance_from_obstacle(vor_result)
result.append((epsilon, end_t - start_t, vertices_count, dist, dist_obt))
print('epsilon ' + str(epsilon) + ' done.')
if __name__ == '__main__':
Line.point_distance = 0.015
Triangle.distance_trash = 0.01
map = 4
if map == 0:
PolygonDetector.rdp_epsilon = 0.01
PolygonDetector.area_threshold = 400
PolygonDetector.gray_thresh_boundary = 3
elif map == 1:
PolygonDetector.rdp_epsilon = 0.025
PolygonDetector.area_threshold = 400
PolygonDetector.gray_thresh_boundary = 5
elif map == 2:
PolygonDetector.rdp_epsilon = 0.025
PolygonDetector.area_threshold = 400
PolygonDetector.gray_thresh_boundary = 5
elif map == 3:
PolygonDetector.rdp_epsilon = 0.01
PolygonDetector.area_threshold = 400
PolygonDetector.gray_thresh_boundary = 5
elif map == 4:
PolygonDetector.rdp_epsilon = 0.002
PolygonDetector.area_threshold = 400
PolygonDetector.gray_thresh_boundary = 5
elif map == 5:
PolygonDetector.rdp_epsilon = 0.005
PolygonDetector.area_threshold = 400
PolygonDetector.gray_thresh_boundary = 5
elif map == 6:
PolygonDetector.rdp_epsilon = 0.001
PolygonDetector.area_threshold = 400
PolygonDetector.gray_thresh_boundary = 5
path = './testdata/'
file = 'map' + str(map) + '.png'
start_points = [[0.05, 0.05], [0.05, 0.95], [0.55, 0.05], [0.05, 0.6]]
end_points = [[0.9, 0.9], [0.95, 0.05], [0.4, 0.95], [0.95, 0.45]]
for i in range(len(start_points)):
if map == 4:
start = [0.1, 0.075]
end = [0.1, 0.9]
elif map == 5:
start = [0.05, 0.05]
end = [0.5, 0.5]
elif map == 6:
start = [0.05, 0.95]
end = [0.95, 0.05]
else:
start = start_points[i]
end = end_points[i]
vor = image_detect(path+file, start, end, False)
result = []
#voronoi(0.0000, vor, start, end, result, True)
#voronoi(0.0002, vor, start, end, result, True)
#voronoi(0.0004, vor, start, end, result, True)
#voronoi(0.0008, vor, start, end, result, True)
#voronoi(0.0016, vor, start, end, result, True)
#voronoi(0.0032, vor, start, end, result, True)
voronoi(0.0064, vor, start, end, result, True)
#voronoi(0.0128, vor, start, end, result, True)
#voronoi(0.0256, vor, start, end, result, True)
#voronoi(0.0500, vor, start, end, result, True)
#vor.generate_plot_only_points()
print_result(file, start, end, result, False)
plt.show()
if map > 3: break
plt.show()