-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSensor.py
1285 lines (1128 loc) · 46.7 KB
/
Sensor.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
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import random
from munkres import Munkres
import numpy as np
from itertools import permutations, product, combinations
import time
import matplotlib.pyplot as plt
import matplotlib
import csv
from Robot import Robot
from Target import Target
matplotlib.rcParams['pdf.fonttype'] = 42
def find_error(d_true, d_est):
"""
This function finds the Euclidean distance between the estimated position and the true position
Parameters
----------
d_true: Ground truth position, 2D array
d_est: Estimated position, 2D array
Returns
-------
d_error: The Euclidean distance, scalar.
"""
return np.linalg.norm(d_true - d_est)
def wrapToPi(angleIn):
if angleIn > np.pi or angleIn < -np.pi:
angleOut = angleIn - 2 * np.pi * (angleIn % 2 * np.pi)
else:
angleOut = angleIn
return angleOut
def euler_angle(pose):
"""
This function transform a given position to euler angle relative to the origin
:param pose: position [x, y], can be an N x 2 array for multiple inputs
:return:
"""
if pose.shape[0] == 1:
angle = np.arctan2(pose[1], pose[0])
else:
angle = np.arctan2(pose[:, 1], pose[:, 0])
return angle
def pos_estimate(perb, ground_truth):
return ground_truth + (2 * perb * np.random.rand(1) - perb)
class SensorAssignment:
def __init__(self, Nt, Nr, sz, scene):
"""
Initialize the sensor assignment code, randomly place the sensor and targets based on the input number.
The placement is within the size input.
:param Nt: Number of target
:param Nr: Number of robot (for bearing and ranging, Nr = Nt)
:param sz: Size of the environment
:return: None
"""
self.Nt = Nt
self.scene = scene
if scene != 1:
self.adjust = 2
else:
self.adjust = 1
self.Nr = self.adjust * Nt
self.dt = 0.5 # set the time step
perb = 0.3
self.r_sen = (
sz * 100
) # assume the sensing range is far larger than the size of the world
self.targets = [Target(sz, i, perb) for i in range(self.Nt)]
self.targets_greedy = self.targets.copy()
self.robots = [Robot(sz, i, scene) for i in range(self.Nr)]
self.robots_comb = None
if self.scene == 1:
self.robots_comb = self.robots
self.robots_list = np.array([i for i in range(self.Nr)])
else:
# total different robots combination (group)
self.robots_list = np.array(
tuple(combinations([ii for ii in range(self.Nr)], 2))
)
# transfer to robot objects
self.robots_comb = [
[self.robots[ii[0]], self.robots[ii[1]]] for ii in self.robots_list
]
self.target_pos_sig = None
self.target_true_pos = None
self.target_pos_hat = None
self.targetVals()
self.robotVals()
self.noise = 0.2 * np.random.rand(200, 1)
self.robot_pos = None
self.v = None # linear velocity list
self.w = None # angular velocity list
self.actions = None # action combinations for single robot
self.action_num = None
self.record = None
self.action_for_each_robot = None
self.action_combs = None # action combination across all the robot
self.sensor_target = None # sensor and target pair
self.cov_matrix = None
self.sig_matrix = None
self.target_est = None
def targetVals(self):
self.target_true_pos = np.array(
[self.targets[i].ground_truth for i in range(self.Nt)]
)
self.target_pos_hat = np.array(
[self.targets[i].estimated_location for i in range(self.Nt)]
)
self.target_pos_sig = np.array(
[[self.targets[i].sigma] for i in range(self.Nt)]
).reshape((2 * self.Nt, 2))
def robotVals(self):
self.robot_pos = np.array([self.robots[i].location for i in range(self.Nr)])
def set_actions(self, v, w):
"""
Set the linear velocity and angular velocity, and then get the combinations for the actions
"""
self.v = v
self.w = w
self.actions = np.array([[ii, jj] for ii in self.v for jj in self.w])
self.action_num = len(self.v) * len(self.w)
for i in range(self.Nr):
self.robots[i].set_steps(self.action_num, self.Nt)
def all_pairs(self, lst):
"""
Credit to shang from StackOverflow
https://stackoverflow.com/questions/5360220/how-to-split-a-list-into-pairs-in-all-possible-ways
:param lst:
:return:
"""
if len(lst) < 2:
yield []
return
if len(lst) % 2 == 1:
# Handle odd length list
for i in range(len(lst)):
for result in self.all_pairs(lst[:i] + lst[i + 1:]):
yield result
else:
a = lst[0]
for i in range(1, len(lst)):
pair = [a, lst[i]]
# print(lst[1:i], lst[i + 1 :])
for rest in self.all_pairs(lst[1:i] + lst[i + 1:]):
yield [pair] + rest
def step(self):
"""
Perform a step operation for all the robots, the robot will perform all the actions and estimate all the targets
using EKF for each action.
"""
self.targetVals()
self.robotVals()
if self.scene != 1:
action_comb = [[u1, u2] for u1 in self.actions for u2 in self.actions]
else:
action_comb = self.actions
self.action_for_each_robot = action_comb
self.cov_matrix = np.zeros((len(self.robots_comb), len(action_comb), self.Nt))
self.sig_matrix = np.zeros(
(len(self.robots_comb), len(action_comb), 2 * self.Nt, 2)
)
self.target_est = np.zeros(
(len(self.robots_comb), len(action_comb), self.Nt, 2)
)
tPos_hat = self.target_pos_hat
tSigma_hat = self.target_pos_sig
tPos_true = self.target_true_pos
for i in range(len(self.robots_comb)):
for j in range(len(action_comb)):
u = action_comb[j]
# print(u)
if self.scene == 1:
rPos = self.robots[i].get_action(u, self.dt).reshape((1, 3))
robot_curr = self.robots[i]
else:
robot_curr = []
rPos = np.zeros((len(self.robots_comb[i]), 3))
for k in range(len(self.robots_comb[0])):
rPos[k] = (
self.robots_comb[i][k]
.get_action(u[k], self.dt)
.reshape((1, 3))
)
robot_curr.append(self.robots_comb[i][k])
# print(len(rPos))
(
quality_ts,
trace_ts,
sqerr_ts,
tPos_hat_kp1,
tSigma_hat_kp1,
trace_t,
sqerr_t,
trace_sig_diff,
) = self.EKF(
tPos_hat, tSigma_hat, tPos_true, 1, rPos, robot_curr
) # r_set = 1
# print(f'robot {self.robots[i].id} with action {j} has {trace_t}')
# print(j)
# print(f'robot {[self.robots_comb[i][0].type, self.robots_comb[i][1].type]} and action {u}, pos {rPos} with trace {trace_sig_diff}')
# print(f"action {u}, pos {rPos} with trace {trace_sig_diff}")
# print(trace_sig_diff)
if self.scene == 1:
self.robots[i].update_cov(trace_sig_diff, j)
self.cov_matrix[i][j] = trace_sig_diff
self.sig_matrix[i][j] = tSigma_hat_kp1
self.target_est[i][j] = tPos_hat_kp1
# print(self.action_num)
# print(len(action_comb))
# print(f"cov_matrix has shape of {self.cov_matrix.shape}")
def OPT(self):
# combination of all the actions
if self.scene == 1:
action_num = self.action_num
self.action_combs = np.array(
tuple(product(range(action_num), repeat=self.Nt))
)
# print(self.action_num)
# combination of all the sensor and target pair
self.sensor_target = np.array(
tuple(permutations(range(len(self.robots_comb)), r=self.Nt))
)
else:
action_num = self.action_num ** 2
all_pair = np.array(tuple(self.all_pairs(self.robots)))
# all_pair = np.array(tuple(self.all_pairs([i for i in range(self.Nr)])))
all_sensor_target_pair = np.array(
[tuple(permutations(pair, r=self.Nt)) for pair in all_pair]
)
all_st_pair_shape = all_sensor_target_pair.shape
all_st_pair_reshape = all_sensor_target_pair.reshape(
(
all_st_pair_shape[0] * all_st_pair_shape[1],
all_st_pair_shape[2],
all_st_pair_shape[3],
)
)
all_st_pair_id = [
[[kk.id for kk in jj] for jj in ii] for ii in all_st_pair_reshape
]
self.sensor_target = all_st_pair_id
self.action_combs = np.array(
tuple(product(range(action_num), repeat=self.Nt))
)
# print(action_num)
# print(f"robots_comb has length = {len(self.robots_comb)}")
# print("a")
# print(
# f"robots pairs = {len(self.robots_comb)}, robot-target pairs = {len(self.sensor_target)}"
# )
# initialize the tracker of sum of trace
# print(self.action_combs)
# print(len(self.action_combs))
# print(all_pair.shape)
# print(all_st_pair_reshape.shape)
# initialize the tracker of sum of trace
self.record = np.zeros((len(self.action_combs), len(self.sensor_target)))
# print(all_pair)
# print(all_pair)
for ii in range(len(self.action_combs)):
action_comb = self.action_combs[ii]
# [robo1_action1, robo2_action1, robo3_action1]...
for jj in range(len(self.sensor_target)):
pair = self.sensor_target[jj]
# [robo1_target, robo2_target, robo3_target]...
for rr in range(self.Nt):
robots_ids = self.robots_list[rr]
if self.scene == 1:
self.record[ii][jj] += self.robots_comb[rr].get_cov()[
action_comb[rr]
][pair[rr]]
else:
robot_loc = np.where(
np.all(self.robots_list == pair[rr], axis=1)
)
action_loc = action_comb[rr]
# print(self.cov_matrix)
# print('---')
# print(self.cov_matrix[robot_loc][0])
# print('---')
# print(self.cov_matrix[robot_loc][0][action_loc])
# print('---')
# print(self.cov_matrix[robot_loc][0][action_loc][rr])
# print(self.record[ii][jj])
self.record[ii][jj] += self.cov_matrix[robot_loc][0][
action_loc
][rr]
# for kk in range(len())
# print(f'Action: {self.action_combs[ii]}, ST pair: {self.sensor_target[jj]}, robot: {self.robots_comb[rr]}')
max_val = np.amax(self.record)
max_loc = np.where(self.record == max_val)
action_ind = max_loc[0][0]
pair_ind = max_loc[1][0]
# print(f"max value: {max_val:0.3f}")
# print(f"action index is {action_ind}, which is {self.action_combs[action_ind]}")
# print(f"pair index is {pair_ind}, which is {self.sensor_target[pair_ind]}")
# print(
# f"total iteration is {len(self.action_combs) * len(self.sensor_target) * self.Nt}"
# )
return max_val # , max_loc
def Greedy(self):
if self.scene == 1:
targets_greedy = self.targets.copy()
target_poped = []
robots_greedy = self.robots.copy()
robot_action_target = np.zeros((len(robots_greedy), 2), dtype=int)
robot = []
while bool(robots_greedy):
if bool(targets_greedy):
record_list = np.zeros(
(len(robots_greedy), self.action_num, len(targets_greedy))
)
# assemble all the trace for all the robot, action, for all targets
for ii in range(len(robots_greedy)):
cov_matrix = robots_greedy[ii].get_cov()
if bool(target_poped):
cov_matrix = np.delete(cov_matrix, target_poped, 1)
record_list[ii] = cov_matrix
# find the max one and the location
max_val = np.amax(record_list)
max_loc = np.where(record_list == max_val)
# keep record of the action and target assigned to the robot
robot_pop = robots_greedy[max_loc[0][0]].id
max_action = max_loc[1][0]
target_pop = targets_greedy[max_loc[2][0]].id
target_poped.append(target_pop)
robot_action_target[robot_pop][0] = max_action
robot_action_target[robot_pop][1] = target_pop
robot.append(robot_pop)
# pop the robot and target selected in this round
targets_greedy.pop(max_loc[2][0])
robots_greedy.pop(max_loc[0][0])
# print(f'robot {robot_pop} with action {max_action} assigned to target {target_pop}')
actions = robot_action_target[:, 0]
pairs = robot_action_target[:, 1]
max_cov = sum(
[
self.robots[ii].get_cov()[robot_action_target[ii][0]][
robot_action_target[ii][1]
]
for ii in range(self.Nr)
]
)
else:
targets_greedy = self.targets.copy()
target_popped = []
if self.scene == 1:
robots_greedy = self.robots.copy()
else:
robots_greedy = self.robots_list.copy()
robots_greedy = robots_greedy.tolist()
robot_action_target = np.zeros((self.Nt, 3), dtype=int)
covariance_matrix = self.cov_matrix.copy()
# robot_action_target = np.zeros((self.Nt, 2), dtype=int)
iter = 0
max_cov = 0
while bool(robots_greedy):
if bool(targets_greedy):
record_list = np.zeros(
(
len(robots_greedy),
len(self.action_for_each_robot),
len(targets_greedy),
)
)
# assemble all the trace for all the robot, action, for all targets
for ii in range(len(robots_greedy)):
cov_matrix = covariance_matrix[ii]
# if bool(target_popped):
# cov_matrix = np.delete(cov_matrix, target_popped, 1)
record_list[ii] = cov_matrix
# find the max one and the location
max_val = np.amax(record_list)
max_loc = np.array(np.where(record_list == max_val))
max_cov += max_val
# keep record of the action and target assigned to the robot
# robots_max_greedy = robots
robot_pop = np.array(
np.where(
np.all(
self.robots_list == robots_greedy[max_loc[0][0]], axis=1
)
)
).reshape((1,))
# robot_pop = robots_greedy[max_loc[0][0]].id
max_action = max_loc[1][0]
target_pop = targets_greedy[max_loc[2][0]].id
target_popped.append(target_pop)
robot_action_target[iter][0] = max_action
robot_action_target[iter][1] = target_pop
robot_action_target[iter][2] = robot_pop
# pop the robot and target selected in this round
targets_greedy.pop(max_loc[2][0])
robots_greedy = np.array(robots_greedy)
# slice = np.where(np.any(robots_greedy == self.robots_list[robot_pop], axis=1))
# something = self.robots_list[robot_pop]
place_to_delete = np.where(
np.any(
robots_greedy
== np.array(self.robots_list[robot_pop][0][0]),
axis=1,
)
) + np.where(
np.any(
robots_greedy
== np.array(self.robots_list[robot_pop][0][1]),
axis=1,
)
)
robots_greedy = np.delete(
robots_greedy,
place_to_delete,
axis=0,
)
covariance_matrix = np.delete(
covariance_matrix, place_to_delete, axis=0
)
covariance_matrix = np.delete(
covariance_matrix, max_loc[2][0], axis=2
)
robots_greedy = robots_greedy.tolist()
iter += 1
# print(f'robot {robot_pop} with action {max_action} assigned to target {target_pop}')
# print('no problem fk')
actions = robot_action_target[:, 0]
pairs = robot_action_target[:, 1]
robot = robot_action_target[:, 2]
return (max_cov, robot, actions)
def Hungarian(self):
"""
Using Hungarian method to get the perfect choice
"""
# initialize the hungarian method solver
m = Munkres()
# reshape the covariance matrix, combine the robots pairs and actions
# the reshaped matrix would be a 2D matrix with row being robots pairs at each actions
# col being the targets
cov_matrix_hun = np.copy(self.cov_matrix)
cov_hun_shape = cov_matrix_hun.shape
cov_hun_row = cov_hun_shape[0] * cov_hun_shape[1]
cov_matrix_hun = cov_matrix_hun.reshape((cov_hun_row, cov_hun_shape[2]))
# print(cov_matrix_hun.shape)
# print(cov_matrix_hun)
# padding with 0 to get a square matrix for Hungarian Method
profit_mat = np.zeros((cov_hun_row, cov_hun_row))
profit_mat[:, 0: cov_hun_shape[2]] = cov_matrix_hun
profit_mat_trans = profit_mat.T
max_element = np.max(profit_mat_trans)
cost_mat = max_element - profit_mat_trans
indexes = m.compute(cost_mat)
# get the first Nt indexes
index_first_Nt = indexes[0: self.Nt]
max_cov = 0
comb_action_lst = []
for row, col in index_first_Nt:
max_cov += profit_mat_trans[row][col]
comb = row // len(self.action_for_each_robot)
action = row % len(self.action_for_each_robot)
comb_action_lst += [comb, action]
# print(f"max value: {max_cov: 0.3f}") # {max_cov: 0.3f}")
return max_cov
# print(f"actions are {}")
def EKF(self, tPos_hat_k, tSigma_hat_k, tPos_true_k, r_set, rPos_kp1, robot_curr):
"""
Extended Kalman Filter for range and bearing sensors (1 to 1 assignment)
:param tPos_hat_k: estimated target position
:param tSigma_hat_k: estimated target covariance
:param tPos_true_k: true target position
:param r_set: number of robot
:param rPos_kp1: position of robot
:return:
"""
# kp1 --> k+1
# initialize tPos and tSigma for the k+1 step
tPos_hat_kp1 = np.zeros([self.Nt, 2])
tSigma_hat_kp1 = np.zeros([self.Nt * 2, 2])
# Simulate measurements for each robot, We assume distance measurements
# z = true distance between robot and target + noise
# noise is zero-mean Gaussian with variance sigma_z^2
sigma_z = 0.1
# sigma_z = 0.2*(1-1/())
# Q: 2x2 state noise covariance
sigma_q = 0.1
Q = sigma_q * np.identity(2)
# check for each target, which robot track it
r_set_t = [np.zeros((1,)) for _ in range(self.Nt)]
# store the trace of covariance for each target
trace_t = np.zeros([1, self.Nt])
trace_t_1 = np.zeros([1, self.Nt])
# store the squared error
sqerr_t = np.zeros([1, self.Nt])
# store the tracking quality of each target
quality_t = np.zeros([1, self.Nt])
# if no robot
if np.isnan(r_set):
for j in range(self.Nt):
tPos_hat_kp1[j, :] = tPos_hat_k[j, :]
tSigma_hat_kp1[2 * j: 2 * j + 1, :] = (
tSigma_hat_k[2 * j: 2 * j + 1, :] + Q
)
# trace
trace_t[j] = np.trace(tSigma_hat_kp1[2 * j: 2 * j + 1, :])
# squared error
sqerr_t[j] = (tPos_hat_kp1[j, :] - tPos_true_k[j, :]) * (
tPos_hat_kp1[j, :] - tPos_true_k[j, :]
).T
# no measurements, tracking quality
quality_t[j] = 0
raise Exception("No Robot Detected")
else:
# loop through all the robot
for j in range(self.Nt):
# r_set_t_j = len(r_set_t[j]) # number of all the
r_set_t_j = 1 ############## hard coded ##############
# KF prediction step for target j
jPos_hat_kp1_1 = tPos_hat_k[j, :].reshape((1, 2))
# predict the pos of the target
jSigma_hat_kp1_1 = tSigma_hat_k[2 * j: 2 * j + 2, :] + Q
r_set_index = np.array([r_set - 1])
for i in range(r_set_index.shape[0]):
# check if the target j is within the sensing range of robot i
if (
np.linalg.norm(rPos_kp1[i, 0:2] - tPos_true_k[j, :])
<= self.r_sen
):
r_set_t[j] = np.append(r_set_t[j], [i])
# if it is not tracked by any robot
if len(r_set_t) == 0:
# copy the prediction step -- no update
tPos_hat_kp1 = jPos_hat_kp1_1
tSigma_hat_kp1[2 * j: 2 * j + 1, :] = jSigma_hat_kp1_1
raise Exception("Target not tracked by any robots")
else:
# if there are some robots r_set_t[0,j] track target j
# KF update step
Zj = np.zeros([2 * r_set_t_j, 1]) # measurement
Zj_hat = 0 * Zj # estimated measurement
# using range and bearing sensors
Hj = np.zeros([2 * r_set_t_j, 2])
Rj = np.zeros([2 * r_set_t_j, 2 * r_set_t_j])
# residual
res = np.zeros([2 * r_set_t_j, 1])
if self.scene == 1:
robots = 1
else:
robots = 2
for i in range(robots):
# distance based measurements and variance
dij = np.linalg.norm(rPos_kp1[i, 0:2] - tPos_true_k[j, :])
# real angle based measurements and variance
head_pi = wrapToPi(rPos_kp1[i, 2]) # TODO: verify the wrapToPi
aij_delta = (
np.arctan2(
tPos_true_k[j, 1] - rPos_kp1[i, 1],
tPos_true_k[j, 0] - rPos_kp1[i, 0],
)
- head_pi
)
aij = aij_delta - 2 * np.pi * np.round(aij_delta / (2 * np.pi))
noisei_d = sigma_z * dij * random.choice(self.noise) + 0.001
noisei_a = (
sigma_z * np.abs(aij) * random.choice(self.noise) + 0.001
)
distLin = self.distLinearization(
jPos_hat_kp1_1, rPos_kp1[i, 0:2]
)
angleLin = self.angleLinearization(
jPos_hat_kp1_1, rPos_kp1[i, 0:2], head_pi
)
# Apply Kalman Filter update
if self.scene == 1:
# real measurements
Zj[0:2, :] = np.array(
[[dij + noisei_d[0]], [wrapToPi(aij + noisei_a[0])]]
) # measurement is noisy
Zj_hat[0:2, :] = np.array([[distLin], [angleLin]])
Hj[0, :] = self.distJac(jPos_hat_kp1_1, rPos_kp1[i, 0:2])
Hj[1, :] = self.angleJac(
jPos_hat_kp1_1, rPos_kp1[i, 0:2], head_pi
)
Rj[0, 0] = sigma_z * dij + 0.0001
Rj[1, 1] = sigma_z * np.abs(aij) + 0.0001
res[0, 0] = Zj[0, 0] - Zj_hat[0, 0]
res[1, 0] = (
Zj[1, 0] - Zj_hat[1, 0]
) - 2 * np.pi * np.round(
(Zj[1, 0] - Zj_hat[1, 0]) / (2 * np.pi)
)
else:
if robot_curr[i].type == 2:
Zj[i, 0] = dij + noisei_d[0]
Zj_hat[i, :] = distLin
Hj[i, :] = self.distJac(
jPos_hat_kp1_1, rPos_kp1[i, 0:2]
)
Rj[i, i] = sigma_z * dij + 0.0001
res[i, 0] = Zj[i, 0] - Zj_hat[i, 0]
elif robot_curr[i].type == 3:
Zj[i, 0] = aij + noisei_a[0]
Zj_hat[i, :] = angleLin
Hj[i, :] = self.angleJac(
jPos_hat_kp1_1, rPos_kp1[i, 0:2], head_pi
)
Rj[i, i] = sigma_z * np.abs(aij) + 0.0001
res[i, 0] = (
Zj[i, 0] - Zj_hat[i, 0]
) - 2 * np.pi * np.round(
(Zj[i, 0] - Zj_hat[i, 0]) / (2 * np.pi)
)
# residual covariance
Sj = Hj @ jSigma_hat_kp1_1 @ Hj.T + Rj
# Kalman Gain
Kj = jSigma_hat_kp1_1 @ Hj.T @ np.linalg.inv(Sj)
# state update
tPos_hat_kp1[j, :] = jPos_hat_kp1_1 + (Kj @ res).T
# covariance update
tSigma_hat_kp1[2 * j: 2 * j + 2, :] = (
np.identity(2) - Kj @ Hj
) @ jSigma_hat_kp1_1
# @ (
# np.identity(2) - Kj @ Hj).T + Kj @ Rj @ Kj.T
# trace
trace_t[0][j] = np.trace(tSigma_hat_kp1[2 * j: 2 * j + 2, :])
trace_t_1[0][j] = np.trace(jSigma_hat_kp1_1)
# squared error
sqerr_t[0][j] = (tPos_hat_kp1[j, :] - tPos_true_k[j, :]) @ (
tPos_hat_kp1[j, :] - tPos_true_k[j, :]
).T
# tracking quality
quality_t[0][j] = np.trace(jSigma_hat_kp1_1) - trace_t[0][j]
trace_sig_diff = trace_t_1 - trace_t
# print(f'updated trace is {trace_t}, prediction trace is {trace_t_1}, diff is {trace_sig_diff}')
trace_ts = np.sum(trace_t)
sqerr_ts = np.sum(sqerr_t)
quality_ts = np.sum(quality_t)
return (
quality_ts,
trace_ts,
sqerr_ts,
tPos_hat_kp1,
tSigma_hat_kp1,
trace_t,
sqerr_t,
trace_sig_diff,
)
def distLinearization(self, targetPose, robotPose):
return np.linalg.norm(targetPose - robotPose)
def angleLinearization(self, targetPose, robotPose, heading):
Zj_hat_br_delta = (
np.arctan2(
targetPose[0, 1] - robotPose[1],
targetPose[0, 0] - robotPose[0],
)
- heading
)
Zj_hat_br = Zj_hat_br_delta - 2 * np.pi * np.round(
Zj_hat_br_delta / (2 * np.pi)
)
return Zj_hat_br
def distJac(self, targetPose, robotPose):
Zj_hat = self.distLinearization(targetPose, robotPose)
jac = 1 / Zj_hat * (targetPose - robotPose)
return jac
def angleJac(self, targetPose, robotPose, heading):
Zj_hat_br_delta = (
np.arctan2(
targetPose[0, 1] - robotPose[1],
targetPose[0, 0] - robotPose[0],
)
- heading
)
Zj_hat = self.distLinearization(targetPose, robotPose)
jac = (
1
/ Zj_hat
* np.array(
[
-np.sin(heading + Zj_hat_br_delta),
np.cos(heading + Zj_hat_br_delta),
]
)
)
return jac
def ten_runs(Nt, Nr, sz, scene, v, w, rounds, if_OPT, if_plot):
if if_OPT:
box_line = [
dict(color="blue", linewidth=1.4),
dict(color="red", linewidth=1.4),
dict(color="orange", linewidth=1.4),
]
names = ["Greedy", "Hungarian", "OPT", "Lower Baseline"]
else:
box_line = [
dict(color="red", linewidth=1.4),
dict(color="orange", linewidth=1.4),
]
names = ["Greedy", "Hungarian", "Lower Baseline"]
bound = 1 / 2 if scene == 1 else 1 / 3
Nt_lst = [i for i in range(1, rounds + 1)]
Nt_lst_str = [str(i) for i in Nt_lst]
pos = np.zeros((len(Nt_lst), 1))
fig, ax = plt.subplots(2)
# ax = plt.axes()
mean_val = np.zeros((len(Nt_lst), (len(names) - 1)))
x_pos = np.zeros((len(Nt_lst), (len(names) - 1)))
test_rlt = np.zeros((len(Nt_lst), 10, (len(names) - 1)))
# Nt = 2
for t in Nt_lst:
Nt = t
ind = t - 1
for i in range(10):
test = SensorAssignment(Nt, Nr, sz, scene)
test.set_actions(v, w)
test.step()
if if_OPT:
test_rlt[ind][i][0] = test.Greedy()[0]
test_rlt[ind][i][1] = test.Hungarian()
test_rlt[ind][i][2] = test.OPT()
else:
test_rlt[ind][i][0] = test.Greedy()[0]
test_rlt[ind][i][1] = test.Hungarian()
print(f"target num: {t} out of {rounds}, run {i}")
# print(test_rlt)
# print(np.mean(test_rlt, axis=0))
mean_val[ind] = np.mean(test_rlt[ind], axis=0)
# pos_val = 2 * (2*t-3)
pos_val = 2 * t - 3
pos[ind] = pos_val
x_pos[ind] = (
[pos_val - 1 / 3, pos_val, pos_val + 1 / 3]
if if_OPT
else [pos_val - 1 / 3, pos_val]
)
if if_OPT:
bp = ax[0].boxplot(
test_rlt[ind], positions=x_pos[ind]
) # , boxprops=box_line[t-2])
else:
bp = ax[0].boxplot(
test_rlt[ind], positions=x_pos[ind]
) # , boxprops=box_line)
write_csv(test_rlt, "ten_run_result" + str(Nt_lst[-1]) + "_scene_" + str(scene))
# save the result
# write_csv(test_rlt, "ten_run_result" + str(Nt_lst[-1]) + "_scene_" + str(scene))
mean_val = mean_val.T
x_pos = x_pos.T
if if_plot:
for i in range(len(x_pos)):
# ax[1].plot(x_pos[i], mean_val[i])
ax[1].plot(Nt_lst, mean_val[i], "-s", label=names[i])
if if_OPT:
ax[1].plot(Nt_lst, mean_val[0] * bound, "-s", label=names[-1])
else:
ax[1].plot(Nt_lst, mean_val[-1] * bound, "-s", label=names[-1])
# ax[1].legend(['OPT', 'Greedy', 'Hungarian', '1/2 OPT'])
ax[0].set_xticks(
list(
pos.reshape(
len(pos),
)
)
)
ax[0].set_xticklabels(Nt_lst_str)
ax[1].set_xticks(Nt_lst)
ax[1].set_xticklabels(Nt_lst_str)
ax[1].legend()
# ax[0].xlim(0, 4*len(Nt_lst))
plt.grid()
plt.show()
def confidence_ellipse(cov, c, color, k=2.296):
# find the eigenvalue and eigenvector of the given covariance matrix
w, v = np.linalg.eig(cov)
largest_eig_val = max(w)
largest_vec_ind = np.where(w == largest_eig_val)[0]
largest_eig_vec = v[:, largest_vec_ind]
smallest_eig_val = min(w)
# calculate the angle between the largest eigenvector and the x-axis
angle = np.arctan2(largest_eig_vec[1], largest_eig_vec[0])
if angle < 0:
angle = angle + 2 * np.pi
# initialize the values for ellipse calculation
chi_square_val = 2.4477
phi = angle[0]
q = np.linspace(0, 2 * np.pi, 30)
# find the a and b for the ellipse
a = chi_square_val * np.sqrt(largest_eig_val)
b = chi_square_val * np.sqrt(smallest_eig_val)
ellipse_x_r = a * np.cos(q)
ellipse_y_r = b * np.sin(q)
# rotate the ellipse to get the correct confidence ellipse
R = np.array([[np.cos(phi), np.sin(phi)], [-np.sin(phi), np.cos(phi)]])
ellipse_r = np.array([ellipse_x_r, ellipse_y_r]).T
r_ellipse = ellipse_r @ R
plt.plot(r_ellipse[:, 0] + c[0], r_ellipse[:, 1] + c[1], color)
plt.axis("equal")
return None
def write_csv(data: np.ndarray, name: str):
"""
Function save the given numpy array data to a csv file with specified name
Parameters
----------
data: numpy array to be saved
name: name of the file to be saved, without .csv
Returns
-------
None
"""
data = data.tolist()
with open(name + ".csv", "w", newline="") as csvfile:
my_writer = csv.writer(csvfile, delimiter=",")
my_writer.writerows(data)
return None
def read_csv(name: str):
"""
Read csv file that contains 3D numpy array, and convert it into ndarray
Parameters
----------
name: name of the file, e.g.: "test_file.csv"
Returns
-------
data_num: 3D numpy array
"""
with open(name, "r") as csvFile:
raw_data = csv.reader(csvFile)
raw_data = list(raw_data)
data_str = []
for row in raw_data:
nwrow = []
for r in row:
nwrow.append(eval(r))
data_str.append(nwrow)
data_num = np.array([[[float(k) for k in j] for j in i] for i in data_str])
return data_num
def read_csv_single_line(name):
with open(name, "r") as csvFile:
raw_data = csv.reader(csvFile)
raw_data = list(raw_data)
for row in raw_data:
nwrow = []
for r in row:
nwrow.append(eval(r))
data = np.array(nwrow)
return data
def target_moving(Nt, Nr, sz, scene, v, w, steps):
test = SensorAssignment(Nt, Nr, sz, scene)
test.set_actions(v, w)
dt = 0.5
u_tar = 1.3
d_theta = 0.2
target_radius = [
np.sqrt(target.ground_truth[0] ** 2 + target.ground_truth[1] ** 2)
for target in test.targets
]
target_angle = [
np.arctan2(target.ground_truth[1], target.ground_truth[0])
for target in test.targets
]
times = np.linspace(0, steps * dt, steps)
colors = ["r", "g", "b"]
u = 1
target_hist = np.zeros((Nt, steps, 2))
robot_hist = np.zeros((test.Nr, steps, 2))
error_hist = np.zeros((steps,))
for i in range(test.Nr):
test.robots[i].pos_hist = np.zeros((steps, 3))
# setup the target trajectory
for step in range(len(times)):
t = times[step]
# perform a step of tracking evaluation
test.step()
# use the pair with the best quality
cov, robot, action = test.Greedy()
plt.figure(step)
plt.title(f"time step {step} at time {t} s with max_cov = {cov: 0.3f}")