Ejemplo n.º 1
0
 def initial(self, classify_dataset, attack_types):
     # 随机生成每个个体的初始位置
     for i in range(self.size):
         self.pos[i] = classify_common.xgb_para_init(self.pos[i])
         # 记录个体的初始适应度值
         self.fit[i] = intrusion_classify.xgb_classify(
             self.pos[i], classify_dataset, attack_types)
     # 记录初始全局最优下标、位置和适应度值
     max_index = np.argsort(-self.fit)[0]
     self.g_best_pos = self.pos[max_index].copy()  # deep copy
     self.g_best_fit = self.fit[max_index]
     self.pos_record[0] = self.g_best_pos.copy()  # deep copy
     self.fit_record[0] = self.g_best_fit
     print('初始最优位置和适应度值为:')
     print(self.g_best_pos)
     print(self.g_best_fit)
Ejemplo n.º 2
0
 def initial(self, classify_dataset, attack_types):
     # 生成每个粒子的初始位置和初始速度(初始速度为0)
     for i in range(self.ps_size):
         # 随机生成XGBoost的每个参数
         self.p_pos[i] = classify_common.xgb_para_init(self.p_pos[i])
         # 记录粒子初始位置和适应度值
         self.p_best_pos[i] = self.p_pos[i]
         self.p_best_fit[i] = intrusion_classify.xgb_classify(self.p_pos[i], classify_dataset, attack_types)
     # 记录全局初始最优位置和适应度值
     max_index = np.where(self.p_best_fit == np.max(self.p_best_fit))[0][0]
     self.g_best_fit = np.max(self.p_best_fit)
     self.g_best_pos = self.p_pos[max_index]
     self.pos_record[0] = self.g_best_pos.copy()    # deep copy
     self.fit_record[0] = self.g_best_fit
     print('初始最优位置和适应度值为:')
     print(self.g_best_pos)
     print(self.g_best_fit)
Ejemplo n.º 3
0
 def initial(self, classify_dataset, attack_types):
     # 随机生成每个个体的初始位置
     for i in range(self.size):
         # 随机生成XGBoost的每个参数
         self.pos_time_fit[i] = classify_common.xgb_para_init(self.pos_time_fit[i])
         # 记录个体的初始适应度值
         self.pos_time_fit[i, -1] = intrusion_classify.xgb_classify(self.pos_time_fit[i, :self.dim], classify_dataset, attack_types)
     # 根据适应度值对种群进行排序
     self.pos_time_fit = self.pos_time_fit[np.argsort(-self.pos_time_fit[:, -1])]
     # 记录全局初始最优位置和适应度
     self.g_best_pos = self.pos_time_fit[0, :self.dim].copy()    # deep copy
     self.g_best_fit = self.pos_time_fit[0, -1]
     self.pos_record[0] = self.g_best_pos.copy()    # deep copy
     self.fit_record[0] = self.g_best_fit
     print('初始最优位置和适应度值为:')
     print(self.g_best_pos)
     print(self.g_best_fit)
Ejemplo n.º 4
0
    def optimal(self, classify_dataset, attack_types):
        # 开始迭代
        for iter_count in range(self.max_iter):
            # 计算当前惯性权重w的值
            # 线性递减
            w = (self.w_max + (self.max_iter - iter_count) * (self.w_max - self.w_min)) / self.max_iter
            # 随机选取(参数优化效果更好)
            # w = random.uniform(self.w_min, self.w_max)

            # 更新粒子位置和速度
            for i in range(self.ps_size):
                # 粒子速度更新
                self.p_vel[i] = w * self.p_vel[i] + \
                                self.c1 * random.uniform(0, 1) * (self.p_best_pos[i] - self.p_pos[i]) + \
                                self.c2 * random.uniform(0, 1) * (self.g_best_pos - self.p_pos[i])

                # 粒子位置更新
                self.p_pos[i] = self.p_pos[i] + self.p_vel[i]

                # 判断各参数是否越界
                self.p_pos[i] = classify_common.xgb_bound_check(self.p_pos[i])

                # 计算当前粒子的适应度值
                curr_fit = intrusion_classify.xgb_classify(self.p_pos[i], classify_dataset, attack_types)

                # 根据粒子适应度值判断是否更新粒子以及全局的最优位置和适应度值
                if curr_fit > self.p_best_fit[i]:
                    # 更新粒子最优位置和适应度值
                    self.p_best_fit[i] = curr_fit
                    self.p_best_pos[i] = self.p_pos[i]
                    if self.p_best_fit[i] > self.g_best_fit:
                        # 更新全局最优位置和适应度值
                        self.g_best_fit = self.p_best_fit[i]
                        self.g_best_pos = self.p_best_pos[i]

            # 输出本次迭代的全局最优位置和适应度值
            print('当前迭代次数:', iter_count + 1)
            print(self.g_best_pos)
            print(self.g_best_fit)
            # 记录本次迭代的最优位置和适应度值
            self.pos_record[iter_count + 1] = self.g_best_pos.copy()    # deep copy
            self.fit_record[iter_count + 1] = self.g_best_fit
