這篇文章主要介紹了python中遺傳算法的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬主機(jī)、營銷軟件、網(wǎng)站建設(shè)、思茅網(wǎng)站維護(hù)、網(wǎng)站推廣。具體內(nèi)容如下
1、基本概念
2、主要步驟
流程圖如下所示:
3、主要操作介紹
3.1 種群初始化
n個(gè)決策變量{x1,x2,…,xn}。每個(gè)決策變量有取值范圍:下界{L1,L2,…,Ln}和上界{U1,U2,…,Un},則種群中個(gè)體的初始化即隨機(jī)地在決策變量的取值范圍內(nèi)生成各個(gè)決策變量的值:Xj={x1,x2,...,xn},其中xi屬于范圍(Li,Ui)內(nèi)。所有的個(gè)體即構(gòu)成種群。當(dāng)每個(gè)個(gè)體都初始化后,即種群完成初始化。
3.2 評價(jià)種群
population有popsize個(gè)個(gè)體。依次計(jì)算每個(gè)個(gè)體的適應(yīng)度值及評價(jià)種群。
3.3 選擇操作
P(Xj) = fit(Xj)/(fit(X1)+fit(X2)+fit(X3)+fit(X4)),j=1,2,3,4
3.4 交叉操作
3.5 變異操作
符號變異:
4、Python代碼
#-*- coding:utf-8 -*- import random import math from operator import itemgetter class Gene: ''''' This is a class to represent individual(Gene) in GA algorithom each object of this class have two attribute: data, size ''' def __init__(self,**data): self.__dict__.update(data) self.size = len(data['data'])#length of gene class GA: ''''' This is a class of GA algorithm. ''' def __init__(self,parameter): ''''' Initialize the pop of GA algorithom and evaluate the pop by computing its' fitness value . The data structure of pop is composed of several individuals which has the form like that: {'Gene':a object of class Gene, 'fitness': 1.02(for example)} Representation of Gene is a list: [b s0 u0 sita0 s1 u1 sita1 s2 u2 sita2] ''' #parameter = [CXPB, MUTPB, NGEN, popsize, low, up] self.parameter = parameter low = self.parameter[4] up = self.parameter[5] self.bound = [] self.bound.append(low) self.bound.append(up) pop = [] for i in range(self.parameter[3]): geneinfo = [] for pos in range(len(low)): geneinfo.append(random.uniform(self.bound[0][pos], self.bound[1][pos]))#initialise popluation fitness = evaluate(geneinfo)#evaluate each chromosome pop.append({'Gene':Gene(data = geneinfo), 'fitness':fitness})#store the chromosome and its fitness self.pop = pop self.bestindividual = self.selectBest(self.pop)#store the best chromosome in the population def selectBest(self, pop): ''''' select the best individual from pop ''' s_inds = sorted(pop, key = itemgetter("fitness"), reverse = False) return s_inds[0] def selection(self, individuals, k): ''''' select two individuals from pop ''' s_inds = sorted(individuals, key = itemgetter("fitness"), reverse=True)#sort the pop by the reference of 1/fitness sum_fits = sum(1/ind['fitness'] for ind in individuals) #sum up the 1/fitness of the whole pop chosen = [] for i in xrange(k): u = random.random() * sum_fits#randomly produce a num in the range of [0, sum_fits] sum_ = 0 for ind in s_inds: sum_ += 1/ind['fitness']#sum up the 1/fitness if sum_ > u: #when the sum of 1/fitness is bigger than u, choose the one, which means u is in the range of [sum(1,2,...,n-1),sum(1,2,...,n)] and is time to choose the one ,namely n-th individual in the pop chosen.append(ind) break return chosen def crossoperate(self, offspring): ''''' cross operation ''' dim = len(offspring[0]['Gene'].data) geninfo1 = offspring[0]['Gene'].data#Gene's data of first offspring chosen from the selected pop geninfo2 = offspring[1]['Gene'].data#Gene's data of second offspring chosen from the selected pop pos1 = random.randrange(1,dim)#select a position in the range from 0 to dim-1, pos2 = random.randrange(1,dim) newoff = Gene(data = [])#offspring produced by cross operation temp = [] for i in range(dim): if (i >= min(pos1,pos2) and i <= max(pos1,pos2)): temp.append(geninfo2[i]) #the gene data of offspring produced by cross operation is from the second offspring in the range [min(pos1,pos2),max(pos1,pos2)] else: temp.append(geninfo1[i]) #the gene data of offspring produced by cross operation is from the frist offspring in the range [min(pos1,pos2),max(pos1,pos2)] newoff.data = temp return newoff def mutation(self, crossoff, bound): ''''' mutation operation ''' dim = len(crossoff.data) pos = random.randrange(1,dim)#chose a position in crossoff to perform mutation. crossoff.data[pos] = random.uniform(bound[0][pos],bound[1][pos]) return crossoff def GA_main(self): ''''' main frame work of GA ''' popsize = self.parameter[3] print("Start of evolution") # Begin the evolution for g in range(NGEN): print("-- Generation %i --" % g) #Apply selection based on their converted fitness selectpop = self.selection(self.pop, popsize) nextoff = [] while len(nextoff) != popsize: # Apply crossover and mutation on the offspring # Select two individuals offspring = [random.choice(selectpop) for i in xrange(2)] if random.random() < CXPB: # cross two individuals with probability CXPB crossoff = self.crossoperate(offspring) fit_crossoff = evaluate(self.xydata, crossoff.data)# Evaluate the individuals if random.random() < MUTPB: # mutate an individual with probability MUTPB muteoff = self.mutation(crossoff,self.bound) fit_muteoff = evaluate(self.xydata, muteoff.data)# Evaluate the individuals nextoff.append({'Gene':muteoff,'fitness':fit_muteoff}) # The population is entirely replaced by the offspring self.pop = nextoff # Gather all the fitnesses in one list and print the stats fits = [ind['fitness'] for ind in self.pop] length = len(self.pop) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 best_ind = self.selectBest(self.pop) if best_ind['fitness'] < self.bestindividual['fitness']: self.bestindividual = best_ind print("Best individual found is %s, %s" % (self.bestindividual['Gene'].data,self.bestindividual['fitness'])) print(" Min fitness of current pop: %s" % min(fits)) print(" Max fitness of current pop: %s" % max(fits)) print(" Avg fitness of current pop: %s" % mean) print(" Std of currrent pop: %s" % std) print("-- End of (successful) evolution --") if __name__ == "__main__": CXPB, MUTPB, NGEN, popsize = 0.8, 0.3, 50, 100#control parameters up = [64, 64, 64, 64, 64, 64, 64, 64, 64, 64]#upper range for variables low = [-64, -64, -64, -64, -64, -64, -64, -64, -64, -64]#lower range for variables parameter = [CXPB, MUTPB, NGEN, popsize, low, up] run = GA(parameter) run.GA_main()
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“python中遺傳算法的示例分析”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
文章名稱:python中遺傳算法的示例分析-創(chuàng)新互聯(lián)
文章分享:http://jinyejixie.com/article20/ddecco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、外貿(mào)建站、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容