0


优化算法 | 人工蜂群算法(附Python代码)

hello,大家好。各位可点击左下方阅读原文,访问公众号官方店铺。谨防上当受骗,感谢各位支持!

今天为各位更新人工蜂群算法(Artificial Bee Colony,ABC)的Python代码,之前我们在MATLAB数学建模(十一) | 人工蜂群算法(附MATLAB代码)这篇推文讲解了ABC算法的基本思想,忘记ABC算法的小伙伴可以点击上述链接复习一下。

目录


1.ABC算法基本步骤

1)初始化各蜜源

  1. X
  2. i
  3. X_i
  4. Xi ; 设定参数
  5. N
  6. P
  7. NP
  8. NP
  9. l
  10. i
  11. m
  12. i
  13. t
  14. limit
  15. limit 以及最大迭代次数
  16. m
  17. a
  18. x
  19. I
  20. t
  21. maxIt
  22. maxIt; 计数器初始化
  23. i
  24. t
  25. =
  26. 1
  27. it=1
  28. it=1;

2)为蜜源

  1. X
  2. i
  3. X_i
  4. Xi​分配一只引领蜂,按式
  5. v
  6. i
  7. d
  8. =
  9. x
  10. i
  11. d
  12. +
  13. φ
  14. (
  15. x
  16. i
  17. d
  18. x
  19. j
  20. d
  21. )
  22. v_{i d}=x_{i d}+\varphi\left(x_{i d}-x_{j d}\right)
  23. vid​=xid​+φ(xid​−xjd​)进行搜索,产生新蜜源
  24. V
  25. i
  26. V_i
  27. Vi ;

3)依据式

  1. f
  2. i
  3. t
  4. i
  5. =
  6. {
  7. 1
  8. /
  9. (
  10. 1
  11. +
  12. f
  13. i
  14. )
  15. ,
  16. f
  17. i
  18. 0
  19. 1
  20. +
  21. abs
  22. (
  23. f
  24. i
  25. )
  26. ,
  27. otherwise
  28. f i t_{i}= \begin{cases}1 /\left(1+f_{i}\right), & f_{i} \geqslant 0 \\ 1+\operatorname{abs}\left(f_{i}\right), & \text { otherwise }\end{cases}
  29. fiti​={1/(1+fi​),1+abs(fi​),​fi​⩾0 otherwise ​评价
  30. V
  31. i
  32. V_i
  33. Vi​的适应度,根据贪婪选择的方法确定保留的蜜源;

4)由式

  1. p
  2. i
  3. =
  4. f
  5. i
  6. t
  7. i
  8. /
  9. i
  10. =
  11. 1
  12. N
  13. P
  14. f
  15. i
  16. t
  17. i
  18. p_{i}=f i t_{i} / \sum_{i=1}^{N P} f i t_{i}
  19. pi​=fiti​/∑i=1NPfiti​计算引领蜂找到的蜜源被跟随的概率;

5)跟随峰采用与引领蜂相同的方式进行搜索,根据贪婪选择的方法确定保留的蜜源;

6)判断蜜源 是否满足被放弃的条件。如满足,对应的引领蜂角色变为侦察蜂,否则直接转到8);

