当前位置:   article > 正文

Python——粒子群算法求解背包问题(详细代码)_粒子群算法 python 背包问题

粒子群算法 python 背包问题

一、粒子群算法简介

二、详细代码

  1. import random
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. '''个人认为参数设置非常的合理,但是效果却是最不好的'''
  5. def init_population(n):
  6. '''生成一个种群'''
  7. population = []
  8. for i in range(100):
  9. cs = [i for i in range(1,n+1)]
  10. random.shuffle(cs)
  11. population.append(cs)
  12. return population
  13. def init_v(n):
  14. '''生成一个初始速度的列表,对应一个种群'''
  15. v = []
  16. for i in range(100):
  17. in1 = []
  18. for j in range(n): #n维
  19. x = random.random()
  20. in1.append(x)
  21. v.append(in1)
  22. return v
  23. def Map(lis):
  24. '''这是一个映射函数,将一个列表变成全排列'''
  25. lis_dup = lis[:]
  26. lis.sort()
  27. #使用两个列表,对其合理的进行排序
  28. location = []
  29. for i in lis_dup:
  30. index = lis.index(i) + 1
  31. location.append(index)
  32. return location
  33. def ff(population,n,v1,v2):
  34. '''传入一个种群,返回不同个体对应函数值的列表'''
  35. y_s = []
  36. for i in population:
  37. location = Map(i)
  38. cost_sum = 0
  39. for j in range(n):
  40. for k in range(n):
  41. loca1 = location.index(j+1)
  42. loca2 = location.index(k+1)
  43. cost = v2[j][k]*v1[loca1][loca2]
  44. cost_sum = cost_sum + cost
  45. y_s.append(cost_sum)
  46. index = y_s.index(min(y_s))
  47. best = population[index] #best为种群中表现最好的个体
  48. return y_s,best
  49. def ff_solo(i,n,v1,v2):
  50. '''传入一个个体,得到这个个体的函数值'''
  51. location = Map(i)
  52. cost_sum = 0
  53. for j in range(n):
  54. for k in range(n):
  55. loca1 = location.index(j+1)
  56. loca2 = location.index(k+1)
  57. cost = v2[j][k]*v1[loca1][loca2]
  58. cost_sum = cost_sum + cost
  59. return cost_sum
  60. def trans_v(v,population,p,g):
  61. '''速度改变函数'''
  62. vs = []
  63. for i in range(len(v)):
  64. r1 = random.random()
  65. r2 = random.random()
  66. j = 10*np.array(v[i]) + 5*r1*(np.array(p[i]) - np.array(population[i])) + 5*r2*(np.array(g) - np.array(population[i]))
  67. j = list(j)
  68. vs.append(j)
  69. return vs
  70. def trans_popu(population,v,n,v1,v2):
  71. '''种群改变函数'''
  72. population_new = np.array(population) + np.array(v)
  73. for i in range(len(population)):
  74. if ff_solo(list(population_new[i]), n, v1, v2) < ff_solo(population[i], n, v1, v2):
  75. population[i] = list(population_new[i])
  76. return population
  77. def trans_p(population,p,n,v1,v2):
  78. '''p也为一个种群,记录粒子到访的最好位置'''
  79. for i in range(len(population)):
  80. if ff_solo(population[i], n, v1, v2) < ff_solo(p[i], n, v1, v2):
  81. p[i] = population[i]
  82. return p
  83. def read():
  84. with open('D:/学习文件/大三上/科研课堂/qap-problems/QAP12.dat','r',encoding='utf-8') as f:
  85. comments = f.read().splitlines()
  86. n = eval(comments[0])
  87. v11 = comments[2:2+n]
  88. v22 = comments[3+n:3+n+n]
  89. v1 = []
  90. v2 = []
  91. for i in v11:
  92. int_list = list(map(int, i.split()))
  93. v1.append(int_list)
  94. for i in v22:
  95. int_list = list(map(int, i.split()))
  96. v2.append(int_list)
  97. return v1,v2,n
  98. def main():
  99. v1,v2,n = read()
  100. population = init_population(n)
  101. v = init_v(n)
  102. y_s_before,g = ff(population,n,v1,v2) #一开始就定义一个全局最优位置
  103. p = population
  104. ans = []
  105. for i in range(1200):
  106. y_s,best = ff(population,n,v1,v2)
  107. #下面这个判断是对全局最优进行判断
  108. if ff_solo(best, n, v1, v2) < ff_solo(g, n, v1, v2):
  109. g = best
  110. #下面更新个体最优
  111. trans_p(population,p,n,v1,v2)
  112. print(min(y_s))
  113. ans.append(min(y_s))
  114. #print(len(v),len(population),len(p))
  115. v = trans_v(v, population, p, best)
  116. population = trans_popu(population,v,n,v1,v2)
  117. plt.plot(ans)
  118. main()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/783581
推荐阅读
相关标签
  

闽ICP备14008679号