#!/usr/bin/env python
# Created by "Thieu" at 19:34, 08/04/2020 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
import numpy as np
from mealpy.optimizer import Optimizer
[docs]class OriginalFPA(Optimizer):
"""
The original version of: Flower Pollination Algorithm (FPA)
Links:
1. https://doi.org/10.1007/978-3-642-32894-7_27
Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
+ p_s (float): [0.5, 0.95], switch probability, default = 0.8
+ levy_multiplier: [0.0001, 1000], mutiplier factor of Levy-flight trajectory, depends on the problem
Examples
~~~~~~~~
>>> import numpy as np
>>> from mealpy import FloatVar, FPA
>>>
>>> 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 = FPA.OriginalFPA(epoch=1000, pop_size=50, p_s = 0.8, levy_multiplier = 0.2)
>>> 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] Yang, X.S., 2012, September. Flower pollination algorithm for global optimization. In International
conference on unconventional computing and natural computation (pp. 240-249). Springer, Berlin, Heidelberg.
"""
def __init__(self, epoch: int = 10000, pop_size: int = 100, p_s: float = 0.8, levy_multiplier: float = 0.1, **kwargs: object) -> None:
"""
Args:
epoch (int): maximum number of iterations, default = 10000
pop_size (int): number of population size, default = 100
p_s (float): switch probability, default = 0.8
levy_multiplier (float): multiplier factor of Levy-flight trajectory, default = 0.2
"""
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.p_s = self.validator.check_float("p_s", p_s, (0, 1.0))
self.levy_multiplier = self.validator.check_float("levy_multiplier", levy_multiplier, (-10000, 10000))
self.set_parameters(["epoch", "pop_size", "p_s", "levy_multiplier"])
self.sort_flag = False
[docs] def amend_solution(self, solution: np.ndarray) -> np.ndarray:
condition = np.logical_and(self.problem.lb <= solution, solution <= self.problem.ub)
random_pos = self.problem.generate_solution()
return np.where(condition, solution, random_pos)
[docs] def evolve(self, epoch):
"""
The main operations (equations) of algorithm. Inherit from Optimizer class
Args:
epoch (int): The current iteration
"""
pop = []
for idx in range(0, self.pop_size):
if self.generator.uniform() < self.p_s:
levy = self.get_levy_flight_step(multiplier=self.levy_multiplier, size=self.problem.n_dims, case=-1)
pos_new = self.pop[idx].solution + 1.0 / np.sqrt(epoch) * levy * (self.pop[idx].solution - self.g_best.solution)
else:
id1, id2 = self.generator.choice(list(set(range(0, self.pop_size)) - {idx}), 2, replace=False)
pos_new = self.pop[idx].solution + self.generator.uniform() * (self.pop[id1].solution - self.pop[id2].solution)
pos_new = self.correct_solution(pos_new)
agent = self.generate_empty_agent(pos_new)
pop.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 = self.update_target_for_population(pop)
self.pop = self.greedy_selection_population(self.pop, pop, self.problem.minmax)