004_wz_使用遗传算法解决0-1背包问题

一、目的

使用传统遗传算法解决0-1经典背包问题。

1.1 传统遗传算法

001_wz_使用遗传算法计算函数的最大值

1.2 0-1经典背包问题

给定 n 件物品,物品的重量为 w [ i ] w[i] w[i],物品的价值为 c [ i ] c[i] c[i]。现挑选物品放入背包中,假定背包能承受的最大重量为 V V V,问应该如何选择装入背包中的物品,使得装入背包中物品的总价值最大?
测试数据:

编号-重量-价值
1: [95, 89],
2: [75, 59],
3: [23, 19],
4: [73, 43],
5: [50, 100],
6: [22, 72],
7: [6, 44],
8: [57, 16],
9: [89, 7],
10: [98, 64]

最优值为388

二、思路与编程

主要流程为以下流程图:
在这里插入图片描述

2.1 导包和全局变量

结合下面的代码理解

import random
import matplotlib.pyplot as plt
import numpy as np

# 物品信息
X = {
    1: [95, 89],
    2: [75, 59],
    3: [23, 19],
    4: [73, 43],
    5: [50, 100],
    6: [22, 72],
    7: [6, 44],
    8: [57, 16],
    9: [89, 7],
    10: [98, 64]
}
# 染色体长度
x_len = len(X)
# 初始化种群数量
population_size = x_len**2
# 重量界限
weight_limit = 300
# 染色体长度
chromosome_size = x_len
# 变异概率mutate_rate
mutate_rate = 0.1

# 前代最大值
fitness_one_max = 0
# 当代最大值
fitness_two_max = 100000
# 停止参数
fitness_limit = 2

2.2 种群初始化

我们初始化的染色体长度为物品数量,使用0来代表不取,1代表取,选择初始化数量为物品数量的平方

def population_init(population_init_size):
    """
    初始化种群
    :param population_init_size: 初始种群数量
    :return: 初始化种群的染色体,列表
    """
    population_init = {}
    for i in range(population_init_size):
        chromosome_temp = []
        for j in range(x_len):
            chromosome_temp.append(random.randint(0, 1))
        population_init[i+1] = chromosome_temp
    populations = list(population_init.values())
    print(populations)
    return populations

2.3 计算适应度

我们的适应度以背包的价值为标准,计算每一个个体所代表的价值

def fitness(populations):
    """
    计算适应度
    :param populations: 初始种群
    :return: 各个个体的适应度,列表
    """
    fitness = {}
    for j, chromosome in enumerate(populations):
        weight_sum = 0
        value_sum = 0
        for i, chromosome_i in enumerate(chromosome):
            if int(chromosome_i) == 1:
                weight_sum += X[i+1][0]
                value_sum += X[i+1][1]
        fitness[j] = [weight_sum, value_sum]
    fitnesses = list(fitness.values())
    return fitnesses

2.4 排序

根据适应度对种群和适应度进行排序(从大到小)

def sort(populations, fitnesses):
    """
    排序,根据适应度排序
    :param populations: 种群
    :param fitnesses: 适应度
    :return: 排好序的populations,fitnesses,列表
    """
    fitness = []
    for fitness_individual in fitnesses:
        fitness.append(fitness_individual[1])
    sort_index = reversed(np.argsort(fitness))
    sort_populations = []
    for i in sort_index:
        sort_populations.append(populations[i])
    sort_fitnesses = sorted(fitnesses, key=lambda x: -x[1])
    return sort_populations, sort_fitnesses

2.5 选择

这里我们先做选择,将停止条件直接放到选择之后即可,将交叉和变异操作放在后面;

我们把种群中重量大于weight_limit的个体剔除掉,之后再判断剩余个体是否多于原种群的一半,如果多于一半我们只取前一半,这样我们每一轮迭代都可以筛选最符合条件的前一半个体,减少了迭代次数,但同时也容易错过最优值,这里可以自己调整每次选择多少个体保留;

