This article mainly introduces the Python genetic algorithm. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
Written in front
In the previous article, I have already talked about the basic process of genetic algorithm and implemented it with MATLAB. . This article is mainly for people who have read my previous articles, so I will not go into details about what a genetic algorithm is and its basic content. It is assumed that everyone already knows how I write a genetic algorithm.
Python's genetic algorithm main function
My idea is to create a chromosome class, which includes two variables: chromosome chrom and fitness fitness. Therefore, we can directly create objects as individuals in the population.
#染色体的类 class Chrom: chrom = [] fitness = 0 def showChrom(self): print(self.chrom) def showFitness(self): print(self.fitness)
So we start setting the basic parameters. I use a dictionary to express the population, which means using a dictionary to save all individuals in the population. This is also the method I came up with to create multiple objects.
Convert the dictionary index to the individual label, such as: chrom1, chrom2, etc. The value of a dictionary index is an object. This object has two attributes, namely chromosome and fitness.
In fact, in this regard, I think the idea is superior to matrix programming using MATLAB. Because this can express the idea of individuals and individual attributes very intuitively, it is logically easier to accept than a bunch of matrices.
#基础参数 N = 200 #种群内个体数目 mut = 0.2 #突变概率 acr = 0.2 #交叉概率 pop = {} #存储染色体的字典 for i in range(N): pop['chrom'+str(i)] = Chrom() chromNodes = 2 #染色体节点数(变量个数) iterNum = 10000 #迭代次数 chromRange = [[0, 10], [0, 10]] #染色体范围 aveFitnessList = [] #平均适应度 bestFitnessList = [] #最优适应度
After that comes the initial chromosome, which involves various functions used to initialize the population, calculate fitness, find the optimal, etc. I have divided two here The files are Genetic.py and Fitness.py respectively.
There are eight functions in Genetic.py, which mainly include functions that act on populations or chromosome operations, namely:
findBest function, used to find the population The optimal chromosome;
findworse function is used to find the worst chromosome in the population;
initialize function is used to initialize the population;
calAveFitness function, used to calculate the average fitness of the population;
mutChrom function, used to mutate chromosomes;
inRange function, used to determine whether the chromosome node value is out of bounds;
acrChrom function, used to cross the chromosome;
compareChrom function is used to compare the superiority of two chromosomes.
There are two functions in Fitness.py, which mainly include functions for fitness operations, namely:
calFitness function, use To iterate each individual and calculate the fitness (calculated using the funcFitness function);
funcFitness function calculates the fitness of a single individual.
So the initialization code can be listed as
#初始染色体 pop = Genetic.initialize(pop, chromNodes, chromRange) pop = Fitness.calFitness(pop) #计算适应度 bestChrom = Genetic.findBest(pop) #寻找最优染色体 bestFitnessList.append(bestChrom[1]) #将当前最优适应度压入列表中 aveFitnessList.append(Genetic.calAveFitness(pop, N)) #计算并存储平均适应度
The idea and logic of the iterative process are the same as MATLAB
#开始迭代 for t in range(iterNum): #染色体突变 pop = Genetic.mutChrom(pop, mut, chromNodes, bestChrom, chromRange) #染色体交换 pop = Genetic.acrChrom(pop, acr, chromNodes) #寻找最优 nowBestChrom = Genetic.findBest(pop) #比较前一个时间的最优和现在的最优 bestChrom = Genetic.compareChrom(nowBestChrom, bestChrom) #寻找与替换最劣 worseChrom = Genetic.findWorse(pop) pop[worseChrom[0]].chrom = pop[bestChrom[0]].chrom.copy() pop[worseChrom[0]].fitness = pop[bestChrom[0]].fitness #存储最优与平均 bestFitnessList.append(bestChrom[1]) aveFitnessList.append(Genetic.calAveFitness(pop, N))
Finally, make an iterative image
plt.figure(1) plt.plot(x, aveFitnessList) plt.plot(x, bestFitnessList) plt.show()
Finally, add various libraries and files at the front to run .
import Genetic import Fitness import matplotlib.pyplot as plt import numpy as np
Enlightenment
It can be said that the most important insight is the category of chromosomes. In fact, the two files Genetic.py and Fitness.py can also be directly packaged into classes, but in this case I think the main file is too bloated, and it would be superfluous to package them into classes in other files. After all, this is just a small program, so That's what I wrote.
I deeply understand the advantages of object-oriented programming. It is a real pleasure to deal with programming logic. You only need to think about the properties of the object, eliminating a lot of complicated thinking.
Another insight is to use the dictionary method to create objects when creating multiple objects. At the beginning, I was also confused about how to create an object array similar to that in C++. I searched for various methods on the Internet, but the results were all evasive (of course, it may be that my search ability is too poor and I couldn't find it), so after trying it, I came across this This method.
I will explain this method in detail when I have time, but this time I will stop here.
The remaining functions are supplemented
First are the eight functions in Genetic.py
import random #寻找最优染色体 def findBest(pop): best = ['1', 0.0000001] for i in pop: if best[1] < pop[i].fitness: best = [i, pop[i].fitness] return best #寻找最劣染色体 def findWorse(pop): worse = ['1', 999999] for i in pop: if worse[1] > pop[i].fitness: worse = [i, pop[i].fitness] return worse #赋初始值 def initialize(pop, chromNodes, chromRange): for i in pop: chromList = [] for j in range(chromNodes): chromList.append(random.uniform(chromRange[j][0], chromRange[j][1]+1)) pop[i].chrom = chromList.copy() return pop #计算平均适应度 def calAveFitness(pop, N): sumFitness = 0 for i in pop: sumFitness = sumFitness + pop[i].fitness aveFitness = sumFitness / N return aveFitness #进行突变 def mutChrom(pop, mut, chromNodes, bestChrom, chromRange): for i in pop: #如果随机数小于变异概率(即可以变异) if mut > random.random(): mutNode = random.randrange(0,chromNodes) mutRange = random.random() * (1-pop[i].fitness/bestChrom[1])**2 pop[i].chrom[mutNode] = pop[i].chrom[mutNode] * (1+mutRange) #判断变异后的范围是否在要求范围内 pop[i].chrom[mutNode] = inRange(pop[i].chrom[mutNode], chromRange[mutNode]) return pop #检验便宜范围是否在要求范围内 def inRange(mutNode, chromRange): if chromRange[0] < mutNode < chromRange[1]: return mutNode elif mutNode-chromRange[0] > mutNode-chromRange[1]: return chromRange[1] else: return chromRange[0] #进行交叉 def acrChrom(pop, acr, chromNodes): for i in pop: for j in pop: if acr > random.random(): acrNode = random.randrange(0, chromNodes) #两个染色体节点进行交换 pop[i].chrom[acrNode], pop[j].chrom[acrNode] = pop[j].chrom[acrNode], pop[i].chrom[acrNode] return pop #进行比较 def compareChrom(nowbestChrom, bestChrom): if bestChrom[1] > nowbestChrom[1]: return bestChrom else: return nowbestChrom
Then Two functions of Fitness.py
import math def calFitness(pop): for i in pop: #计算每个染色体的适应度 pop[i].fitness = funcFitness(pop[i].chrom) return pop def funcFitness(chrom): #适应度函数 fitness = math.sin(chrom[0])+math.cos(chrom[1])+0.1*(chrom[0]+chrom[1])
The above is the detailed content of Python code to implement genetic algorithm. For more information, please follow other related articles on the PHP Chinese website!