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
| import numpy as np import random import copy import matplotlib.pyplot as plt import math
class ABCIndividual: ''' 模拟蜜蜂个体 ''' def __init__(self, vardim, bound): ''' 个体初始化 vardim: 变量维数 bound: 变量取值范围 ''' self.vardim = vardim self.bound = bound self.fitness = 0. self.trials = 0.
def generate(self): ''' 生成随机性 ''' len = self.vardim rnd = np.random.random(size=len) self.chrom = np.zeros(len) for i in range(0, len): self.chrom[i] = self.bound[0, i] + (self.bound[1, i] - self.bound[0, i]) * rnd[i]
def GrieFunc(self, vardim, x, bound): ''' 差分算法 ''' s1 = 0. s2 = 1. for i in range(1, vardim + 1): s1 = s1 + x[i - 1] ** 2 s2 = s2 * math.cos(x[i - 1] / math.sqrt(i)) y = (1. / 4000.) * s1 - s2 + 1 y = 1. / (1. + y) return y def calculateFitness(self): ''' 计算适应度 ''' self.fitness = self.GrieFunc(self.vardim, self.chrom, self.bound)
class ABC: ''' 人工蜂群算法 ''' def __init__(self, sizepop, vardim, bound, MAXGEN, params): ''' sizepop: 种群规模 vardim: 变量维数 bound: 变量取值范围 MAXGEN: 最大迭代次数(终止条件) param: 其他参数(列表) ''' self.sizepop = sizepop self.vardim = vardim self.bound = bound self.foodSource = int(self.sizepop / 2) self.MAXGEN = MAXGEN self.params = params self.population = [] self.fitness = np.zeros((self.sizepop, 1)) self.history = np.zeros((self.MAXGEN, 2))
def initialize(self): ''' 初始化蜂群 ''' for i in range(0, self.foodSource): ind = ABSIndividual(self.vardim, self.bound) ind.generate() self.population.append(ind)
def evaluation(self): ''' 迭代 ''' for i in range(0, self.foodSource): self.population[i].calculateFitness() self.fitness[i] = self.population[i].fitness
def employedBeePhase(self): ''' 引领蜂 ''' for i in range(0, self.foodSource): k = np.random.random_integers(0, self.vardim - 1) j = np.random.random_integers(0, self.foodSource - 1) while j == i: j = np.random.random_integers(0, self.foodSource - 1) vi = copy.deepcopy(self.population[i]) vi.chrom[k] += np.random.uniform(low=-1, high=1.0, size=1) * (vi.chrom[k] - self.population[j].chrom[k]) if vi.chrom[k] < self.bound[0, k]: vi.chrom[k] = self.bound[0, k] if vi.chrom[k] > self.bound[1, k]: vi.chrom[k] = self.bound[1, k] vi.calculateFitness() if vi.fitness > self.fitness[i]: self.population[i] = vi self.fitness[i] = vi.fitness if vi.fitness > self.best.fitness: self.best = vi vi.calculateFitness() if vi.fitness > self.fitness[i]: self.population[i] = vi self.fitness[i] = vi.fitness if vi.fitness > self.best.fitness: self.best = vi else: self.population[i].trials += 1
def onlookerBeePhase(self): ''' 跟随蜂 ''' accuFitness = np.zeros((self.foodSource, 1)) maxFitness = np.max(self.fitness)
for i in range(0, self.foodSource): accuFitness[i] = 0.9 * self.fitness[i] / maxFitness + 0.1
for i in range(0, self.foodSource): for fi in range(0, self.foodSource): r = random.random() if r < accuFitness[i]: k = np.random.random_integers(0, self.vardim - 1) j = np.random.random_integers(0, self.foodSource - 1) while j == fi: j = np.random.random_integers(0, self.foodSource - 1) vi = copy.deepcopy(self.population[i]) vi.chrom[k] += np.random.uniform(low=-1, high=1.0, size=1) * (vi.chrom[k] - self.population[j].chrom[k]) if vi.chrom[k] < self.bound[0, k]: vi.chrom[k] = self.bound[0, k] if vi.chrom[k] > self.bound[1, k]: vi.chrom[k] = self.bound[1, k] vi.calculateFitness() if vi.fitness > self.fitness[i]: self.population[i] = vi self.fitness[i] = vi.fitness if vi.fitness > self.best.fitness: self.best = vi else: self.population[i].trials += 1 break
def scoutBeePhase(self): ''' 侦查蜂 ''' for i in range(0, self.foodSource): if self.population[i].trials > self.params[0]: self.population[i].generate() self.population[i].trials = 0 self.population[i].calculateFitness() self.fitness[i] = self.population[i].fitness
def printResult(self): ''' 绘制蜂群迭代过程 ''' x = np.arange(0, self.MAXGEN) y1 = self.history[:, 0] y2 = self.history[:, 1] plt.plot(x, y1, 'r', label='optimal value') plt.plot(x, y2, 'g', label='average value') plt.xlabel("Iteration") plt.ylabel("function value") plt.title("Artificial Bee Swarm algorithm for function optimization") plt.legend() plt.show()
def main(self): ''' 主流程:蜂群搜索过程 ''' self.t = 0 self.initialize() self.evaluation() best = np.max(self.fitness) bestIndex = np.argmax(self.fitness) self.best = copy.deepcopy(self.population[bestIndex]) self.avefitness = np.mean(self.fitness) self.history[self.t, 0] = (1 - self.best.fitness) / self.best.fitness self.history[self.t, 1] = (1 - self.avefitness) / self.avefitness print("Generation %d: optimal function value is: %f; average function value is %f" % (self.t, self.history[self.t, 0], self.history[self.t, 1])) while self.t < self.MAXGEN - 1: self.t += 1 self.employedBeePhase() self.onlookerBeePhase() self.scoutBeePhase() best = np.max(self.fitness) bestIndex = np.argmax(self.fitness) if best > self.best.fitness: self.best = copy.deepcopy(self.population[bestIndex]) self.avefitness = np.mean(self.fitness) self.history[self.t, 0] = (1 - self.best.fitness) / self.best.fitness self.history[self.t, 1] = (1 - self.avefitness) / self.avefitness print("Generation %d: optimal function value is: %f; average function value is %f" % (self.t, self.history[self.t, 0], self.history[self.t, 1])) print("Optimal function value is: %f; " % self.history[self.t, 0]) print("Optimal solution is:") print(self.best.chrom) self.printResult()
if __name__ == "__main__": bound = np.tile([[-600], [600]], 25) abc = ABC(60, 25, bound, 1000, [100, 0.5]) abc.main()
|