# !/usr/bin/env python
# Created by "Thieu" at 14:01, 16/11/2020 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
import numpy as np
from mealpy.optimizer import Optimizer
[docs]class OriginalFOA(Optimizer):
"""
The original version of: Fruit-fly Optimization Algorithm (FOA)
Links:
1. https://doi.org/10.1016/j.knosys.2011.07.001
Notes
~~~~~
+ This optimization can't apply to complicated objective function in this library.
+ So I changed the implementation Original FOA in BaseFOA version
+ This algorithm is the weakest algorithm in MHAs, that's why so many researchers produce papers based on this algorithm (Easy to improve, and easy to implement)
Examples
~~~~~~~~
>>> import numpy as np
>>> from mealpy.swarm_based.FOA import OriginalFOA
>>>
>>> def fitness_function(solution):
>>> return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>> "fit_func": fitness_function,
>>> "lb": [-10, -15, -4, -2, -8],
>>> "ub": [10, 15, 12, 8, 20],
>>> "minmax": "min",
>>> "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> model = OriginalFOA(problem_dict1, epoch, pop_size)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")
References
~~~~~~~~~~
[1] Pan, W.T., 2012. A new fruit fly optimization algorithm: taking the financial distress model
as an example. Knowledge-Based Systems, 26, pp.69-74.
"""
def __init__(self, problem, epoch=10000, pop_size=100, **kwargs):
"""
Args:
problem (dict): The problem dictionary
epoch (int): maximum number of iterations, default = 10000
pop_size (int): number of population size, default = 100
"""
super().__init__(problem, kwargs)
self.nfe_per_epoch = pop_size
self.sort_flag = False
self.epoch = epoch
self.pop_size = pop_size
[docs] def norm_consecutive_adjacent(self, position=None):
return np.array([np.linalg.norm([position[x], position[x + 1]]) for x in range(0, self.problem.n_dims - 1)] + \
[np.linalg.norm([position[-1], position[0]])])
[docs] def create_solution(self):
"""
To get the position, fitness wrapper, target and obj list
+ A[self.ID_POS] --> Return: position
+ A[self.ID_TAR] --> Return: [target, [obj1, obj2, ...]]
+ A[self.ID_TAR][self.ID_FIT] --> Return: target
+ A[self.ID_TAR][self.ID_OBJ] --> Return: [obj1, obj2, ...]
Returns:
list: wrapper of solution with format [position, [target, [obj1, obj2, ...]]]
"""
position = np.random.uniform(self.problem.lb, self.problem.ub)
s = self.norm_consecutive_adjacent(position)
pos = self.amend_position(s)
fit = self.get_fitness_position(pos)
return [position, fit]
[docs] def evolve(self, epoch):
"""
The main operations (equations) of algorithm. Inherit from Optimizer class
Args:
epoch (int): The current iteration
"""
pop_new = []
for idx in range(0, self.pop_size):
pos_new = self.pop[idx][self.ID_POS] + np.random.normal(self.problem.lb, self.problem.ub)
pos_new = self.norm_consecutive_adjacent(pos_new)
pos_new = self.amend_position(pos_new)
pop_new.append([pos_new, None])
self.pop = self.update_fitness_population(pop_new)
[docs]class BaseFOA(OriginalFOA):
"""
My changed version of: Fruit-fly Optimization Algorithm (FOA)
Notes
~~~~~
+ The fitness function (small function) is changed by taking the distance each 2 adjacent dimensions
+ Update the position if only new generated solution is better
Examples
~~~~~~~~
>>> import numpy as np
>>> from mealpy.swarm_based.FOA import BaseFOA
>>>
>>> def fitness_function(solution):
>>> return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>> "fit_func": fitness_function,
>>> "lb": [-10, -15, -4, -2, -8],
>>> "ub": [10, 15, 12, 8, 20],
>>> "minmax": "min",
>>> "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> model = BaseFOA(problem_dict1, epoch, pop_size)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")
"""
def __init__(self, problem, epoch=10000, pop_size=100, **kwargs):
"""
Args:
problem (dict): The problem dictionary
epoch (int): maximum number of iterations, default = 10000
pop_size (int): number of population size, default = 100
"""
super().__init__(problem, epoch, pop_size, **kwargs)
[docs] def evolve(self, epoch):
"""
The main operations (equations) of algorithm. Inherit from Optimizer class
Args:
epoch (int): The current iteration
"""
pop_new = []
for idx in range(0, self.pop_size):
if np.random.rand() < 0.5:
pos_new = self.pop[idx][self.ID_POS] + np.random.normal(0, 1, self.problem.n_dims)
else:
pos_new = self.g_best[self.ID_POS] + np.random.normal(0, 1, self.problem.n_dims)
pos_new = self.norm_consecutive_adjacent(pos_new)
pos_new = self.amend_position(pos_new)
pop_new.append([pos_new, None])
pop_new = self.update_fitness_population(pop_new)
self.pop = self.greedy_selection_population(self.pop, pop_new)
[docs]class WhaleFOA(OriginalFOA):
"""
The original version of: Whale Fruit-fly Optimization Algorithm (WFOA)
Links:
1. https://doi.org/10.1016/j.eswa.2020.113502
Examples
~~~~~~~~
>>> import numpy as np
>>> from mealpy.swarm_based.FOA import WhaleFOA
>>>
>>> def fitness_function(solution):
>>> return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>> "fit_func": fitness_function,
>>> "lb": [-10, -15, -4, -2, -8],
>>> "ub": [10, 15, 12, 8, 20],
>>> "minmax": "min",
>>> "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> model = WhaleFOA(problem_dict1, epoch, pop_size)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")
References
~~~~~~~~~~
[1] Fan, Y., Wang, P., Heidari, A.A., Wang, M., Zhao, X., Chen, H. and Li, C., 2020. Boosted hunting-based
fruit fly optimization and advances in real-world problems. Expert Systems with Applications, 159, p.113502.
"""
def __init__(self, problem, epoch=10000, pop_size=100, **kwargs):
"""
Args:
problem (dict): The problem dictionary
epoch (int): maximum number of iterations, default = 10000
pop_size (int): number of population size, default = 100
"""
super().__init__(problem, epoch, pop_size, **kwargs)
[docs] def evolve(self, epoch):
"""
The main operations (equations) of algorithm. Inherit from Optimizer class
Args:
epoch (int): The current iteration
"""
a = 2 - 2 * epoch / (self.epoch - 1) # linearly decreased from 2 to 0
pop_new = []
for idx in range(0, self.pop_size):
r = np.random.rand()
A = 2 * a * r - a
C = 2 * r
l = np.random.uniform(-1, 1)
p = 0.5
b = 1
if np.random.rand() < p:
if np.abs(A) < 1:
D = np.abs(C * self.g_best[self.ID_POS] - self.pop[idx][self.ID_POS])
pos_new = self.g_best[self.ID_POS] - A * D
else:
# select random 1 position in pop
x_rand = self.pop[np.random.randint(self.pop_size)]
D = np.abs(C * x_rand[self.ID_POS] - self.pop[idx][self.ID_POS])
pos_new = (x_rand[self.ID_POS] - A * D)
else:
D1 = np.abs(self.g_best[self.ID_POS] - self.pop[idx][self.ID_POS])
pos_new = D1 * np.exp(b * l) * np.cos(2 * np.pi * l) + self.g_best[self.ID_POS]
smell = self.norm_consecutive_adjacent(pos_new)
pos_new = self.amend_position(smell)
pop_new.append([pos_new, None])
self.pop = self.update_fitness_population(pop_new)