人工蜂群算法-python实现
ABSIndividual.py
1 import numpy as np 2 import ObjFunction 3 4 5 class ABSIndividual: 6 7 ''' 8 individual of artificial bee swarm algorithm 9 ''' 10 11 def __init__(self, vardim, bound): 12 ''' 13 vardim: dimension of variables 14 bound: boundaries of variables 15 ''' 16 self.vardim = vardim 17 self.bound = bound 18 self.fitness = 0. 19 self.trials = 0 20 21 def generate(self): 22 ''' 23 generate a random chromsome for artificial bee swarm algorithm 24 ''' 25 len = self.vardim 26 rnd = np.random.random(size=len) 27 self.chrom = np.zeros(len) 28 for i in xrange(0, len): 29 self.chrom[i] = self.bound[0, i] + \ 30 (self.bound[1, i] - self.bound[0, i]) * rnd[i] 31 32 def calculateFitness(self): 33 ''' 34 calculate the fitness of the chromsome 35 ''' 36 self.fitness = ObjFunction.GrieFunc( 37 self.vardim, self.chrom, self.bound)
ABS.py
1 import numpy as np 2 from ABSIndividual import ABSIndividual 3 import random 4 import copy 5 import matplotlib.pyplot as plt 6 7 8 class ArtificialBeeSwarm: 9 10 ''' 11 the class for artificial bee swarm algorithm 12 ''' 13 14 def __init__(self, sizepop, vardim, bound, MAXGEN, params): 15 ''' 16 sizepop: population sizepop 17 vardim: dimension of variables 18 bound: boundaries of variables 19 MAXGEN: termination condition 20 params: algorithm required parameters, it is a list which is consisting of[trailLimit, C] 21 ''' 22 self.sizepop = sizepop 23 self.vardim = vardim 24 self.bound = bound 25 self.foodSource = self.sizepop / 2 26 self.MAXGEN = MAXGEN 27 self.params = params 28 self.population = [] 29 self.fitness = np.zeros((self.sizepop, 1)) 30 self.trace = np.zeros((self.MAXGEN, 2)) 31 32 def initialize(self): 33 ''' 34 initialize the population of abs 35 ''' 36 for i in xrange(0, self.foodSource): 37 ind = ABSIndividual(self.vardim, self.bound) 38 ind.generate() 39 self.population.append(ind) 40 41 def evaluation(self): 42 ''' 43 evaluation the fitness of the population 44 ''' 45 for i in xrange(0, self.foodSource): 46 self.population[i].calculateFitness() 47 self.fitness[i] = self.population[i].fitness 48 49 def employedBeePhase(self): 50 ''' 51 employed bee phase 52 ''' 53 for i in xrange(0, self.foodSource): 54 k = np.random.random_integers(0, self.vardim - 1) 55 j = np.random.random_integers(0, self.foodSource - 1) 56 while j == i: 57 j = np.random.random_integers(0, self.foodSource - 1) 58 vi = copy.deepcopy(self.population[i]) 59 # vi.chrom = vi.chrom + np.random.uniform(-1, 1, self.vardim) * ( 60 # vi.chrom - self.population[j].chrom) + np.random.uniform(0.0, self.params[1], self.vardim) * (self.best.chrom - vi.chrom) 61 # for k in xrange(0, self.vardim): 62 # if vi.chrom[k] < self.bound[0, k]: 63 # vi.chrom[k] = self.bound[0, k] 64 # if vi.chrom[k] > self.bound[1, k]: 65 # vi.chrom[k] = self.bound[1, k] 66 vi.chrom[ 67 k] += np.random.uniform(low=-1, high=1.0, size=1) * (vi.chrom[k] - self.population[j].chrom[k]) 68 if vi.chrom[k] < self.bound[0, k]: 69 vi.chrom[k] = self.bound[0, k] 70 if vi.chrom[k] > self.bound[1, k]: 71 vi.chrom[k] = self.bound[1, k] 72 vi.calculateFitness() 73 if vi.fitness > self.fitness[fi]: 74 self.population[fi] = vi 75 self.fitness[fi] = vi.fitness 76 if vi.fitness > self.best.fitness: 77 self.best = vi 78 vi.calculateFitness() 79 if vi.fitness > self.fitness[i]: 80 self.population[i] = vi 81 self.fitness[i] = vi.fitness 82 if vi.fitness > self.best.fitness: 83 self.best = vi 84 else: 85 self.population[i].trials += 1 86 87 def onlookerBeePhase(self): 88 ''' 89 onlooker bee phase 90 ''' 91 accuFitness = np.zeros((self.foodSource, 1)) 92 maxFitness = np.max(self.fitness) 93 94 for i in xrange(0, self.foodSource): 95 accuFitness[i] = 0.9 * self.fitness[i] / maxFitness + 0.1 96 97 for i in xrange(0, self.foodSource): 98 for fi in xrange(0, self.foodSource): 99 r = random.random() 100 if r < accuFitness[i]: 101 k = np.random.random_integers(0, self.vardim - 1) 102 j = np.random.random_integers(0, self.foodSource - 1) 103 while j == fi: 104 j = np.random.random_integers(0, self.foodSource - 1) 105 vi = copy.deepcopy(self.population[fi]) 106 # vi.chrom = vi.chrom + np.random.uniform(-1, 1, self.vardim) * ( 107 # vi.chrom - self.population[j].chrom) + np.random.uniform(0.0, self.params[1], self.vardim) * (self.best.chrom - vi.chrom) 108 # for k in xrange(0, self.vardim): 109 # if vi.chrom[k] < self.bound[0, k]: 110 # vi.chrom[k] = self.bound[0, k] 111 # if vi.chrom[k] > self.bound[1, k]: 112 # vi.chrom[k] = self.bound[1, k] 113 vi.chrom[ 114 k] += np.random.uniform(low=-1, high=1.0, size=1) * (vi.chrom[k] - self.population[j].chrom[k]) 115 if vi.chrom[k] < self.bound[0, k]: 116 vi.chrom[k] = self.bound[0, k] 117 if vi.chrom[k] > self.bound[1, k]: 118 vi.chrom[k] = self.bound[1, k] 119 vi.calculateFitness() 120 if vi.fitness > self.fitness[fi]: 121 self.population[fi] = vi 122 self.fitness[fi] = vi.fitness 123 if vi.fitness > self.best.fitness: 124 self.best = vi 125 else: 126 self.population[fi].trials += 1 127 break 128 129 def scoutBeePhase(self): 130 ''' 131 scout bee phase 132 ''' 133 for i in xrange(0, self.foodSource): 134 if self.population[i].trials > self.params[0]: 135 self.population[i].generate() 136 self.population[i].trials = 0 137 self.population[i].calculateFitness() 138 self.fitness[i] = self.population[i].fitness 139 140 def solve(self): 141 ''' 142 the evolution process of the abs algorithm 143 ''' 144 self.t = 0 145 self.initialize() 146 self.evaluation() 147 best = np.max(self.fitness) 148 bestIndex = np.argmax(self.fitness) 149 self.best = copy.deepcopy(self.population[bestIndex]) 150 self.avefitness = np.mean(self.fitness) 151 self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness 152 self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness 153 print("Generation %d: optimal function value is: %f; average function value is %f" % ( 154 self.t, self.trace[self.t, 0], self.trace[self.t, 1])) 155 while self.t < self.MAXGEN - 1: 156 self.t += 1 157 self.employedBeePhase() 158 self.onlookerBeePhase() 159 self.scoutBeePhase() 160 best = np.max(self.fitness) 161 bestIndex = np.argmax(self.fitness) 162 if best > self.best.fitness: 163 self.best = copy.deepcopy(self.population[bestIndex]) 164 self.avefitness = np.mean(self.fitness) 165 self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness 166 self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness 167 print("Generation %d: optimal function value is: %f; average function value is %f" % ( 168 self.t, self.trace[self.t, 0], self.trace[self.t, 1])) 169 print("Optimal function value is: %f; " % self.trace[self.t, 0]) 170 print "Optimal solution is:" 171 print self.best.chrom 172 self.printResult() 173 174 def printResult(self): 175 ''' 176 plot the result of abs algorithm 177 ''' 178 x = np.arange(0, self.MAXGEN) 179 y1 = self.trace[:, 0] 180 y2 = self.trace[:, 1] 181 plt.plot(x, y1, 'r', label='optimal value') 182 plt.plot(x, y2, 'g', label='average value') 183 plt.xlabel("Iteration") 184 plt.ylabel("function value") 185 plt.title("Artificial Bee Swarm algorithm for function optimization") 186 plt.legend() 187 plt.show()
运行程序:
1 if __name__ == "__main__": 2 3 bound = np.tile([[-600], [600]], 25) 4 abs = ABS(60, 25, bound, 1000, [100, 0.5]) 5 abs.solve()
ObjFunction见简单遗传算法-python实现。
作者:Alex Yu
出处:http://www.cnblogs.com/biaoyu/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。