Exemplo n.º 1
0
def crossover(pop, pc, fit_value, chrom_length, max_value):
    """

    :param pop: 二进制种群
    :param pc: 交配概率
    :param fit_value:
    :param chrom_length: 编码基因长度
    :param max_value: 最大值限制
    :return: 交叉后新种群,新种群适应度
    """
    pop_len = len(pop)
    for i in range(pop_len):
        if random.random() < pc:
            mother, father = findMoFa(pop, fit_value, chrom_length, max_value)
            cpoint = random.randint(1, len(pop[0]) - 2)  # 随机交叉点(单点交叉策略)
            temp1 = []
            temp2 = []
            temp1.extend(mother[0:cpoint])
            temp1.extend(father[cpoint:chrom_length])
            temp2.extend(father[0:cpoint])
            temp2.extend(mother[cpoint:chrom_length])
            pop.append(temp1)
            pop.append(temp2)
            fit_value = calobjValue(pop, chrom_length, max_value)
    return pop, fit_value
Exemplo n.º 2
0
def crossover(pop, pc, fit_value, chrom_length, max_value):
    """

    :param pop: 二进制种群
    :param pc: 交配概率
    :param fit_value:
    :param chrom_length: 编码基因长度
    :param max_value: 最大值限制
    :return: 交叉后新种群,新种群适应度
    """
    pop_len = len(pop)
    for i in range(pop_len):
        if random.random() < pc:
            # mother, father = findMoFa(pop, fit_value, chrom_length, max_value)
            mother, mother_fit = best(pop, fit_value)
            father = pop[i]
            cpoint = random.randint(0, chrom_length - 1)  # 随机交叉点(单点交叉策略)
            new_one1 = []
            new_one2 = []
            for j in range(len(mother)):
                temp1 = []
                temp2 = []
                temp1.extend(mother[j][0:cpoint])
                temp1.extend(father[j][cpoint:chrom_length])
                temp2.extend(father[j][0:cpoint])
                temp2.extend(mother[j][cpoint:chrom_length])
                new_one1.append(temp1)
                new_one2.append(temp2)
            pop.append(new_one1)
            pop.append(new_one2)
            pop = calfitValue(pop, chrom_length, max_value)
            fit_value = calobjValue(pop, chrom_length, max_value)
    return pop, fit_value
Exemplo n.º 3
0
def selection(pop, fit_value, pop_size, chrom_length, max_value):
    """

    :param pop:
    :param fit_value:
    :return:
    """
    n = len(pop)
    max_p, max_fit = best(pop, fit_value)
    newpop = []  # 选择后新种群
    newpop.append(max_p)

    # print(len(newpop))

    # 概率累加计算
    newfit_value = cumsum(fit_value)
    # 轮盘选择
    # n = pop_size - 1
    # while n > 0:
    #     choose = random.random()
    #     for j in range(n):
    #         if newfit_value[j] >= choose:
    #             if boolcondition(pop[j]):
    #                 n -= 1
    #                 newpop.append(pop[j])
    #                 break
    for i in range(pop_size - 1):
        choose = random.random()
        for j in range(n):
            if newfit_value[j] >= choose:
                newpop.append(pop[j])
                break
    return newpop, calobjValue(newpop, chrom_length, max_value)
def mutation(pop, pm, chrom_length, max_value):
    """
    基因突变
    :param pop:二进制种群
    :param pm: 变异概率
    :return: 变异后新种群,适应度
    """
    px = len(pop)
    py = len(pop[0])
    for i in range(px):
        if random.random() < pm:
            m = random.randint(1, py)  # 随机变异m个点
            for j in range(m):
                point = random.randint(0, py - 1)  # 随机生成变异点位置
                if pop[i][point] == 1:
                    pop[i][point] = 0
                else:
                    pop[i][point] = 1
    return pop, calobjValue(pop, chrom_length, max_value)
def mutation(pop, pm, chrom_length, max_value):
    """
    基因突变
    :param pop:二进制种群
    :param pm: 变异概率
    :return: 变异后新种群,适应度
    """
    pop_size = len(pop)
    type_num = len(pop[0])
    for i in range(pop_size):
        if random.random() < pm:
            # x = random.randint(0, type_num)  # 随机变异的基因组下标
            x = i  # 随机变异的基因组下标
            for j in range(type_num):
                m = random.randint(1, chrom_length)  # 变异点个数
                for z in range(m):
                    point = random.randint(0, chrom_length - 1)  # 随机生成变异点位置
                    if pop[x][j][point] == 1:
                        pop[x][j][point] = 0
                    else:
                        pop[x][j][point] = 1
    pop = calfitValue(pop, chrom_length, max_value)
    return pop, calobjValue(pop, chrom_length, max_value)
pop_size = 10  # 种群数量
iterations = 500  # 迭代进化次数
max_value = 10  # 基因中允许出现的最大值
chrom_length = 10  # 染色体长度
pc = 0.6  # 交配概率
pm = 0.01  # 变异概率
results = []  # 每代最优解集
fit_value = []  # 个体适应度
fit_mean = []  # 平均适应度

pop = geneEncoding(pop_size, chrom_length)  # 初始化二进制种群基因编码

for i in range(iterations):
    # print('NO. ', i + 1)
    fit_value = calobjValue(pop, chrom_length, max_value)  # 适应度计算
    # print('初始', fit_value)
    # fit_value = calfitValue(obj_value)  # 筛选淘汰
    best_indiviual, best_fit = best(pop, fit_value)  # 最优解集
    best_indiviual_ten = decoding(best_indiviual, chrom_length,
                                  max_value)  # 最优解集进制转换
    results.append([best_indiviual_ten, best_fit])
    pop, fit_value = selection(pop, fit_value, pop_size, chrom_length,
                               max_value)
    # print('选择', fit_value)
    pop, fit_value = crossover(pop, pc, fit_value, chrom_length, max_value)
    # print('交叉', fit_value)
    pop, fit_value = mutation(pop, pm, chrom_length, max_value)
    # print('变异', fit_value)

print(results[iterations - 1][1])