1 importnumpy as np2 from BFOIndividual importBFOIndividual3 importrandom4 importcopy5 importmatplotlib.pyplot as plt6 importmath7

8

9 classBacterialForagingOptimization:10

11 ‘‘‘

12 The class for baterial foraging optimization algorithm13 ‘‘‘

14

15 def __init__(self, sizepop, vardim, bound, params):16 ‘‘‘

17 sizepop: population sizepop18 vardim: dimension of variables19 bound: boundaries of variables20 param: algorithm required parameters, it is a list which is consisting of [Ned, Nre, Nc, Ns, C, ped, d_attract, w_attract, d_repellant, w_repellant]21 ‘‘‘

22 self.sizepop =sizepop23 self.vardim =vardim24 self.bound =bound25 self.population =[]26 self.bestPopulation =[]27 self.accuFitness =np.zeros(self.sizepop)28 self.fitness =np.zeros(self.sizepop)29 self.params =params30 self.trace =np.zeros(31 (self.params[0] * self.params[1] * self.params[2], 2))32

33 definitialize(self):34 ‘‘‘

35 initialize the population36 ‘‘‘

37 for i inxrange(0, self.sizepop):38 ind =BFOIndividual(self.vardim, self.bound)39 ind.generate()40 self.population.append(ind)41

42 defevaluate(self):43 ‘‘‘

44 evaluation of the population fitnesses45 ‘‘‘

46 for i inxrange(0, self.sizepop):47 self.population[i].calculateFitness()48 self.fitness[i] =self.population[i].fitness49

50 defsortPopulation(self):51 ‘‘‘

52 sort population according descending order53 ‘‘‘

54 sortedIdx =np.argsort(self.accuFitness)55 newpop =[]56 newFitness =np.zeros(self.sizepop)57 for i inxrange(0, self.sizepop):58 ind =self.population[sortedIdx[i]]59 newpop.append(ind)60 self.fitness[i] =ind.fitness61 self.population =newpop62

63 defsolve(self):64 ‘‘‘

65 evolution process of baterial clony foraging algorithm66 ‘‘‘

67 self.t =068 self.initialize()69 self.evaluate()70 bestIndex =np.argmin(self.fitness)71 self.best =copy.deepcopy(self.population[bestIndex])72

73 for i inxrange(0, self.params[0]):74 for j in xrange(0, self.params[1]):75 for k in xrange(0, self.params[2]):76 self.t += 1

77 self.chemotaxls()78 self.evaluate()79 best =np.min(self.fitness)80 bestIndex =np.argmin(self.fitness)81 if best <82 self.best="copy.deepcopy(self.population[bestIndex])83" self.avefitness="np.mean(self.fitness)84" self.trace print optimal function value is: average is self.t self.reproduction self.eliminationanddispersal>

91 print("Optimal function value is: %f;" %

92 self.trace[self.t - 1, 0])93 print "Optimal solution is:"

94 printself.best.chrom95 self.printResult()96

97 defchemotaxls(self):98 ‘‘‘

99 chemotaxls behavior of baterials100 ‘‘‘

101 for i inxrange(0, self.sizepop):102 tmpInd =copy.deepcopy(self.population[i])103 self.fitness[i] +=self.communication(tmpInd)104 Jlast =self.fitness[i]105 rnd = np.random.uniform(low=-1, high=1.0, size=self.vardim)106 phi = rnd /np.linalg.norm(rnd)107 tmpInd.chrom += self.params[4] *phi108 for k inxrange(0, self.vardim):109 if tmpInd.chrom[k] self.bound[1, k]:112 tmpInd.chrom[k] = self.bound[1, k]113 tmpInd.calculateFitness()114 m =0115 while m < self.params[3]:116 if tmpInd.fitness <117 jlast="tmpInd.fitness118" self.population m>

120 tmpInd.fitness +=self.communication(tmpInd)121 tmpInd.chrom += self.params[4] *phi122 for k inxrange(0, self.vardim):123 if tmpInd.chrom[k] self.bound[1, k]:126 tmpInd.chrom[k] = self.bound[1, k]127 tmpInd.calculateFitness()128 m += 1

129 else:130 m = self.params[3]131 self.fitness[i] =Jlast132 self.accuFitness[i] +=Jlast133

134 defcommunication(self, ind):135 ‘‘‘

136 cell to cell communication137 ‘‘‘

138 Jcc = 0.0

139 term1 = 0.0

140 term2 = 0.0

141 for j inxrange(0, self.sizepop):142 term = 0.0

143 for k inxrange(0, self.vardim):144 term += (ind.chrom[k] -

145 self.population[j].chrom[k]) ** 2

146 term1 -= self.params[6] * np.exp(-1 * self.params[7] *term)147 term2 += self.params[8] * np.exp(-1 * self.params[9] *term)148 Jcc = term1 +term2149

150 returnJcc151

152 defreproduction(self):153 ‘‘‘

154 reproduction of baterials155 ‘‘‘

156 self.sortPopulation()157 newpop =[]158 for i in xrange(0, self.sizepop / 2):159 newpop.append(self.population[i])160 for i in xrange(self.sizepop / 2, self.sizepop):161 self.population[i] = newpop[i - self.sizepop / 2]162

163 defeliminationAndDispersal(self):164 ‘‘‘

165 elimination and dispersal of baterials166 ‘‘‘

167 for i inxrange(0, self.sizepop):168 rnd =random.random()169 if rnd < self.params[5]:170 self.population[i].generate()171

172 defprintResult(self):173 ‘‘‘

