-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAO.py
205 lines (179 loc) · 9.45 KB
/
AO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
# Created by "Thieu" at 15:53, 07/07/2021 ----------%
# Email: [email protected] %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
import numpy as np
from mealpy.optimizer import Optimizer
class OriginalAO(Optimizer):
"""
The original version of: Aquila Optimization (AO)
Links:
1. https://doi.org/10.1016/j.cie.2021.107250
Examples
~~~~~~~~
>>> import numpy as np
>>> from mealpy import FloatVar, AO
>>>
>>> def objective_function(solution):
>>> return np.sum(solution**2)
>>>
>>> problem_dict = {
>>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"),
>>> "obj_func": objective_function,
>>> "minmax": "min",
>>> }
>>>
>>> model = AO.OriginalAO(epoch=1000, pop_size=50)
>>> g_best = model.solve(problem_dict)
>>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}")
>>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
References
~~~~~~~~~~
[1] Abualigah, L., Yousri, D., Abd Elaziz, M., Ewees, A.A., Al-Qaness, M.A. and Gandomi, A.H., 2021.
Aquila optimizer: a novel meta-heuristic optimization algorithm. Computers & Industrial Engineering, 157, p.107250.
"""
def __init__(self, epoch=10000, pop_size=100, **kwargs):
"""
Args:
epoch (int): maximum number of iterations, default = 10000
pop_size (int): number of population size, default = 100
"""
super().__init__(**kwargs)
self.epoch = self.validator.check_int("epoch", epoch, [1, 100000])
self.pop_size = self.validator.check_int("pop_size", pop_size, [5, 10000])
self.set_parameters(["epoch", "pop_size"])
self.sort_flag = False
def evolve(self, epoch):
"""
The main operations (equations) of algorithm. Inherit from Optimizer class
Args:
epoch (int): The current iteration
"""
alpha = delta = 0.1
g1 = 2 * self.generator.random() - 1 # Eq. 16
g2 = 2 * (1 - epoch / self.epoch) # Eq. 17
dim_list = np.array(list(range(1, self.problem.n_dims + 1)))
miu = 0.00565
r0 = 10
r = r0 + miu * dim_list
w = 0.005
phi0 = 3 * np.pi / 2
phi = -w * dim_list + phi0
x = r * np.sin(phi) # Eq.(9)
y = r * np.cos(phi) # Eq.(10)
QF = epoch ** ((2 * self.generator.random() - 1) / (1 - self.epoch) ** 2) # Eq.(15) Quality function
pop_new = []
for idx in range(0, self.pop_size):
x_mean = np.mean(np.array([agent.target.fitness for agent in self.pop]), axis=0)
levy_step = self.get_levy_flight_step(beta=1.5, multiplier=1.0, case=-1)
if epoch <= (2 / 3) * self.epoch: # Eq. 3, 4
if self.generator.random() < 0.5:
pos_new = self.g_best.solution * (1 - epoch / self.epoch) + self.generator.random() * (x_mean - self.g_best.solution)
else:
idx = self.generator.choice(list(set(range(0, self.pop_size)) - {idx}))
pos_new = self.g_best.solution * levy_step + self.pop[idx].solution + self.generator.random() * (y - x) # Eq. 5
else:
if self.generator.random() < 0.5:
pos_new = alpha * (self.g_best.solution - x_mean) - self.generator.random() * (self.generator.random() * (self.problem.ub - self.problem.lb) + self.problem.lb) * delta # Eq. 13
else:
pos_new = QF * self.g_best.solution - (g2 * self.pop[idx].solution * self.generator.random()) - g2 * levy_step + self.generator.random() * g1 # Eq. 14
pos_new = self.correct_solution(pos_new)
agent = self.generate_empty_agent(pos_new)
pop_new.append(agent)
if self.mode not in self.AVAILABLE_MODES:
agent.target = self.get_target(pos_new)
self.pop[idx] = self.get_better_agent(agent, self.pop[idx], self.problem.minmax)
if self.mode in self.AVAILABLE_MODES:
pop_new = self.update_target_for_population(pop_new)
self.pop = self.greedy_selection_population(self.pop, pop_new, self.problem.minmax)
class AdaptiveAO(Optimizer): # for Mealpy version 3.0.1
"""
The Adaptive Aquila Optimizer (AAO)
Links:
1. https://doi.org/10.1016/j.rineng.2024.103261
Examples
~~~~~~~~
>>> import numpy as np
>>> from mealpy import FloatVar, AO
>>>
>>> def objective_function(solution):
>>> return np.sum(solution**2)
>>>
>>> problem_dict = {
>>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"),
>>> "obj_func": objective_function,
>>> "minmax": "min",
>>> }
>>>
>>> model = AO.AdaptiveAO(epoch=1000, pop_size=50)
>>> g_best = model.solve(problem_dict)
>>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}")
>>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
References
~~~~~~~~~~
[1] Al-Selwi, S. M., Hassan, M. F., Abdulkadir, S. J., & et al. (2024).
Smart Grid Stability Prediction Using Adaptive Aquila Optimizer and Ensemble Stacked BiLSTM.
Results in Engineering, 103261.
doi: https://doi.org/10.1016/j.rineng.2024.103261.
"""
def __init__(self, epoch=10000, pop_size=100, sharpness = 10.0, sigmoid_midpoint=0.5, **kwargs):
"""
Args:
epoch (int): maximum number of iterations, default = 10000
pop_size (int): number of population size, default = 100
sharpness (float): is a positive variable that controls the sharpness of the transition between exploration and exploitation, default is 10.0, Valid range: [0.1, 10000.0].
sigmoid_midpoint (float): a variable that controls the midpoint of the sigmoid function as it determines when the transition should be applied, default is 0.5, Valid range: [0.0, 1.0].
"""
super().__init__(**kwargs)
self.epoch = self.validator.check_int("epoch", epoch, [1, 100000])
self.pop_size = self.validator.check_int("pop_size", pop_size, [5, 10000])
self.sharpness = self.validator.check_float("sharpness", sharpness, [0.1, 10000.0])
self.sigmoid_midpoint = self.validator.check_float("sigmoid_midpoint", sigmoid_midpoint, [0.0, 1.0])
self.set_parameters(["epoch", "pop_size","sharpness","sigmoid_midpoint"])
self.sort_flag = False
def evolve(self, epoch):
"""
The main operations (equations) of algorithm. Inherit from Optimizer class
Args:
epoch (int): The current iteration
"""
alpha = delta = 0.1
g1 = 2 * self.generator.random() - 1 # Eq. 16
g2 = 2 * (1 - epoch / self.epoch) # Eq. 17
dim_list = np.array(list(range(1, self.problem.n_dims + 1)))
miu = 0.00565
r0 = 10
r = r0 + miu * dim_list
w = 0.005
phi0 = 3 * np.pi / 2
phi = -w * dim_list + phi0
x = r * np.sin(phi) # Eq.(9)
y = r * np.cos(phi) # Eq.(10)
QF = epoch ** ((2 * self.generator.random() - 1) / (1 - self.epoch) ** 2) # Eq.(15) Quality function
pop_new = []
for idx in range(0, self.pop_size):
x_mean = np.mean(np.array([agent.target.fitness for agent in self.pop]), axis=0)
levy_step = self.get_levy_flight_step(beta=1.5, multiplier=1.0, case=-1)
# Dynamically balance the exploration and exploitation phases
sigmoid_factor = 1 / (1 + np.exp (-self.sharpness * (epoch/self.epoch -self.sigmoid_midpoint)))
if np.random.rand() <= (1 - sigmoid_factor):
if self.generator.random() < 0.5:
pos_new = self.g_best.solution * (1 - epoch / self.epoch) + self.generator.random() * (x_mean - self.g_best.solution) # Eq. (3) and Eq. (4)
else:
idx = self.generator.choice(list(set(range(0, self.pop_size)) - {idx}))
pos_new = self.g_best.solution * levy_step + self.pop[idx].solution + self.generator.random() * (y - x) # Eq. 5
else:
if self.generator.random() < 0.5:
pos_new = alpha * (self.g_best.solution - x_mean) - self.generator.random() * (self.generator.random() * (self.problem.ub - self.problem.lb) + self.problem.lb) * delta # Eq. 13
else:
pos_new = QF * self.g_best.solution - (g2 * self.pop[idx].solution * self.generator.random()) - g2 * levy_step + self.generator.random() * g1 # Eq. 14
pos_new = self.correct_solution(pos_new)
agent = self.generate_empty_agent(pos_new)
pop_new.append(agent)
if self.mode not in self.AVAILABLE_MODES:
agent.target = self.get_target(pos_new)
self.pop[idx] = self.get_better_agent(agent, self.pop[idx], self.problem.minmax)
if self.mode in self.AVAILABLE_MODES:
pop_new = self.update_target_for_population(pop_new)
self.pop = self.greedy_selection_population(self.pop, pop_new, self.problem.minmax)