esindividual.py
1 ImportNumPy as NP2 Importobjfunction3 4 5 classesindividual:6 7 " "8 individual of evolutionary strategy9 " "Ten One def __init__(self, Vardim, bound): A " " - Vardim:dimension of Variables - Bound:boundaries of Variables the " " -Self.vardim =Vardim -Self.bound =bound -Self.fitness =0. +Self.trials =0 - + defGenerate (self): A " " at generate a random chromsome for evolutionary strategy - " " -Len =Self.vardim -Rnd = Np.random.random (size=len) -Self.chrom =Np.zeros (len) - forIinchxrange (0, Len): inSelf.chrom[i] = self.bound[0, I] + -(Self.bound[1, I]-self.bound[0, I]) *Rnd[i] to + defcalculatefitness (self): - " " the calculate the fitness of the Chromsome * " " $Self.fitness =Objfunction.griefunc (Panax NotoginsengSelf.vardim, Self.chrom, Self.bound)
es.py
1 ImportNumPy as NP2 fromEsindividualImportesindividual3 ImportRandom4 ImportCopy5 ImportMatplotlib.pyplot as Plt6 7 8 classEvolutionarystrategy:9 Ten " " One The class for evolutionary strategy A " " - - def __init__(self, sizepop, Vardim, bound, Maxgen, params): the " " - sizepop:population Sizepop - Vardim:dimension of Variables - Bound:boundaries of Variables + maxgen:termination Condition - params:algorithm Required parameters, it is a list which is consisting Of[delta_max, Delta_min] + " " ASelf.sizepop =Sizepop atSelf.vardim =Vardim -Self.bound =bound -Self. Maxgen =Maxgen -Self.params =params -Self.population = [] -Self.fitness =Np.zeros (Self.sizepop) inSelf.trace = Np.zeros (self. Maxgen, 2)) - to defInitialize (self): + " " - Initialize the population of ES the " " * forIinchxrange (0, Self.sizepop): $IND =esindividual (Self.vardim, Self.bound)Panax Notoginseng ind.generate () - Self.population.append (Ind) the + defEvaluation (self): A " " the Evaluation The fitness of the population + " " - forIinchxrange (0, Self.sizepop): $ self.population[i].calculatefitness () $Self.fitness[i] =self.population[i].fitness - - defSolve (self): the " " - The evolution process of the evolutionary strategyWuyi " " theSELF.T =0 - self.initialize () Wu self.evaluation () -Bestindex =Np.argmax (self.fitness) AboutSelf.best =copy.deepcopy (Self.population[bestindex]) $ whileSELF.T <Self . Maxgen: -SELF.T + = 1 -Tmppop =self.mutation () - self.selection (Tmppop) ABest =Np.max (self.fitness) +Bestindex =Np.argmax (self.fitness) the ifBest >self.best.fitness: -Self.best =copy.deepcopy (Self.population[bestindex]) $ theSelf.avefitness =Np.mean (self.fitness) theSelf.trace[self.t-1, 0] = the(1-self.best.fitness)/self.best.fitness theSelf.trace[self.t-1, 1] = (1-self.avefitness)/self.avefitness - Print("Generation%d:optimal function value is:%f, average function value is%f"% ( inSELF.T, self.trace[self.t-1, 0], self.trace[self.t-1, 1])) the Print("Optimal function value is:%f;"% self.trace[self.t-1, 0]) the Print "Optimal solution is:" About PrintSelf.best.chrom the Self.printresult () the the defmutation (self): + " " - mutate the population by a random normal distribution the " "BayiTmppop = [] the forIinchxrange (0, Self.sizepop): theIND =copy.deepcopy (Self.population[i]) -Delta = self.params[0] + self.t * -(Self.params[1]-self.params[0])/Self . Maxgen theInd.chrom + = Np.random.normal (0.0, Delta, Self.vardim) the forKinchxrange (0, Self.vardim): the ifIND.CHROM[K] <self.bound[0, K]: theIND.CHROM[K] =self.bound[0, K] - ifInd.chrom[k] > Self.bound[1, K]: theInd.chrom[k] = self.bound[1, K] the ind.calculatefitness () the Tmppop.append (Ind)94 returnTmppop the the defselection (self, tmppop): the " "98 Update the population About " " - forIinchxrange (0, Self.sizepop):101 ifSelf.fitness[i] <tmppop[i].fitness:102Self.population[i] =Tmppop[i]103Self.fitness[i] =tmppop[i].fitness104 the defPrintresult (self):106 " "107 plot The result of evolutionary strategy108 " "109x =np.arange (0, self. Maxgen) theY1 =self.trace[:, 0]111y2 = self.trace[:, 1] thePlt.plot (x, y1,'R', label='Optimal Value')113Plt.plot (x, Y2,'g', label='Average value') thePlt.xlabel ("Iteration") thePlt.ylabel ("function Value") thePlt.title ("evolutionary strategy for function optimization")117 plt.legend ()118Plt.show ()
To run the program:
1 if __name__ " __main__ " : 2 3 bound = Np.tile ([[ -600], [+]], 4 es = es (1, bound, +, [])
5 es.solve ()
Objfunction See simple genetic algorithm-python realization.
Evolutionary strategy-python Implementation