174 plot the result of the baterial clony foraging algorithm175 ‘‘‘

176 x =np.arange(0, self.t)177 y1 =self.trace[:, 0]178 y2 = self.trace[:, 1]179 plt.plot(x, y1, ‘r‘, label=‘optimal value‘)180 plt.plot(x, y2, ‘g‘, label=‘average value‘)181 plt.xlabel("Iteration")182 plt.ylabel("function value")183 plt.title(184 "Baterial clony foraging algorithm for function optimization")185 plt.legend()186 plt.show()

117>82>

Java实现细菌觅食算法_细菌觅食算法-python实现相关推荐

  1. 【Matlab】智能优化算法_蜻蜓优化算法DA

    [Matlab]智能优化算法_蜻蜓优化算法DA 1.背景介绍 2.灵感 3.公式推导 3.1 勘探和开发操作 4.算法流程图 5.文件结构 6.伪代码 7.详细代码及注释 7.1 DA.m 7.2 d ...

  2. 【Matlab】智能优化算法_蚁狮优化算法ALO

    [Matlab]智能优化算法_蚁狮优化算法ALO 1.背景介绍 2.基本思想 3.公式推导 3.1 ALO算法的运算符 3.2 蚂蚁的随机游动 3.3 困在蚂蚁坑里 3.4 修建陷阱 3.5 蚂蚁划向 ...

  3. 【Matlab】智能优化算法_灰狼优化算法GWO

    [Matlab]智能优化算法_灰狼优化算法GWO 1.背景介绍 2.基本思想 2.1 等级制度 2.2 狩猎方式 3.公式推导 3.1 社会等级制度 3.2 包围猎物 3.3 包围猎物 3.4 攻击猎 ...

  4. java 最少使用(lru)置换算法_缓存置换算法 - LRU算法

    LRU算法 1 原理 对于在内存中并且不被使用的数据块就是LRU,这类数据需要从内存中删除,以腾出空间来存储常用的数据. LRU算法(Least Recently Used,最近最少使用),是内存管理 ...

  5. java 投票算法_摩尔投票算法 - woshixin的个人空间 - OSCHINA - 中文开源技术交流社区...

    摩尔投票算法(Moore majority vote algorithm) 这个在wiki的介绍在https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_m ...

  6. java 排名算法_排行榜的算法

    好久不来博客园了,前几天更新个人状态时,也把"技术博客"四个字改成了"荒废已久的博客". 好久不总结自己的工作和学习了,怎么说也过不去,就来这写一篇浅显的文章, ...

  7. 机器学习 线性回归算法_探索机器学习算法简单线性回归

    机器学习 线性回归算法 As we dive into the world of Machine Learning and Data Science, one of the easiest and f ...

  8. 垃圾回收算法_垃圾回收算法有哪些

    垃圾检测通常通过建立一个根对象的集合以及建立一个从这些根对象开始能够触及的对象集合来实现.如果正在执行的程序可以访问到根对象和某个对象之间存在引用路径,这个对象就是可触及的.对于程序来说,根对象总是可 ...

  9. prim算法_数据结构与算法

    根据MOOC上课程总结,文章目录为: 一.引论 数据结构的基本概念 数据的逻辑结构和存储结构 算法及其时间复杂度 时间复杂度及应用 二.线性表 线性表的概念及顺序存储 单链表的概念及其基本操作 建立单 ...

  10. python实现洗牌算法_洗牌算法及 random 中 shuffle 方法和 sample 方法浅析

    对于算法书买了一本又一本却没一本读完超过 10%,Leetcode 刷题从来没坚持超过 3 天的我来说,算法能力真的是渣渣.但是,今天决定写一篇跟算法有关的文章.起因是读了吴师兄的文章 <扫雷与 ...

最新文章

  1. 十五、插入排序算法(两种方式)
  2. 【转】在生产环境中部署前端代码
  3. 2014\Province_C_C++_B\1 啤酒和饮料
  4. 第二阶段个人工作总结04
  5. mysql设计功能设置表_MySQL数据表的设计
  6. 边缘计算、雾计算、云计算区别几何?
  7. 【转载】正则表达式30分钟入门教程
  8. 设计模式—适配器模式(思维导图)
  9. JS疑难点和GC原理
  10. 微信客服系统开发SDK使用教程-给好友发消息任务
  11. EasiCSDeep:利用表面肌电信号识别颈椎病的深度学习模型
  12. 几款引擎比较 BigWorld Unreal CryEngine等
  13. linux用独显运行steam,修复在Linux系统上与Nvidia不兼容的Steam游戏
  14. photoshop基础操作集合01
  15. 计算机组成基础(2)-- 微体系结构层
  16. vue3中tsx的基本语法使用
  17. windows10系统如何安装日语输入法
  18. 怎么安装Nginx的监控模块
  19. 教你如何用cmd命令清除流氓软件
  20. SecureCRT自动保存日志设置

热门文章

  1. Python自动登录淘宝
  2. 计算机毕业设计asp.net党员信息管理系统(源码+系统+mysql数据库+Lw文档)
  3. Oracle DG主备切换VIP
  4. 在IDEA中查看依赖关系(*)
  5. VM虚拟机 系统出现鼠标定位不准确、双鼠标问题
  6. 无忧答案c语言程序设计,西工大16春《C语言程序设计》平时作业
  7. 谷歌查看html地址_独立站被谷歌收录的方法和技巧分享:三招让Google收录你的独立站...
  8. 昭通高考2021成绩查询,2021昭通中考成绩查询入口
  9. 股票交易费及利润计算器
  10. 使用htk搭建语音拨号系统