Implementation of Whale Optimization Algorithm


Introduction

Whale Optimization Algorithm is a technique for solving optimization problems in Mathematics and Machine Learning. It is based on the behavior of humpback whales which uses operators like prey searching, encircling the prey, and forging bubble net behavior of humpback whales in the ocean. It was given by Mirjalili and Lewis in 2016.

In this article, we are going to look into the different phases of the WOA algorithm

A History of Humpback Whales

Humpback whales are one of the largest mammals on Earth. They have a special type of hunting mechanism known as the bubble−net hunting mechanism. They are very intelligent as they have brains containing spindle cells and fibers. They hunt in three steps −

  • The whale which is the leader creates a spiral−shaped bubble around the prey by diving down 12 meters and finding the prey.

  • A supporting whale who is quite experienced makes the other whale synchronize.

  • All the other whales make a formation and try to attack the prey.

WAO Algorithm

WAO algorithm inspired by the hunting behavior of humpback whales has below phases.

1. The phase of Exploration: Searching the Model

In this phase, the agent ( humpback whale) first randomly searches for the best solution based on each agent's position. The search agent's position is updated using a randomly selected search agent. The mathematic equation of this can be given as

$$\mathrm{𝑊\: =\: |\: m \: *\:Y_{rand}\:-\:Y\: |}$$

$$\mathrm{ Y\:(t+1) \: =\:Y_{rand}\:-\:a\:*\:W |}$$

where Yrand is the random position vector from the current population.

[a,m] are the coefficients. if r is a random vector between ranges [0,1] b decreases linearly from 2 to 0 dimensions as given below

$$\mathrm{ a \:= \:2 * b * r – b}$$

$$\mathrm{ m \:= \:2 * r}$$

2. Model encircle

Whales encircle their while hunting. The current best agent is taken as the best solution, close to the optimal solution. Through this encircling behavior, the position of other agents are updated.

$$\mathrm{𝑊\: =\: |\: m \: *\:Y'_{(t)}\:-\:Y(t)\: |}$$

$$\mathrm{ Y\:(t+1) \: =\:Y'_{(t)}\:-\:a\:*\:W }$$

3. Bubble Net and Exploitation

There are two approaches in this phase.

  • Shrink Encircle − The value of a is a random value between [-n,n], and the value of decreases from 2 to 0 over iterations. For any pair of a such as [-2,2] a new position of the search agent is defined between the current best position and the original position.

  • Spiral Update − In this mechanism, we calculate the distance between the whale and prey. The movement of the whale is given as a helix or spiral equation.

$$\mathrm{ X\:(t+1) \: = 𝑊′′*\:e^{pq}\:*\:2\pi\:r\:+\:X'}$$

where p is a random number between [−1,2] and p is the radius of the spiral.

Implementation in Python

Example

!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)

Output

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

Conclusion

Whale Optimization Algorithm is a novel approach to solving optimization problems in Machine Learning or Mathematics and Science in General. Inspired by humpback whales and their hunting habits this optimization technique is very useful in solving modern problems.

Updated on: 30-Dec-2022

923 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements