1 importnumpy as np2 from BFOIndividual importBFOIndividual3 importrandom4 importcopy5 importmatplotlib.pyplot as plt6 importmath7
8
9 classBacterialForagingOptimization:10
11 '''
12 The class for baterial foraging optimization algorithm13 '''
14
15 def __init__(self, sizepop, vardim, bound, params):16 '''
17 sizepop: population sizepop18 vardim: dimension of variables19 bound: boundaries of variables20 param: algorithm required parameters, it is a list which is consisting of [Ned, Nre, Nc, Ns, C, ped, d_attract, w_attract, d_repellant, w_repellant]21 '''
22 self.sizepop =sizepop23 self.vardim =vardim24 self.bound =bound25 self.population =[]26 self.bestPopulation =[]27 self.accuFitness =np.zeros(self.sizepop)28 self.fitness =np.zeros(self.sizepop)29 self.params =params30 self.trace =np.zeros(31 (self.params[0] * self.params[1] * self.params[2], 2))32
33 definitialize(self):34 '''
35 initialize the population36 '''
37 for i inxrange(0, self.sizepop):38 ind =BFOIndividual(self.vardim, self.bound)39 ind.generate()40 self.population.append(ind)41
42 defevaluate(self):43 '''
44 evaluation of the population fitnesses45 '''
46 for i inxrange(0, self.sizepop):47 self.population[i].calculateFitness()48 self.fitness[i] =self.population[i].fitness49
50 defsortPopulation(self):51 '''
52 sort population according descending order53 '''
54 sortedIdx =np.argsort(self.accuFitness)55 newpop =[]56 newFitness =np.zeros(self.sizepop)57 for i inxrange(0, self.sizepop):58 ind =self.population[sortedIdx[i]]59 newpop.append(ind)60 self.fitness[i] =ind.fitness61 self.population =newpop62
63 defsolve(self):64 '''
65 evolution process of baterial clony foraging algorithm66 '''
67 self.t =068 self.initialize()69 self.evaluate()70 bestIndex =np.argmin(self.fitness)71 self.best =copy.deepcopy(self.population[bestIndex])72
73 for i inxrange(0, self.params[0]):74 for j in xrange(0, self.params[1]):75 for k in xrange(0, self.params[2]):76 self.t += 1
77 self.chemotaxls()78 self.evaluate()79 best =np.min(self.fitness)80 bestIndex =np.argmin(self.fitness)81 if best <82 self.best="copy.deepcopy(self.population[bestIndex])83" self.avefitness="np.mean(self.fitness)84" self.trace print optimal function value is: average is self.t self.reproduction self.eliminationanddispersal>
91 print("Optimal function value is: %f;" %
92 self.trace[self.t - 1, 0])93 print "Optimal solution is:"
94 printself.best.chrom95 self.printResult()96
97 defchemotaxls(self):98 '''
99 chemotaxls behavior of baterials100 '''
101 for i inxrange(0, self.sizepop):102 tmpInd =copy.deepcopy(self.population[i])103 self.fitness[i] +=self.communication(tmpInd)104 Jlast =self.fitness[i]105 rnd = np.random.uniform(low=-1, high=1.0, size=self.vardim)106 phi = rnd /np.linalg.norm(rnd)107 tmpInd.chrom += self.params[4] *phi108 for k inxrange(0, self.vardim):109 if tmpInd.chrom[k] self.bound[1, k]:112 tmpInd.chrom[k] = self.bound[1, k]113 tmpInd.calculateFitness()114 m =0115 while m < self.params[3]:116 if tmpInd.fitness <117 jlast="tmpInd.fitness118" self.population m>
120 tmpInd.fitness +=self.communication(tmpInd)121 tmpInd.chrom += self.params[4] *phi122 for k inxrange(0, self.vardim):123 if tmpInd.chrom[k] self.bound[1, k]:126 tmpInd.chrom[k] = self.bound[1, k]127 tmpInd.calculateFitness()128 m += 1
129 else:130 m = self.params[3]131 self.fitness[i] =Jlast132 self.accuFitness[i] +=Jlast133
134 defcommunication(self, ind):135 '''
136 cell to cell communication137 '''
138 Jcc = 0.0
139 term1 = 0.0
140 term2 = 0.0
141 for j inxrange(0, self.sizepop):142 term = 0.0
143 for k inxrange(0, self.vardim):144 term += (ind.chrom[k] -
145 self.population[j].chrom[k]) ** 2
146 term1 -= self.params[6] * np.exp(-1 * self.params[7] *term)147 term2 += self.params[8] * np.exp(-1 * self.params[9] *term)148 Jcc = term1 +term2149
150 returnJcc151
152 defreproduction(self):153 '''
154 reproduction of baterials155 '''
156 self.sortPopulation()157 newpop =[]158 for i in xrange(0, self.sizepop / 2):159 newpop.append(self.population[i])160 for i in xrange(self.sizepop / 2, self.sizepop):161 self.population[i] = newpop[i - self.sizepop / 2]162
163 defeliminationAndDispersal(self):164 '''
165 elimination and dispersal of baterials166 '''
167 for i inxrange(0, self.sizepop):168 rnd =random.random()169 if rnd < self.params[5]:170 self.population[i].generate()171
172 defprintResult(self):173 '''
174 plot the result of the baterial clony foraging algorithm175 '''
176 x =np.arange(0, self.t)177 y1 =self.trace[:, 0]178 y2 = self.trace[:, 1]179 plt.plot(x, y1, 'r', label='optimal value')180 plt.plot(x, y2, 'g', label='average value')181 plt.xlabel("Iteration")182 plt.ylabel("function value")183 plt.title(184 "Baterial clony foraging algorithm for function optimization")185 plt.legend()186 plt.show()
117>82>