首页 > 代码库 > 标准遗传算法(二进制编码 python实现)

标准遗传算法(二进制编码 python实现)

代码地址: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-8
import copy
import 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 =http://www.mamicode.com/0.5-((math.sin( math.sqrt(x**2+y**2) ))**2-0.5)/(1+0.001*(x**2+y**2))**2>"""

 

 

 

 

主函数部分:

binary_sga/sga.py

N=200
V=2
nbits=(17, 17)
maxBinVal=(-5, -5)
minBinVal=(5, 5)
population=[]
population_real=[]
alfa=0.9
belta=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实现)