Example #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)
            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
Example #2
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)
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])

X = []
Y = []