然后将选择的个体进行轮盘赌选择,被选择的个体才允许进行繁衍(交叉和变异),注意这里选择的个体数量与保留的个体数量一致,当然你也可以选择一半,这样下面的交叉操作就只会出现一半的新个体,再一次缩小了种群数量,轮盘赌算法

def filter(populations, fitnesses):
    """
    筛选,将重量大于80的个体淘汰,如果淘汰的数量不足一半,直接排序只保留前一半,并使用轮盘赌方法遴选
    :param populations: 初始种群
    :param fitnesses: 适应度
    :return: 排序后的种群与适应度,轮盘赌选择的个体繁衍,列表
    """
    pop = []
    for i, fitness in enumerate(fitnesses):
        if fitness[0] > weight_limit:
            pop.append(i)
    pop = reversed(pop)
    for pop_index in pop:
        populations.pop(pop_index)
        fitnesses.pop(pop_index)
    populations_len = len(populations)
    if populations_len > population_size/2:
        populations, fitnesses = sort(populations, fitnesses)
        populations = populations[:int(population_size/2)]
        fitnesses = fitnesses[:int(population_size/2)]
    populations_len = len(populations)
    selected_index = [0] * populations_len
    for j in range(populations_len):
        index = populations.index(random.choice(populations))
        selected_index[index] += 1
    return populations, fitnesses, selected_index

2.6 交叉

将可以进行繁衍的个体取出,再随机选择一个个体与其进行交叉操作,得到全新的个体;

我在这里没有用到交叉算子,因为使用了轮盘赌算法选择可以繁衍的个体
在这里插入图片描述

def crossover(populations, selected_index):
    """
    交叉
    :param populations: 适应度选择后的种群
    :param selected_index: 被选择繁衍的个体
    :return:
    """
    new_populations = []
    for select_num, population in zip(selected_index, populations):
        for i in range(select_num):
            population_crossed = random.choice(populations)
            cross_point = random.randint(1, population_size-1)
            new_population = population[:cross_point] + population_crossed[cross_point:]
            new_populations.append(new_population)
    return new_populations

2.7 变异

这里使用变异算子,对可以进行变异的个体进行控制,一般变异概率是非常小的,比如说0.01,而我这里将其设置的较大0.1;

变异采用单点变异,使用随机数产生变异点(1–x_len-1),并且只对0点变异

def mutation(populations):
    """
    变异
    :param populations: 种群
    :return: 新种群,列表
    """
    for population in populations:
        if mutate_rate > random.random():
            mutate_point = random.randint(1, x_len-1)
            if population[mutate_point] == 1:
                continue
            else:
                population[mutate_point] = 1
    return populations

2.8 停止条件

若是连续两代种群中最优个体的价值差不足fitness_limit,那么我们认为达到了最优值,停止迭代,输出最优值;

还要防止种群消失;

def is_finished(populations, fitnesses):
    """
    判断退出
    :param fitnesses: 适应度
    :return: 真假值,布尔型
    """
    global fitness_one_max
    global fitness_two_max
    fitness_max = 0
    if len(populations) == 1:
        return False
    for fitness in fitnesses:
        if fitness[1] > fitness_max:
            fitness_max = fitness[1]
    generate_one = fitness_max - fitness_one_max
    if generate_one < fitness_limit and fitness_two_max < fitness_limit:
        return True
    else:
        fitness_two_max = generate_one
        fitness_one_max = fitness_max
        return False

2.9 总控程序

总控程序按照上面流程图走就行,注意我把选择和终止条件提前了;

在总控程序中我还计算了每代的平均适应度;并防止没有最佳个体,若不满足终止条件,将最后一个个体输出即可;

