1+ """Walrus Optimization Algorithm.
2+ """
3+
4+ import copy
5+ from typing import Any , Dict , Optional
6+
7+ import numpy as np
8+
9+ import opytimizer .math .random as r
10+ import opytimizer .utils .logging as l
11+ from opytimizer .core .optimizer import Optimizer
12+ from opytimizer .core .function import Function
13+ from opytimizer .core .space import Space
14+
15+ logger = l .get_logger (__name__ )
16+
17+ class WAOA (Optimizer ):
18+ """A WAOA class, inherited from Optimizer.
19+
20+ This is the designed class to dife WAOA-related
21+ variables and methods.
22+
23+ References:
24+ P. Trojovský and M. Dehghani. A new bio-inspired metaheuristic algorithm for
25+ solving optimization problems based on walruses behavior. Scientific Reports (2023).
26+
27+ """
28+
29+ def __init__ (self , params : Optional [Dict [str , Any ]] = None ) -> None :
30+ """Initialization method.
31+
32+ Args:
33+ params (str): Contains key-value parameters to the meta-heuristics.
34+ """
35+
36+ logger .info ('Overriding class: Optimizer -> SSA' )
37+
38+ super (WAOA , self ).__init__ ()
39+
40+ self .build (params )
41+
42+ logger .info ('Class overrided.' )
43+
44+ def update (self , space : Space , function : Function , iteration : int ) -> None :
45+ """Wraps Walrus Optimization Algorithm over all agents and variables.
46+
47+ Args:
48+ space: Space containing agents and update-related information.
49+ function: A Function object that will be used as the objective function.
50+ iteration: Current iteration.
51+
52+ """
53+
54+ for i , agent in enumerate (space .agents ):
55+ a = copy .deepcopy (agent )
56+
57+ for j in range (space .n_variables ):
58+
59+ r1 = r .generate_integer_random_number (1 , 2 )
60+ r2 = r .generate_uniform_random_number ()
61+
62+ a .position [j ] = agent .position [j ] + r2 * (space .best_agent .position [j ] - r1 * agent .position [j ])
63+
64+ a .clip_by_bound ()
65+
66+ a .fit = function (a .position )
67+ if a .fit < agent .fit :
68+ agent .position = copy .deepcopy (a .position )
69+ agent .fit = copy .deepcopy (a .fit )
70+
71+ k = r .generate_integer_random_number (0 , space .n_agents , i )
72+
73+ if (space .agents [k ].fit < agent .fit ):
74+
75+ for j in range (space .n_variables ):
76+
77+ r1 = r .generate_integer_random_number (1 , 2 )
78+ r2 = r .generate_uniform_random_number ()
79+
80+ a .position [j ] = agent .position [j ] + r2 * (space .agents [k ].position [j ] - r1 * agent .position [j ])
81+
82+ else :
83+
84+ for j in range (space .n_variables ):
85+
86+ r2 = r .generate_uniform_random_number ()
87+
88+ a .position [j ] = agent .position [j ] + r2 * (agent .position [j ] - space .agents [k ].position [j ])
89+
90+ a .clip_by_bound ()
91+
92+ a .fit = function (a .position )
93+ if a .fit < agent .fit :
94+ agent .position = copy .deepcopy (a .position )
95+ agent .fit = copy .deepcopy (a .fit )
96+
97+ for j in range (space .n_variables ):
98+
99+ r2 = r .generate_uniform_random_number ()
100+
101+ a .position [j ] = agent .position [j ] + ((agent .ub [j ]/ (iteration + 1 )) - r2 * (agent .lb [j ]/ (iteration + 1 )))
102+
103+ a .clip_by_bound ()
104+
105+ a .fit = function (a .position )
106+ if a .fit < agent .fit :
107+ agent .position = copy .deepcopy (a .position )
108+ agent .fit = copy .deepcopy (a .fit )
0 commit comments