人工蜂群算法-python实现
ABSIndividual.py
- import numpy as np
- import ObjFunction
- class ABSIndividual:
- '''
- individual of artificial bee swarm algorithm
- '''
- def __init__(self, vardim, bound):
- '''
- vardim: dimension of variables
- bound: boundaries of variables
- '''
- self.vardim = vardim
- self.bound = bound
- self.fitness = 0.
- self.trials = 0
- def generate(self):
- '''
- generate a random chromsome for artificial bee swarm algorithm
- '''
- len = self.vardim
- rnd = np.random.random(size=len)
- self.chrom = np.zeros(len)
- for i in xrange(0, len):
- self.chrom[i] = self.bound[0, i] + \
- (self.bound[1, i] - self.bound[0, i]) * rnd[i]
- def calculateFitness(self):
- '''
- calculate the fitness of the chromsome
- '''
- self.fitness = ObjFunction.GrieFunc(
- self.vardim, self.chrom, self.bound)
ABS.py
- import numpy as np
- from ABSIndividual import ABSIndividual
- import random
- import copy
- import matplotlib.pyplot as plt
- class ArtificialBeeSwarm:
- '''
- the class for artificial bee swarm algorithm
- '''
- def __init__(self, sizepop, vardim, bound, MAXGEN, params):
- '''
- sizepop: population sizepop
- vardim: dimension of variables
- bound: boundaries of variables
- MAXGEN: termination condition
- params: algorithm required parameters, it is a list which is consisting of[trailLimit, C]
- '''
- self.sizepop = sizepop
- self.vardim = vardim
- self.bound = bound
- self.foodSource = self.sizepop / 2
- self.MAXGEN = MAXGEN
- self.params = params
- self.population = []
- self.fitness = np.zeros((self.sizepop, 1))
- self.trace = np.zeros((self.MAXGEN, 2))
- def initialize(self):
- '''
- initialize the population of abs
- '''
- for i in xrange(0, self.foodSource):
- ind = ABSIndividual(self.vardim, self.bound)
- ind.generate()
- self.population.append(ind)
- def evaluation(self):
- '''
- evaluation the fitness of the population
- '''
- for i in xrange(0, self.foodSource):
- self.population[i].calculateFitness()
- self.fitness[i] = self.population[i].fitness
- def employedBeePhase(self):
- '''
- employed bee phase
- '''
- for i in xrange(0, self.foodSource):
- k = np.random.random_integers(0, self.vardim - 1)
- j = np.random.random_integers(0, self.foodSource - 1)
- while j == i:
- j = np.random.random_integers(0, self.foodSource - 1)
- vi = copy.deepcopy(self.population[i])
- # vi.chrom = vi.chrom + np.random.uniform(-1, 1, self.vardim) * (
- # vi.chrom - self.population[j].chrom) + np.random.uniform(0.0, self.params[1], self.vardim) * (self.best.chrom - vi.chrom)
- # for k in xrange(0, self.vardim):
- # if vi.chrom[k] < self.bound[0, k]:
- # vi.chrom[k] = self.bound[0, k]
- # if vi.chrom[k] > self.bound[1, k]:
- # vi.chrom[k] = self.bound[1, k]
- vi.chrom[
- k] += np.random.uniform(low=-1, high=1.0, size=1) * (vi.chrom[k] - self.population[j].chrom[k])
- if vi.chrom[k] < self.bound[0, k]:
- vi.chrom[k] = self.bound[0, k]
- if vi.chrom[k] > self.bound[1, k]:
- vi.chrom[k] = self.bound[1, k]
- vi.calculateFitness()
- if vi.fitness > self.fitness[fi]:
- self.population[fi] = vi
- self.fitness[fi] = vi.fitness
- if vi.fitness > self.best.fitness:
- self.best = vi
- vi.calculateFitness()
- if vi.fitness > self.fitness[i]:
- self.population[i] = vi
- self.fitness[i] = vi.fitness
- if vi.fitness > self.best.fitness:
- self.best = vi
- else:
- self.population[i].trials += 1
- def onlookerBeePhase(self):
- '''
- onlooker bee phase
- '''
- accuFitness = np.zeros((self.foodSource, 1))
- maxFitness = np.max(self.fitness)
- for i in xrange(0, self.foodSource):
- accuFitness[i] = 0.9 * self.fitness[i] / maxFitness + 0.1
- for i in xrange(0, self.foodSource):
- for fi in xrange(0, self.foodSource):
- r = random.random()
- if r < accuFitness[i]:
- k = np.random.random_integers(0, self.vardim - 1)
- j = np.random.random_integers(0, self.foodSource - 1)
- while j == fi:
- j = np.random.random_integers(0, self.foodSource - 1)
- vi = copy.deepcopy(self.population[fi])
- # vi.chrom = vi.chrom + np.random.uniform(-1, 1, self.vardim) * (
- # vi.chrom - self.population[j].chrom) + np.random.uniform(0.0, self.params[1], self.vardim) * (self.best.chrom - vi.chrom)
- # for k in xrange(0, self.vardim):
- # if vi.chrom[k] < self.bound[0, k]:
- # vi.chrom[k] = self.bound[0, k]
- # if vi.chrom[k] > self.bound[1, k]:
- # vi.chrom[k] = self.bound[1, k]
- vi.chrom[
- k] += np.random.uniform(low=-1, high=1.0, size=1) * (vi.chrom[k] - self.population[j].chrom[k])
- if vi.chrom[k] < self.bound[0, k]:
- vi.chrom[k] = self.bound[0, k]
- if vi.chrom[k] > self.bound[1, k]:
- vi.chrom[k] = self.bound[1, k]
- vi.calculateFitness()
- if vi.fitness > self.fitness[fi]:
- self.population[fi] = vi
- self.fitness[fi] = vi.fitness
- if vi.fitness > self.best.fitness:
- self.best = vi
- else:
- self.population[fi].trials += 1
- break
- def scoutBeePhase(self):
- '''
- scout bee phase
- '''
- for i in xrange(0, self.foodSource):
- if self.population[i].trials > self.params[0]:
- self.population[i].generate()
- self.population[i].trials = 0
- self.population[i].calculateFitness()
- self.fitness[i] = self.population[i].fitness
- def solve(self):
- '''
- the evolution process of the abs algorithm
- '''
- self.t = 0
- self.initialize()
- self.evaluation()
- best = np.max(self.fitness)
- bestIndex = np.argmax(self.fitness)
- self.best = copy.deepcopy(self.population[bestIndex])
- self.avefitness = np.mean(self.fitness)
- self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness
- self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness
- print("Generation %d: optimal function value is: %f; average function value is %f" % (
- self.t, self.trace[self.t, 0], self.trace[self.t, 1]))
- while self.t < self.MAXGEN - 1:
- self.t += 1
- self.employedBeePhase()
- self.onlookerBeePhase()
- self.scoutBeePhase()
- best = np.max(self.fitness)
- bestIndex = np.argmax(self.fitness)
- if best > self.best.fitness:
- self.best = copy.deepcopy(self.population[bestIndex])
- self.avefitness = np.mean(self.fitness)
- self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness
- self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness
- print("Generation %d: optimal function value is: %f; average function value is %f" % (
- self.t, self.trace[self.t, 0], self.trace[self.t, 1]))
- print("Optimal function value is: %f; " % self.trace[self.t, 0])
- print "Optimal solution is:"
- print self.best.chrom
- self.printResult()
- def printResult(self):
- '''
- plot the result of abs algorithm
- '''
- x = np.arange(0, self.MAXGEN)
- y1 = self.trace[:, 0]
- y2 = self.trace[:, 1]
- plt.plot(x, y1, 'r', label='optimal value')
- plt.plot(x, y2, 'g', label='average value')
- plt.xlabel("Iteration")
- plt.ylabel("function value")
- plt.title("Artificial Bee Swarm algorithm for function optimization")
- plt.legend()
- plt.show()
运行程序:
- if __name__ == "__main__":
- bound = np.tile([[-600], [600]], 25)
- abs = ABS(60, 25, bound, 1000, [100, 0.5])
- abs.solve()
ObjFunction见简单遗传算法-python实现。
人工蜂群算法-python实现的更多相关文章
- 基于改进人工蜂群算法的K均值聚类算法(附MATLAB版源代码)
其实一直以来也没有准备在园子里发这样的文章,相对来说,算法改进放在园子里还是会稍稍显得格格不入.但是最近邮箱收到的几封邮件让我觉得有必要通过我的博客把过去做过的东西分享出去更给更多需要的人.从论文刊登 ...
- 人工鱼群算法-python实现
AFSIndividual.py import numpy as np import ObjFunction import copy class AFSIndividual: "" ...
- pageRank算法 python实现
一.什么是pagerank PageRank的Page可是认为是网页,表示网页排名,也可以认为是Larry Page(google 产品经理),因为他是这个算法的发明者之一,还是google CEO( ...
- 常见排序算法-Python实现
常见排序算法-Python实现 python 排序 算法 1.二分法 python 32行 right = length- : ] ): test_list = [,,,,,, ...
- kmp算法python实现
kmp算法python实现 kmp算法 kmp算法用于字符串的模式匹配,也就是找到模式字符串在目标字符串的第一次出现的位置比如abababc那么bab在其位置1处,bc在其位置5处我们首先想到的最简单 ...
- KMP算法-Python版
KMP算法-Python版 传统法: 从左到右一个个匹配,如果这个过程中有某个字符不匹配,就跳回去,将模式串向右移动一位.这有什么难的? 我们可以 ...
- 压缩感知重构算法之IRLS算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之OLS算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之CoSaMP算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
随机推荐
- flex4 s:Datagrid <s:typicalItem
<s:DataGird <s:typicalItem 这个标签相信大家很陌生吧, 我也是今天准备讲的时候才看到,估计是 flex4.5.1 新加东西,果然摸索 了下,这个标签作用也蛮好用的 ...
- SqlMapConfig.xml中的setting属性设置
<settings cacheModelsEnabled="true" lazyLoadingEnabled="false" en ...
- java 21 - 8 复制文本文件的5种方式
需求:复制文本文件 分析: 由于文本文件我们用记事本打开后可以读懂,所以使用字符流. 而字符流有5种复制的方式: 首先写main方法 public static void main(String[] ...
- java 16 -7 泛型方法和泛型接口(泛型类相似)
写一个ObjectTool类 泛型方法:把泛型定义在方法上 格式 public <泛型类型> 返回类型 方法名(泛型类型) 这样的好处是: 这个泛型方法可以接收任意类型的数据 public ...
- Centos中yum方式安装java
查看CentOS自带JDK是否已安装.◆输入:yum list installed |grep java. 若有自带安装的JDK,如何卸载CentOS系统自带Java环境?◆卸载JDK相关文件输入:y ...
- RDLC系列之二 子报表
本文实现简单的子报表 一.效果图
- 3110 PHP常见问题
1.中文显示 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&g ...
- android Camera 如何判断当前使用的摄像头是前置还是后置
现在 android 平台的智能手机一般都标配有两颗摄像头.在 Camera 中都存在摄像头切换的功能. 并且有一些功能前后置摄像头上会有所不同.譬如人脸检测,人脸识别,自动对焦,闪光灯等功能, 如果 ...
- C# 与 Unity 同名函数
1,Random,直接使用Random会报错,要么使用UnityEngine.Random,要么使用System.Random
- 基于.Net FrameWork的 RestFul Service
关于本文 这篇文章的目的就是向大家阐述如何在.net framework 4.0中创建RestFul Service并且使用它. 什么是web Services,什么是WCF 首先讲到的是web Se ...