Please enable JavaScript.
Coggle requires JavaScript to display documents.
mealpy.mealpy.swarm_based.woa 21102020 - Coggle Diagram
mealpy.mealpy.swarm_based.woa
21102020
!/usr/bin/env python
------------------------------------------------------------------------------------------------------%
Created by "Thieu Nguyen" at 10:06, 17/03/2020 %
%
Email:
nguyenthieu2102@gmail.com
%
Homepage:
https://www.researchgate.net/profile/Thieu_Nguyen6
%
Github:
https://github.com/thieu1995
%
-------------------------------------------------------------------------------------------------------%
from numpy.random import uniform, rand
from numpy import abs, exp, cos, pi
from mealpy.root import Root
class BaseWOA(Root):
"""
The original version of: Whale Optimization Algorithm (WOA)
In this algorithms: Prey means the best position
"""
def
init
(self, obj_func=None, lb=None, ub=None, problem_size=50, batch_size=10, verbose=True, epoch=700, pop_size=50):
Root.
init
(self, obj_func, lb, ub, problem_size, batch_size, verbose)
self.epoch = epoch
self.pop_size = pop_size
def train(self):
pop = [self.create
solution() for
in range(self.pop_size)]
Create the populations, Truoc tien la khoi tao quan the
g_best = self.get_global_best_solution(pop=pop, id_fit=self.ID_FIT, id_best=self.ID_MIN_PROB)
for epoch in range(self.epoch):
a = 2 - 2 * epoch / (self.epoch - 1) # linearly decreased from 2 to 0
for i in range(self.pop_size):
r = rand()
A = 2
a
r - a
C = 2 * r
l = uniform(-1, 1)
p = 0.5
b = 1
if uniform() < p:
if abs(A) < 1:
D = abs(C * g_best[self.ID_POS] - pop[i][self.ID_POS] )
Cau lenh nay co y nghia gi nhi?
pop[i][self.ID_POS] chua thong tin gi?
new_position = g_best[self.ID_POS] - A * D
else :
x_rand = pop[np.random.randint(self.pop_size)] # select random 1 position in pop
x_rand = self.create_solution()
D = abs(C * x_rand[self.ID_POS] - pop[i][self.ID_POS])
new_position = (x_rand[self.ID_POS] - A * D)
else:
D1 = abs(g_best[self.ID_POS] - pop[i][self.ID_POS])
new_position = D1
exp(b
l)
cos(2
pi * l) + g_best[self.ID_POS]
new_position = self.amend_position_faster(new_position)
fit = self.get_fitness_position(new_position)
pop[i] = [new_position, fit]
batch size idea
if i % self.batch_size:
g_best = self.update_global_best_solution(pop, self.ID_MIN_PROB, g_best)
self.loss_train.append(g_best[self.ID_FIT])
if self.verbose:
print("> Epoch: {}, Best fit: {}".format(epoch + 1, g_best[self.ID_FIT]))
self.solution = g_best
return g_best[self.ID_POS], g_best[self.ID_FIT],self.loss_train