Source code for mealpy.swarm_based.SCSO

#!/usr/bin/env python
# Created by "Thieu" at 17:36, 21/05/2022 ----------%                                                                               
#       Email: nguyenthieu2102@gmail.com            %                                                    
#       Github: https://github.com/thieu1995        %                         
# --------------------------------------------------%

import numpy as np
from mealpy.optimizer import Optimizer


[docs]class OriginalSCSO(Optimizer): """ The original version of: Sand Cat Swarm Optimization (SCSO) Links: 1. https://link.springer.com/article/10.1007/s00366-022-01604-x 2. https://www.mathworks.com/matlabcentral/fileexchange/110185-sand-cat-swarm-optimization Examples ~~~~~~~~ >>> import numpy as np >>> from mealpy import FloatVar, SCSO >>> >>> 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 = SCSO.OriginalSCSO(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] Seyyedabbasi, A., & Kiani, F. (2022). Sand Cat swarm optimization: a nature-inspired algorithm to solve global optimization problems. Engineering with Computers, 1-25. """ def __init__(self, epoch: int = 10000, pop_size: int = 100, **kwargs: object) -> None: """ 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
[docs] def initialize_variables(self): self.ss = 2 # maximum Sensitivity range self.pp = np.arange(1, 361)
[docs] def get_index_roulette_wheel_selection__(self, p): p = p / np.sum(p) c = np.cumsum(p) return np.argwhere(self.generator.random() < c)[0][0]
[docs] def evolve(self, epoch): """ The main operations (equations) of algorithm. Inherit from Optimizer class Args: epoch (int): The current iteration """ guides_r = self.ss - (self.ss * epoch / self.epoch) pop_new = [] for idx in range(0, self.pop_size): r = self.generator.random() * guides_r R = (2*guides_r)*self.generator.random() - guides_r # controls to transition phases pos_new = self.pop[idx].solution.copy() for jdx in range(0, self.problem.n_dims): teta = self.get_index_roulette_wheel_selection__(self.pp) if -1 <= R <= 1: rand_pos = np.abs(self.generator.random() * self.g_best.solution[jdx] - self.pop[idx].solution[jdx]) pos_new[jdx] = self.g_best.solution[jdx] - r * rand_pos * np.cos(teta) else: cp = int(self.generator.random() * self.pop_size) pos_new[jdx] = r * (self.pop[cp].solution[jdx] - self.generator.random() * self.pop[idx].solution[jdx]) 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: pop_new[-1].target = self.get_target(pos_new) self.pop = self.update_target_for_population(pop_new)