-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
705 lines (625 loc) · 37.4 KB
/
Copy pathutil.py
File metadata and controls
705 lines (625 loc) · 37.4 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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
"""
%% ------------------------------ Gaussian Mixture(GM) Probability Hypothesis Density(PHD) filter ------------------------------ %%
This Python code is reproduction for the "point target GM-PHD filter" originally proposed in paper [1], with assumption
of no target spawning. The original Matlab code for "point target GM-PHD filter" could be available from authors website
http://ba-tuong.vo-au.com/codes.html
%% ----------------------------------- Reference Papers ------------------------------------------ %%
% [1] 2006. B.-N. Vo, W.-K. Ma, "The Gaussian Mixture Probability Hypothesis Density Filter", IEEE Transactions on Signal
Processing
"""
import numpy as np
import copy
from scipy.stats import multivariate_normal
import math
import numpy as np
import numpy.matlib
import matplotlib.pyplot as plt
import time
import pickle
import argparse
"""
Parse Arguments
"""
def parse_args():
parser = argparse.ArgumentParser()
#parser.add_argument('--path_to_save_results', default='D:/Tech_Resource/Paper_Resource/Signal Processing in General/RFS Filter/PHD Filter/GM_PHD_PointTarget_Python_Demo/',type=str, help="path to result folder")
parser.add_argument('--path_to_save_results', default='/gs/home/zhubing/Radar_Perception_Project/Project_3/',type=str, help="path to result folder")
parser.add_argument('--scenario', default='scenario1/',type=str, help="path to scenario folder")
parser.add_argument('--Bayesian_filter_config', default='Kalman', type=str, help='Config the Bayesian filter used inside filter')
parser.add_argument('--motion_model_type', default='Constant Velocity',type=str, help='Config the motion_model_type used inside filter')
parser.add_argument('--simulation_scenario', default="No Intersection Varying Cardinality", type=str, help='scenario for the simulation')
parser.add_argument('--number_of_monte_carlo_simulations', default=100,type=int, help='number_of_monte_carlo_simulations')
parser.add_argument('--n_scan', default=101,type=int, help='number frames per simulation')
# choose display configuration
# silent mode = True : does not display the figure, only print out pertinent information
# silent mode = False: display figure and print out information
parser.add_argument('--plot', default=False, type=bool, help='choose if plot')
return parser.parse_args()
"""
Ultility functions
"""
def gen_ground_truth_parameters():
"""
This is the configuration file for all parameters used in GM-PHD filter model, which is used to tracking the multi-targets.
"""
ground_truth_parameters = {} # model is the dictionary which has all the corresponding parameters of the generated model
T = 1.0 # Sampling period, time step duration between two scans(frames).
# Dynamic motion model parameters(The motion used here is Constant Velocity (CV) model):
# State transition matrix, F_k.
# F_k = np.array([
# [1, 0, T_s, 0],
# [0, 1, 0, T_s],
# [0, 0, 1, 0],
# [0, 0, 0, 1],
# ])
ground_truth_parameters['F_k'] = np.eye(4, dtype=np.float64)
I = T*np.eye(2, dtype=np.float64)
ground_truth_parameters['F_k'][0:2, 2:4] = I
sigma_v = 0.1 # Standard deviation of the process noise.
Q1 = np.array([[T ** 4 / 4, T ** 3 / 2], [T ** 3 / 2, T ** 2]], dtype=np.float64)
Q = np.zeros((4, 4), dtype=np.float64)
Q[np.ix_([0, 2], [0, 2])] = Q1
Q[np.ix_([1, 3], [1, 3])] = Q1
ground_truth_parameters['Q_k'] = sigma_v ** 2 * Q # Covariance of process noise
# Initial state covariance matrix, P_k.
P_k = np.diag([150**2,150**2, 1**2, 1**2])
ground_truth_parameters['P_k'] = np.array(P_k, dtype=np.float64)
# Observation/Measurement model parameters (noisy x and y only rather than v_x, v_y):
ground_truth_parameters['H_k'] = np.array([[1., 0, 0, 0], [0, 1., 0, 0]], dtype=np.float64) # Observation model matrix.
sigma_r = 1 # Standard deviation of the measurement noise.
ground_truth_parameters['R_k'] = sigma_r ** 2 * np.eye(2, dtype=np.float64) # Covariance of observation noise (change with the size of detection?).
# Measurements parameters. See equation (20) in [1].
ground_truth_parameters['p_D'] = 0.90 # Probability of measurements of targets(The probability target could be detected, so probability of miss-detected of targets is 1 - p_D)
# Compute clutter intensity. See equation (47) and corresponding explanation in [1].
average_number_of_clutter_per_frame = 10
x_range = [0, 300] # X range of measurements
y_range = [0, 300] # Y range of measurements
A = (x_range[1] - x_range[0])*(y_range[1]-y_range[0]) # Size of area.
clutterIntensity = average_number_of_clutter_per_frame/A # Generate clutter intensity (clutter intensity lambda_c = lambda_t/A)
ground_truth_parameters['clutterIntensity'] = clutterIntensity
ground_truth_parameters['xrange'] = x_range
ground_truth_parameters['yrange'] = y_range
ground_truth_parameters['average_number_of_clutter_per_frame']= average_number_of_clutter_per_frame
return ground_truth_parameters
def gen_filter_model():
"""
This is the configuration file for all parameters used in GM-PHD filter model, which is used to tracking the multi-targets.
"""
filter_model = {} # model is the dictionary which has all the corresponding parameters of the generated model
T = 1.0 # Sampling period, time step duration between two scans(frames).
# Dynamic motion model parameters(The motion used here is Constant Velocity (CV) model):
# State transition matrix, F_k.
# F_k = np.array([
# [1, 0, T_s, 0],
# [0, 1, 0, T_s],
# [0, 0, 1, 0],
# [0, 0, 0, 1],
# ])
filter_model['F_k'] = np.eye(4, dtype=np.float64)
I = T*np.eye(2, dtype=np.float64)
filter_model['F_k'][0:2, 2:4] = I
sigma_v = 0.1 # Standard deviation of the process noise.
Q1 = np.array([[T ** 4 / 4, T ** 3 / 2], [T ** 3 / 2, T ** 2]], dtype=np.float64)
Q = np.zeros((4, 4), dtype=np.float64)
Q[np.ix_([0, 2], [0, 2])] = Q1
Q[np.ix_([1, 3], [1, 3])] = Q1
filter_model['Q_k'] = sigma_v ** 2 * Q # Covariance of process noise
# Initial state covariance matrix, P_k.
P_k = np.diag([150**2,150**2, 1**2, 1**2])
filter_model['P_k'] = np.array(P_k, dtype=np.float64)
# Observation/Measurement model parameters (noisy x and y only rather than v_x, v_y):
filter_model['H_k'] = np.array([[1., 0, 0, 0], [0, 1., 0, 0]], dtype=np.float64) # Observation model matrix.
sigma_r = 1 # Standard deviation of the measurement noise.
filter_model['R_k'] = sigma_r ** 2 * np.eye(2, dtype=np.float64) # Covariance of observation noise (change with the size of detection?).
# Measurements parameters. See equation (20) in [1].
filter_model['p_D'] = 0.90 # Probability of measurements of targets(The probability target could be detected, so probability of miss-detected of targets is 1 - p_D)
filter_model['p_S'] = 0.99
# Compute clutter intensity. See equation (47) and corresponding explanation in [1].
average_number_of_clutter_per_frame = 10
x_range = [0, 300] # X range of measurements
y_range = [0, 300] # Y range of measurements
A = (x_range[1] - x_range[0])*(y_range[1]-y_range[0]) # Size of area.
clutterIntensity = average_number_of_clutter_per_frame/A # Generate clutter intensity (clutter intensity lambda_c = lambda_t/A)
filter_model['clutterIntensity'] = clutterIntensity
filter_model['xrange'] = x_range
filter_model['yrange'] = y_range
filter_model['average_number_of_clutter_per_frame']= average_number_of_clutter_per_frame
# Define gating threshold
filter_model['use_gating'] = False
filter_model['gating_threshold'] = 10
# cap the number of Gaussian components
filter_model['capping_gaussian_components'] = True
filter_model['maximum_number_of_gaussian_components'] = 30
# choose if cholsky is used when compute invS
filter_model['using_cholsky_decomposition_for_calculating_inverse_of_measurement_covariance_matrix'] = False
# GM-PHD filter merge, pruning and state extraction parameters, see tabel II in [1].
filter_model['T'] = 10**-5 # Pruning weight threshold.
filter_model['U'] = 0.1 # Merge distance threshold.
filter_model['w_thresh'] = 0.8 # State extraction weight threshold(i.e. Existence probability used to extract estimates, only the Gaussian components with weight higher than this threshold will be extracted)
# Target birth parameters
filter_model['w_birthsum'] = 0.005 #0.02 # The total weight of birth targets. It is chosen depending on handling false positives.
filter_model['n_birth'] = 4
# Target Birth Initial Step
filter_model['w_birthsuminit'] = 1 #0.02 # The total weight of birth targets. It is chosen depending on handling false positives.
filter_model['n_birthinit'] = 3
filter_model['z_init'] = [[0, 0] for x in range(filter_model['n_birthinit'])]
return filter_model
def gen_ground_truth_states(ground_truth_parameters, targetStates, noiseless):
"""
Generate ground truth states of all targets per scan
"""
truthStates = []
for i in range(len(targetStates)):
if noiseless == True:
# New target ground truth state without any changes on the initial state setting up, which means the moving trajectary will
# be a straight line with constant velocity.
truthState = ground_truth_parameters['F_k'].dot(targetStates[i])
truthStates.append(truthState)
else:
# New target ground truth state with changes('process noise') on the initial state setting up, which means the moving trajectary
# will not be a straight line with constant velocity but can be any shape and velocity.
W = np.sqrt(ground_truth_parameters['Q_k']).dot(np.random.randn(ground_truth_parameters['Q_k'].shape[0], targetStates[i].shape[1])) #.reshape(-1,1) # Process noise
truthState = ground_truth_parameters['F_k'].dot(targetStates[i]) + W # New target true state
truthStates.append(truthState)
return truthStates
def gen_observations(ground_truth_parameters, truthStates):
"""
Generate observations of targets per scan
"""
obserations = []
# We are not guaranteed to detect the target - there is only a probability
for i in range(len(truthStates)):
detect_i = np.random.rand() # Uniformly distribution.
if detect_i <= ground_truth_parameters['p_D']: # Only generate observations for those targets which have been detected
V = np.sqrt(ground_truth_parameters['R_k']).dot(np.random.randn(ground_truth_parameters['R_k'].shape[0], truthStates[i].shape[1]) ) #.reshape(-1,1) # Observation noise
# Beware there is only one observation which comes from the target which has been detected, that is why this is point target model
observation = ground_truth_parameters['H_k'].dot(truthStates[i]) + V
obserations.append(observation)
return obserations
def gen_clutter(ground_truth_parameters):
"""
Generate clutter of whole area per scan
"""
number_of_clutter_in_current_frame = np.random.poisson(ground_truth_parameters['average_number_of_clutter_per_frame'])
x_range = ground_truth_parameters['xrange']
y_range = ground_truth_parameters['yrange']
clutter = []
for n in range(number_of_clutter_in_current_frame):
clutterX = np.random.rand() * (x_range[1] - x_range[0]) + x_range[0] # Random number between x_range[0] and x_range[1], uniformly distributed.
clutterY = np.random.rand() * (y_range[1] - y_range[0]) + y_range[0]
clutter.append(np.array([clutterX, clutterY]).reshape(-1,1))
return clutter
# The probability density function (pdf) of the d-dimensional multivariate normal distribution
def mvnpdf(x, mean, covariance):
# x = np.array(x, dtype=np.float64)
# mean = np.array(mean, dtype=np.float64)
# covariance = np.array(covariance, dtype=np.float64)
d = mean.shape[0]
delta_m = x - mean
pdf_res = 1.0/(np.sqrt((2*np.pi)**d *np.linalg.det(covariance))) * np.exp(-0.5*np.transpose(delta_m).dot(np.linalg.inv(covariance)).dot(delta_m))[0][0]
# pdf_res = 1.0 / (np.sqrt((2 * np.pi) ** d * np.linalg.det(covariance))) * math.exp(-0.5 * np.transpose(delta_m).dot(np.linalg.inv(covariance)).dot(delta_m))
return pdf_res
# generate simulation
def gen_simulation(ground_truth_parameters,n_scan, simulation_scenario):
"""
scenario:
No Intersection
Intersection
No Intersection Varying Cardinality
Intersection Varying Cardinality
"""
# initiate the data structure to be a list of n_scan dictionaries
Z_k_all = [{} for i in range(n_scan)]
targetStates_all = [{} for i in range(n_scan)]
observations_all = [{} for i in range(n_scan)]
clutter_all = [{} for i in range(n_scan)]
if simulation_scenario == "No Intersection":
# Setup the starting target states (positions , velocities)
m_start1 = np.array([230, 220, -1.3, -0.5]).reshape(4, 1)
m_start2 = np.array([250,250, -0.5, -1.3]).reshape(4, 1)
m_start3 = np.array([100, 100, 0.7, 0.5]).reshape(4, 1)
m_start4 = np.array([100, 250, 0.5, -0.8]).reshape(4, 1)
# Initialize the initial position of targets
targetStates_init = [m_start1, m_start2, m_start3, m_start4]
for i in range(n_scan):
if i == 0:
targetStates_i = targetStates_init
# generate data for each frame
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[i]=Z_k_i
targetStates_all[i]=targetStates_i
observations_all[i]=observations_i
clutter_all[i]=clutter_i
elif simulation_scenario == "Intersection":
# Setup the n_scan/2 target states (positions , velocities)
m_start1 = np.array([150, 150, -1.3, -0.5]).reshape(4, 1)
m_start2 = np.array([150, 150, -0.5, -1.3]).reshape(4, 1)
m_start3 = np.array([150, 150, 0.7, 0.5]).reshape(4, 1)
m_start4 = np.array([150, 150, 0.5, -0.8]).reshape(4, 1)
# Initialize the initial position of targets at n_scan/2
targetStates = [m_start1, m_start2, m_start3, m_start4]
if n_scan%2 == 0:
IOError("Please make sure that n_scan is an odd number")
else:
for i in range(int((n_scan-1)/2)+1)[1:]:
if i == 1:
targetStates_i = targetStates
# generate data from frame 0 to fram n_scan/2 -1
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[int(n_scan/2)-i]=Z_k_i
targetStates_all[int((n_scan-1)/2)-i]=targetStates_i
observations_all[int((n_scan-1)/2)-i]=observations_i
clutter_all[int((n_scan-1)/2)-i]=clutter_i
for i in range(int((n_scan-1)/2)+1):
if i == 0:
targetStates_i = targetStates
# generate data from frame n_scan/2 to n_scan
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[int(n_scan/2)+i]=Z_k_i
targetStates_all[int((n_scan-1)/2)+i]=targetStates_i
observations_all[int((n_scan-1)/2)+i]=observations_i
clutter_all[int((n_scan-1)/2)+i]=clutter_i
elif simulation_scenario == "No Intersection Varying Cardinality":
# Setup the starting target states (positions , velocities)
m_start1 = np.array([230, 220, -1.3, -0.5]).reshape(4, 1)
m_start2 = np.array([250,250, -0.5, -1.3]).reshape(4, 1)
m_start3 = np.array([100, 100, 0.7, 0.5]).reshape(4, 1)
m_start4 = np.array([100, 250, 0.5, -0.8]).reshape(4, 1)
# Initialize the initial position of targets
targetStates = [m_start1, m_start2, m_start3, m_start4]
for i in range(n_scan):
if i == 0:
targetStates_i = targetStates
# generate data for each frame
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
# every 10 frames
if i/30 == 1:
targetStates_i.remove(targetStates_i[0])
if i/30 ==2:
new_target = np.array([10,10,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
if i/30 == 3:
targetStates_i.remove(targetStates_i[0])
if i/30 == 4:
new_target = np.array([30,30,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
if i/30 == 5:
targetStates_i.remove(targetStates_i[0])
if i/30 ==6:
new_target = np.array([50,50,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
if i/30 == 7:
targetStates_i.remove(targetStates_i[0])
if i/30 == 8:
new_target = np.array([70,70,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
if i/30 == 9:
new_target = np.array([90,90,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
#if i%10 == 0 and i != 0:
# either a target disapear or appear
# choice = np.random.choice([0,1])
# if the choice is 0 and there are at least 2 targets, then disappear
# if choice == 0 and len(targetStates_i)>1:
# targetStates_i.remove(targetStates_i[0])
# else:
# position = np.random.uniform(20,280,2)
# velocity = np.random.uniform(-1,1,2)
# new_target = np.array([position[0],position[1], velocity[0],velocity[1]]).reshape(4, 1)
# targetStates_i.append(new_target)
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[i]=Z_k_i
targetStates_all[i]=targetStates_i
observations_all[i]=observations_i
clutter_all[i]=clutter_i
elif simulation_scenario == "Intersection Varying Cardinality":
# Setup the n_scan/2 target states (positions , velocities)
m_start1 = np.array([150, 150, -1.3, -0.5]).reshape(4, 1)
m_start2 = np.array([150, 150, -0.5, -1.3]).reshape(4, 1)
m_start3 = np.array([150, 150, 0.7, 0.5]).reshape(4, 1)
m_start4 = np.array([150, 150, 0.5, -0.8]).reshape(4, 1)
# Initialize the initial position of targets at n_scan/2
targetStates = [m_start1, m_start2, m_start3, m_start4]
if n_scan%2 == 0:
IOError("Please make sure that n_scan is an odd number")
else:
for i in range(int((n_scan-1)/2)+1)[1:]:
if i == 1:
targetStates_i = targetStates
# generate data from frame 0 to fram n_scan/2 -1
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
if i/30 == 1:
targetStates_i.remove(targetStates_i[0])
if i/30 ==2:
new_target = np.array([10,10,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
if i/30 == 3:
targetStates_i.remove(targetStates_i[0])
if i/30 == 4:
new_target = np.array([30,30,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
# every 10 frames
#if i%10 == 0 and i != 0 and i != (int((n_scan-1)/2)):
# either a target disapear or appear
# choice = np.random.choice([0,1])
# if the choice is 0 and there are at least 2 targets, then disappear
# if choice == 0 and len(targetStates_i)>1:
# targetStates_i.remove(targetStates_i[0])
# else:
# position = np.random.uniform(20,280,2)
# velocity = np.random.uniform(-1,1,2)
# new_target = np.array([position[0],position[1], velocity[0],velocity[1]]).reshape(4, 1)
# targetStates_i.append(new_target)
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[int(n_scan/2)-i]=Z_k_i
targetStates_all[int((n_scan-1)/2)-i]=targetStates_i
observations_all[int((n_scan-1)/2)-i]=observations_i
clutter_all[int((n_scan-1)/2)-i]=clutter_i
for i in range(int((n_scan-1)/2)+1):
if i == 0:
targetStates_i = targetStates
# generate data from frame n_scan/2 to n_scan
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
if i/30 == 1:
targetStates_i.remove(targetStates_i[0])
if i/30 ==2:
new_target = np.array([10,10,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
if i/30 == 3:
targetStates_i.remove(targetStates_i[0])
if i/30 == 4:
new_target = np.array([30,30,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
# every 10 frames
#if i%10 == 0 and i != 0 and i != (int((n_scan-1)/2)):
# # either a target disapear or appear
# choice = np.random.choice([0,1])
# # if the choice is 0 and there are at least 2 targets, then disappear
# if choice == 0 and len(targetStates_i)>1:
# targetStates_i.remove(targetStates_i[0])
# else:
# position = np.random.uniform(20,280,2)
# velocity = np.random.uniform(-1,1,2)
# new_target = np.array([position[0],position[1], velocity[0],velocity[1]]).reshape(4, 1)
# targetStates_i.append(new_target)
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[int(n_scan/2)+i]=Z_k_i
targetStates_all[int((n_scan-1)/2)+i]=targetStates_i
observations_all[int((n_scan-1)/2)+i]=observations_i
clutter_all[int((n_scan-1)/2)+i]=clutter_i
elif simulation_scenario == "Travel in Proximity":
m_start1 = np.array([10, 10, 1.3, 1.5]).reshape(4, 1)
m_start2 = np.array([12, 17, 1.5, 1.3]).reshape(4, 1)
m_start3 = np.array([8, 15, 1.7, 1.5]).reshape(4, 1)
m_start4 = np.array([20, 15, 1.5, 1.8]).reshape(4, 1)
# Initialize the initial position of targets
targetStates = [m_start1, m_start2, m_start3, m_start4]
for i in range(n_scan):
if i == 0:
targetStates_i = targetStates
# generate data for each frame
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[i]=Z_k_i
targetStates_all[i]=targetStates_i
observations_all[i]=observations_i
clutter_all[i]=clutter_i
elif simulation_scenario == "Intersection More than one Cardinality Changes":
# Setup the n_scan/2 target states (positions , velocities)
m_start1 = np.array([155, 155, -1.3, -0.5]).reshape(4, 1)
m_start2 = np.array([150, 150, -0.5, -1.3]).reshape(4, 1)
m_start3 = np.array([140, 140, 0.7, 0.5]).reshape(4, 1)
m_start4 = np.array([145, 145, 0.5, -0.8]).reshape(4, 1)
# Initialize the initial position of targets at n_scan/2
targetStates = [m_start1, m_start2, m_start3, m_start4]
if n_scan%2 == 0:
IOError("Please make sure that n_scan is an odd number")
else:
for i in range(int((n_scan-1)/2)+1)[1:]:
if i == 1:
targetStates_i = targetStates
# generate data from frame 0 to fram n_scan/2 -1
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
if i/20 == 1:
new_target = np.array([10,10,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
new_target = np.array([20,50,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
targetStates_i.remove(targetStates_i[0])
targetStates_i.remove(targetStates_i[0])
if i/20 ==2:
new_target = np.array([10,10,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
new_target = np.array([20,50,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
targetStates_i.remove(targetStates_i[0])
targetStates_i.remove(targetStates_i[0])
if i/20 == 3:
new_target = np.array([10,10,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
new_target = np.array([20,50,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
targetStates_i.remove(targetStates_i[0])
targetStates_i.remove(targetStates_i[0])
if i/20 == 4:
new_target = np.array([10,10,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
new_target = np.array([20,20,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
targetStates_i.remove(targetStates_i[0])
targetStates_i.remove(targetStates_i[0])
# every 10 frames
#if i%10 == 0 and i != 0 and i != (int((n_scan-1)/2)):
# either a target disapear or appear
# choice = np.random.choice([0,1])
# if the choice is 0 and there are at least 2 targets, then disappear
# if choice == 0 and len(targetStates_i)>1:
# targetStates_i.remove(targetStates_i[0])
# else:
# position = np.random.uniform(20,280,2)
# velocity = np.random.uniform(-1,1,2)
# new_target = np.array([position[0],position[1], velocity[0],velocity[1]]).reshape(4, 1)
# targetStates_i.append(new_target)
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[int(n_scan/2)-i]=Z_k_i
targetStates_all[int((n_scan-1)/2)-i]=targetStates_i
observations_all[int((n_scan-1)/2)-i]=observations_i
clutter_all[int((n_scan-1)/2)-i]=clutter_i
for i in range(int((n_scan-1)/2)+1):
if i == 0:
targetStates_i = targetStates
# generate data from frame n_scan/2 to n_scan
targetStates_i = gen_ground_truth_states(ground_truth_parameters, targetStates_i, noiseless = False) #add guassian noise to desired targetStates
if i/20 == 1:
new_target = np.array([250,250,-1,-1]).reshape(4, 1)
targetStates_i.append(new_target)
new_target = np.array([200,250,-1,-1]).reshape(4, 1)
targetStates_i.append(new_target)
targetStates_i.remove(targetStates_i[0])
targetStates_i.remove(targetStates_i[0])
if i/20 ==2:
new_target = np.array([250,250,-1,-1]).reshape(4, 1)
targetStates_i.append(new_target)
new_target = np.array([200,250,-1,-1]).reshape(4, 1)
targetStates_i.append(new_target)
targetStates_i.remove(targetStates_i[0])
targetStates_i.remove(targetStates_i[0])
if i/20 == 3:
targetStates_i.remove(targetStates_i[0])
if i/20 == 4:
new_target = np.array([30,30,1,1]).reshape(4, 1)
targetStates_i.append(new_target)
# every 10 frames
#if i%10 == 0 and i != 0 and i != (int((n_scan-1)/2)):
# # either a target disapear or appear
# choice = np.random.choice([0,1])
# # if the choice is 0 and there are at least 2 targets, then disappear
# if choice == 0 and len(targetStates_i)>1:
# targetStates_i.remove(targetStates_i[0])
# else:
# position = np.random.uniform(20,280,2)
# velocity = np.random.uniform(-1,1,2)
# new_target = np.array([position[0],position[1], velocity[0],velocity[1]]).reshape(4, 1)
# targetStates_i.append(new_target)
observations_i = gen_observations(ground_truth_parameters, targetStates_i) #add measurement noise to observation
clutter_i = gen_clutter(ground_truth_parameters) #get the clutter generated by this environment
Z_k_i = observations_i + clutter_i # Add clutter to the observations to mimic measruements under cluttered environment(The measurements is union of observations and clutter)
# store data
Z_k_all[int(n_scan/2)+i]=Z_k_i
targetStates_all[int((n_scan-1)/2)+i]=targetStates_i
observations_all[int((n_scan-1)/2)+i]=observations_i
clutter_all[int((n_scan-1)/2)+i]=clutter_i
else:
print('you have entered {} as simulation scenario'.format(simulation_scenario))
print('please enter the valid simulation scenario')
return Z_k_all, targetStates_all, observations_all, clutter_all
# plot function for the demo
def filter_plot(fig,truthStates, observations, estimatedStates, clutter,ground_truth_parameters):
"""
Plot all information(ground truth states, measurements which include both observations and clutters,
and estimated states for all targets) per scan(frame)
"""
# Plot the ground truth state of targets.
for i in range(len(truthStates)):
truthState = truthStates[i]
# plt.plot(truthState[0], truthState[1], '.b', markersize = 10.0, label='ground truth')
plt.plot(truthState[0], truthState[1], '.b', markersize = 10.0)
# Plot the measurements.
for i in range(len(observations)):
observation = observations[i]
# plt.plot(observation[0], observation[1], '.r', markersize = 10.0, label='measurement')
if len(observation) > 0:
plt.plot(observation[0], observation[1], '.r', markersize = 10.0)
# Plot the clutters.
for i in range(len(clutter)):
clut = clutter[i]
# plt.plot(observation[0], observation[1], 'xk', markersize = 5.0, label='clutter')
plt.plot(clut[0], clut[1], 'xk', markersize = 5.0)
# Plot the estimated state of targets.
for i in range(len(estimatedStates)):
estimatedState = np.array(estimatedStates[i], dtype=np.float64)
# plt.plot(estimatedState[0], estimatedState[1], '.g', markersize = 10.0, label='estimated state')
plt.plot(estimatedState[0], estimatedState[1], '.g', markersize = 10.0)
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.title('Ground truth (blue), observations (red), estimated states (green) and clutter (black x)', fontsize=8)
plt.xlim((ground_truth_parameters['xrange'][0], ground_truth_parameters['xrange'][1]))
plt.ylim((ground_truth_parameters['yrange'][0], ground_truth_parameters['yrange'][1]))
fig.canvas.draw()
fig.canvas.flush_events()
time.sleep(0.1)
def plot_gospa(path_to_save_results,scenario,x,gospa_record_all_average,gospa_localization_record_all_average,gospa_missed_record_all_average,gospa_false_record_all_average):
'''
plt.title("RMS GOSPA Error")
plt.xlabel("frame number")
plt.ylabel("RMS GOSPA Error")
plt.plot(x,gospa_record_all_average)
#plt.show()
plt.savefig(path_to_save_results +scenario+'gospa_error.png')
plt.close()
plt.title("RMS GOSPA Normalized Localization Error")
plt.xlabel("frame number")
plt.ylabel("RMS GOSPA Normalized Localization Error")
plt.plot(x,gospa_localization_record_all_average)
#plt.show()
plt.savefig(path_to_save_results +scenario+'localization.png')
plt.close()
plt.title("RMS GOSPA Missed Target Error")
plt.xlabel("frame number")
plt.ylabel("RMS GOSPA Missed Target Error")
plt.plot(x,gospa_missed_record_all_average)
#plt.show()
plt.savefig(path_to_save_results + scenario+'missed.png')
plt.close()
plt.title("RMS GOSPA False Target Error")
plt.xlabel("frame number")
plt.ylabel("RMS GOSPA False Target Error")
plt.plot(x,gospa_false_record_all_average)
#plt.show()
plt.savefig(path_to_save_results +scenario+'false.png')
plt.close()
'''
# Store Data
path = path_to_save_results + 'compare/' + scenario + 'pmb/gospa_record.pickle'
f = open(path, 'wb')
pickle.dump(gospa_record_all_average, f)
f.close()
path = path_to_save_results + 'compare/'+scenario+'pmb/gospa_localization_record.pickle'
f = open(path, 'wb')
pickle.dump(gospa_localization_record_all_average, f)
f.close()
path = path_to_save_results + 'compare/'+scenario+'pmb/gospa_missed_record.pickle'
f = open(path, 'wb')
pickle.dump(gospa_missed_record_all_average, f)
f.close()
path = path_to_save_results + 'compare/'+scenario+'pmb/gospa_false_record.pickle'
f = open(path, 'wb')
pickle.dump(gospa_false_record_all_average, f)
f.close()