飞蛾扑火优化(Moth-flame optimization,MFO),由Seyedali Mirjalili在2015年提出,为优化领域提供了一种新的启发式搜索范式:螺旋搜索。
飞蛾在夜间有一种特殊的导航方式:横向定向。即它会与月亮(光源)保持一定的角度飞行,从而能够保持直线的飞行路径,但是,这种方式只在光源离飞蛾较远的情况下才有效。当有人造光源存在时,飞蛾会被人工灯光所欺骗,一直保持与人造灯光相同的角度飞行,由于它与光源的距离过近,它飞行的路径已经不是直线,而是一种螺旋的路径。
受这种自然现象的启发,Seyedali Mirjalili将飞蛾绕着光源螺旋飞行的过程抽象成为一个寻优的过程,飞蛾飞行的整个空间即是问题的解空间,一只飞蛾即是问题的一个解,而火焰(光源)即是问题的一个较优解,每一只飞蛾对应一个光源,避免了算法陷入局部最优;当飞蛾与火焰足够多的时候,飞蛾的飞行能够搜索解空间的绝大部分区域,从而保证了算法的exploration能力;而在寻优的过程中,火焰数随着迭代次数的增加而减少,使飞蛾能够充分搜索更优解的邻域空间,保证了算法的exploitation能力。
正是基于以上特点,MFO在exploration与exploitation之间找到了平衡,从而使算法在优化问题中有一个较好的效果。
总的来说MFO也是一种基于种群的随机启发式搜索算法,它与PSO、GSA等算法最大的区别就在于其粒子搜索路径是螺旋形的,粒子围绕着更优解以一种螺旋的方式移动,而不是直线移动。
MFO的过程如下:
1.初始化飞蛾种群
2.对飞蛾种群进行适应度评价
3.重复如下过程直到达到停止标准:
3.1自适应更新火焰个数n,当迭代次数为1时,飞蛾个数即为火焰个数
3.2对飞蛾种群适应度进行排序,取出适应度较好的n个飞蛾作为火焰
3.3更新飞蛾的搜索参数。
3.4根据每只飞蛾对应的火焰与飞行参数更新飞蛾的位置
4.输出所得最优解(火焰)
具体的飞蛾位置更新公式见论文:Moth-flame optimization algorithm: A novel nature-inspired heuristic paradigm
# Moth-flame optimization algorithm
import random as rd
from math import exp, cos, pi
from copy import deepcopy
def ini(n, d):
population, fitness = [], []
for i in range(n):
moth = []
for j in range(d):
moth.append(rd.uniform(-10, 10))
population.append(moth)
return population
def getFitness(moths):
fitness = []
for i in range(len(moths)):
fitness.append(objFunction(moths[i]))
return fitness
def objFunction(moth):
objFunctionValue = 0
for i in range(len(moth)):
objFunctionValue += moth[i] ** 2
return objFunctionValue
def run():
number, dimension = 10, 10
b = 1
mothPopulation = ini(number, dimension)
iterx, maxIterx = 0, 100
while iterx < maxIterx:
mothFitness = getFitness(mothPopulation)
if iterx > 90:
flameNumber = 1
elif iterx == 0:
flameNumber = 10
else:
flameNumber = int((maxIterx - iterx) / 10) + 1
flamePopulation, flameFitness = getFlame(mothPopulation, mothFitness, flameNumber)
for i in range(number):
for j in range(dimension):
r = -1 - 0.01 * iterx
t = rd.uniform(r, 1)
if i < len(flamePopulation):
distance = abs(flamePopulation[i][j] - mothPopulation[i][j])
mothPopulation[i][j] = distance * exp(b * t) * cos(2 * pi * t) + flamePopulation[i][j]
mothPopulation[i][j] = check(mothPopulation[i][j])
else:
distance = abs(flamePopulation[0][j] - mothPopulation[i][j])
mothPopulation[i][j] = distance * exp(b * t) * cos(2 * pi * t) + flamePopulation[0][j]
mothPopulation[i][j] = check(mothPopulation[i][j])
iterx += 1
print(flamePopulation, flameFitness)
def getFlame(mothPopulation, mothFitness, flameNumber):
flamePopulation, flameFitness = [], []
fitness = deepcopy(mothFitness)
fitness.sort()
for i in range(flameNumber):
flameFitness.append(fitness[i])
flamePopulation.append(mothPopulation[mothFitness.index(fitness[i])])
return flamePopulation, flameFitness
def check(x):
if x < -10:
return -10
elif x > 10:
return 10
else:
return x
if __name__ == '__main__':
run()
参考文献:
Mirjalili S. Moth-flame optimization algorithm: A novel nature-inspired heuristic paradigm[J]. Knowledge-based systems, 2015, 89: 228-249.