Ejemplo n.º 5
0
    def optimal(self, classify_dataset, attack_types):
        # 开始迭代
        for iter_count in range(self.max_iter):
            # 执行选择操作
            if self.select_type == 'rws':
                # 轮盘赌选择
                self.roulette_wheel_selection()

            # 执行交叉操作
            if self.cross_type == 'spc':
                # 单点交叉
                self.single_point_crossover()

            # 执行变异操作
            if self.mutation_type == 'rm':
                # 单点变异
                self.random_mutation()

            # 重新计算适应度值
            for i in range(self.size):
                # 判断各参数是否越界
                self.pos[i] = classify_common.xgb_bound_check(self.pos[i])
                # 计算适应度值
                self.fit[i] = intrusion_classify.xgb_classify(self.pos[i], classify_dataset, attack_types)

            # 更新全局最优下标、位置和适应度值
            max_index = np.argsort(-self.fit)[0]
            self.g_best_index = max_index
            self.g_best_pos = self.pos[max_index].copy()    # deep copy
            self.g_best_fit = self.fit[max_index]
            # 更新需要保留的精英个体下标
            self.keep_elite_index = np.argsort(-self.fit)[0:self.keep_elite]
            # 输出本次迭代的全局最优位置和适应度值
            print('当前迭代次数:', iter_count + 1)
            print(self.g_best_pos)
            print(self.g_best_fit)
            # 记录本次迭代的最优适应度值
            self.pos_record[iter_count + 1] = self.g_best_pos.copy()    # deep copy
            self.fit_record[iter_count + 1] = self.g_best_fit
Ejemplo n.º 6
0
    def optimal(self, classify_dataset, attack_types):
        # 开始迭代
        for iter_count in range(self.max_iter):
            # 对每个个体进行变异、交叉、选择操作
            for i in range(self.size):
                # Mutation
                # 记录target_vector
                target_vector = self.pos[i].copy()  # deep copy
                # 随机选取r1,r2,r3,需与i不同
                r_list = []
                # 循环选取
                while len(r_list) < 3:
                    # 随机选取一个数
                    r_temp = random.randint(0, self.size - 1)
                    # 若该数不与i相同
                    if r_temp != i:
                        # 则将该数添加进被选数组
                        r_list.append(r_temp)
                # r1,r2,r3
                r1 = r_list[0]
                r2 = r_list[1]
                r3 = r_list[2]
                # 生成mutant_vector
                mutant_vector = self.pos[r1] + self.F * (self.pos[r2] -
                                                         self.pos[r3])

                # Crossover
                # 创建trial_vector
                trial_vector = np.zeros(self.dim)
                # 随机生成 rnbr
                rnbr = random.randint(0, self.dim - 1)
                # 开始交叉过程
                for j in range(self.dim):
                    # 生成决定是否交叉的随机数
                    randb = random.uniform(0, 1)
                    # 判断是否进行交叉操作
                    if randb <= self.CR or j == rnbr:
                        # 进行交叉操作
                        trial_vector[j] = mutant_vector[j]
                    else:
                        # 不进行交叉操作
                        trial_vector[j] = target_vector[j]

                # Selection
                # 记录target_vector的适应度值
                target_vector_fit = self.fit[i]
                # 判断trial_vector各参数是否越界
                trial_vector = classify_common.xgb_bound_check(trial_vector)
                # 计算trial_vector的适应度值
                trial_vector_fit = intrusion_classify.xgb_classify(
                    trial_vector, classify_dataset, attack_types)
                # 比较双方的适应度值
                if trial_vector_fit > target_vector_fit:
                    # 若trial_vector适应度值优于target_vector,则替换之
                    self.pos[i] = trial_vector.copy()  # deep copy
                    # 并同时替换适应度值
                    self.fit[i] = trial_vector_fit

            # 更新全局最优下标、位置和适应度值
            max_index = np.argsort(-self.fit)[0]
            self.g_best_pos = self.pos[max_index].copy()  # deep copy
            self.g_best_fit = self.fit[max_index]
            # 输出本次迭代的全局最优位置和适应度值
            print('当前迭代次数:', iter_count + 1)
            print(self.g_best_pos)
            print(self.g_best_fit)
            # 记录本次迭代的最优位置和适应度值
            self.pos_record[iter_count + 1] = self.g_best_pos
            self.fit_record[iter_count + 1] = self.g_best_fit
