forked from mingujeon/NLoSVehicleLocalization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleFilter_Site1.py
571 lines (453 loc) · 19.5 KB
/
ParticleFilter_Site1.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import numpy as np
import random
import math
import os
from copy import deepcopy
from shapely.geometry import LineString, Point
from shapely.affinity import scale
from itertools import permutations, combinations
import matplotlib
matplotlib.use("AGG")
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
def mirror_point(point, line, max_scale=100):
"""
mirroring point w.r.t line segment
"""
line_long = scale(line, xfact=max_scale, yfact=max_scale)
p_inter = line_long.interpolate(line_long.project(point))
p_new = Point(2 * p_inter.x - point.x , 2 * p_inter.y - point.y)
return p_new
def measurement(z, angle, n):
idx = (angle/math.pi+0.5)*n
idx_u = min(math.floor(idx+0.5), n-1)
idx_l = max(math.floor(idx-0.5), 0)
z_inter = z[idx_u] * (idx-idx_l-0.5) + z[idx_l] * (idx_u+0.5-idx)
return z_inter
class Particle:
def __init__(self, weight_rate):
self.weight = weight_rate
self.w_direct = weight_rate[0]
self.w_reflect_1st = weight_rate[1]
self.w_reflect_2nd = weight_rate[2]
self.w_diffract = weight_rate[3]
# print('weight_rate: ', weight_rate)
def change_weight_rate(self, weight_rate) :
self.weight = weight_rate
self.w_direct, self.w_reflect_1st, self.w_reflect_2nd, self.w_diffract = self.weight
def random_initialize(self, _map):
"""
Randomly initialize particle state
Depend on map(only initialize at unknown region)
"""
self.x = random.uniform(-_map.road_range[0]/2, _map.road_range[0]/2)
self.y = random.uniform(0, _map.road_range[1]) + _map.road_range[2]
self.head = random.choice([0, math.pi])
# self.head = random.uniform(0,2*math.pi)
self.v = random.random()*3 # 0~10m/s
self.w_head = self.v/2.8*math.tan(math.radians((random.random()-0.5)*10))
def set_weight(self):
self.w_direct, self.w_reflect_1st, self.w_reflect_2nd, self.w_diffract = self.weight
def move(self, t):
"""
Move given time step
Langevin's model
Noise level
"""
self.x = self.x + self.v * math.cos(self.head) * t
self.y = self.y + self.v * math.sin(self.head) * t
self.head = self.head + self.w_head * t
self.v = self.v + random.normalvariate(0,1)
def measure_prob_addition(self, _map, z, weight_rate):
"""
Measuring Pseudo Likelihood
"""
self.change_weight_rate(weight_rate)
# print(self.weight)
p = Point(self.x, self.y)
# particle go out of map
if self.x > _map.width/2 or self.x < -_map.width/2 or self.y > _map.height or self.y < _map.road_range[2] + 0.2 :
return 0
direct = _map.direct(p)
w_direct, w_reflect_1st, w_reflect_2nd, w_diffract = self.w_direct, self.w_reflect_1st, self.w_reflect_2nd, self.w_diffract
prob_list = []
# direct
if len(direct) > 0:
prob_list += [measurement(z, direct[1], z.shape[0]) * w_direct]
reflect_1st = _map.reflect_1st(p)
for r in reflect_1st:
prob_list += [measurement(z, r[1], z.shape[0]) * w_reflect_1st]
else:
reflect_1st = _map.reflect_1st(p)
for r in reflect_1st:
prob_list += [measurement(z, r[1], z.shape[0]) * w_reflect_1st]
reflect_2nd = _map.reflect_2nd(p)
for r in reflect_2nd:
prob_list += [measurement(z, r[1], z.shape[0]) * w_reflect_2nd]
diffract = _map.diffract(p)
for di in diffract:
prob_list += [measurement(z, di[1], z.shape[0]) * w_diffract]
prob_list.sort(reverse=True)
prob = np.sum(prob_list)
return prob
class Map:
"""
Design map
"""
def __init__(self, map_info):
self.width = map_info['width']
self.height = map_info['height']
self.walls = map_info['walls']
self.corners = map_info['corners']
self.thres = map_info['diffraction_angle_threshold']
self.road_range = map_info['road_range']
self.front_range = map_info['front_range']
self.front_height = map_info['front_height']
self.ego_vehicle = Point(0,0)
def move(self):
"""
Only for dynamic situation
"""
assert NotImplementedError
def direct(self, p, walls=None, vis=False):
"""
Direct acoustic ray direction
"""
if walls is None:
walls = self.walls
l = LineString([p, self.ego_vehicle])
for wall in walls:
if l.intersects(wall):
return []
r = math.sqrt((self.ego_vehicle.x-p.x)**2 + (self.ego_vehicle.y-p.y)**2)
theta = math.atan(p.x/p.y)
if not vis:
return [r , theta]
else:
return [r, theta], [l]
def direct_line(self, l, walls=None):
if walls is None:
walls = self.walls
for wall in walls:
if l.intersects(wall):
return False
return True
def reflect_1st(self, p, vis=False):
"""
1st-order reflected acoustic ray directions
"""
reflects = []
lines = []
for wall in self.walls:
p_prime = mirror_point(p, wall)
l = LineString([self.ego_vehicle, p_prime])
if l.intersects(wall):
p_inter = l.intersection(wall)
l1 = LineString([p, p_inter])
l2 = LineString([p_inter, self.ego_vehicle])
walls = [w for w in self.walls if w != wall]
if self.direct_line(l1, walls):
loc = self.direct(p_inter, walls)
if len(loc) > 0:
reflects.append(loc)
lines += [l1,l2]
if not vis:
return reflects
else:
return reflects, lines
def reflect_2nd(self,p, vis=False):
"""
2nd-order reflected acoustic ray directions
"""
reflects = []
lines = []
for wall1, wall2 in list(permutations(self.walls,2)):
p_prime = mirror_point(p, wall1)
o_prime = mirror_point(self.ego_vehicle, wall2)
l = LineString([o_prime, p_prime])
if l.intersects(wall1) and l.intersects(wall2):
p_inter_1 = l.intersection(wall1)
p_inter_2 = l.intersection(wall2)
l1 = LineString([p, p_inter_1])
l2 = LineString([p_inter_1, p_inter_2])
l3 = LineString([p_inter_2, self.ego_vehicle])
walls_1 = [w for w in self.walls if w != wall1]
walls_2 = [w for w in self.walls if w != wall2 and w != wall1]
if self.direct_line(l1, walls_1):
if self.direct_line(l2, walls_2):
loc = self.direct(p_inter_2, walls_2)
if len(loc) > 0:
reflects.append(loc)
lines += [l1,l2,l3]
if not vis:
return reflects
else:
return reflects, lines
def diffract(self, p, vis=False):
"""
Diffracted acoustic ray direction
thres : maximum theta that is possible to diffract
"""
diffracts = []
lines = []
if not self.direct_line(LineString([p, self.ego_vehicle])):
for i,c in enumerate(self.corners):
theta_corner = abs(math.atan(c.x/c.y))
theta_p = abs(math.atan(p.x/p.y))
if theta_p > theta_corner:
corner = Point(c.x, c.y+random.random())
v1 = [corner.x-p.x, corner.y-p.y]
v2 = [self.ego_vehicle.x-corner.x, self.ego_vehicle.y-corner.y]
theta = math.acos((v1[0]*v2[0] + v1[1]*v2[1])/(math.sqrt(v1[0]**2+v1[1]**2)*math.sqrt(v2[0]**2+v2[1]**2)))
if theta < self.thres:
l1 = LineString([p, corner])
l2 = LineString([corner, self.ego_vehicle])
if self.direct_line(l1):
loc = self.direct(corner)
if len(loc) > 0:
diffracts.append(loc)
lines += [l1, l2]
if not vis:
return diffracts
else:
return diffracts, lines
class ParticleFilter:
def __init__(self, n, _map, epsilon=0.05, weight_rate=[1,0.5,0.5,0.3]):
self.n = n
self.weight_rate = weight_rate
self.particles = [Particle(self.weight_rate) for _ in range(self.n)]
self.weight = np.array([1/self.n for _ in range(self.n)])
self.time = 0
self.epsilon = epsilon
self.Map = _map
# print(self.Map)
self.prediction = None
self.Max_prediction = None
self.bbox_height = 3
self.bbox_width = 5
self.init_particles()
def init_particles(self):
"""
Initialize particles.
Use init instance when you want to set particle to specific state
"""
for particle in self.particles:
particle.random_initialize(self.Map)
def propagate(self, t):
"""
Motion propagation of particles
t : time-step
"""
for particle in self.particles:
particle.move(t)
self.time += t
def update(self, z, weight_rate):
"""
Motion update for each particles
"""
for i in range(self.n):
self.weight[i] = max(self.particles[i].measure_prob_addition(self.Map ,z, weight_rate), 0)
if max(self.weight) == min(self.weight):
self.weight = np.ones_like(self.weight)/self.weight.shape[0]
self.predict()
def resample(self):
"""
Resampling
Random particle with probability of epsilon
"""
k = int((self.n)*(1-self.epsilon))
sample_idx = random.choices(population=range(0,self.n), weights=self.weight, k=k)
particles = []
for idx in sample_idx:
particles.append(deepcopy(self.particles[idx]))
# random particle with probability of epsilon
for _ in range(self.n-k):
p = Particle(self.weight_rate)
p.random_initialize(self.Map)
particles.append(p)
self.particles = particles
def predict(self):
"""
Predict sound source location
"""
idx = np.argmax(self.weight)
self.prediction = deepcopy(self.particles[idx])
self.Max_prediction = deepcopy(self.particles[idx])
# avg_with_intensity -> x, y
self.prediction.x = sum(x_val.x * w_val for x_val, w_val in zip(self.particles, self.weight)) / sum(self.weight)
self.prediction.y = sum(y_val.y * w_val for y_val, w_val in zip(self.particles, self.weight)) / sum(self.weight)
# prediction range
self.prediction.x = max(-self.Map.width/2, min(self.Map.width/2, self.prediction.x))
def distance(self,x1,x2,y1,y2) :
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
def distance_rect(self, x1,x2, y1,y2,width,height) :
if x1 <= x2 and x2 <= x1 + width :
return 0
else :
return min(abs(x2-x1),abs(x2+width - x1))
def average_particle(self, x_points, y_points) :
return round(sum(x_points) / len(x_points),2), round(sum(y_points)/len(y_points),2)
def average_particle_with_intensity(self, x_points, y_points, w) :
return round(sum(x_val * w_val for x_val, w_val in zip(x_points, w)) / sum(w),2), round(sum(y_val * w_val for y_val, w_val in zip(y_points, w)) / sum(w),2)
def render(self, save_dir, is_ray = False):
"""
Rendering
"""
fig, ax = plt.subplots()
# plot map
x_map = [-self.Map.width/2, self.Map.width/2, self.Map.width/2, -self.Map.width/2, -self.Map.width/2]
y_map = [0,0,self.Map.height, self.Map.height,0]
ax.plot(x_map,y_map, color='black', linewidth=1.0)
left_x_fill = [-self.Map.width / 2, self.Map.front_range[0], self.Map.front_range[0], -self.Map.width / 2]
right_x_fill = [self.Map.front_range[1], self.Map.width / 2, self.Map.width / 2, self.Map.front_range[1]]
y_fill = [0, 0, self.Map.front_height[0], self.Map.front_height[0]]
ax.fill(left_x_fill, y_fill, 'gray', alpha=0.5)
ax.fill(right_x_fill, y_fill, 'gray', alpha=0.5)
if len(self.Map.walls) == 4:
square_y = 6.5
else :
x_upper_fill = [-self.Map.width / 2, self.Map.width / 2, self.Map.width / 2, -self.Map.width / 2]
y_upper_fill = [self.Map.front_height[1], self.Map.front_height[1], self.Map.height, self.Map.height]
ax.fill(x_upper_fill, y_upper_fill, 'gray', alpha=0.5)
for wall in self.Map.walls:
x_wall, y_wall = wall.xy
ax.plot(x_wall, y_wall, color='black', linewidth=1.0)
# ego_vehicle
ax.plot([0],[0], 'o',color='black', markersize=3)
# ax.text(0, -0.2, 'ego vehicle', ha='center', va='top', fontsize=9)
# particles
x_p, y_p, dx_p, dy_p, weight_p = [], [], [], [], []
for particle in self.particles:
if -self.Map.width/2 < particle.x < self.Map.width/2 and self.Map.road_range[2] < particle.y < self.Map.height :
x_p.append(particle.x)
y_p.append(particle.y)
dx_p.append(particle.v * math.cos(particle.head))
dy_p.append(particle.v * math.sin(particle.head))
ax.scatter(x_p, y_p, s=10, alpha=0.3, c='red')
# prediction visualization -> Avg intensity
ax.plot(self.prediction.x, self.prediction.y, c=(0,1,0,1), marker='o',markersize=7)
# prediction visualization -> Max intensity
# ax.plot(self.Max_prediction.x, self.Max_prediction.y, 'ro', markersize=7)
if is_ray: # ploting ray
p = Point(self.prediction.x, self.prediction.y)
# p = Point(self.Max_prediction.x, self.Max_prediction.y)
direct = []
try:
_,direct = self.Map.direct(p, vis=True)
except:
pass
_,reflect_1st = self.Map.reflect_1st(p, vis=True)
_,reflect_2nd = self.Map.reflect_2nd(p, vis=True)
_,diffract = self.Map.diffract(p, vis=True)
for i,l in enumerate(direct):
x,y = l.xy
ax.plot(x,y, c=(0,0,0,1), alpha=0.3, label="direct" if i==0 else "") # black
for i,l in enumerate(reflect_1st):
x,y = l.xy
ax.plot(x,y, c=(0,1,1,1), alpha=0.3, label="1st reflect" if i==0 else "") # yellow
for i,l in enumerate(reflect_2nd):
x,y = l.xy
ax.plot(x,y, c=(0,1,0,1), alpha=0.3, label="2nd reflect" if i==0 else "") # green
if len(direct) == 0:
for i,l in enumerate(diffract):
x,y = l.xy
ax.plot(x,y, c=(0,0,1,1), alpha=0.3, label="diffract" if i==0 else "") # blue
plt.axis("equal")
plt.title("%.2f sec"%self.time)
# plt.xlim(-self.Map.width/2, self.Map.width/2)
# plt.ylim(0, self.Map.height)
# plt.xticks(range(int(-self.Map.width/2), int(self.Map.width/2)+1, 5))
# plt.yticks(range(0,self.Map.height+1,5))
plt.xticks([])
plt.yticks([])
plt.savefig(os.path.join(save_dir,'test_%06d.png'%(self.time*100)), bbox_inches = 'tight', pad_inches = 0)
plt.close()
class ParticleFilter_FIX(ParticleFilter):
"""
Particle with uniform spreaden fixed particles.
"""
def __init__(self, n, _map, epsilon):
super().__init__(n, _map, epsilon)
def init_fixed_particles(self):
"""
Initialize fixed particles.
Use init instance when you want to set particle to specific state
"""
self.w,self.h,self.h0 = self.Map.road_range
self.n = (int(self.w)+1)*(int(self.h)+1)
self.particles = [Particle() for _ in range(self.n)]
self.weight = np.array([1/self.n for _ in range(self.n)])
self.time = 0
for i,p in zip(range(self.n),self.particles):
wi = int(i / (int(self.h)+1))
hi = int(i % (int(self.h)+1))
p.x = wi * (self.w/int(self.w)) - self.w/2
p.y = self.h0 + hi * (self.h/int(self.h))
# self.head = random.uniform(0, 2*math.pi)
p.head = random.choice([0, math.pi])
p.v = random.random()*3 # 0~10m/s
# self.w_head = (random.random()-0.5)
p.w_head = 0
def set_weight(self, weight):
for p in self.particles:
p.set_weight(weight)
def propagate(self, t):
self.time += t
def trick(self):
for i in range(self.w):
self.weight[i*self.h:(i+1)*self.h] = np.average(self.weight[i*self.h:(i+1)*self.h])
def predict(self):
"""
Predict sound source location
"""
# get particle mean
idx = np.argmax(self.weight)
particle = self.particles[idx]
self.prediction = deepcopy(particle)
theta = math.atan(particle.x/particle.y)
# theta = math.atan2(self.particles[idx].x, self.particles[idx].y)
theta_left = math.atan(self.Map.corners[0].x/self.Map.corners[0].y)
theta_right = math.atan(self.Map.corners[1].x/self.Map.corners[1].y)
# print(np.rad2deg(theta), np.rad2deg(theta_left), np.rad2deg(theta_right))
if theta < theta_left:
return 'left'
elif theta > theta_right:
return 'right'
else:
return 'front'
def render(self, save_dir, name):
"""
Rendering
"""
fig, ax = plt.subplots()
# plot map
x_map = [-self.Map.width/2, self.Map.width/2, self.Map.width/2, -self.Map.width/2, -self.Map.width/2]
# x_map = [-self.Map.width_left, self.Map.width_right, self.Map.width_right, -self.Map.width_left, -self.Map.width_left]
y_map = [0,0,self.Map.height, self.Map.height,0]
ax.plot(x_map,y_map, color='black', linewidth=3.0)
for wall in self.Map.walls:
x_wall, y_wall = wall.xy
ax.plot(x_wall, y_wall, color='black', linewidth=2.0)
# ego_vehicle
ax.plot([0],[0], 'bo', markersize=10)
# particles
x_p, y_p, dx_p, dy_p = [], [], [], []
for particle, w in zip(self.particles, self.weight):
color = [[1,0.9*(1-w/self.weight.max()),0.9*(1-w/self.weight.max()),1]]
s = 40 * w / self.weight.max()
ax.scatter([particle.x], [particle.y], s=s, c=color)
plt.axis("equal")
plt.axis('off')
plt.title(name)
# plt.xlim(-self.Map.width_left, self.Map.width_right)
# plt.ylim(0, self.Map.height)
# plt.xticks(range(int(-self.Map.width_left), int(self.Map.width_right)+1, 5))
# plt.yticks(range(0,self.Map.height+1,5))
# plt.savefig(os.path.join(save_dir,'test_{}.png'.format(name)))
# plt.close()
plt.xlim(-self.Map.width/2, self.Map.width/2)
plt.ylim(0, self.Map.height)
plt.xticks(range(int(-self.Map.width/2), int(self.Map.width/2)+1, 5))
plt.yticks(range(0,self.Map.height+1,5))
plt.savefig(os.path.join(save_dir,'test_{}.png'.format(name)))
plt.close()