萤火虫算法-python实现
2024-08-25 16:31:37
FAIndividual.py
import numpy as np
import ObjFunction class FAIndividual: '''
individual of firefly 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 firefly 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)
FA.py
import numpy as np
from FAIndividual import FAIndividual
import random
import copy
import matplotlib.pyplot as plt class FireflyAlgorithm: '''
The class for firefly algorithm
''' def __init__(self, sizepop, vardim, bound, MAXGEN, params):
'''
sizepop: population sizepop
vardim: dimension of variables
bound: boundaries of variables
MAXGEN: termination condition
param: algorithm required parameters, it is a list which is consisting of [beta0, gamma, alpha]
'''
self.sizepop = sizepop
self.MAXGEN = MAXGEN
self.vardim = vardim
self.bound = bound
self.population = []
self.fitness = np.zeros((self.sizepop, 1))
self.trace = np.zeros((self.MAXGEN, 2))
self.params = params def initialize(self):
'''
initialize the population
'''
for i in xrange(0, self.sizepop):
ind = FAIndividual(self.vardim, self.bound)
ind.generate()
self.population.append(ind) def evaluate(self):
'''
evaluation of the population fitnesses
'''
for i in xrange(0, self.sizepop):
self.population[i].calculateFitness()
self.fitness[i] = self.population[i].fitness def solve(self):
'''
evolution process of firefly algorithm
'''
self.t = 0
self.initialize()
self.evaluate()
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.move()
self.evaluate()
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 move(self):
'''
move the a firefly to another brighter firefly
'''
for i in xrange(0, self.sizepop):
for j in xrange(0, self.sizepop):
if self.fitness[j] > self.fitness[i]:
r = np.linalg.norm(
self.population[i].chrom - self.population[j].chrom)
beta = self.params[0] * \
np.exp(-1 * self.params[1] * (r ** 2))
# beta = 1 / (1 + self.params[1] * r)
# print beta
self.population[i].chrom += beta * (self.population[j].chrom - self.population[
i].chrom) + self.params[2] * np.random.uniform(low=-1, high=1, size=self.vardim)
for k in xrange(0, self.vardim):
if self.population[i].chrom[k] < self.bound[0, k]:
self.population[i].chrom[k] = self.bound[0, k]
if self.population[i].chrom[k] > self.bound[1, k]:
self.population[i].chrom[k] = self.bound[1, k]
self.population[i].calculateFitness()
self.fitness[i] = self.population[i].fitness def printResult(self):
'''
plot the result of the firefly 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("Firefly Algorithm for function optimization")
plt.legend()
plt.show()
运行程序:
if __name__ == "__main__": bound = np.tile([[-600], [600]], 25)
fa = FA(60, 25, bound, 200, [1.0, 0.000001, 0.6])
fa.solve()
ObjFunction见简单遗传算法-python实现。
最新文章
- iOS开发之自定义表情键盘(组件封装与自动布局)
- Microsoft Visual C++ Compiler for Python 2.7
- 烂泥:通过binlog恢复mysql数据库
- 手动编译并运行Java项目的过程
- ecshop教程:重置后台密码MD5+salt
- java coder的水平
- POJ 3903:Stock Exchange(裸LIS + 二分优化)
- 【转】Tomcat中部署java web应用程序
- [SQL]SQL删除数据的各种方式总结
- ASP.NET MVC - 发布网站
- <;转>;SpringMVC与Struts2 比较总结
- S3C2410 实验三——跑马灯实验
- JavaScript实现多栏目切换效果
- webuploader问题
- SQLServer之修改标量值函数
- Dynamic CRM 2015学习笔记(2)更改系统显示语言
- 版本控制:tortoise svn的 revert to this revision和 revert changes from this revision有什么区别?
- little kernel 小结
- exit(0)与exit(1)、return的区别
- python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制
热门文章
- Unity 协程与线程
- Linux搭建python环境
- oracle转Mysql中,varchar2(10)和number应该转换为什么类型?
- 啊D工具语句 适合Access和Mssql注入
- iOS单例模式(Singleton)写法简析
- OXM
- 007医疗项目-模块一:用户的查找:3.用户表查询的Action和Service
- 元祖签约K2 BPM,引领绿色健康食品!
- Verilog学习笔记基本语法篇(九)&#183;&#183;&#183;&#183;&#183;&#183;&#183;&#183; 任务和函数
- Gradle tip #1: tasks