python遗传算法计算实例_遗传算法python简单例子(详解)
# -*-coding:utf-8 -*- #目標求解sin(x)最大值 import random import math import matplotlib.pyplot as plt #初始化種群 生成chromosome_length大小的population_size個個體的種群 def species_origin(population_size,chromosome_length): population=[[]] #one dimension represent a individual for i in range(population_size): temporary=[] #染色體暫存器 for j in range(chromosome_length): temporary.append(random.randint(0,1)) #隨機產生一個染色體,由二進制數組成 population.append(temporary) #將染色體添加到種群中 return population[1:] # 將種群返回,種群是個二維數組,個體和染色體兩維 #從二進制到十進制 #編碼 input:種群,染色體長度 def translation(population,chromosome_length): temporary=[] for i in range(len(population)): total=0 for j in range(chromosome_length): total+=population[i][j]*(math.pow(2,j)) #從第一個基因開始,每位對2求冪,再求和 # 如:0101 轉成十進制為:1 * 20 + 0 * 21 + 1 * 22 + 0 * 23 = 1 + 0 + 4 + 0 = 5 temporary.append(total) #一個染色體編碼完成,由一個二進制數編碼為一個十進制數 return temporary # 返回種群中所有個體編碼完成后的十進制數 #from protein to function,according to its functoin value #a protein realize its function according its structure # 目標函數相當于環境 對染色體進行篩選,這里是sin(x) def function(population,chromosome_length,max_value): temporary=[] function1=[] temporary=translation(population,chromosome_length) # 暫存種群中的所有的染色體(十進制) for i in range(len(temporary)): x=temporary[i]*max_value/(math.pow(2,chromosome_length)-1) # function1.append(math.sin(x)) #這里將sin(x)作為目標函數 return function1 #定義適應度 def fitness(function1): fitness1=[] min_fitness=mf=0 for i in range(len(function1)): if(function1[i]+mf>0): temporary=mf+function1[i] else: temporary=0.0 # 如果適應度小于0,則定為0 fitness1.append(temporary) #將適應度添加到列表中 return fitness1 #計算適應度和 def sum(fitness1): total=0 for i in range(len(fitness1)): total+=fitness1[i] return total #計算適應度斐伯納且列表 def cumsum(fitness1): for i in range(len(fitness1)-2,-1,-1): # range(start,stop,[step]) # 倒計數 total=0 j=0 while(j<=i): total+=fitness1[j] j+=1 fitness1[i]=total fitness1[len(fitness1)-1]=1 #3.選擇種群中個體適應度最大的個體 def selection(population,fitness1): new_fitness=[] #單個公式暫存器 total_fitness=sum(fitness1) #將所有的適應度求和 for i in range(len(fitness1)): new_fitness.append(fitness1[i]/total_fitness) #將所有個體的適應度正則化 cumsum(new_fitness) # ms=[] #存活的種群 population_length=pop_len=len(population) #求出種群長度 #根據隨機數確定哪幾個能存活 for i in range(pop_len): ms.append(random.random()) # 產生種群個數的隨機值 ms.sort() # 存活的種群排序 fitin=0 newin=0 new_population=new_pop=population #輪盤賭方式 while newinbestfitness): bestfitness=fitness1[i] bestindividual=population[i] return [bestindividual,bestfitness] population_size=500 max_value=10 # ?這個值是干什么的 chromosome_length=10 pc=0.6 pm=0.01 results=[[]] fitness1=[] fitmean=[] population=pop=species_origin(population_size,chromosome_length) #生成一個初始的種群 for i in range(population_size): function1=function(population,chromosome_length,max_value) fitness1=fitness(function1) best_individual,best_fitness=best(population,fitness1) results.append([best_fitness,b2d(best_individual,max_value,chromosome_length)]) #將最好的個體和最好的適應度保存,并將最好的個體轉成十進制 selection(population,fitness1) crossover(population,pc) mutation(population,pm) results=results[1:] results.sort() X=[] Y=[] for i in range(500): X.append(i) Y.append(results[i][0]) plt.plot(X,Y) plt.show()
總結
以上是生活随笔為你收集整理的python遗传算法计算实例_遗传算法python简单例子(详解)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qt编译实现简单的文本编译器有粘贴复制_
- 下一篇: websocket python爬虫_p