我鲸鱼优化算法的实现

2025-06-04 19:19:27

鲸鱼优化算法的实现

简介

鲸鱼优化算法是一种解决数学和机器学习中优化问题的技术。它是基于座头鲸的行为,它使用了座头鲸在海洋中搜索猎物、包围猎物和锻造气泡网行为等运算符。它是由Mirjalili和Lewis在2016年提出的。

在这篇文章中,我们将研究WOA算法的不同阶段

座头鲸的历史

座头鲸是地球上最大的哺乳动物之一。它们有一种特殊的捕猎机制,称为气泡网捕猎机制。它们非常聪明,因为它们的大脑含有纺锤形细胞和纤维。它们的捕猎分为三个步骤—

作为首领的鲸鱼通过下潜12米在猎物周围形成一个螺旋形的气泡,并找到猎物。

一个相当有经验的辅助鲸鱼使其他鲸鱼同步。

所有其他的鲸鱼组成一个队形,试图攻击猎物。

WAO算法

受座头鲸捕猎行为的启发,WAO算法有以下几个阶段。

1.探索的阶段,搜索模型

在这个阶段,代理(座头鲸)首先根据每个代理的位置随机搜索出最佳解决方案。使用随机选择的搜索代理的位置来更新搜索代理的位置。这方面的数学公式可以给定为

其中Y rand是当前种群的随机位置向量。

[a,m]是系数。如果r是范围[0,1]之间的一个随机向量,b从2维线性下降到0维,如下所述

2.模型包围

鲸鱼在捕食时环绕着它们。当前最好的代理被认为是最佳解决方案,接近最佳解决方案。通过这种包围行为,其他代理的位置被更新。

3.泡沫网和剥削

在这个阶段有两种方法。

缩小包围圈 – a的值是[-n,n]之间的一个随机值,其值在迭代中从2减少到0。对于任何一对a,如[-2,2],搜索代理的新位置被定义在当前最佳位置和原始位置之间。

螺旋式更新 – 在这个机制中,我们计算鲸鱼和猎物之间的距离。鲸鱼的运动是以螺旋线或螺旋方程的形式给出的。

其中p是[-1,2]之间的一个随机数,p是螺旋的半径。

用Python实现

示例

!pip install pyMetaheuristic

from pyMetaheuristic.algorithm import whale_optimization_algorithm as woa

from pyMetaheuristic.utils import graphs

import numpy as np

# Easom Function - target func

def easy_om(var_values = [0, 0]):

x_1, x_2 = var_values

function_value = -np.cos(x_1) * np.cos(x_2) * np.exp(-(x_1 - np.pi) ** 2 - (x_2 - np.pi) ** 2)

return function_value

plot_parameters = {

'min_values': (-6, -6),

'max_values': (6, 6),

'step': (0.1, 0.1),

'solution': [],

'proj_view': '3D',

'view': 'notebook'

}

graphs.plot_single_function(target_function = easy_om, **plot_parameters)

# Parameter of woa algorithm

parameters = {

'hunting_party': 100,

'min_values': (-6, -6),

'max_values': (6, 6),

'iterations': 20,

'spiral_param': 0.4,

'verbose': True

}

woa_value = woa(target_function = easy_om, **parameters)

variab = woa_value[0][:-1]

min = woa_value[0][ -1]

print('Variables in the woa: ', np.around(variab, 4) , ' Minimum Value Found: ', round(min, 4) )

# Solution plotting woa

plot_parameters = {

'min_values': (-6, -6),

'max_values': (6, 6),

'step': (0.1, 0.1),

'solution': [variab],

'proj_view': '3D',

'view': 'notebook'

}

graphs.plot_single_function(target_function = easy_om, **plot_parameters)

输出

Iteration = 0 f(x) = -2.675287991074243e-09

Iteration = 1 f(x) = -0.5463250054450847

Iteration = 2 f(x) = -0.9616666553027987

Iteration = 3 f(x) = -0.9997741596613828

Iteration = 4 f(x) = -0.9997741596613828

Iteration = 5 f(x) = -0.9997741596613828

Iteration = 6 f(x) = -0.9997741596613828

Iteration = 7 f(x) = -0.9997741596613828

Iteration = 8 f(x) = -0.9997741596613828

Iteration = 9 f(x) = -0.9997741596613828

Iteration = 10 f(x) = -0.9997741596613828

Iteration = 11 f(x) = -0.9997741596613828

Iteration = 12 f(x) = -0.9998973527853484

Iteration = 13 f(x) = -0.9998973527853484

Iteration = 14 f(x) = -0.9999426874370445

Iteration = 15 f(x) = -0.9999426874370445

Iteration = 16 f(x) = -0.9999820386300734

Iteration = 17 f(x) = -0.9999860799836825

Iteration = 18 f(x) = -0.9999903470458049

Iteration = 19 f(x) = -0.9999966229369239

Iteration = 20 f(x) = -0.9999984095434976

Variables in the woa: [3.1414 3.142 ] Minimum Value Found: -1.0

总结

鲸鱼优化算法是解决机器学习或一般数学和科学中的优化问题的一种新方法。灵感来自于座头鲸和它们的狩猎习惯,这种优化技术对解决现代问题非常有用。