Ejemplo n.º 7
0
    def optimal(self, classify_dataset, attack_types):
        # 开始迭代
        for iter_count in range(self.max_iter):
            # 创建保持系 恢复系 不育系索引
            maintainer_index = np.arange(0, int(self.size/3))
            restorer_index = np.arange(int(self.size/3), int(self.size/3)*2)
            sterile_index = np.arange(int(self.size/3)*2, self.size)

            # 保持系与不育系进行杂交
            for index in sterile_index:
                # 初始化杂交后产生的新不育个体
                new_sterile = np.zeros(self.dim + 2)
                # 随机选择一个保持系个体
                selected_maintainer = self.pos_time_fit[random.choice(maintainer_index)]
                # 随机选择一个不育系个体
                selected_sterile = self.pos_time_fit[random.choice(sterile_index)]
                # 开始杂交过程
                for i in range(self.dim):
                    # 生成随机数r1 r2
                    # r1 = random.uniform(-1, 1)    # 原始
                    r1 = random.uniform(0, 1)    # 改进
                    # r2 = random.uniform(-1, 1)
                    # 根据所定义的公式进行杂交
                    # new_sterile[i] = (r1 * selected_maintainer[i] + r2 * selected_sterile[i]) / (r1 + r2)    # 原始
                    new_sterile[i] = (r1 * selected_maintainer[i] + (1 - r1) * selected_sterile[i])   # 改进1
                # 判断各参数是否越界
                new_sterile = classify_common.xgb_bound_check(new_sterile)
                # 计算新个体的适应度值
                new_sterile[-1] = intrusion_classify.xgb_classify(new_sterile[0:self.dim], classify_dataset, attack_types)
                # 如果新个体的适应度值优于当前不育系个体,则替换之
                if new_sterile[-1] > self.pos_time_fit[index, -1]:
                    self.pos_time_fit[index] = new_sterile

            # 杂交结束后更新全局最优位置和最优适应度值
            best_index = np.where(self.pos_time_fit == np.max(self.pos_time_fit[:, -1]))[0][0]
            self.g_best_pos = self.pos_time_fit[best_index, :self.dim].copy()    # deep copy
            self.g_best_fit = self.pos_time_fit[best_index, -1]

            # 恢复系自交或重置
            for index in restorer_index:
                # 判断当前个体自交次数是否已达上限
                if self.pos_time_fit[index, self.dim] < self.max_self:
                    # 若自交次数未达上限
                    # 初始化自交后产生的新恢复个体
                    new_restorer = np.zeros(self.dim + 2)
                    # 开始自交过程
                    for i in range(self.dim):
                        # 随机选择一个恢复系个体(与当前个体不重复)
                        selected_restorer = self.pos_time_fit[random.choice(restorer_index[restorer_index != index])]
                        # 生成随机数r3
                        r3 = random.uniform(0, 1)
                        # 根据所定义的公式进行自交
                        new_restorer[i] = r3 * (self.g_best_pos[i] - selected_restorer[i]) + self.pos_time_fit[index, i]
                    # 判断各参数是否越界
                    new_restorer = classify_common.xgb_bound_check(new_restorer)
                    # 计算新个体的适应度值
                    new_restorer[-1] = intrusion_classify.xgb_classify(new_restorer[0:self.dim], classify_dataset, attack_types)
                    # 判断新生成的个体适应度值是否优于之前的个体
                    if new_restorer[-1] > self.pos_time_fit[index, -1]:
                        # 如若优于,则替换之
                        self.pos_time_fit[index] = new_restorer
                        # 同时该个体自交次数置0
                        self.pos_time_fit[index, self.dim] = 0
                    else:
                        # 如若未优于,则个体自交次数+1
                        self.pos_time_fit[index, self.dim] = self.pos_time_fit[index, self.dim] + 1
                else:
                    # 若自交次数已达上限,则进行重置操作
                    # 随机生成XGBoost的每个参数
                    self.pos_time_fit[index] = classify_common.xgb_para_init(self.pos_time_fit[index])
                    # 重新计算该个体的适应度值
                    self.pos_time_fit[index, -1] = intrusion_classify.xgb_classify(self.pos_time_fit[index, :self.dim], classify_dataset, attack_types)
                    # 将该个体自交次数置0
                    self.pos_time_fit[index, self.dim] = 0

                # 针对当前个体的操作完成后,更新全局最优位置和最优适应度值
                best_index = np.where(self.pos_time_fit == np.max(self.pos_time_fit[:, -1]))[0][0]
                self.g_best_pos = self.pos_time_fit[best_index, :self.dim].copy()    # deep copy
                self.g_best_fit = self.pos_time_fit[best_index, -1]

            # 当前迭代完成,根据适应度值对种群重新排序
            self.pos_time_fit = self.pos_time_fit[np.argsort(-self.pos_time_fit[:, -1])]

            # 输出全局最优位置和最优适应度值
            print('当前迭代次数:', iter_count + 1)
            print(self.g_best_pos)
            print(self.g_best_fit)
            # 记录本次迭代后的最优位置和适应度值
            self.pos_record[iter_count + 1] = self.g_best_pos.copy()    # deep copy
            self.fit_record[iter_count + 1] = self.g_best_fit
