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