-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpso.py
116 lines (92 loc) · 4.14 KB
/
pso.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
from particle import Particle
import numpy as np
from scapy.all import *
class PSO:
def __init__(self, max_iter, particle_num, grp_size=0, alg='L'):
self.max_iter = max_iter
self.particle_num = particle_num
self.grp_size = grp_size
self.grp_best_dis = [-1.] * (self.particle_num // self.grp_size)
self.grp_best_index = [-1] * (self.particle_num // self.grp_size)
self.global_best_dis = -1.
self.global_best_index = -1
self.global_best_pktlist = None
self.swarm = []
self.STA_glb_dis_list = []
self.STA_avg_dis_list = []
def execute(
self,
last_end_time, # initializer
groupList, # initializer
max_time_extend, # initializer
max_cft_pkt, # initializer
min_time_extend,
max_crafted_pkt_prob,
mimic_set, # evaluate
nstat, # evaluate
# tmp_pcap_file,
knormer,
w,
c1,
c2,
show_info=True,
heuristic=False,
):
# print("*" * 64)
# establish the swarm
# print("--@PSO: Creating particle swarm...")
for i in range(self.particle_num):
# print("PSO:",max_cft_pkt)
self.swarm.append(
Particle(last_end_time, groupList, max_time_extend,
max_cft_pkt, min_time_extend, max_crafted_pkt_prob))
# begin optimization loop
# print("--@PSO: Executing PSO algorithm...")
FE_time = 0
last_glb_best = np.Inf
iter = 0
while True:
avg_dis = 0
for i in range(0, self.particle_num, self.grp_size):
# print("--@PSO: process grp %d, no.%d~%d"%(i/self.grp_size,i,i+self.grp_size-1), "...")
for j in range(i, i + self.grp_size):
grp_i = int(i / self.grp_size)
FE_time += self.swarm[j].evaluate(mimic_set, nstat,
knormer) # tmp_pcap_file
avg_dis += self.swarm[j].dis
if self.swarm[j].dis < self.grp_best_dis[
grp_i] or self.grp_best_dis[grp_i] == -1.:
self.grp_best_index[grp_i] = j
self.grp_best_dis[grp_i] = self.swarm[j].dis
if self.grp_best_dis[
grp_i] < self.global_best_dis or self.global_best_dis == -1.:
self.global_best_index = self.grp_best_index[grp_i]
self.global_best_dis = self.grp_best_dis[grp_i]
grp_best_X = self.swarm[self.grp_best_index[grp_i]].X
for j in range(i, i + self.grp_size):
# print("--@PSO: update_V swarm", j, "...")
self.swarm[j].update_v(grp_best_X, w, c1, c2)
# print("--@PSO: update_X swarm", j, "...")
self.swarm[j].update_x()
self.STA_glb_dis_list.append(self.global_best_dis)
self.STA_avg_dis_list.append(avg_dis / self.particle_num)
if show_info:
print("--@PSO:Step", iter, "Finished...Global best value:",
self.global_best_dis)
# enhanced mode
if heuristic:
if last_glb_best > self.global_best_dis * (
1. + 0.1): # delta must > 10%
last_glb_best = self.global_best_dis
iter -= 1
iter += 1
if iter >= self.max_iter:
break
self.global_best_pktlist = self.swarm[self.global_best_index].pktList
STA_best_X = self.swarm[self.global_best_index].X
STA_best_feature = self.swarm[self.global_best_index].feature
STA_best_pktList = self.global_best_pktlist
STA_best_all_feature = self.swarm[self.global_best_index].all_feature
cur_end_time = self.swarm[self.global_best_index].X.mal[-1][0]
ics_time = cur_end_time - float(groupList[-1].time)
return ics_time, cur_end_time, STA_best_X, STA_best_feature, STA_best_pktList, self.STA_glb_dis_list, self.STA_avg_dis_list, STA_best_all_feature, FE_time