#!/usr/bin/env python
# Created by "Thieu" at 14:53, 17/03/2020 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
import numpy as np
from mealpy.optimizer import Optimizer
[docs]class OriginalGOA(Optimizer):
"""
The original version of: Grasshopper Optimization Algorithm (GOA)
Links:
1. https://dx.doi.org/10.1016/j.advengsoft.2017.01.004
2. https://www.mathworks.com/matlabcentral/fileexchange/61421-grasshopper-optimisation-algorithm-goa
Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
+ c_min (float): coefficient c min, default = 0.00004
+ c_max (float): coefficient c max, default = 2.0
Examples
~~~~~~~~
>>> import numpy as np
>>> from mealpy import FloatVar, GOA
>>>
>>> def objective_function(solution):
>>> return np.sum(solution**2)
>>>
>>> problem_dict = {
>>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"),
>>> "minmax": "min",
>>> "obj_func": objective_function
>>> }
>>>
>>> model = GOA.OriginalGOA(epoch=1000, pop_size=50, c_min = 0.00004, c_max = 1.0)
>>> 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] Saremi, S., Mirjalili, S. and Lewis, A., 2017. Grasshopper optimisation algorithm:
theory and application. Advances in Engineering Software, 105, pp.30-47.
"""
def __init__(self, epoch: int = 10000, pop_size: int = 100, c_min: float = 0.00004, c_max: float = 2.0, **kwargs: object) -> None:
"""
Args:
epoch (int): maximum number of iterations, default = 10000
pop_size (int): number of population size, default = 100
c_min (float): coefficient c min, default=0.00004
c_max (float): coefficient c max, default=2.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.c_min = self.validator.check_float("c_min", c_min, [0.00001, 0.2])
self.c_max = self.validator.check_float("c_max", c_max, [0.2, 5.0])
self.set_parameters(["epoch", "pop_size", "c_min", "c_max"])
self.sort_flag = False
[docs] def s_function__(self, r_vector=None):
f = 0.5
l = 1.5
# Eq.(2.3) in the paper
return f * np.exp(-r_vector / l) - np.exp(-r_vector)
[docs] def evolve(self, epoch):
"""
The main operations (equations) of algorithm. Inherit from Optimizer class
Args:
epoch (int): The current iteration
"""
# Eq.(2.8) in the paper
c = self.c_max - epoch * ((self.c_max - self.c_min) / self.epoch)
pop_new = []
for idx in range(0, self.pop_size):
S_i_total = np.zeros(self.problem.n_dims)
for j in range(0, self.pop_size):
dist = np.sqrt(np.sum((self.pop[idx].solution - self.pop[j].solution) ** 2))
r_ij_vector = (self.pop[idx].solution - self.pop[j].solution) / (dist + self.EPSILON) # xj - xi / dij in Eq.(2.7)
xj_xi = 2 + np.remainder(dist, 2) # |xjd - xid| in Eq. (2.7)
## The first part inside the big bracket in Eq. (2.7) 16 955 230 764 212 047 193 643
ran = (c / 2) * (self.problem.ub - self.problem.lb)
s_ij = ran * self.s_function__(xj_xi) * r_ij_vector
S_i_total += s_ij
x_new = c * self.generator.normal(0, 1, self.problem.n_dims) * S_i_total + self.g_best.solution # Eq. (2.7) in the paper
pos_new = self.correct_solution(x_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)