python怎么画蝙蝠_蝙蝠算法-python实现

1 importnumpy as np2 from BAIndividual importBAIndividual3 importrandom4 importcopy5 importmatplotlib.pyplot as plt6

7

8 classBatAlgorithm:9

10 '''

11 the class for bat algorithm12 '''

13

14 def __init__(self, sizepop, vardim, bound, MAXGEN, params):15 '''

16 sizepop: population sizepop17 vardim: dimension of variables18 bound: boundaries of variables19 MAXGEN: termination condition20 params: algorithm required parameters, it is a list which is consisting of[fmax, fmin, Amax, Amin, alpha, gamma]21 '''

22 self.sizepop =sizepop23 self.vardim =vardim24 self.bound =bound25 self.MAXGEN =MAXGEN26 self.params =params27 self.population =[]28 self.fitness =np.zeros(self.sizepop)29 self.freq =np.zeros(self.sizepop)30 self.loudness =np.zeros(self.sizepop)31 self.emissionrate =np.zeros(self.sizepop)32 self.initEmissionrate =np.zeros(self.sizepop)33 self.trace = np.zeros((self.MAXGEN, 2))34

35 definitialize(self):36 '''

37 initialize the population of ba38 '''

39 for i inxrange(0, self.sizepop):40 ind =BAIndividual(self.vardim, self.bound)41 ind.generate()42 self.population.append(ind)43 self.freq[i] = self.params[1] +\44 (self.params[0] - self.params[1]) * np.random.random(1)45 self.loudness[i] = self.params[3] +\46 (self.params[2] - self.params[3]) * np.random.random(1)47 self.initEmissionrate[i] = np.random.random(1)48 self.emissionrate[i] =self.initEmissionrate[i]49

50 defevaluation(self):51 '''

52 evaluation the fitness of the population53 '''

54 for i inxrange(0, self.sizepop):55 self.population[i].calculateFitness()56 self.fitness[i] =self.population[i].fitness57

58 defsolve(self):59 '''

60 the evolution process of the bat algorithm61 '''

62 self.t =063 self.initialize()64 self.evaluation()65 bestIndex =np.argmax(self.fitness)66 self.best =copy.deepcopy(self.population[bestIndex])67 while self.t <68 self.t>

69 self.update()70 #idx = self.select()

71 self.evaluation()72 best =np.max(self.fitness)73 bestIndex =np.argmax(self.fitness)74 if best >self.best.fitness:75 self.best =copy.deepcopy(self.population[bestIndex])76

77 self.avefitness =np.mean(self.fitness)78 self.trace[self.t - 1, 0] =\79 (1 - self.best.fitness) /self.best.fitness80 self.trace[self.t - 1, 1] = (1 - self.avefitness) /self.avefitness81 print("Generation %d: optimal function value is: %f; average function value is %f" %(82 self.t, self.trace[self.t - 1, 0], self.trace[self.t - 1, 1]))83 print("Optimal function value is: %f;" % self.trace[self.t - 1, 0])84 print "Optimal solution is:"

85 printself.best.chrom86 self.printResult()87

88 defupdate(self):89 '''

90 update the population91 '''

92 for i inxrange(0, self.sizepop):93 self.freq[i] = self.params[1] +\94 (self.params[0] - self.params[1]) * np.random.random(1)95 self.population[96 i].velocity += (self.best.chrom - self.population[i].chrom) *self.freq[i]97

98 self.population[i].chrom +=self.population[i].velocity99 for k inxrange(0, self.vardim):100 if self.population[i].chrom[k] self.bound[1, k]:103 self.population[i].chrom[k] = self.bound[1, k]104 rnd = np.random.random(1)105 A =np.mean(self.emissionrate)106 tmpInd =copy.deepcopy(self.best)107 if rnd >self.emissionrate[i]:108 tmpInd.chrom += np.random.uniform(low=-1,109 high=1.0, size=self.vardim) *A110 for k inxrange(0, self.vardim):111 if tmpInd.chrom[k] self.bound[1, k]:114 tmpInd.chrom[k] = self.bound[1, k]115 tmpInd.calculateFitness()116 if tmpInd.fitness > self.best.fitness and random.random() self.best.fitness:122 self.best =copy.deepcopy(tmpInd)123

124 defselectOne(self):125 '''

126 select one individual from the population127 '''

128 totalFitness =np.sum(self.fitness)129 accuFitness =np.zeros(self.sizepop)130

131 sum1 =0.132 for i inxrange(0, self.sizepop):133 accuFitness[i] = sum1 + self.fitness[i] /totalFitness134 sum1 =accuFitness[i]135

136 r =random.random()137 idx =0138 for j in xrange(0, self.sizepop - 1):139 if j == 0 and r

142 elif r >= accuFitness[j] and r < accuFitness[j + 1]:143 idx = j + 1

144 break

145 returnidx146

147 defprintResult(self):148 '''

149 plot the result of bat algorithm150 '''

151 x =np.arange(0, self.MAXGEN)152 y1 =self.trace[:, 0]153 y2 = self.trace[:, 1]154 plt.plot(x, y1, 'r', label='optimal value')155 plt.plot(x, y2, 'g', label='average value')156 plt.xlabel("Iteration")157 plt.ylabel("function value")158 plt.title("Bat algorithm for function optimization")159 plt.legend()160 plt.show()

68>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值