Ejemplo n.º 8
0
    def optimal(self, classify_dataset, attack_types):
        # 开始迭代
        for iter_count in range(self.max_iter):
            # 确定线性递减参数a1 a2
            a1 = self.a - iter_count * (self.a / self.max_iter)
            a2 = -1 + iter_count * (-1 / self.max_iter)

            # 利用每个个体开始寻优
            for i in range(self.size):
                # 计算当前个体的参数A
                A = 2 * a1 * random.uniform(0, 1) - a1
                # 计算当前个体的参数C
                C = 2 * random.uniform(0, 1)
                # 生成随机数p
                p = random.uniform(0, 1)

                # 判断当前所要进行的操作
                if p < 0.5:
                    # Encircling Prey 或 Search for Prey
                    if abs(A) < 1:
                        # Encircling Prey
                        # 对个体中的每个位置进行操作
                        for j in range(self.dim):
                            # 计算参数D
                            D = abs(C * self.g_best_pos[j] - self.pos[i, j])
                            # 更新后的位置
                            self.pos[i, j] = self.g_best_pos[j] - A * D
                    else:
                        # Search for Prey
                        # 随机选择一个个体
                        rand_index = random.randint(0, self.size - 1)
                        # 对个体中的每个位置进行操作
                        for j in range(self.dim):
                            # 计算参数D
                            D = abs(C * self.pos[rand_index, j] -
                                    self.pos[i, j])
                            # 更新后的位置
                            self.pos[i, j] = self.pos[rand_index, j] - A * D
                else:
                    # Attacking
                    # 生成随机数l
                    l = (a2 - 1) * random.uniform(0, 1) + 1
                    # 对个体中的每个位置进行操作
                    for j in range(self.dim):
                        # 计算参数D
                        D = abs(self.g_best_pos[j] - self.pos[i, j])
                        # 更新后的位置
                        self.pos[i, j] = D * np.exp(self.b * l) * np.cos(
                            2 * np.pi * l) + self.g_best_pos[j]

                # 判断新生成的个体位置是否越界
                self.pos[i] = classify_common.xgb_bound_check(self.pos[i])

                # 计算当前个体的适应度值
                curr_fit = intrusion_classify.xgb_classify(
                    self.pos[i], classify_dataset, attack_types)

                # 如果当前个体的适应度值优于全局最优适应度值
                if curr_fit > self.g_best_fit:
                    # 替换全局最优位置和最优适应度值
                    self.g_best_pos = self.pos[i].copy()  # deep copy
                    self.g_best_fit = curr_fit

            # 本次迭代结束
            # 输出本次迭代的全局最优位置和适应度值
            print('当前迭代次数:', iter_count + 1)
            print(self.g_best_pos)
            print(self.g_best_fit)
            # 记录本次迭代的最优位置和适应度值
            self.pos_record[iter_count +
                            1] = self.g_best_pos.copy()  # deep copy
            self.fit_record[iter_count + 1] = self.g_best_fit