if __name__ == "__main__":
    fitness_avg = []
    populations = population_init(population_size)
    for i in range(100):
        print("第%d轮迭代" % (i+1))
        fitnesses = fitness(populations)
        fitnesses_num = 0
        for fitness_individual in fitnesses:
            fitnesses_num += fitness_individual[1]
        if len(fitnesses) == 1:
            break
        fitness_avg.append(fitnesses_num/len(fitnesses))
        if is_finished(populations, fitnesses):
            break
        populations, fitnesses, selected_index = filter(populations, fitnesses)
        populations = crossover(populations, selected_index)
        populations = mutation(populations)
        population_size = len(populations)
        print(populations)
        print(fitnesses)
    fitnesses = fitness(populations)
    fitness_max = 0
    for i, fitness_individual in enumerate(fitnesses):
        if fitness_individual[1] > fitness_max and fitness_individual[0] <= weight_limit:
            fitness_max = fitness_individual[1]
            index_max = i
    print(populations[index_max], fitnesses[index_max])
    plt.plot(fitness_avg)
    plt.xlabel('epoch')
    plt.ylabel('fitness')
    plt.show()

2.10 结果

[[1, 1, 1, 1, 0, 1, 1, 0, 1, 1], [0, 0, 1, 1, 0, 1, 0, 1, 0, 1], [1, 1, 1, 1, 1, 0, 1, 1, 0, 1], [0, 1, 0, 0, 1, 0, 0, 1, 1, 0], [1, 0, 1, 1, 1, 0, 1, 1, 0, 1], [0, 1, 0, 1, 0, 0, 0, 1, 1, 1], [1, 1, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 1, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 1, 1], [1, 0, 1, 1, 1, 0, 1, 0, 1, 1], [1, 1, 0, 1, 0, 1, 0, 1, 0, 1], [0, 1, 1, 1, 0, 1, 0, 1, 1, 1], [1, 0, 1, 0, 1, 1, 0, 0, 1, 0], [1, 0, 0, 1, 1, 1, 0, 0, 1, 0], [1, 1, 0, 1, 0, 1, 1, 0, 1, 0], [1, 1, 1, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 0, 0, 0, 0, 1], [0, 0, 0, 0, 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0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 1, 1, 1, 0, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 0, 1, 0, 1, 0, 1], [0, 1, 0, 1, 0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0, 1, 1], [0, 0, 0, 1, 0, 1, 0, 0, 1, 1], [0, 0, 0, 1, 0, 1, 0, 0, 1, 1], [1, 0, 0, 0, 0, 1, 0, 1, 0, 0], [0, 1, 1, 1, 0, 0, 1, 0, 1, 0], [0, 1, 1, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0, 1, 0, 1, 0], [0, 0, 1, 0, 0, 1, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 0, 0, 0, 1, 1, 1], [0, 1, 1, 0, 0, 0, 1, 0, 0, 0]]
[[294, 388], [248, 364], [294, 326], [294, 312], [278, 299], [247, 296], [300, 295], [279, 287], [265, 287], [279, 287], [286, 283], [190, 280], [287, 271], [250, 270], [231, 268], [227, 266], [275, 248], [275, 248], [204, 246], [201, 239], [280, 239], [291, 238], [268, 238], [256, 227], [177, 227], [211, 224], [209, 222], [273, 214], [279, 211], [252, 210], [131, 203], [199, 197], [282, 186], [271, 182], [205, 180], [174, 177], [266, 172], [200, 171], [269, 166], [190, 166], [146, 162], [282, 160], [180, 159], [170, 148], [228, 137], [283, 133], [250, 131], [228, 123], [104, 122], [28, 116]]2轮迭代
[[1, 0, 1, 0, 1, 1, 1, 0, 0, 1], [1, 0, 1, 1, 1, 1, 1, 0, 0, 1], [1, 1, 0, 0, 1, 1, 1, 0, 0, 0], [1, 1, 0, 0, 1, 0, 1, 1, 0, 1], [1, 1, 1, 1, 0, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 1, 0, 1, 1, 0, 1, 0, 0], [1, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 1, 0, 0, 1, 0, 1, 1, 0, 1], [1, 1, 0, 0, 0, 1, 1, 0, 1, 0], [0, 1, 0, 1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 1, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 1, 0, 1], [0, 1, 0, 0, 1, 0, 0, 1, 0, 1], [0, 1, 0, 1, 0, 1, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0, 0, 1], [0, 0, 1, 0, 1, 0, 1, 0, 0, 1], [1, 1, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 1, 0, 1]]
[[294, 388], [248, 364], [294, 326], [294, 312], [294, 312], [247, 296], [279, 287], [286, 283], [190, 280], [287, 271], [204, 246], [280, 239], [280, 239], [280, 239], [268, 238], [268, 238], [291, 238], [291, 238], [177, 227], [177, 227], [256, 227], [211, 224], [211, 224], [211, 224], [211, 224]]3轮迭代
[[1, 0, 1, 0, 1, 1, 1, 0, 0, 1], [1, 0, 1, 0, 1, 1, 1, 0, 0, 1], [1, 1, 0, 0, 1, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 1, 1, 0, 1, 1, 0, 0, 1], [1, 0, 1, 0, 1, 1, 0, 1, 0, 1], [0, 1, 0, 0, 1, 0, 1, 1, 0, 1], [1, 1, 0, 1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 1, 1, 0, 1, 0, 0, 0]]
[[294, 388], [248, 364], [294, 326], [294, 312], [294, 312], [294, 312], [247, 296], [286, 283], [287, 271], [293, 253], [204, 246], [204, 246]]4轮迭代
[[1, 0, 1, 0, 1, 1, 1, 0, 0, 1], [1, 1, 0, 0, 1, 1, 1, 0, 1, 0], [1, 1, 1, 0, 1, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 0, 0, 1, 1, 1, 0, 0, 0]]
[[294, 388], [294, 388], [248, 364], [299, 335], [294, 312], [294, 312]]5轮迭代
[[1, 0, 1, 0, 1, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0, 0, 1]]
[[294, 388], [294, 312], [294, 312]]6轮迭代
[[1, 0, 1, 0, 1, 1, 1, 0, 0, 1]]
[[294, 388]]7轮迭代
[1, 0, 1, 0, 1, 1, 1, 0, 0, 1] [294, 388]

