-
Notifications
You must be signed in to change notification settings - Fork 0
/
corridor.py
executable file
·245 lines (215 loc) · 9.5 KB
/
corridor.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
from air_corridor.d3.geometry.geom3d import Cylinder, newTorus
from air_corridor.tools.util import *
'''
corridors={'A':{'object':Torus,'connections':{'B':LineSegment,'D':LineSegment }},
'B':{},
'C':{}}
corridors=[{'object':Torus,'connections':{'B':LineSegment,'D':LineSegment }},
{ },
{ }]
* corridors
- object:
-- Torus_1
- connections:
-- LineSegment_1
-- LineSegment_2
'''
class Corridor:
# all_corridors = []
graph = None
# consider_next_corridor = False
num_corridor_in_state = 1
def __init__(self, name, connections, reduce_space):
# Initialize corridor properties
self.name = name
self.connections = connections
self.reduce_space = reduce_space
@classmethod
def convert2graph(cls, corridors):
cls.graph = {}
for name, one_corridor in corridors.items():
cls.graph[name] = one_corridor.connections
return cls.graph
def evaluate_action(self, a_uav, alignment=1, crossed=False):
'''
alignement [-1,1]
corssed [False, True]
'''
reward = PENALTY_TIME
# all specified corridors follow dual inheritance, is_inside is from geom
# a=self.is_inside(a_uav.next_position)
flag, ststus = self.is_inside(a_uav.next_position)
if flag:
# reward += aligned * REWARD_POSITIVE_STEP
pass
elif crossed:
'''
whether consider the next corridor, considering two corridors' reward as one episode
reward only make sense during training, here is trained with considering two corridors.
'''
path = a_uav.enroute['path']
if path[-1] == self.name:
a_uav.status = 'won'
reward += REWARD_REACH
else:
reward += REWARD_INTERMEDIA
path_index = path.index(self.name)
a_uav.enroute['current'] = path[path_index + 1]
reward += alignment * REACH_ALIGNMENT
else:
# breach boundary
# a_uav.outside_counter += 1
# reward += PENALTY_BREACH
# if a_uav.outside_counter > BREACH_TOLERANCE:
# a_uav.status = 'breached'
reward += PENALTY_BREACH
a_uav.status = ststus
return reward
class CylinderCorridor(Corridor, Cylinder):
def __init__(self,
anchor_point,
length,
width,
name,
connections,
reduce_space,
orientation_rad=None,
orientation_vec=None, ):
Corridor.__init__(self, name, connections, reduce_space)
self.radius = width / 2
Cylinder.__init__(self,
anchor_point=anchor_point,
orientation_vec=orientation_vec,
orientation_rad=orientation_rad,
length=length,
radius=self.radius)
self.shapeType = [1, 0]
self.reduce_space = reduce_space
def evaluate_action(self, a_uav):
alignment = 0
cross_flag = self.endCirclePlane.cross_circle_plane(line_start=a_uav.position,
line_end=a_uav.next_position)
if cross_flag:
alignment = align_measure(end=a_uav.next_position, start=a_uav.position, direction=self.orientation_vec)
reward = super().evaluate_action(a_uav, alignment=alignment, crossed=cross_flag)
return reward
def render_self(self, ax):
def cylinder(r, h, theta_res=100, z_res=100):
theta = np.linspace(0, 2 * np.pi, theta_res)
z = np.linspace(0, h, z_res)
theta, z = np.meshgrid(theta, z)
x = r * np.cos(theta)
y = r * np.sin(theta)
return x, y, z
Xc, Yc, Zc = cylinder(self.radius, self.length)
x_rot, y_rot, z_rot = [], [], []
for a, b, c in zip(Xc, Yc, Zc):
x_p, y_p, z_p = np.dot(self.rotation_matrix, np.array([a, b, c]))
x_rot.append(x_p)
y_rot.append(y_p)
z_rot.append(z_p)
ax.plot_surface(np.array(x_rot), np.array(y_rot), np.array(z_rot), edgecolor='royalblue', lw=0.1, rstride=20,
cstride=8, alpha=0.3)
@lru_cache(maxsize=8)
def report(self, base=None):
# 7+2+4=13,
# last two 0 are padding, keeping the format the same as
if self.reduce_space:
common_part = super().report(base)
corridor_status = common_part + self.shapeType + [self.length, self.radius] + [0] * 4
# if self == base:
# # 8 elements
# # 4 for the rest
# # 4 for padding
# # 16 intotal
# common_part = super().report(base)
# corridor_status = common_part + self.shapeType + [self.length, self.radius] + [0] * 4
#
# else:
# corridor_status = self.shapeType + list(Z_UNIT) + [self.length, self.radius] + [0] * 6
else:
corridor_status = (self.shapeType + list(self.orientation_vec) +
[self.length, self.radius] +
[0] * 6)
if any(np.isnan(corridor_status)):
print('nan in cylinder')
input("Press Enter to continue...")
return corridor_status
def release_uav(self, plane_offset_assigned):
plane_offset = self.x * plane_offset_assigned[0] + self.y * plane_offset_assigned[1]
direction_offset = (0.2 - self.length / 2) * self.orientation_vec
return self.anchor_point + plane_offset + direction_offset
class DirectionalPartialTorusCorridor(Corridor, newTorus):
def __init__(self,
anchor_point: np.ndarray,
major_radius: float,
minor_radius: float,
begin_rad: float,
end_rad: float,
orientation_rad=None,
orientation_vec=None,
name=None,
connections=None,
reduce_space=True):
Corridor.__init__(self, name, connections, reduce_space)
newTorus.__init__(self,
anchor_point=anchor_point,
orientation_vec=orientation_vec,
orientation_rad=orientation_rad,
major_radius=major_radius,
minor_radius=minor_radius,
begin_rad=begin_rad,
end_rad=end_rad)
assert -np.pi <= self.begin_rad <= np.pi, "Error, begin radian needs to be in [-pi,pi]"
self.shapeType = [0, 1]
def evaluate_action(self, a_uav):
alignment = 0
cross_flag = self.endCirclePlane.cross_circle_plane(line_start=a_uav.position,
line_end=a_uav.next_position)
if cross_flag:
positive_direction = self.determine_positive_direction(a_uav.position)
alignment = align_measure(end=a_uav.next_position, start=a_uav.position, direction=positive_direction)
reward = super().evaluate_action(a_uav, alignment=alignment, crossed=cross_flag)
return reward
@lru_cache(maxsize=8)
def report(self, base=None):
if self.reduce_space:
# 8 elements for common
# 8 for the rest
# 16 intotal
common_part = super().report(base)
corridor_status = common_part + self.shapeType + \
[self.major_radius, self.minor_radius, np.pi / 2 - (self.end_rad - self.begin_rad),
np.pi / 2, self.major_radius + self.minor_radius,
self.major_radius - self.minor_radius]
else:
corridor_status = self.shapeType + list(self.directionRad) + list(self.orientation_vec) + \
[self.major_radius, self.minor_radius, self.begin_rad, self.end_rad,
self.major_radius + self.minor_radius, self.major_radius - self.minor_radius]
if any(np.isnan(corridor_status)):
print('nan in torus')
input("Press Enter to continue...")
return corridor_status
def release_uav(self, plane_offset_assigned):
plane_offset = self.beginCirclePlane.x * plane_offset_assigned[0] + \
self.beginCirclePlane.y * plane_offset_assigned[1]
direction_offset = 0.2 * self.beginCirclePlane.orientation_vec
return self.beginCirclePlane.anchor_point + plane_offset + direction_offset
def render_self(self, ax):
def torus(R, r, R_res=100, r_res=100):
u = np.linspace(0, 1.5 * np.pi, R_res)
v = np.linspace(0, 2 * np.pi, r_res)
u, v = np.meshgrid(u, v)
x = (R + r * np.cos(v)) * np.cos(u)
y = (R + r * np.cos(v)) * np.sin(u)
z = r * np.sin(v)
return x, y, z
Xc, Yc, Zc = torus(self.major_radius, self.minor_radius)
x_rot, y_rot, z_rot = [], [], []
for a, b, c in zip(Xc, Yc, Zc):
x_p, y_p, z_p = np.dot(self.rotation_matrix, np.array([a, b, c]))
x_rot.append(x_p)
y_rot.append(y_p)
z_rot.append(z_p)
ax.plot_surface(np.array(x_rot), np.array(y_rot), np.array(z_rot), edgecolor='royalblue', lw=0.1, rstride=20,
cstride=8, alpha=0.3)