-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_implementation.py
More file actions
1469 lines (1211 loc) · 59.7 KB
/
Copy pathstreamlit_implementation.py
File metadata and controls
1469 lines (1211 loc) · 59.7 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 streamlit as st
import numpy as np
import random
import networkx as nx
import matplotlib.pyplot as plt
from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
import seaborn as sns
from collections import defaultdict
import time
from typing import List, Tuple, Dict, Set
import math
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from matplotlib.animation import FuncAnimation
from matplotlib.patches import Circle
import matplotlib.patches as mpatches
# Set page configuration
st.set_page_config(
page_title="MANET Black Hole Attack Detection",
page_icon="🌐",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
font-size: 3rem;
color: #1e3d59;
text-align: center;
margin-bottom: 2rem;
}
.sub-header {
font-size: 1.5rem;
color: #3e5c76;
text-align: center;
margin-bottom: 3rem;
}
.metric-card {
background-color: #f5f5f5;
border-radius: 10px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.stProgress .st-bo {
background-color: #1e3d59;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
if 'simulation_results' not in st.session_state:
st.session_state.simulation_results = None
if 'current_scenario' not in st.session_state:
st.session_state.current_scenario = None
class MANETNode:
"""Represents a node in the MANET"""
def __init__(self, node_id: int, x: float, y: float, is_blackhole: bool = False):
self.id = node_id
self.x = x
self.y = y
self.is_blackhole = is_blackhole
self.neighbors = set()
self.routing_table = {}
self.packet_drop_rate = 0.0 if not is_blackhole else random.uniform(0.7, 1.0)
self.energy = 100.0
self.trust_value = 1.0
self.packets_sent = 0
self.packets_received = 0
self.packets_forwarded = 0
self.packets_dropped = 0
def distance_to(self, other: 'MANETNode') -> float:
return np.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
class MANETSimulator:
"""Simulates a Mobile Ad-hoc Network with black hole attacks"""
def __init__(self, num_nodes: int, area_size: float, transmission_range: float,
blackhole_percentage: float = 0.2):
self.num_nodes = num_nodes
self.area_size = area_size
self.transmission_range = transmission_range
self.nodes = []
self.blackhole_nodes = set()
self.network_graph = nx.Graph()
# Create nodes
num_blackholes = int(num_nodes * blackhole_percentage)
blackhole_indices = random.sample(range(num_nodes), num_blackholes)
for i in range(num_nodes):
x = random.uniform(0, area_size)
y = random.uniform(0, area_size)
is_blackhole = i in blackhole_indices
node = MANETNode(i, x, y, is_blackhole)
self.nodes.append(node)
if is_blackhole:
self.blackhole_nodes.add(i)
self._update_network_topology()
def _update_network_topology(self):
"""Update network topology based on transmission range"""
self.network_graph.clear()
# Add all nodes to the graph first (including isolated nodes)
for i in range(self.num_nodes):
self.network_graph.add_node(i)
# Clear neighbors
for node in self.nodes:
node.neighbors.clear()
# Add edges based on transmission range
for i, node1 in enumerate(self.nodes):
for j, node2 in enumerate(self.nodes[i+1:], i+1):
if node1.distance_to(node2) <= self.transmission_range:
node1.neighbors.add(j)
node2.neighbors.add(i)
self.network_graph.add_edge(i, j)
def simulate_packet_transmission(self, source_id: int, dest_id: int) -> Tuple[bool, List[int]]:
"""Simulate packet transmission from source to destination"""
if source_id == dest_id:
return True, [source_id]
# Check if both nodes exist in the graph
if source_id not in self.network_graph or dest_id not in self.network_graph:
return False, []
try:
path = nx.shortest_path(self.network_graph, source_id, dest_id)
except nx.NetworkXNoPath:
return False, []
# Simulate packet forwarding through the path
for i in range(len(path) - 1):
current_node = self.nodes[path[i]]
next_node = self.nodes[path[i + 1]]
current_node.packets_sent += 1
# Check if next node is a black hole
if next_node.is_blackhole:
# Black hole drops packet based on its drop rate
if random.random() < next_node.packet_drop_rate:
next_node.packets_dropped += 1
return False, path[:i+2]
next_node.packets_received += 1
if i < len(path) - 2: # Not the final destination
next_node.packets_forwarded += 1
return True, path
def collect_node_features(self) -> np.ndarray:
"""Collect features for each node for detection algorithms"""
features = []
for node in self.nodes:
total_packets = node.packets_sent + node.packets_received + node.packets_forwarded
if total_packets > 0:
drop_ratio = node.packets_dropped / total_packets
else:
drop_ratio = 0
# Calculate neighbor trust average
neighbor_trust = []
for neighbor_id in node.neighbors:
neighbor_trust.append(self.nodes[neighbor_id].trust_value)
avg_neighbor_trust = np.mean(neighbor_trust) if neighbor_trust else 1.0
# Node features
node_features = [
drop_ratio, # Packet drop ratio
node.energy / 100.0, # Normalized energy
node.trust_value, # Trust value
len(node.neighbors) / self.num_nodes, # Normalized degree
avg_neighbor_trust, # Average neighbor trust
node.packets_forwarded / (node.packets_received + 1), # Forward ratio
]
features.append(node_features)
return np.array(features)
class DolphinEcholocationOptimizer:
"""Basic Dolphin Echolocation Algorithm for black hole detection"""
def __init__(self, n_dolphins: int = 20, max_iterations: int = 100):
self.n_dolphins = n_dolphins
self.max_iterations = max_iterations
self.detection_threshold = 0.5
def _calculate_fitness(self, position: np.ndarray, features: np.ndarray) -> float:
"""Calculate fitness based on detection accuracy"""
# Use position as weights for feature importance
weights = np.abs(position) / (np.sum(np.abs(position)) + 1e-10)
scores = np.dot(features, weights)
return np.mean(scores)
def detect_blackholes(self, features: np.ndarray) -> np.ndarray:
"""Detect black holes using Dolphin Echolocation"""
n_features = features.shape[1]
# Initialize dolphin positions
dolphins = np.random.randn(self.n_dolphins, n_features)
best_dolphin = None
best_fitness = -np.inf
for iteration in range(self.max_iterations):
# Evaluate fitness for each dolphin
fitness_values = []
for dolphin in dolphins:
fitness = self._calculate_fitness(dolphin, features)
fitness_values.append(fitness)
if fitness > best_fitness:
best_fitness = fitness
best_dolphin = dolphin.copy()
# Update dolphin positions (echolocation)
for i in range(self.n_dolphins):
# Generate echolocation clicks
frequency = np.random.uniform(0.1, 0.9)
amplitude = np.random.uniform(0.5, 1.5)
# Update position based on best dolphin
dolphins[i] += frequency * (best_dolphin - dolphins[i]) * amplitude
# Add random exploration
dolphins[i] += np.random.randn(n_features) * 0.1
# Calculate detection scores
weights = np.abs(best_dolphin) / (np.sum(np.abs(best_dolphin)) + 1e-10)
detection_scores = np.dot(features, weights)
# Threshold-based detection
predictions = (detection_scores > self.detection_threshold).astype(int)
return predictions
class HybridDEABCOptimizer:
"""Original Hybrid Dolphin Echolocation and Artificial Bee Colony Algorithm"""
def __init__(self, n_dolphins: int = 20, n_bees: int = 30, max_iterations: int = 100):
self.n_dolphins = n_dolphins
self.n_bees = n_bees
self.max_iterations = max_iterations
self.detection_threshold = 0.5
self.employed_bees = n_bees // 2
self.onlooker_bees = n_bees // 2
def _calculate_fitness(self, position: np.ndarray, features: np.ndarray) -> float:
"""Calculate fitness based on detection accuracy"""
weights = np.abs(position) / (np.sum(np.abs(position)) + 1e-10)
scores = np.dot(features, weights)
# Enhanced fitness with variance consideration
score_variance = np.var(scores)
return np.mean(scores) + 0.3 * score_variance
def _abc_phase(self, food_sources: np.ndarray, features: np.ndarray) -> np.ndarray:
"""Artificial Bee Colony optimization phase"""
n_features = food_sources.shape[1]
fitness_values = []
# Calculate fitness for all food sources
for source in food_sources:
fitness = self._calculate_fitness(source, features)
fitness_values.append(fitness)
fitness_values = np.array(fitness_values)
# Employed bee phase
for i in range(self.employed_bees):
# Select random dimension and neighbor
j = random.randint(0, n_features - 1)
k = random.choice([x for x in range(self.employed_bees) if x != i])
# Generate new solution
phi = random.uniform(-1, 1)
new_source = food_sources[i].copy()
new_source[j] = food_sources[i][j] + phi * (food_sources[i][j] - food_sources[k][j])
# Greedy selection
new_fitness = self._calculate_fitness(new_source, features)
if new_fitness > fitness_values[i]:
food_sources[i] = new_source
fitness_values[i] = new_fitness
# Calculate probabilities for onlooker bees
total_fitness = np.sum(fitness_values)
if total_fitness > 0:
probabilities = fitness_values / total_fitness
else:
probabilities = np.ones(len(fitness_values)) / len(fitness_values)
# Onlooker bee phase
for _ in range(self.onlooker_bees):
# Select food source based on probability
i = np.random.choice(self.employed_bees, p=probabilities)
# Similar to employed bee phase
j = random.randint(0, n_features - 1)
k = random.choice([x for x in range(self.employed_bees) if x != i])
phi = random.uniform(-1, 1)
new_source = food_sources[i].copy()
new_source[j] = food_sources[i][j] + phi * (food_sources[i][j] - food_sources[k][j])
new_fitness = self._calculate_fitness(new_source, features)
if new_fitness > fitness_values[i]:
food_sources[i] = new_source
fitness_values[i] = new_fitness
return food_sources
def detect_blackholes(self, features: np.ndarray) -> np.ndarray:
"""Detect black holes using hybrid DE-ABC algorithm"""
n_features = features.shape[1]
# Initialize populations
dolphins = np.random.randn(self.n_dolphins, n_features)
food_sources = np.random.randn(self.employed_bees, n_features)
best_solution = None
best_fitness = -np.inf
for iteration in range(self.max_iterations):
# Dolphin Echolocation phase
dolphin_fitness = []
for dolphin in dolphins:
fitness = self._calculate_fitness(dolphin, features)
dolphin_fitness.append(fitness)
if fitness > best_fitness:
best_fitness = fitness
best_solution = dolphin.copy()
# Update dolphin positions
for i in range(self.n_dolphins):
frequency = np.random.uniform(0.1, 0.9)
amplitude = np.random.uniform(0.5, 1.5)
# Echolocation update
dolphins[i] += frequency * (best_solution - dolphins[i]) * amplitude
# Information exchange with ABC
if i < self.employed_bees:
dolphins[i] += 0.2 * (food_sources[i] - dolphins[i])
# Random exploration
dolphins[i] += np.random.randn(n_features) * 0.05
# ABC phase
food_sources = self._abc_phase(food_sources, features)
# Update best solution from ABC
for source in food_sources:
fitness = self._calculate_fitness(source, features)
if fitness > best_fitness:
best_fitness = fitness
best_solution = source.copy()
# Migration: Exchange best solutions between populations
if iteration % 10 == 0 and len(dolphin_fitness) > 0:
worst_dolphin_idx = np.argmin(dolphin_fitness)
source_fitness = [self._calculate_fitness(s, features) for s in food_sources]
if len(source_fitness) > 0:
best_source_idx = np.argmax(source_fitness)
dolphins[worst_dolphin_idx] = food_sources[best_source_idx].copy()
# Calculate detection scores using best solution
weights = np.abs(best_solution) / (np.sum(np.abs(best_solution)) + 1e-10)
detection_scores = np.dot(features, weights)
# Dynamic threshold adjustment
mean_score = np.mean(detection_scores)
std_score = np.std(detection_scores)
adaptive_threshold = mean_score + 0.5 * std_score
predictions = (detection_scores > adaptive_threshold).astype(int)
return predictions
class ImprovedHybridDEABCOptimizer:
"""Enhanced Hybrid Dolphin Echolocation and Artificial Bee Colony Algorithm"""
def __init__(self, n_dolphins: int = 30, n_bees: int = 40, max_iterations: int = 150):
self.n_dolphins = n_dolphins
self.n_bees = n_bees
self.max_iterations = max_iterations
self.employed_bees = n_bees // 2
self.onlooker_bees = n_bees // 2
self.abandonment_limit = 10
self.trial_counters = None
self.scaler = StandardScaler()
def _enhanced_features(self, features: np.ndarray) -> np.ndarray:
"""Extract enhanced features for better detection"""
n_samples, n_features = features.shape
enhanced = np.zeros((n_samples, n_features + 4))
# Original features
enhanced[:, :n_features] = features
# Additional statistical features
enhanced[:, n_features] = np.mean(features[:, :3], axis=1) # Mean of first 3 features
enhanced[:, n_features + 1] = np.std(features[:, :3], axis=1) # Std of first 3 features
enhanced[:, n_features + 2] = features[:, 0] * features[:, 5] # Drop ratio * forward ratio
enhanced[:, n_features + 3] = features[:, 2] * features[:, 4] # Trust * neighbor trust
return enhanced
def _calculate_adaptive_fitness(self, position: np.ndarray, features: np.ndarray,
labels_pred: np.ndarray = None) -> float:
"""Enhanced fitness calculation with adaptive weighting"""
weights = np.abs(position) / (np.sum(np.abs(position)) + 1e-10)
scores = np.dot(features, weights)
# Statistical measures
score_mean = np.mean(scores)
score_std = np.std(scores)
score_skew = np.mean(((scores - score_mean) / (score_std + 1e-10)) ** 3)
# Clustering penalty - penalize if predicted blackholes are too clustered
if labels_pred is not None:
blackhole_indices = np.where(labels_pred == 1)[0]
if len(blackhole_indices) > 1:
clustering_penalty = 1.0 / (np.std(blackhole_indices) + 1)
else:
clustering_penalty = 0
else:
clustering_penalty = 0
# Combined fitness with multiple objectives
fitness = (score_mean +
0.3 * score_std +
0.2 * abs(score_skew) -
0.1 * clustering_penalty)
return fitness
def _levy_flight(self, position: np.ndarray, best_position: np.ndarray,
iteration: int) -> np.ndarray:
"""Levy flight for exploration in dolphin movement"""
beta = 1.5
sigma = (math.gamma(1 + beta) * np.sin(np.pi * beta / 2) /
(math.gamma((1 + beta) / 2) * beta * 2 ** ((beta - 1) / 2))) ** (1 / beta)
u = np.random.randn(len(position)) * sigma
v = np.random.randn(len(position))
step = u / (np.abs(v) ** (1 / beta))
# Adaptive step size
step_size = 0.01 * np.exp(-iteration / self.max_iterations) * step
new_position = position + step_size * (best_position - position)
return new_position
def _adaptive_abc_phase(self, food_sources: np.ndarray, features: np.ndarray,
iteration: int) -> np.ndarray:
"""Enhanced ABC phase with adaptive mechanisms"""
n_features = food_sources.shape[1]
fitness_values = []
# Calculate fitness for all food sources
for source in food_sources:
fitness = self._calculate_adaptive_fitness(source, features)
fitness_values.append(fitness)
fitness_values = np.array(fitness_values)
# Employed bee phase with adaptive neighborhood
for i in range(self.employed_bees):
# Adaptive neighborhood size
neighborhood_size = max(2, int(self.employed_bees * (1 - iteration / self.max_iterations)))
neighbors = random.sample([x for x in range(self.employed_bees) if x != i],
min(neighborhood_size, self.employed_bees - 1))
# Multi-dimensional update
new_source = food_sources[i].copy()
n_dims = random.randint(1, max(1, n_features // 2))
dims = random.sample(range(n_features), n_dims)
for j in dims:
k = random.choice(neighbors)
phi = random.uniform(-1, 1) * (1 - 0.5 * iteration / self.max_iterations)
new_source[j] = food_sources[i][j] + phi * (food_sources[i][j] - food_sources[k][j])
# Boundary handling
new_source = np.clip(new_source, -3, 3)
# Greedy selection with probabilistic acceptance
new_fitness = self._calculate_adaptive_fitness(new_source, features)
if new_fitness > fitness_values[i]:
food_sources[i] = new_source
fitness_values[i] = new_fitness
self.trial_counters[i] = 0
else:
self.trial_counters[i] += 1
# Calculate adaptive probabilities
min_fitness = np.min(fitness_values)
max_fitness = np.max(fitness_values)
if max_fitness > min_fitness:
normalized_fitness = (fitness_values - min_fitness) / (max_fitness - min_fitness)
probabilities = 0.1 + 0.9 * normalized_fitness
probabilities = probabilities / np.sum(probabilities)
else:
probabilities = np.ones(len(fitness_values)) / len(fitness_values)
# Onlooker bee phase with tournament selection
for _ in range(self.onlooker_bees):
# Tournament selection
tournament_size = 3
candidates = np.random.choice(self.employed_bees, tournament_size, p=probabilities)
i = candidates[np.argmax(fitness_values[candidates])]
# Similar update as employed bees
new_source = food_sources[i].copy()
n_dims = random.randint(1, max(1, n_features // 2))
dims = random.sample(range(n_features), n_dims)
for j in dims:
k = random.choice([x for x in range(self.employed_bees) if x != i])
phi = random.uniform(-1, 1) * (1 - 0.5 * iteration / self.max_iterations)
new_source[j] = food_sources[i][j] + phi * (food_sources[i][j] - food_sources[k][j])
new_source = np.clip(new_source, -3, 3)
new_fitness = self._calculate_adaptive_fitness(new_source, features)
if new_fitness > fitness_values[i]:
food_sources[i] = new_source
fitness_values[i] = new_fitness
self.trial_counters[i] = 0
# Scout bee phase - abandon exhausted sources
for i in range(self.employed_bees):
if self.trial_counters[i] > self.abandonment_limit:
# Generate new source using Levy flight from best source
best_idx = np.argmax(fitness_values)
food_sources[i] = self._levy_flight(food_sources[i], food_sources[best_idx], iteration)
self.trial_counters[i] = 0
return food_sources
def _ensemble_decision(self, detection_scores: np.ndarray, features: np.ndarray) -> np.ndarray:
"""Ensemble decision making with multiple detection strategies"""
n_samples = len(detection_scores)
predictions = np.zeros((n_samples, 3))
# Strategy 1: Adaptive threshold based on score distribution
mean_score = np.mean(detection_scores)
std_score = np.std(detection_scores)
adaptive_threshold = mean_score + 0.5 * std_score
predictions[:, 0] = (detection_scores > adaptive_threshold).astype(int)
# Strategy 2: Isolation Forest for anomaly detection
iso_forest = IsolationForest(contamination=0.2, random_state=42)
predictions[:, 1] = (iso_forest.fit_predict(features) == -1).astype(int)
# Strategy 3: Percentile-based detection
percentile_threshold = np.percentile(detection_scores, 75)
predictions[:, 2] = (detection_scores > percentile_threshold).astype(int)
# Weighted voting
weights = [0.3, 0.3, 0.4] # Weights for each strategy
final_predictions = np.zeros(n_samples)
for i in range(n_samples):
weighted_vote = sum(predictions[i, j] * weights[j] for j in range(3))
final_predictions[i] = 1 if weighted_vote > 0.5 else 0
return final_predictions.astype(int)
def detect_blackholes(self, features: np.ndarray) -> np.ndarray:
"""Main detection method with enhanced hybrid algorithm"""
# Feature enhancement
enhanced_features = self._enhanced_features(features)
scaled_features = self.scaler.fit_transform(enhanced_features)
n_features = scaled_features.shape[1]
# Initialize populations
dolphins = np.random.randn(self.n_dolphins, n_features) * 0.5
food_sources = np.random.randn(self.employed_bees, n_features) * 0.5
self.trial_counters = np.zeros(self.employed_bees)
best_solution = None
best_fitness = -np.inf
fitness_history = []
# Early stopping parameters
patience = 20
no_improvement_count = 0
for iteration in range(self.max_iterations):
# Dolphin Echolocation phase with Levy flight
dolphin_fitness = []
for i, dolphin in enumerate(dolphins):
fitness = self._calculate_adaptive_fitness(dolphin, scaled_features)
dolphin_fitness.append(fitness)
if fitness > best_fitness:
best_fitness = fitness
best_solution = dolphin.copy()
no_improvement_count = 0
else:
no_improvement_count += 1
# Update dolphin positions with adaptive mechanisms
for i in range(self.n_dolphins):
# Adaptive frequency and amplitude
frequency = 0.1 + 0.8 * np.exp(-iteration / self.max_iterations)
amplitude = 1.5 * (1 - iteration / self.max_iterations)
# Echolocation with Levy flight
if random.random() < 0.3: # 30% chance of Levy flight
dolphins[i] = self._levy_flight(dolphins[i], best_solution, iteration)
else:
# Standard echolocation
dolphins[i] += frequency * (best_solution - dolphins[i]) * amplitude
# Information exchange with ABC
if i < self.employed_bees:
exchange_rate = 0.3 * (1 - iteration / self.max_iterations)
dolphins[i] += exchange_rate * (food_sources[i] - dolphins[i])
# Mutation for diversity
if random.random() < 0.1:
mutation_idx = random.randint(0, n_features - 1)
dolphins[i][mutation_idx] += np.random.randn() * 0.1
# Boundary handling
dolphins[i] = np.clip(dolphins[i], -3, 3)
# Enhanced ABC phase
food_sources = self._adaptive_abc_phase(food_sources, scaled_features, iteration)
# Update best solution from ABC
for source in food_sources:
fitness = self._calculate_adaptive_fitness(source, scaled_features)
if fitness > best_fitness:
best_fitness = fitness
best_solution = source.copy()
no_improvement_count = 0
# Population diversity maintenance
if iteration % 20 == 0:
# Replace worst performers with new random solutions
all_solutions = np.vstack([dolphins, food_sources])
all_fitness = [self._calculate_adaptive_fitness(s, scaled_features) for s in all_solutions]
worst_indices = np.argsort(all_fitness)[:5]
for idx in worst_indices:
if idx < self.n_dolphins:
dolphins[idx] = np.random.randn(n_features) * 0.5
else:
food_idx = idx - self.n_dolphins
if food_idx < len(food_sources):
food_sources[food_idx] = np.random.randn(n_features) * 0.5
fitness_history.append(best_fitness)
# Early stopping
if no_improvement_count > patience:
break
# Final detection with ensemble decision
weights = np.abs(best_solution) / (np.sum(np.abs(best_solution)) + 1e-10)
detection_scores = np.dot(scaled_features, weights)
# Use ensemble decision making
predictions = self._ensemble_decision(detection_scores, scaled_features)
return predictions
def visualize_network(manet: MANETSimulator, predictions: np.ndarray = None):
"""Visualize the MANET network"""
fig, ax = plt.subplots(figsize=(10, 10))
# Set up the plot
ax.set_xlim(0, manet.area_size)
ax.set_ylim(0, manet.area_size)
ax.set_aspect('equal')
ax.grid(True, alpha=0.3)
ax.set_xlabel('X Coordinate', fontsize=12)
ax.set_ylabel('Y Coordinate', fontsize=12)
ax.set_title('MANET Network Topology', fontsize=16, fontweight='bold')
# Draw transmission ranges for black holes
for node_id in manet.blackhole_nodes:
node = manet.nodes[node_id]
circle = Circle((node.x, node.y), manet.transmission_range,
fill=False, edgecolor='red', alpha=0.2, linestyle='--')
ax.add_patch(circle)
# Draw edges
for edge in manet.network_graph.edges():
node1 = manet.nodes[edge[0]]
node2 = manet.nodes[edge[1]]
ax.plot([node1.x, node2.x], [node1.y, node2.y],
'gray', alpha=0.3, linewidth=1)
# Draw nodes
for i, node in enumerate(manet.nodes):
if predictions is not None:
# Color based on detection results
if node.is_blackhole and predictions[i] == 1: # True Positive
color = 'darkred'
marker = 'X'
size = 200
label = 'True Positive'
elif node.is_blackhole and predictions[i] == 0: # False Negative
color = 'orange'
marker = 'v'
size = 200
label = 'False Negative'
elif not node.is_blackhole and predictions[i] == 1: # False Positive
color = 'yellow'
marker = '^'
size = 150
label = 'False Positive'
else: # True Negative
color = 'green'
marker = 'o'
size = 100
label = 'True Negative'
else:
# Color based on actual status
if node.is_blackhole:
color = 'red'
marker = 'X'
size = 200
label = 'Black Hole'
else:
color = 'blue'
marker = 'o'
size = 100
label = 'Normal Node'
ax.scatter(node.x, node.y, c=color, s=size, marker=marker,
edgecolors='black', linewidth=1, alpha=0.8)
ax.text(node.x, node.y-3, str(node.id), fontsize=8, ha='center')
# Create legend
if predictions is not None:
legend_elements = [
plt.scatter([], [], c='darkred', s=200, marker='X', edgecolors='black', label='True Positive'),
plt.scatter([], [], c='orange', s=200, marker='v', edgecolors='black', label='False Negative'),
plt.scatter([], [], c='yellow', s=150, marker='^', edgecolors='black', label='False Positive'),
plt.scatter([], [], c='green', s=100, marker='o', edgecolors='black', label='True Negative')
]
else:
legend_elements = [
plt.scatter([], [], c='red', s=200, marker='X', edgecolors='black', label='Black Hole'),
plt.scatter([], [], c='blue', s=100, marker='o', edgecolors='black', label='Normal Node')
]
ax.legend(handles=legend_elements, loc='upper right', bbox_to_anchor=(1.15, 1))
plt.tight_layout()
return fig
def create_performance_radar_chart(results: Dict):
"""Create radar chart for algorithm comparison"""
algorithms = list(results.keys())
# Calculate average metrics
metrics_data = {}
for algo in algorithms:
avg_precision = np.mean([r['precision'] for r in results[algo]])
avg_recall = np.mean([r['recall'] for r in results[algo]])
avg_f1 = np.mean([r['f1_score'] for r in results[algo]])
metrics_data[algo] = [avg_precision, avg_recall, avg_f1]
# Create radar chart
categories = ['Precision', 'Recall', 'F1-Score']
fig = go.Figure()
for algo in algorithms:
fig.add_trace(go.Scatterpolar(
r=metrics_data[algo],
theta=categories,
fill='toself',
name=algo
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 1]
)),
showlegend=True,
title="Algorithm Performance Comparison",
height=500
)
return fig
def create_metrics_comparison_chart(results: Dict, metric: str):
"""Create bar chart for specific metric comparison across scenarios"""
data = []
for algo_name, algo_results in results.items():
for result in algo_results:
data.append({
'Algorithm': algo_name,
'Scenario': result['scenario'],
'Value': result[metric]
})
df = pd.DataFrame(data)
fig = px.bar(df, x='Scenario', y='Value', color='Algorithm',
barmode='group',
title=f'{metric.replace("_", " ").title()} Comparison Across Scenarios',
labels={'Value': metric.replace('_', ' ').title()})
fig.update_layout(
xaxis_tickangle=-45,
height=500,
showlegend=True
)
return fig
def create_confusion_matrix_plot(results: Dict, algo_name: str):
"""Create confusion matrix visualization"""
# Calculate total confusion matrix
total_tp = sum(r['true_positives'] for r in results[algo_name])
total_fp = sum(r['false_positives'] for r in results[algo_name])
total_fn = sum(r['false_negatives'] for r in results[algo_name])
total_tn = sum(r['true_negatives'] for r in results[algo_name])
cm = np.array([[total_tn, total_fp], [total_fn, total_tp]])
fig = go.Figure(data=go.Heatmap(
z=cm,
text=cm,
texttemplate='%{text}',
textfont={"size": 20},
x=['Predicted Normal', 'Predicted Black Hole'],
y=['Actual Normal', 'Actual Black Hole'],
colorscale='Blues',
showscale=True
))
fig.update_layout(
title=f'Confusion Matrix - {algo_name}',
xaxis_title='Predicted Label',
yaxis_title='Actual Label',
height=400
)
return fig
def simulate_packet_animation(manet: MANETSimulator, path: List[int], success: bool):
"""Create packet transmission animation"""
fig, ax = plt.subplots(figsize=(10, 10))
# Set up the plot
ax.set_xlim(0, manet.area_size)
ax.set_ylim(0, manet.area_size)
ax.set_aspect('equal')
ax.grid(True, alpha=0.3)
# Draw network
for edge in manet.network_graph.edges():
node1 = manet.nodes[edge[0]]
node2 = manet.nodes[edge[1]]
ax.plot([node1.x, node2.x], [node1.y, node2.y],
'gray', alpha=0.3, linewidth=1)
# Draw nodes
for node in manet.nodes:
if node.is_blackhole:
color = 'red'
marker = 'X'
size = 200
else:
color = 'blue'
marker = 'o'
size = 100
ax.scatter(node.x, node.y, c=color, s=size, marker=marker,
edgecolors='black', linewidth=1, alpha=0.8)
ax.text(node.x, node.y-3, str(node.id), fontsize=8, ha='center')
# Highlight path
if len(path) > 1:
path_x = [manet.nodes[node_id].x for node_id in path]
path_y = [manet.nodes[node_id].y for node_id in path]
ax.plot(path_x, path_y, 'g-', linewidth=3, alpha=0.5, label='Packet Path')
# Mark source and destination
ax.scatter(path_x[0], path_y[0], c='green', s=300, marker='s',
edgecolors='black', linewidth=2, label='Source', zorder=5)
ax.scatter(path_x[-1], path_y[-1], c='blue', s=300, marker='D',
edgecolors='black', linewidth=2, label='Destination', zorder=5)
# Show packet position
packet_marker, = ax.plot([], [], 'yo', markersize=15, label='Packet', zorder=10)
# Animation frames
frames = []
for i in range(len(path)):
frame_x = manet.nodes[path[i]].x
frame_y = manet.nodes[path[i]].y
frames.append((frame_x, frame_y))
# Check if packet was dropped
if not success and i == len(path) - 1:
# Show packet drop
ax.text(frame_x, frame_y + 10, 'DROPPED!',
fontsize=12, color='red', ha='center',
bbox=dict(boxstyle="round,pad=0.3", facecolor="yellow", alpha=0.7))
ax.legend(loc='upper right')
ax.set_title(f'Packet Transmission {"Success" if success else "Failed"}',
fontsize=16, fontweight='bold')
plt.tight_layout()
return fig
def run_single_scenario_simulation(scenario: dict, progress_bar, status_text):
"""Run simulation for a single scenario"""
status_text.text(f"Testing {scenario['name']}...")
# Create MANET
manet = MANETSimulator(
num_nodes=scenario['nodes'],
area_size=scenario['area'],
transmission_range=scenario['range'],
blackhole_percentage=scenario['blackhole_pct']
)
# Simulate network traffic
n_transmissions = scenario['nodes'] * 20
successful_transmissions = 0
transmission_logs = []
for i in range(n_transmissions):
src = random.randint(0, scenario['nodes'] - 1)
dst = random.randint(0, scenario['nodes'] - 1)
if src != dst:
success, path = manet.simulate_packet_transmission(src, dst)
if success:
successful_transmissions += 1
transmission_logs.append({
'src': src,
'dst': dst,
'success': success,
'path': path
})
progress_bar.progress((i + 1) / n_transmissions)
# Collect features
features = manet.collect_node_features()
# Ground truth
y_true = np.array([1 if node.is_blackhole else 0 for node in manet.nodes])
# Test algorithms
algorithms = [
("Dolphin Echolocation", DolphinEcholocationOptimizer()),
("Hybrid DE-ABC", HybridDEABCOptimizer()),
("Improved Hybrid DE-ABC", ImprovedHybridDEABCOptimizer())
]
results = {}
predictions = {}
for algo_name, algorithm in algorithms:
status_text.text(f"Testing {algo_name}...")
# Time the detection
start_time = time.time()
y_pred = algorithm.detect_blackholes(features)
detection_time = time.time() - start_time
# Calculate metrics
precision = precision_score(y_true, y_pred, zero_division=0)
recall = recall_score(y_true, y_pred, zero_division=0)
f1 = f1_score(y_true, y_pred, zero_division=0)
results[algo_name] = {
'scenario': scenario['name'],
'precision': precision,
'recall': recall,
'f1_score': f1,
'detection_time': detection_time,
'true_positives': np.sum((y_true == 1) & (y_pred == 1)),
'false_positives': np.sum((y_true == 0) & (y_pred == 1)),
'false_negatives': np.sum((y_true == 1) & (y_pred == 0)),
'true_negatives': np.sum((y_true == 0) & (y_pred == 0))
}