在这里插入图片描述
可以看到总体适应度呈上升趋势,但是经过我多次试验,发现这个算法的巡优效果不是很强,我知道是因为选择种群的对半选择太过残酷,或许可以全部选择,只是在每轮迭代去除不适应的个体,在经过多次迭代之后,会出现一个基因非常优秀的种群,那时候选择最佳个体即可,相应的算法占用的资源会更多;

试验次数最佳个体重量,价值
1[1, 0, 1, 0, 1, 1, 1, 1, 0, 0][253, 340]
2[1, 0, 1, 0, 1, 1, 1, 1, 0, 0][253, 340]
3[1, 0, 1, 1, 1, 1, 1, 0, 0, 0][269, 367]
4[1, 1, 0, 0, 1, 1, 0, 1, 0, 0][299, 336]
5[0, 1, 1, 0, 1, 1, 0, 0, 0, 0][170, 250]
6[1, 0, 1, 0, 1, 1, 0, 0, 0, 1][288, 344]
7[1, 0, 0, 1, 1, 1, 1, 0, 0, 0][246, 348]
8[1, 0, 0, 0, 1, 1, 1, 0, 0, 1][271, 369]
9[0, 1, 0, 0, 1, 1, 1, 0, 0, 1][251, 339]
10[1, 0, 0, 0, 1, 1, 1, 1, 0, 0][230, 321]

在10次试验中并没有一个达到最佳值,并且还有一次价值仅仅只有250,平均下来在330左右。算法还需改进,在选择方面进行改进,我相信可以得到更好的结果。

三、参考

遗传算法求解背包问题

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值