1 importnumpy as np2 from FAIndividual importFAIndividual3 importrandom4 importcopy5 importmatplotlib.pyplot as plt6
7
8 classFireflyAlgorithm:9
10 '''
11 The class for firefly 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 param: algorithm required parameters, it is a list which is consisting of [beta0, gamma, alpha]21 '''
22 self.sizepop =sizepop23 self.MAXGEN =MAXGEN24 self.vardim =vardim25 self.bound =bound26 self.population =[]27 self.fitness = np.zeros((self.sizepop, 1))28 self.trace = np.zeros((self.MAXGEN, 2))29 self.params =params30
31 definitialize(self):32 '''
33 initialize the population34 '''
35 for i inxrange(0, self.sizepop):36 ind =FAIndividual(self.vardim, self.bound)37 ind.generate()38 self.population.append(ind)39
40 defevaluate(self):41 '''
42 evaluation of the population fitnesses43 '''
44 for i inxrange(0, self.sizepop):45 self.population[i].calculateFitness()46 self.fitness[i] =self.population[i].fitness47
48 defsolve(self):49 '''
50 evolution process of firefly algorithm51 '''
52 self.t =053 self.initialize()54 self.evaluate()55 best =np.max(self.fitness)56 bestIndex =np.argmax(self.fitness)57 self.best =copy.deepcopy(self.population[bestIndex])58 self.avefitness =np.mean(self.fitness)59 self.trace[self.t, 0] = (1 - self.best.fitness) /self.best.fitness60 self.trace[self.t, 1] = (1 - self.avefitness) /self.avefitness61 print("Generation %d: optimal function value is: %f; average function value is %f" %(62 self.t, self.trace[self.t, 0], self.trace[self.t, 1]))63 while (self.t < self.MAXGEN - 1):64 self.t += 1
65 self.move()66 self.evaluate()67 best =np.max(self.fitness)68 bestIndex =np.argmax(self.fitness)69 if best >self.best.fitness:70 self.best =copy.deepcopy(self.population[bestIndex])71 self.avefitness =np.mean(self.fitness)72 self.trace[self.t, 0] = (1 - self.best.fitness) /self.best.fitness73 self.trace[self.t, 1] = (1 - self.avefitness) /self.avefitness74 print("Generation %d: optimal function value is: %f; average function value is %f" %(75 self.t, self.trace[self.t, 0], self.trace[self.t, 1]))76
77 print("Optimal function value is: %f;" %
78 self.trace[self.t, 0])79 print "Optimal solution is:"
80 printself.best.chrom81 self.printResult()82
83 defmove(self):84 '''
85 move the a firefly to another brighter firefly86 '''
87 for i inxrange(0, self.sizepop):88 for j inxrange(0, self.sizepop):89 if self.fitness[j] >self.fitness[i]:90 r =np.linalg.norm(91 self.population[i].chrom -self.population[j].chrom)92 beta = self.params[0] *\93 np.exp(-1 * self.params[1] * (r ** 2))94 #beta = 1 / (1 + self.params[1] * r)
95 #print beta
96 self.population[i].chrom += beta * (self.population[j].chrom -self.population[97 i].chrom) + self.params[2] * np.random.uniform(low=-1, high=1, size=self.vardim)98 for k inxrange(0, self.vardim):99 if self.population[i].chrom[k] self.bound[1, k]:102 self.population[i].chrom[k] = self.bound[1, k]103 self.population[i].calculateFitness()104 self.fitness[i] =self.population[i].fitness105
106 defprintResult(self):107 '''
108 plot the result of the firefly algorithm109 '''
110 x =np.arange(0, self.MAXGEN)111 y1 =self.trace[:, 0]112 y2 = self.trace[:, 1]113 plt.plot(x, y1, 'r', label='optimal value')114 plt.plot(x, y2, 'g', label='average value')115 plt.xlabel("Iteration")116 plt.ylabel("function value")117 plt.title("Firefly Algorithm for function optimization")118 plt.legend()119 plt.show()