鯨魚最佳化演算法的實現
介紹
鯨魚最佳化演算法是一種用於解決數學和機器學習中最佳化問題的技術。它基於座頭鯨的行為,利用座頭鯨在海洋中搜尋獵物、包圍獵物和形成氣泡網捕獵等行為運算元。該演算法由Mirjalili和Lewis於2016年提出。
在這篇文章中,我們將深入探討WOA演算法的不同階段。
座頭鯨的歷史
座頭鯨是地球上最大的哺乳動物之一。它們擁有特殊的捕獵機制,稱為氣泡網捕獵機制。它們非常聰明,因為它們的大腦含有紡錘細胞和纖維。它們的捕獵分三個步驟:
作為領導者的鯨魚透過下潛12米並找到獵物,在其周圍建立一個螺旋形的氣泡。
一位經驗豐富的輔助鯨魚使另一隻鯨魚同步。
所有其他鯨魚形成隊形並試圖攻擊獵物。
WAO演算法
WAO演算法的靈感來源於座頭鯨的捕獵行為,其階段如下所示。
1. 探索階段:搜尋模型
在這個階段,代理(座頭鯨)首先根據每個代理的位置隨機搜尋最佳解決方案。搜尋代理的位置使用隨機選擇的搜尋代理進行更新。其數學方程可以表示為:
$$\mathrm{𝑊\: =\: |\: m \: *\:Y_{rand}\:-\:Y\: |}$$
$$\mathrm{ Y\:(t+1) \: =\:Y_{rand}\:-\:a\:*\:W |}$$
其中Yrand是當前種群中隨機選擇的向量。
[a,m]是係數。如果r是[0,1]範圍內的一個隨機向量,b則線性地從2維減小到0維,如下所示:
$$\mathrm{ a \:= \:2 * b * r – b}$$
$$\mathrm{ m \:= \:2 * r}$$
2. 模型包圍
鯨魚在捕獵時會包圍獵物。當前最佳代理被視為最佳解決方案,接近最優解。透過這種包圍行為,其他代理的位置得到更新。
$$\mathrm{𝑊\: =\: |\: m \: *\:Y'_{(t)}\:-\:Y(t)\: |}$$
$$\mathrm{ Y\:(t+1) \: =\:Y'_{(t)}\:-\:a\:*\:W }$$
3. 氣泡網和開發
此階段有兩種方法。
收縮包圍 - a的值是在[-n,n]之間的隨機值,並且a的值在迭代過程中從2減小到0。對於任何一對a,例如[-2,2],搜尋代理的新位置定義在當前最佳位置和原始位置之間。
螺旋更新 - 在這種機制中,我們計算鯨魚和獵物之間的距離。鯨魚的運動用螺旋方程表示。
$$\mathrm{ X\:(t+1) \: = 𝑊′′*\:e^{pq}\:*\:2\pi\:r\:+\:X'}$$
其中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
結論
鯨魚最佳化演算法是一種新穎的解決機器學習或數學和一般科學中最佳化問題的方法。這種最佳化技術受到座頭鯨及其捕獵習慣的啟發,在解決現代問題方面非常有用。