-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHDFilterNetwork.py
More file actions
1077 lines (870 loc) · 36.4 KB
/
PHDFilterNetwork.py
File metadata and controls
1077 lines (870 loc) · 36.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
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 networkx as nx
from numba import njit
from operator import attrgetter
import pandas as pd
import scipy
from scipy.spatial.distance import mahalanobis
from ospa import *
from target import Target
from BranchAndBoundSolver import BBTreeNode, get_possible_edges
from optimization_utils import *
from reconfig_utils import *
class PHDFilterNetwork:
def __init__(self,
nodes,
weights,
G,
merge_thresh=1):
self.network = G
nx.set_node_attributes(self.network, nodes, 'node')
nx.set_node_attributes(self.network, weights, 'weights')
self.target_estimates = []
"""
Dictionary of form {node_id : components} which indicates which
components belonging to node_id to share with neighbors
"""
self.node_share = {}
"""
Dictionary of form {node_id : {neighbor_id: components}} which
indicates which components of neighbor_id were shared with node_id
"""
self.node_neighbor_comps = {}
"""
Dictionary of form {node_id : {comp_id: components}} which
indicates which components to fuse with the comp_id of node_id
"""
self.node_fuse_comps = {}
"""
Dictionary of form {node_id : {comp_id: weights}} which indicates
which weights to use when fusing components in self.node_fuse_comps
with comp_id of node_id
"""
self.node_fuse_weights = {}
"""
Dictionary of form {node_id : cardinality_estimate} the estimate
of total targets for node_id
"""
self.cardinality = {}
self.merge_thresh = merge_thresh
# TRACKERS
self.failures = {}
self.adjacencies = {}
self.weighted_adjacencies = {}
self.errors = {}
self.max_trace_cov = {}
self.mean_trace_cov = {}
self.gospa = {}
self.nmse_card = {}
"""
Simulation Operations
"""
def step_through(self, measurements, true_targets,
L=3, how='geom', opt='agent',
fail_int=None, fail_sequence=None,
single_node_fail=False,
base=False, noise_mult=1):
nodes = nx.get_node_attributes(self.network, 'node')
if not isinstance(measurements, dict):
measurements = {0: measurements}
true_targets = {0: true_targets}
failure = False
for i, m in measurements.items():
if fail_int is not None or fail_sequence is not None:
if fail_int is not None:
if i in fail_int:
failure = True
fail_node = self.apply_failure(i, mult=noise_mult, single_node_fail=single_node_fail)
else:
if i in fail_sequence:
failure = True
fail_node = self.apply_failure(i, fail=fail_sequence[i], single_node_fail=single_node_fail)
"""
Local PHD Estimation
"""
for id, n in nodes.items():
n.step_through(m, i)
"""
Do Optimization and Formation Synthesis
"""
if failure and not base:
if opt == 'agent':
self.do_agent_opt(fail_node, how=how)
elif opt == 'team':
self.do_team_opt(fail_node, how=how)
elif opt == 'greedy':
self.do_greedy_opt(fail_node, how=how)
# Random strategy
else:
self.do_random_opt(fail_node)
# Formation Synthesis
current_coords = {nid: n.position for nid, n in nodes.items()}
fov = {nid: n.fov for nid, n in nodes.items()}
centroid = self.get_centroid(fail_node)
new_coords = generate_coords(self.adjacency_matrix(),
current_coords, fov, centroid)
if new_coords:
for id, n in nodes.items():
n.update_position(new_coords[id])
failure = False
"""
Core Fusion Steps
1) Cardinality Consensus
2) Geometric or Arithmetic Fusion
3) Rescaling fused weights according to cardinality consensus
"""
L = int(max(3.0, len(nodes) / 2.0))
for l in range(L):
self.cardinality_consensus()
self.rescale_component_weights()
for id, n in nodes.items():
n.update_trackers(i, pre_consensus=False)
trace_covs = self.get_trace_covariances()
self.max_trace_cov[i] = max(trace_covs)
self.mean_trace_cov[i] = np.mean(trace_covs)
self.errors[i] = self.calc_errors(true_targets[i])
self.gospa[i] = self.calc_ospa(true_targets[i])
self.nmse_card[i] = self.calc_nmse_card(true_targets[i])
self.adjacencies[i] = self.adjacency_matrix()
self.weighted_adjacencies[i] = self.weighted_adjacency_matrix()
def apply_failure(self, i, fail=None, mult=1, single_node_fail=False):
nodes = nx.get_node_attributes(self.network, 'node')
# Generate new R
if fail is None:
if single_node_fail:
fail_node = 0
else:
fail_node = np.random.choice(list(nodes.keys()))
# Get R from Node
R = nodes[fail_node].R
r_mat_size = R.shape[0]
r = scipy.random.rand(r_mat_size, r_mat_size) * mult
rpd = np.dot(r, r.T)
else:
fail_node = fail[0]
rpd = fail[1]
R = nodes[fail_node].R
R = R + rpd
nodes[fail_node].R = R
self.failures[i] = (fail_node, rpd)
return fail_node
def get_centroid(self, fail_node):
node = nx.get_node_attributes(self.network, 'node')[fail_node]
all_phd_states = []
for t in node.targets:
pos = np.array([[t.state[0][0]], [t.state[1][0]]])
all_phd_states.append(pos)
if len(all_phd_states) == 0:
return np.array([[0], [0]])
x, y = zip(*all_phd_states)
center_x = sum(x) / float(len(x))
center_y = sum(y) / float(len(x))
return np.array([[center_x], [center_y]])
"""
Optimization
"""
def construct_blockdiag_cov(self, node_id, min_cardinality):
node = nx.get_node_attributes(self.network, 'node')[node_id]
node_comps = node.targets
if len(node_comps) == 1:
return node_comps[0].state_cov
else:
bd = scipy.linalg.block_diag(node_comps[0].state_cov,
node_comps[1].state_cov)
for i in range(2, int(np.floor(min_cardinality))):
bd = scipy.linalg.block_diag(bd, node_comps[i].state_cov)
return bd
def prep_optimization_data(self, how='geom'):
# get min_cardinality
min_cardinality_index = min(self.cardinality.keys(),
key=(lambda k: self.cardinality[k]))
min_cardinality = self.cardinality[min_cardinality_index]
# get the covariance data
cov_data = []
for node_id in list(self.network.nodes()):
P = self.construct_blockdiag_cov(node_id, min_cardinality)
if how == 'geom':
P = np.linalg.inv(P)
cov_data.append(P)
return min_cardinality, cov_data
def do_agent_opt(self, failed_node, how='geom'):
nodes = nx.get_node_attributes(self.network, 'node')
min_cardinality, cov_data = self.prep_optimization_data(how=how)
current_weights = nx.get_node_attributes(self.network, 'weights')
covariance_data = []
for c in cov_data:
det_c = np.linalg.det(c)
covariance_data.append((1/min_cardinality) * det_c)
# _, _, new_config, new_weights = agent_opt(self.adjacency_matrix(),
# current_weights,
# covariance_data,
# failed_node=failed_node)
# GET POSSIBLE EDGES
possible_edge_decisions, current_edge_decisions = \
get_possible_edges(failed_node, self.adjacency_matrix())
# SOLVE PROBLEM WITH BRANCH AND BOUND SOLVER
root = BBTreeNode(possible_edge_decisions, current_edge_decisions,
self.adjacency_matrix(), current_weights,
covariance_data, failed_node=failed_node, opt='agent')
bestobj, bestnode, new_config, new_weights = root.bbsolve()
G = nx.from_numpy_matrix(new_config)
self.network = G
nx.set_node_attributes(self.network, nodes, 'node')
# new_weights = self.get_metro_weights()
nx.set_node_attributes(self.network, new_weights, 'weights')
def do_team_opt(self, failed_node, how='geom'):
nodes = nx.get_node_attributes(self.network, 'node')
min_cardinality, cov_data = self.prep_optimization_data(how=how)
current_weights = nx.get_node_attributes(self.network, 'weights')
# _, _, new_config, new_weights = team_opt(self.adjacency_matrix(),
# current_weights,
# cov_data,
# how=how)
# GET POSSIBLE EDGES
possible_edge_decisions, current_edge_decisions = \
get_possible_edges(failed_node, self.adjacency_matrix())
# SOLVE PROBLEM WITH BRANCH AND BOUND SOLVER
root = BBTreeNode(possible_edge_decisions, current_edge_decisions,
self.adjacency_matrix(), current_weights,
cov_data, opt='team')
bestobj, bestnode, new_config, new_weights = \
root.bbsolve(fuse_method=how)
G = nx.from_numpy_matrix(new_config)
self.network = G
nx.set_node_attributes(self.network, nodes, 'node')
# new_weights = self.get_metro_weights()
nx.set_node_attributes(self.network, new_weights, 'weights')
def do_greedy_opt(self, failed_node, how='geom'):
nodes = nx.get_node_attributes(self.network, 'node')
min_cardinality, cov_data = self.prep_optimization_data(how=how)
current_neighbors = list(self.network.neighbors(failed_node))
best_cov_id = None
best_cov = np.inf
for neighbor_id in list(nodes):
if neighbor_id not in current_neighbors:
if np.linalg.det(cov_data[neighbor_id]) < best_cov:
best_cov_id = neighbor_id
if best_cov_id is None:
pass
else:
new_config = self.adjacency_matrix()
new_config[failed_node, best_cov_id] = 1
new_config[best_cov_id, failed_node] = 1
G = nx.from_numpy_matrix(new_config)
self.network = G
nx.set_node_attributes(self.network, nodes, 'node')
new_weights = self.get_metro_weights()
nx.set_node_attributes(self.network, new_weights, 'weights')
def do_random_opt(self, failed_node):
nodes = nx.get_node_attributes(self.network, 'node')
current_neighbors = list(self.network.neighbors(failed_node))
non_neighbors = []
for neighbor_id in list(nodes):
if neighbor_id not in current_neighbors:
non_neighbors.append(neighbor_id)
if len(non_neighbors) == 0:
pass
else:
new_neighbor_id = np.random.choice(non_neighbors)
new_config = self.adjacency_matrix()
new_config[failed_node, new_neighbor_id] = 1
new_config[new_neighbor_id, failed_node] = 1
G = nx.from_numpy_matrix(new_config)
self.network = G
nx.set_node_attributes(self.network, nodes, 'node')
new_weights = self.get_metro_weights()
nx.set_node_attributes(self.network, new_weights, 'weights')
"""
Core Fusion Steps
1) Cardinality Consensus
2) Geometric or Arithmetic Fusion
3) Rescaling fused weights according to cardinality consensus
"""
def cardinality_consensus(self):
"""
1) Each sensor gets local estimation of number of targets
2a) Each sensor shares local estimation with neighbors
2b) Each sensor updates their local estimation of number of targets
Note: the third step weight scaling occurs after the
geometric/arithmetic fusion
:return:
"""
nodes = nx.get_node_attributes(self.network, 'node')
metro_weights = nx.get_node_attributes(self.network, 'weights')
"""
Get Local Estimation of number of targets
"""
local_estimates = {}
for n in list(self.network.nodes()):
all_weights = [t.weight for t in nodes[n].targets]
sum_weights = np.nansum(all_weights)
tot_targets = len(all_weights)
local_estimates[n] = min([tot_targets, sum_weights])
"""
Share Local Estimation with neighbors
shared_estimates is dict of form: {node_id: {neighbor_id: estimate}}
"""
shared_estimates = {}
for node_id in list(self.network.nodes()):
neighbors = list(self.network.neighbors(node_id))
neighbor_estimates = {}
for neighbor_id in neighbors:
neighbor_estimates[neighbor_id] = local_estimates[neighbor_id]
shared_estimates[node_id] = neighbor_estimates
"""
Updates local estimation of number of targets via fusion with
neighbor estimates
"""
cardinality = {}
for node_id in list(self.network.nodes()):
weighted_estimate = 0
neighbor_weights = metro_weights[node_id]
for neighbor_id, estimate in shared_estimates[node_id].items():
neighbor_estimate = estimate
neighbor_weight = neighbor_weights[neighbor_id]
weighted_estimate += neighbor_weight * neighbor_estimate
if np.floor(weighted_estimate) > len(nodes[node_id].targets):
c = len(nodes[node_id].targets)
else:
c = np.floor(weighted_estimate)
cardinality[node_id] = c
self.cardinality = cardinality
def fuse_components(self, how='geom'):
"""
Run utils to help with fusion
"""
self.share_comps()
self.get_neighbors_comps()
self.get_comps_to_fuse()
new_comps = {}
for node_id in list(self.network.nodes()):
if how == 'geom':
fused_comps = self.geometric_fusion(node_id)
else:
fused_comps = self.arithmetic_fusion(node_id)
new_comps[node_id] = fused_comps
return new_comps
def rescale_component_weights(self):
nodes = nx.get_node_attributes(self.network, 'node')
for node_id in list(self.network.nodes()):
old_estimate = np.nansum([t.weight
for t in nodes[node_id].targets])
new_estimate = self.cardinality[node_id]
if old_estimate == 0:
rescaler = 1
else:
rescaler = new_estimate / old_estimate
targets = nodes[node_id].targets
for t in targets:
t.weight = rescaler * t.weight
targets.sort(key=attrgetter('weight'), reverse=True)
nodes[node_id].targets = targets
"""
Fusion Utils (for both Geometric and Arithmetic fusion)
"""
def share_comps(self):
"""
Prerequisite: after local PHD estimates
Creates dictionary of form {node_id : components} which indicates which
components belonging to node_id to share with neighbors
:param node_id:
:return:
"""
node_share = {}
for node_id in list(self.network.nodes()):
node = nx.get_node_attributes(self.network, 'node')[node_id]
node_share[node_id] = node.targets
self.node_share = node_share
def get_neighbors_comps(self):
"""
Prerequisite: after local PHD estimates
:return: None
Dictionary of form {node_id : {neighbor_id: components}} which
indicates which components of neihbor_id were shared with node_id
"""
G = self.network
node_neighbor_comps = {}
for node_id in list(self.network.nodes()):
node_neighbor_comps[node_id] = {}
neighbors = list(G.neighbors(node_id))
for neighbor_id in neighbors:
neighbor_node = nx.get_node_attributes(G, 'node')[neighbor_id]
neighbor_comps = neighbor_node.targets
node_neighbor_comps[node_id][neighbor_id] = neighbor_comps
self.node_neighbor_comps = node_neighbor_comps
def get_comps_to_fuse(self):
"""
Prerequisite: after sharing comps and getting neighbor comps
Updates the dictionaries self.node_fuse_comps and
self.node_fuse_weights to aid fusion
:param node_id:
:return None:
"""
G = self.network
for node_id in list(self.network.nodes()):
node_comps = self.node_share[node_id]
neighbor_comps = self.node_neighbor_comps[node_id]
metro_weights = nx.get_node_attributes(self.network,
'weights')[node_id]
node_weight = metro_weights[node_id]
self.node_fuse_comps[node_id] = {}
self.node_fuse_weights[node_id] = {}
for i in range(len(node_comps)):
c0 = node_comps[i]
fuse_comps = [c0]
fuse_weights = [node_weight]
for neighbor_id, comps in neighbor_comps.items():
"""
For each neighbor, find closest comp to node_comp[i]
within merge_thresh (using mahalanobis distance)
"""
closest_comp = None
distances = [self.get_mahalanobis(c1, c0) for c1 in comps]
closest_comp_index = int(np.argmin(distances))
if distances[closest_comp_index] <= self.merge_thresh:
closest_comp = comps[closest_comp_index]
if closest_comp is not None:
fuse_comps.append(closest_comp)
fuse_weights.append(metro_weights[neighbor_id])
self.node_fuse_comps[node_id][i] = fuse_comps
self.node_fuse_weights[node_id][i] = fuse_weights
"""
Arithmetic Fusion
"""
def arithmetic_fusion(self, node_id):
"""
PRE-REQUISITE: that you ran self.get_comps_to_fuse
:param node_id:
:return list of new components for node_i:
"""
node_comps = self.node_share[node_id]
covs = self.fuse_covs_arith(node_id)
states, alphas = self.fuse_states_alphas_arith(node_id)
fuse_comps = []
for i in range(len(node_comps)):
new_comp = Target(init_weight=alphas[i],
init_state=states[i],
init_cov=covs[i])
fuse_comps.append(new_comp)
return fuse_comps
def fuse_covs_arith(self, node_id):
node_comps = self.node_share[node_id]
new_covs = []
for i in range(len(node_comps)):
fuse_comps = self.node_fuse_comps[node_id][i]
fuse_weights = self.node_fuse_weights[node_id][i]
if len(fuse_comps) == 1:
"""
If nothing to fuse with component (no close enough
components from neighbors to fuse) keep current covariance
"""
new_covs.append(node_comps[i].state_cov)
else:
"""
Rescale weights to equal 1 if necessary
(only necessary if not all neighbors contributed)
"""
sum_fuse_weights = float(sum(fuse_weights))
fuse_weights = [fw / sum_fuse_weights for fw in fuse_weights]
"""
Fuse According to Arith Fusion in paper
"""
fuse_comps_weighted = []
for j in range(len(fuse_comps)):
w = fuse_weights[j]
cov = fuse_comps[j].state_cov
fuse_comps_weighted.append(w * cov)
sum_covs = np.sum(fuse_comps_weighted, 0)
new_covs.append(sum_covs)
return new_covs
def fuse_states_alphas_arith(self, node_id):
node_comps = self.node_share[node_id]
new_states = []
new_alphas = []
for i in range(len(node_comps)):
fuse_comps = self.node_fuse_comps[node_id][i]
if len(fuse_comps) == 1:
"""
If nothing to fuse with component (no close enough
components from neighbors to fuse) keep current state
"""
new_states.append(node_comps[i].state)
new_alphas.append(node_comps[i].weight)
else:
"""
Fuse According to Arith Fusion in paper
"""
fuse_alpha_weighted_states = []
fuse_alphas = []
for j in range(len(fuse_comps)):
alpha = fuse_comps[j].weight
x = fuse_comps[j].state
fuse_alpha_weighted_states.append(alpha * x)
fuse_alphas.append(alpha)
sum_alpha_weighted_states = np.sum(fuse_alpha_weighted_states,
0)
sum_alphas = np.sum(fuse_alphas)
if sum_alphas == 0:
new_states.append(sum_alpha_weighted_states)
new_alphas.append(sum_alphas)
continue
new_states.append(sum_alpha_weighted_states / sum_alphas)
new_alphas.append(sum_alphas)
return new_states, new_alphas
"""
Geometric Fusion
"""
def geometric_fusion(self, node_id):
"""
PRE-REQUISITE: that you ran self.get_comps_to_fuse
:param node_id:
:return list of new components for node_i:
"""
node_comps = self.node_share[node_id]
Ks = self.get_k(node_id)
covs = self.fuse_covs_geom(node_id)
states = self.fuse_states_geom(node_id, covs)
alphas = self.fuse_alphas_geom(node_id, Ks)
fuse_comps = []
for i in range(len(node_comps)):
new_comp = Target(init_weight=alphas[i],
init_state=states[i],
init_cov=covs[i])
fuse_comps.append(new_comp)
return fuse_comps
def get_k(self, node_id):
"""
For each node_id, for each set of components to fuse, calculate K factor
:param node_id:
:return list of Ks:
"""
node_comps = self.node_share[node_id]
Ks = []
for i in range(len(node_comps)):
fuse_comps = self.node_fuse_comps[node_id][i]
fuse_weights = self.node_fuse_weights[node_id][i]
if len(fuse_comps) == 1:
"""
If nothing to fuse with component (no close enough
components from neighbors to fuse) no need to calculate K
"""
Ks.append(1)
else:
"""
Rescale weights to equal 1 if necessary
(only necessary if not all neighbors contributed)
"""
sum_fuse_weights = float(sum(fuse_weights))
fuse_weights = [fw / sum_fuse_weights for fw in fuse_weights]
k = self.calcK(fuse_comps, fuse_weights)
Ks.append(k)
return Ks
def fuse_covs_geom(self, node_id):
"""
For each component of node_id, fuse with the closest components from
neighboprs of node_id
:param node_id:
:return new covariances:
"""
node_comps = self.node_share[node_id]
new_covs = []
for i in range(len(node_comps)):
fuse_comps = self.node_fuse_comps[node_id][i]
fuse_weights = self.node_fuse_weights[node_id][i]
if len(fuse_comps) == 1:
"""
If nothing to fuse with component (no close enough
components from neighbors to fuse) keep current covariance
"""
new_covs.append(node_comps[i].state_cov)
else:
"""
Rescale weights to equal 1 if necessary
(only necessary if not all neighbors contributed)
"""
sum_fuse_weights = float(sum(fuse_weights))
fuse_weights = [fw / sum_fuse_weights for fw in fuse_weights]
"""
Fuse According to Geom Fusion in paper
"""
fuse_comps_weighted = []
for j in range(len(fuse_comps)):
w = fuse_weights[j]
inv_cov = np.linalg.inv(fuse_comps[j].state_cov)
fuse_comps_weighted.append(w * inv_cov)
sum_covs = np.sum(fuse_comps_weighted, 0)
new_covs.append(np.linalg.inv(sum_covs))
return new_covs
def fuse_states_geom(self, node_id, new_covs):
"""
For each component of node_id, fuse with the closest components from
neighboprs of node_id
:param node_id:
:return new states:
"""
node_comps = self.node_share[node_id]
new_states = []
for i in range(len(node_comps)):
fuse_comps = self.node_fuse_comps[node_id][i]
fuse_weights = self.node_fuse_weights[node_id][i]
if len(fuse_comps) == 1:
"""
If nothing to fuse with component (no close enough
components from neighbors to fuse) keep current state
"""
new_states.append(node_comps[i].state)
else:
"""
Rescale weights to equal 1 if necessary
(only necessary if not all neighbors contributed)
"""
sum_fuse_weights = float(sum(fuse_weights))
fuse_weights = [fw / sum_fuse_weights for fw in fuse_weights]
"""
Fuse According to Geom Fusion in paper
"""
fuse_comps_weighted = []
for j in range(len(fuse_comps)):
w = fuse_weights[j]
inv_cov = np.linalg.inv(fuse_comps[j].state_cov)
x = fuse_comps[j].state
fuse_comps_weighted.append(w * inv_cov * x)
sum_states = np.sum(fuse_comps_weighted, 0)
new_states.append(new_covs[i] * sum_states)
return new_states
def fuse_alphas_geom(self, node_id, K):
"""
For each component of node_id, fuse with the closest components from
neighboprs of node_id
:param node_id:
:return new alphas:
"""
node_comps = self.node_share[node_id]
new_alphas = []
for i in range(len(node_comps)):
fuse_comps = self.node_fuse_comps[node_id][i]
fuse_weights = self.node_fuse_weights[node_id][i]
if len(fuse_comps) == 1:
"""
If nothing to fuse with component (no close enough
components from neighbors to fuse) keep current alpha
(component weight)
"""
new_alphas.append(node_comps[i].weight)
else:
"""
Rescale weights to equal 1 if necessary
(only necessary if not all neighbors contributed)
"""
sum_fuse_weights = float(sum(fuse_weights))
fuse_weights = [fw / sum_fuse_weights for fw in fuse_weights]
"""
Fuse According to Geom Fusion in paper
"""
fuse_comps_weighted = []
for j in range(len(fuse_comps)):
w = fuse_weights[j]
alpha = fuse_comps[j].weight
alpha_w = alpha ** w
r = self.rescaler(fuse_comps[j].state_cov, fuse_weights[j])
fuse_comps_weighted.append(alpha_w * r)
prod_alpha = np.prod(fuse_comps_weighted, 0)
new_alphas.append(prod_alpha * K[i])
return new_alphas
"""
Network Operations
"""
def adjacency_matrix(self):
G = self.network
num_nodes = len(list(G.nodes()))
A = nx.adjacency_matrix(G).todense()
if not np.array_equal(np.diag(A), np.ones(num_nodes)):
A = A + np.diag(np.ones(num_nodes))
return A
def weighted_adjacency_matrix(self):
A = self.adjacency_matrix()
G = self.network
for n in list(G.nodes()):
weights = nx.get_node_attributes(G, 'weights')
A[n, n] = weights[n][n]
for i, neighbor in enumerate(list(G.neighbors(n))):
A[n, neighbor] = weights[n][neighbor]
return A
def get_metro_weights(self):
G = self.network
num_nodes = len(list(self.network.nodes))
weight_attrs = {}
for i in range(num_nodes):
weight_attrs[i] = {}
self_degree = G.degree(i)
metropolis_weights = []
for n in G.neighbors(i):
degree = G.degree(n)
mw = 1 / (1 + max(self_degree, degree))
weight_attrs[i][n] = mw
metropolis_weights.append(mw)
weight_attrs[i][i] = 1 - sum(metropolis_weights)
return weight_attrs
"""
Metric Calculations
"""
def get_trace_covariances(self):
min_cardinality_index = min(self.cardinality.keys(),
key=(lambda k: self.cardinality[k]))
min_cardinality = self.cardinality[min_cardinality_index]
cov_data = []
for node_id in list(self.network.nodes()):
P = self.construct_blockdiag_cov(node_id, min_cardinality)
cov_data.append(np.trace(P))
return cov_data
def calc_errors(self, true_targets):
nodes = nx.get_node_attributes(self.network, 'node')
max_errors = []
for i, t in enumerate(true_targets):
errors = []
for n, node in nodes.items():
if not node.check_measure_oob(t) and len(node.targets) > 0:
distances = [math.hypot(t[0] - comp.state[0][0],
t[1] - comp.state[1][0])
for comp in node.targets]
errors.append(min(distances))
if len(errors) == 0:
max_errors.append(0)
else:
max_errors.append(max(errors))
return np.max(max_errors)
def calc_ospa(self, true_targets):
tracks = []
nodes = nx.get_node_attributes(self.network, 'node')
for n, node in nodes.items():
for t in node.targets:
tracks.append(np.array([[t.state[0][0]],
[t.state[1][0]]]))
gospa, \
target_to_track_assigments, \
gospa_localization, \
gospa_missed, \
gospa_false = calculate_gospa(true_targets, tracks)
return gospa
def calc_nmse_card(self, true_targets):
N = len(true_targets)
squared_error = 0
sum_card = 0
for n, card in self.cardinality.items():
squared_error += (card - N) ** 2
sum_card += card
if N == 0 and sum_card == 0:
return 0
if N == 0 or sum_card == 0:
return squared_error
return squared_error / (N * sum_card)
"""
Static Methods
"""
@staticmethod
def calcK(comps, weights):
# Omega and q
covs_weighted = []
states_weighted = []
for j in range(len(comps)):
w = weights[j]
inv_cov = np.linalg.inv(comps[j].state_cov)
x = comps[j].state
covs_weighted.append(w * inv_cov)
states_weighted.append(w * np.dot(inv_cov, x))
omega = np.sum(covs_weighted, 0)
q = np.sum(states_weighted, 0)
d = comps[0].state.shape[0]
# Kappa 1:n
firstterm_1n = len(comps) * d * np.log(2 * np.pi)
secondterm_1n = 0 # Filled in later
thirdterm_1n = 0 # Filled in later
# Kappa n
firstterm_n = d * np.log(2 * np.pi)
secondterm_n = 0 # Filled in later
thirdterm_n = np.dot(q.T, np.dot(np.linalg.inv(omega), q))
for i in range(len(comps)):
weight = weights[i]
state = comps[i].state
cov = comps[i].state_cov
secondterm_1n += np.log(np.linalg.det(weight * np.linalg.inv(cov)))
thirdterm_1n += weight * np.dot(state.T, np.dot(np.linalg.inv(cov),
state))
secondterm_n += np.linalg.det(weight * np.linalg.inv(cov))
kappa_1n = -0.5 * (firstterm_1n - secondterm_1n + thirdterm_1n)
kappa_n = -0.5 * (firstterm_n - np.log(secondterm_n) + thirdterm_n)
K = np.exp(kappa_1n[0][0] - kappa_n[0][0])
return K
@staticmethod
@njit
def rescaler(cov, weight):
numer = np.linalg.det(2 * np.pi * np.linalg.inv(cov / weight))
denom = np.linalg.det(2 * np.pi * cov) ** weight
return (numer / denom) ** 0.5