基于遗传算法背包问题java_遗传算法解决0-1背包问题

1 import numpy2 import matplotlib.pyplot asplt3

4

5 data = numpy.array([[77, 92],6 [22, 22],7 [29, 87],8 [50, 46],9 [99, 90]])10

11

12 class GA(object):13 """14 遗传算法解决0-1背包问题15 """16

17 def __init__(self, length, number, iter_number):18 """19 参数初始化20 :param length: 5

21 :param number: 300

22 :param iter_number: 300

23 """24 self.length =length # 确定染色体编码长度25 self.number =number # 确定初始化种群数量26 self.iteration =iter_number # 设置迭代次数27 self.bag_capacity = 100# 背包容量28

29 self.retain_rate = 0.2 # 每一代精英选择出前20%

30 self.random_selection_rate = 0.5 # 对于不是前20%的,有0.5的概率可以进行繁殖31 self.mutation_rate = 0.01 # 变异概率0.01

32

33 def initial_population(self):34 """35 种群初始化,36

37 :return: 返回种群集合38 """39 init_population = numpy.random.randint(low=0, high=2, size=[self.length, self.number], dtype=numpy.int16)40 returninit_population41

42 def weight_price(self, chromosome):43 """44 计算累计重量和累计价格45 :param chromosome:46 :return:返回每一个个体的累计重量和价格47 """48 w_accumulation = 0

49 p_accumulation = 0

50 for i inrange(len(chromosome)):51

52 w = chromosome[i]*data[i][0]53 p = chromosome[i]*data[i][1]54 w_accumulation = w +w_accumulation55 p_accumulation = p +p_accumulation56

57 returnw_accumulation, p_accumulation58

59 def fitness_function(self, chromosome):60 """61 计算适应度函数,一般来说,背包的价值越高越好,但是62 当重量超过100时,适应度函数=0

63 :param chromosome:64 :return:65 """66

67 weight, price =self.weight_price(chromosome)68 if weight >self.bag_capacity:69 fitness = 0

70 else:71 fitness =price72

73 returnfitness74

75 def fitness_average(self, init_population):76 """77 求出这个种群的平均适应度,才能知道种群已经进化好了78 :return:返回的是一个种群的平均适应度79 """80 f_accumulation = 0

81 for z in range(init_population.shape[1]):82 f_tem =self.fitness_function(init_population[:, z])83 f_accumulation = f_accumulation +f_tem84 f_accumulation = f_accumulation/init_population.shape[1]85 returnf_accumulation86

87 def selection(self, init_population):88 """89 选择90 :param init_population:91 :return: 返回选择后的父代,数量是不定的92 """93 sort_population =numpy.array([[], [], [], [], [], []]) # 生成一个排序后的种群列表,暂时为空94 for i in range(init_population.shape[1]):95

96 x1 =init_population[:, i]97 # print('打印x1', x1)98 x2 =self.fitness_function(x1)99 x =numpy.r_[x1, x2]100 # print('打印x', x)101 sort_population =numpy.c_[sort_population, x]102

103 sort_population =sort_population.T[numpy.lexsort(sort_population)].T # 联合排序,从小到大排列104

105 # print('排序后长度', sort_population.shape[1])106 print(sort_population)107

108 # 选出适应性强的个体,精英选择109 retain_length = sort_population.shape[1]*self.retain_rate110

111 parents =numpy.array([[], [], [], [], [], []]) # 生成一个父代列表,暂时为空112 for j in range(int(retain_length)):113 y1 = sort_population[:, -(j+1)]114 parents =numpy.c_[parents, y1]115

116 # print(parents.shape[1])117

118 rest = sort_population.shape[1] -retain_length # 精英选择后剩下的个体数119 for q in range(int(rest)):120

121 if numpy.random.random() <122 y2="sort_population[:," q parents="numpy.c_[parents,">

125 parents = numpy.delete(parents, -1, axis=0) # 删除最后一行,删除了f值126 # print('打印选择后的个体数')127 # print(parents.shape[0])128

129 parents = numpy.array(parents, dtype=numpy.int16)130

131 returnparents132

133 def crossover(self, parents):134 """135 交叉生成子代,和初始化的种群数量一致136 :param parents:137 :return:返回子代138 """139 children =numpy.array([[], [], [], [], []]) # 子列表初始化140

141 while children.shape[1] <142 father="numpy.random.randint(0," parents.shape mother="numpy.random.randint(0," if cross_point="numpy.random.randint(0," self.length mark="0</p">

149 for i inrange(cross_point):150 mark |= (1 <

152 father =parents[:, father]153 # print(father)154 mother =parents[:, mother]155

156 # 子代将获得父亲在交叉点前的基因和母亲在交叉点后(包括交叉点)的基因157 child = ((father & mark) | (mother & ~mark)) & ((1 << self.length) - 1)158

159 children =numpy.c_[children, child]160

161 # 经过繁殖后,子代的数量与原始种群数量相等,在这里可以更新种群。162 # print('子代数量', children.shape[1])163 # print(children.dtype)164 children = numpy.array(children, dtype=numpy.int16)165 returnchildren166

167 def mutation(self, children):168 """169 变异170

171 :return:172 """173 for i in range(children.shape[1]):174

175 if numpy.random.random() <176 j="numpy.random.randint(0," self.length s children i dtype="numpy.int16)179" returnchildren180>

181 def plot_figure(self, iter_plot, f_plot, f_set_plot):182 """183 画出迭代次数和平均适应度曲线图184 画出迭代次数和每一步迭代最大值图185 :return:186 """187 plt.figure()188

189 ax1 = plt.subplot(121)190 ax2 = plt.subplot(122)191

192 plt.sca(ax1)193 plt.plot(iter_plot, f_plot)194 plt.ylim(0, 140) # 设置y轴范围195

196 plt.sca(ax2)197 plt.plot(iter_plot, f_set_plot)198 plt.ylim(0, 140) # 设置y轴范围199 plt.show()200

201 def main(self):202 """203 main函数,用来进化204 对当前种群依次进行选择、交叉并生成新一代种群,然后对新一代种群进行变异205 :return:206 """207 init_population =self.initial_population()208 # print(init_population)209

210 iter_plot =[]211 f_plot =[]212 iteration = 0

213

214 f_set_plot =[]215

216 while iteration

218 parents =self.selection(init_population) # 选择后的父代219 children =self.crossover(parents)220 mutation_children =self.mutation(children)221

222 init_population =mutation_children223

224 f_set =[] # 求出每一步迭代的最大值225 for init in range(init_population.shape[1]):226 f_set_tem =self.fitness_function(init_population[:, init])227 f_set.append(f_set_tem)228

229 f_set =max(f_set)230

231 f_set_plot.append(f_set)232

233 iter_plot.append(iteration)234 iteration = iteration+1

235 print("第%s进化得如何******************************************" %iteration)236 f_average =self.fitness_average(init_population)237 f_plot.append(f_average)238 print(f_set)239 # f_accumulation = f_accumulation +f240 # f_print = f_accumulation/(iteration + 1)241 # print(f_print)242 self.plot_figure(iter_plot, f_plot, f_set_plot)243

244

245 if __name__ == '__main__':246 g1 = GA(5, 300, 100)247 g1.main()

176>142>122>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值