標準遺傳演算法(二進位編碼 python實現)

來源:互聯網
上載者:User

標籤:最大   sel   問題   break   主函數   selection   cti   its   class   

代碼地址:https://github.com/guojun007/binary_sga

 

種群初始化:

binary_sga/population_init/population_init.py

#種群初始化函數def population_init(population, N, V, nbits):    #自變數個數V並沒有用到    del population[:]    for i in xrange(N):        tempIndividual=[]        for j in nbits:            tempVal=[]            for k in xrange(j):                tempVal.append(random.randint(0, 1))            tempIndividual.append(tempVal)        population.append(tempIndividual)

 

選擇操作:(輪盤賭選擇)

binary_sga/selection/selection.py

#!/usr/bin/env python#encoding:UTF-8import copyimport random#輪盤賭選擇法def selection(population, xbin):    s=sum(xbin)    temp=[k*1.0/s for k in xbin]    temp2=[]    s2=0    for k in temp:        s2=s2+k        temp2.append(s2)    temp3=[]    for _ in xrange(len(population)):        r=random.random()        for i in xrange(len(temp2)):            if r<=temp2[i]:                temp3.append(i)                break    temp4=[]    temp5=[]    for i in temp3:        temp4.append(copy.deepcopy(population[i]))        temp5.append(xbin[i])    population[:]=temp4    xbin[:]=temp5

 

 

交叉操作:(單點交叉)

binary_sga/crossover/crossover.py

#二進位編碼,單點交叉def crossover(population, alfa, nbits):    for i in xrange(len(population), 2):        for j in xrange(len(nbits)):            r=random.random()            if r<alfa:                p=random.randint(1, nbits[j]-1)                population[i][j][p:], population[i+1][j][p:]=population[i+1][j][p:], population[i][j][p:]

 

 

變異操作:

binary_sga/mutation/mutation.py

def mutation(population, belta, nbits):    for i in xrange(len(population)):        for j in xrange(len(nbits)):            for k in xrange(nbits[j]):                r=random.random()                if r<belta:                    population[i][j][k]^=1

 

 

二進位個體解碼操作:

binary_sga/decode/decode.py

#種群個體解碼def decode(population, population_real, minBinVal, maxBinVal, nbits):    del population_real[:]    def iner(valList):        L=len(valList)        s=0        for i in valList:            s=s+i*(2**(L-1))            L=L-1        return s    for i in population:        temp=[]        for j in i:            temp.append(iner(j))        for j in xrange(len(temp)):            temp[j]=temp[j]*(maxBinVal[j]-minBinVal[j])*1.0/(2**(nbits[j])-1)+minBinVal[j]        population_real.append(temp)

 

 

測試函數部分:

binary_sga/function/object_fun.py

#對偶問題, 轉化為求最大值#二維 Rastrigin測試函數def object_fun(p):    import math    x=p[0]    y=p[1]    object_value=20.0+x**2+y**2-10.0*(math.cos(2*math.pi*x)+math.cos(2*math.pi*y))    return 100.0-object_value"""#求最大值,無需轉化#二維 Schaffer測試函數def object_fun(p):    import math    x=p[0]    y=p[1]    object_value =0.5-((math.sin( math.sqrt(x**2+y**2) ))**2-0.5)/(1+0.001*(x**2+y**2))**2    return object_value"""

 

 

 

 

主函數部分:

binary_sga/sga.py

N=200V=2nbits=(17, 17)maxBinVal=(-5, -5)minBinVal=(5, 5)population=[]population_real=[]alfa=0.9belta=0.05#目標函數值列表xbin=[]def per_run():    population_init(population, N, V, nbits)    for i in xrange(200):        decode(population, population_real, minBinVal, maxBinVal, nbits)        eval_fun(population_real, xbin)        selection(population, xbin)        crossover(population, alfa, nbits)        mutation(population, belta, nbits)    decode(population, population_real, minBinVal, maxBinVal, nbits)    eval_fun(population_real, xbin)    return 100.0-max(xbin)

N為個體種群數。

V為自變數個數。

nbits 各個自變數二進位編碼的長度。

maxBinVal 各個自變數 上限。
minBinVal 各個自變數 下限。
種群個體 二進位編碼 列表 population。
種群個體 實數解碼  列表 population_real。

alfa=0.9  交叉機率。belta=0.05 變異機率。


 

標準遺傳演算法(二進位編碼 python實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.