7)侦察蜂根据式

  1. X
  2. i
  3. t
  4. +
  5. 1
  6. =
  7. {
  8. L
  9. d
  10. +
  11. rand
  12. (
  13. 0
  14. ,
  15. 1
  16. )
  17. (
  18. U
  19. d
  20. L
  21. d
  22. )
  23. ,
  24. trial
  25. i
  26. limit
  27. X
  28. i
  29. t
  30. ,
  31. trial
  32. i
  33. <
  34. limit
  35. X_{i}^{t+1}=\left\{\begin{array}{l} L_{d}+\operatorname{rand}(0,1)\left(U_{d}-L_{d}\right), \operatorname{trial}_{i} \geqslant \text { limit } \\ X_{i}^{t}, \operatorname{trial}_{i}<\text { limit } \end{array}\right.
  36. Xit+1​={Ld​+rand(0,1)(Ud​−Ld​),triali​⩾ limit Xit​,triali​< limit ​随机产生新蜜源;

8)

  1. i
  2. t
  3. =
  4. i
  5. t
  6. +
  7. 1
  8. it=it+1
  9. it=it+1; 判断算法是否满足终止条件,若满足则终止,输出最优解,否则转到2)。

更多关于ABC算法详细内容详见MATLAB数学建模(十一) | 人工蜂群算法(附MATLAB代码)。


2.ABC算法Python代码

整个ABC算法Python代码共包含两个.py文件,即artificial_bee_colony.py和app.py。**这里需要注意的是,需要各位自行安装ypstruct库,安装方法可以参考https://pypi.org/project/ypstruct/**。

artificial_bee_colony.py文件如下所示:

  1. import numpy as np
  2. from ypstruct import structure
  3. defrun(problem, params):# 函数信息
  4. costfunc = problem.costfunc
  5. nvar = problem.nvar
  6. varmin = problem.varmin
  7. varmax = problem.varmax
  8. # 参数信息
  9. maxit = params.maxit
  10. npop = params.npop
  11. nonlooker = params.nonlooker
  12. limit =int(np.round(0.6*nvar*npop))
  13. a = params.a
  14. # 空的蜂群结构
  15. empty_bee = structure()
  16. empty_bee.position =None
  17. empty_bee.cost =None# 临时蜂群结构
  18. newbee = structure()
  19. newbee.position =None
  20. newbee.cost =None# 初始化全局最优解
  21. bestsol = empty_bee.deepcopy()
  22. bestsol.cost = np.inf
  23. # 种群初始化
  24. pop = empty_bee.repeat(npop)for i inrange(npop):
  25. pop[i].position = np.random.uniform(varmin, varmax, nvar)
  26. pop[i].cost = costfunc(pop[i].position)if pop[i].cost < bestsol.cost:
  27. bestsol = pop[i].deepcopy()# 初始化每个个体的抛弃次数
  28. count = np.empty(npop)# 记录每一代中全局最优个体目标函数值
  29. bestcost = np.empty(maxit)# 人工蜂群算法主循环for it inrange(maxit):# 引领蜂for i inrange(npop):# 随机选择k,不等于i
  30. K = np.append(np.arange(0,i),np.arange(i+1,npop))
  31. k = K[np.random.randint(K.size)]# 定义加速系数
  32. phi = a * np.random.uniform(-1,1, nvar)# 新的蜜蜂位置
  33. newbee.position = pop[i].position + phi *(pop[i].position - pop[k].position)# 计算新蜜蜂目标函数值
  34. newbee.cost = costfunc(newbee.position)# 通过比较目标函数值,更新第i个蜜蜂的位置if newbee.cost < pop[i].cost:
  35. pop[i]= newbee.deepcopy()else:
  36. count[i]+=1# 计算适应度值和选择概率
  37. fit = np.empty(npop)
  38. meancost = np.mean([pop[i].cost for i inrange(npop)])for i inrange(npop):
  39. fit[i]= np.exp(-pop[i].cost/meancost)#将目标函数值转换为适应度值
  40. probs = fit / np.sum(fit)# 跟随蜂for m inrange(nonlooker):# 通过轮盘赌的方式选择蜜源
  41. i = roulette_wheel_selection(probs)# 随机选择k,不等于i
  42. K = np.append(np.arange(0, i), np.arange(i +1, npop))
  43. k = K[np.random.randint(K.size)]# 定义加速系数
  44. phi = a * np.random.uniform(-1,1, nvar)# 新的蜜蜂位置
  45. newbee.position = pop[i].position + phi *(pop[i].position - pop[k].position)# 计算新蜜蜂目标函数值
  46. newbee.cost = costfunc(newbee.position)# 通过比较目标函数值,更新第i个蜜蜂的位置if newbee.cost < pop[i].cost:
  47. pop[i]= newbee.deepcopy()else:
  48. count[i]+=1# 侦察蜂for i inrange(npop):if count[i]> limit:
  49. pop[i].position = np.random.uniform(varmin, varmax, nvar)
  50. pop[i].cost = costfunc(pop[i].position)
  51. count[i]=0# 更新全局最优解for i inrange(npop):if pop[i].cost < bestsol.cost:
  52. bestsol = pop[i].deepcopy()# 存储每一代全局最优解的目标函数值
  53. bestcost[it]= bestsol.cost
  54. # 展示迭代信息print("Iteration {}: Best Cost = {}".format(it, bestcost[it]))# 返回值
  55. out = structure()
  56. out.pop = pop
  57. out.bestsol = bestsol
  58. out.bestcost = bestcost
  59. return out
  60. defroulette_wheel_selection(p):
  61. c = np.cumsum(p)
  62. r =sum(p)* np.random.rand()
  63. ind = np.argwhere(r <= c)return ind[0][0]

app.py文件如下所示:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from ypstruct import structure
  4. import time
  5. import artificial_bee_colony
  6. start = time.time()#运行开始时刻# 测试函数defsphere(x):returnsum(x**2)# 问题定义
  7. problem = structure()
  8. problem.costfunc = sphere
  9. problem.nvar =10
  10. problem.varmin =-100* np.ones(10)
  11. problem.varmax =100* np.ones(10)# ABC参数
  12. params = structure()
  13. params.maxit =500
  14. params.npop =100
  15. params.nonlooker =100
  16. params.a =1# 运行ABC
  17. out = artificial_bee_colony.run(problem, params)# 运行结果
  18. plt.rcParams['font.sans-serif']=['KaiTi']#设置字体为楷体
  19. plt.plot(out.bestcost)print("最优解:{}".format(out.bestsol))
  20. end = time.time()# 运行结束时刻print('运行时间:{}s'.format(end-start))
  21. plt.xlim(0, params.maxit)
  22. plt.xlabel('迭代次数')
  23. plt.ylabel('全局最优目标函数值')
  24. plt.title('人工蜂群算法')
  25. plt.grid(True)
  26. plt.show()

3.ABC算法实例验证

测试函数如下:

  1. min
  2. f
  3. (
  4. x
  5. )
  6. =
  7. i
  8. =
  9. 1
  10. 10
  11. x
  12. i
  13. 2
  14. 100
  15. x
  16. i
  17. 100
  18. \min f\left ( x \right ) =\sum_{i=1}^{10} x_{i}^2 \quad -100\le x_{i} \le 100
  19. minf(x)=i=110xi2​−100xi​≤100

运行app.py文件,运行结果如下:

请添加图片描述
请添加图片描述

参考文献

[1] 秦全德, 程适, 李丽, 等. 人工蜂群算法研究综述[J]. 2014


OK,今天就到这里啦,各位可点击下方图片留言。
在这里插入图片描述

我们已经推出粉丝QQ交流群,各位小伙伴赶快加入吧!!!

在这里插入图片描述


咱们下期再见

近期你可能错过了的好文章

新书上架 | 《MATLAB智能优化算法:从写代码到算法思想》

优化算法 | 灰狼优化算法(文末有福利)

优化算法 | 鲸鱼优化算法

遗传算法(GA)求解带时间窗的车辆路径(VRPTW)问题MATLAB代码

粒子群优化算法(PSO)求解带时间窗的车辆路径问题(VRPTW)MATLAB代码

知乎 | bilibili | CSDN:随心390


本文转载自: https://blog.csdn.net/weixin_40730979/article/details/125820560
版权归原作者 随心390 所有, 如有侵权,请联系我们删除。

“优化算法 | 人工蜂群算法(附Python代码)”的评论:

还没有评论