From 4d955049fc72921d9f11010c5372e144872ecaa3 Mon Sep 17 00:00:00 2001 From: soumitri2001 <56516406+soumitri2001@users.noreply.github.com> Date: Sat, 14 Aug 2021 22:45:39 +0530 Subject: [PATCH 1/8] [ADD] AOA code --- Py_FS/wrapper/nature_inspired/AOA.py | 134 +++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Py_FS/wrapper/nature_inspired/AOA.py diff --git a/Py_FS/wrapper/nature_inspired/AOA.py b/Py_FS/wrapper/nature_inspired/AOA.py new file mode 100644 index 0000000..1f2361e --- /dev/null +++ b/Py_FS/wrapper/nature_inspired/AOA.py @@ -0,0 +1,134 @@ +""" +Programmer: Soumitri Chattopadhyay +Date of Development: 11/07/2021 +This code has been developed according to the procedures mentioned in the following research article: +"Laith A., Diabat A., Mirjalili S., Elaziz M.A., Gandomi A.H. The Arithmetic Optimization Algorithm. +Computer Methods in Applied Mechanics and Engineering, 376, 113609 (2021)" +""" + +import math + +import numpy as np +from sklearn import datasets + +from Py_FS.wrapper.nature_inspired.algorithm import Algorithm + + +class AOA(Algorithm): + + # Arithmetic Optimization Algorithm + ############################### Parameters #################################### + # # + # num_agents: number of agents # + # max_iter: maximum number of generations # + # train_data: training samples of data # + # train_label: class labels for the training samples # + # obj_function: the function to maximize while doing feature selection # + # trans_function_shape: shape of the transfer function used # + # save_conv_graph: boolean value for saving convergence graph # + # # + ############################################################################### + + def __init__(self, + num_agents, + max_iter, + train_data, + train_label, + save_conv_graph=False, + trans_func_shape='s', + seed=0): + + super().__init__(num_agents=num_agents, + max_iter=max_iter, + train_data=train_data, + train_label=train_label, + save_conv_graph=save_conv_graph, + trans_func_shape=trans_func_shape, + seed=seed) + + self.algo_name = 'AOA' + self.agent_name = 'Agent' + self.algo_params = {} + + def user_input(self): + # initializing parameters + self.algo_params['Min'] = 0.1 + self.algo_params['Max'] = 0.9 + self.algo_params['EPS'] = 1e-6 + self.algo_params['alpha'] = 5 + self.algo_params['mu'] = 0.5 + + def moa(self, Min, Max): + return Min + (Max - Min) * self.cur_iter / self.max_iter + + def mop(self, alpha=5): + return 1 - (math.pow(self.cur_iter, (1 / alpha)) / math.pow(self.max_iter, (1 / alpha))) + + def exploration(self, i, j, MoP): + eps = self.algo_params['EPS'] + mu = self.algo_params['mu'] + + # Eq. (3) + r2 = np.random.random() + if r2 >= 0.5: + self.population[i][j] = self.Leader_agent[j] * (MoP + eps) * mu + else: + self.population[i][j] = self.Leader_agent[j] / (MoP + eps) * mu + + def exploitation(self, i, j, MoP): + eps = self.algo_params['EPS'] + mu = self.algo_params['mu'] + + # Eq. (5) + r3 = np.random.random() + if r3 >= 0.5: + self.population[i][j] = self.Leader_agent[j] + MoP * mu + else: + self.population[i][j] = self.Leader_agent[j] - MoP * mu + + def transfer_to_binary(self, i, j): + if np.random.random() < self.trans_function(self.population[i][j]): + self.population[i][j] = 1 + else: + self.population[i][j] = 0 + + def next(self): + print('\n================================================================================') + print(' Iteration - {}'.format(self.cur_iter + 1)) + print('================================================================================\n') + + Min = self.algo_params['Min'] + Max = self.algo_params['Max'] + alpha = self.algo_params['alpha'] + + # Eq. (2) + MoA = self.moa(Min, Max) + + # Eq. (4) + MoP = self.mop(alpha) + + for i in range(self.num_agents): + for j in range(self.num_features): + + r1 = np.random.random() + if r1 > MoA: + self.exploration(i, j, MoP) # Exploration phase (M,D) + else: + self.exploitation(i, j, MoP) # Exploitation phase (A,S) + + # convert to binary using transfer function + self.transfer_to_binary(i, j) + + # increment current iteration + self.cur_iter += 1 + + +if __name__ == '__main__': + data = datasets.load_digits() + algo = AOA(num_agents=20, + max_iter=100, + train_data=data.data, + train_label=data.target, + trans_func_shape='s') + + solution = algo.run() From 7d1d43241a0b598e51abb6f827857438b6e04808 Mon Sep 17 00:00:00 2001 From: soumitri2001 <56516406+soumitri2001@users.noreply.github.com> Date: Sat, 14 Aug 2021 22:46:03 +0530 Subject: [PATCH 2/8] [ADD] AROA code --- Py_FS/wrapper/nature_inspired/AROA.py | 199 ++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 Py_FS/wrapper/nature_inspired/AROA.py diff --git a/Py_FS/wrapper/nature_inspired/AROA.py b/Py_FS/wrapper/nature_inspired/AROA.py new file mode 100644 index 0000000..a0b3c41 --- /dev/null +++ b/Py_FS/wrapper/nature_inspired/AROA.py @@ -0,0 +1,199 @@ +""" +Programmer: Soumitri Chattopadhyay +Date of Development: 11/07/2021 +This code has been developed according to the procedures mentioned in the following research article: +"Hashim, F.A., Hussain, K., Houssein, E.H. et al. Archimedes Optimization Algorithm. +Applied Intelligence, 51, 1531–1551 (2021)" +""" + +import numpy as np +from sklearn import datasets + +from Py_FS.wrapper.nature_inspired.algorithm import Algorithm + + +class AROA(Algorithm): + + # Archimedes Optimization Algorithm + ############################### Parameters #################################### + # # + # num_agents: number of agents # + # max_iter: maximum number of generations # + # train_data: training samples of data # + # train_label: class labels for the training samples # + # obj_function: the function to maximize while doing feature selection # + # trans_function_shape: shape of the transfer function used # + # save_conv_graph: boolean value for saving convergence graph # + # # + ############################################################################### + + def __init__(self, + num_agents, + max_iter, + train_data, + train_label, + save_conv_graph=False, + trans_func_shape='s', + seed=0): + + super().__init__(num_agents=num_agents, + max_iter=max_iter, + train_data=train_data, + train_label=train_label, + save_conv_graph=save_conv_graph, + trans_func_shape=trans_func_shape, + seed=seed) + + self.algo_name = 'AROA' + self.agent_name = 'Particle' + self.algo_params = {} + + def user_input(self): + # initializing parameters + self.algo_params['C1'] = 2 + self.algo_params['C2'] = 6 + self.algo_params['C3'] = 2 + self.algo_params['C4'] = 0.5 + self.algo_params['upper'] = 0.9 + self.algo_params['lower'] = 0.1 + + def initialize(self): + super(AROA, self).initialize() + # initializing agent attributes + self.position = np.random.rand(self.num_agents, self.num_features) # Eq. (4) + self.volume = np.random.rand(self.num_agents, self.num_features) # Eq. (5) + self.density = np.random.rand(self.num_agents, self.num_features) # Eq. (5) + self.acceleration = np.random.rand(self.num_agents, self.num_features) # Eq. (6) + + # initializing leader agent attributes + self.Leader_position = np.zeros((1, self.num_features)) + self.Leader_volume = np.zeros((1, self.num_features)) + self.Leader_density = np.zeros((1, self.num_features)) + self.Leader_acceleration = np.zeros((1, self.num_features)) + + # rank initial agents + self.sort_agents_attr() + + def sort_agents_attr(self): + # sort the agents according to fitness + if self.num_agents == 1: + self.fitness = self.obj_function(self.population, self.training_data) + else: + fitnesses = self.obj_function(self.population, self.training_data) + idx = np.argsort(-fitnesses) + self.population = self.population[idx].copy() + self.fitness = fitnesses[idx].copy() + self.position = self.position[idx].copy() + self.density = self.density[idx].copy() + self.volume = self.volume[idx].copy() + self.acceleration = self.acceleration[idx].copy() + + self.Leader_agent = self.population[0].copy() + self.Leader_fitness = self.fitness[0].copy() + self.Leader_position = self.position[0].copy() + self.Leader_volume = self.volume[0].copy() + self.Leader_density = self.density[0].copy() + self.Leader_acceleration = self.acceleration[0].copy() + + def exploration(self, i, j, Df): + C1 = self.algo_params['C1'] + + # update acceleration + rand_vol, rand_density, rand_accn = np.random.random(3) + self.acceleration[i][j] = (rand_density + rand_vol * rand_accn) / (self.density[i][j] * self.volume[i][j]) + # update position + r1, rand_pos = np.random.random(2) + # Eq. (13) + self.position[i][j] = self.position[i][j] + C1 * r1 * Df * (rand_pos - self.position[i][j]) + + def exploitation(self, i, j, Tf, Df): + C2 = self.algo_params['C2'] + C3 = self.algo_params['C3'] + C4 = self.algo_params['C4'] + + # update acceleration + self.acceleration[i][j] = (self.Leader_density[j] + self.Leader_volume[j] * self.Leader_acceleration[j]) / ( + self.density[i][j] * self.volume[i][j]) + # update position + r2, r3 = np.random.random(2) + T_ = C3 * Tf + P = 2 * r3 - C4 + # Eq. (15) + F = 1 if P <= 0.5 else -1 + # Eq. (14) + self.position[i][j] = self.position[i][j] + F * C2 * r2 * self.acceleration[i][j] * Df * ( + (T_ * self.Leader_position[j]) - self.position[i][j]) + + def normalize_accn(self, i, j): + upper = self.algo_params['upper'] + lower = self.algo_params['lower'] + + # Normalize accelerations + max_accn = np.amax(self.acceleration[i]) + min_accn = np.amin(self.acceleration[i]) + + # Eq. (12) + self.acceleration[i][j] = lower + (self.acceleration[i][j] - min_accn) / (max_accn - min_accn) * upper + + def transfer_to_binary(self, i, j): + # lower acceleration => closer to equilibrium + if self.trans_function(self.acceleration[i][j]) < np.random.random(): + self.population[i][j] = 1 + else: + self.population[i][j] = 0 + + def post_processing(self): + super(AROA, self).post_processing() + # update other leader attributes + if self.fitness[0] > self.Leader_fitness: + self.Leader_agent = self.population[0].copy() + self.Leader_fitness = self.fitness[0].copy() + self.Leader_position = self.position[0].copy() + self.Leader_volume = self.volume[0].copy() + self.Leader_density = self.density[0].copy() + self.Leader_acceleration = self.acceleration[0].copy() + + def next(self): + print('\n================================================================================') + print(' Iteration - {}'.format(self.cur_iter + 1)) + print('================================================================================\n') + + # weight factors + Tf = np.exp((self.cur_iter - self.max_iter) / self.max_iter) # Eq. (8) + Df = np.exp((self.max_iter - self.cur_iter) / self.max_iter) - (self.cur_iter / self.max_iter) # Eq. (9) + + for i in range(self.num_agents): + for j in range(self.num_features): + # Eq. (7) + r1, r2 = np.random.random(2) + # update density + self.density[i][j] = self.density[i][j] + r1 * (self.Leader_density[j] - self.density[i][j]) + # update volume + self.volume[i][j] = self.volume[i][j] + r2 * (self.Leader_volume[j] - self.volume[i][j]) + + if Tf <= 0.5: + # Exploration phase + self.exploration(i, j, Df) + else: + # Exploitation phase + self.exploitation(i, j, Tf, Df) + + # normalize accelerations + self.normalize_accn(i, j) + + # convert to binary using transfer function + self.transfer_to_binary(i, j) + + # increment current iteration + self.cur_iter += 1 + + +if __name__ == '__main__': + data = datasets.load_digits() + algo = AROA(num_agents=20, + max_iter=100, + train_data=data.data, + train_label=data.target, + trans_func_shape='s') + + solution = algo.run() From 8bc9c1603b73b0e6069f5f9a785e1145dd3ca0bd Mon Sep 17 00:00:00 2001 From: soumitri2001 <56516406+soumitri2001@users.noreply.github.com> Date: Sat, 14 Aug 2021 22:46:32 +0530 Subject: [PATCH 3/8] [ADD] MVO code --- Py_FS/wrapper/nature_inspired/MVO.py | 125 +++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Py_FS/wrapper/nature_inspired/MVO.py diff --git a/Py_FS/wrapper/nature_inspired/MVO.py b/Py_FS/wrapper/nature_inspired/MVO.py new file mode 100644 index 0000000..458f95e --- /dev/null +++ b/Py_FS/wrapper/nature_inspired/MVO.py @@ -0,0 +1,125 @@ +""" +Programmer: Soumitri Chattopadhyay +Date of Development: 27/07/2021 +This code has been developed according to the procedures mentioned in the following research article: +"Mirjalili, S. and Mirjalili, S.M. and Hatamlou, A. Multi-Verse Optimizer: a nature-inspired algorithm for global optimization +Neural Computing & Applications 27, 495–513 (2016)." +""" + +import math + +import numpy as np +from sklearn import datasets +from sklearn import preprocessing + +from Py_FS.wrapper.nature_inspired.algorithm import Algorithm + + +class MVO(Algorithm): + + # Multi-Verse Optimizer + ############################### Parameters #################################### + # # + # num_agents: number of agents # + # max_iter: maximum number of generations # + # train_data: training samples of data # + # train_label: class labels for the training samples # + # obj_function: the function to maximize while doing feature selection # + # trans_function_shape: shape of the transfer function used # + # save_conv_graph: boolean value for saving convergence graph # + # # + ############################################################################### + + def __init__(self, + num_agents, + max_iter, + train_data, + train_label, + trans_func_shape='s', + save_conv_graph=False, + seed=0): + + super().__init__(num_agents=num_agents, + max_iter=max_iter, + train_data=train_data, + train_label=train_label, + trans_func_shape=trans_func_shape, + save_conv_graph=save_conv_graph, + seed=seed) + + self.algo_name = 'MVO' + self.agent_name = 'Universe' + self.algo_params = {} + + def user_input(self): + self.algo_params['Min'] = 0.2 + self.algo_params['Max'] = 1.0 + self.algo_params['p'] = 0.6 + + def normalize(self, fitness): + # normalize the fitness values + fitness = np.asarray(fitness.reshape(1, -1), dtype=float) + normalized_fitness = preprocessing.normalize(fitness, norm='l2', axis=1) + normalized_fitness = np.reshape(normalized_fitness, -1) + return normalized_fitness + + def roulette_wheel(self, fitness): + # Perform roulette wheel selection + maximum = sum([f for f in fitness]) + selection_probs = [f / maximum for f in fitness] + return np.random.choice(len(fitness), p=selection_probs) + + def transfer_to_binary(self, i, j): + if np.random.random() < self.trans_function(self.population[i, j]): + self.population[i, j] = 1 + else: + self.population[i, j] = 0 + + def next(self): + print('\n================================================================================') + print(' Iteration - {}'.format(self.cur_iter + 1)) + print('================================================================================\n') + + Min = self.algo_params['Min'] + Max = self.algo_params['Max'] + p = self.algo_params['p'] + + normalized_fitness = self.normalize(self.fitness) + WEP = Min + (self.cur_iter * (Max - Min) / self.max_iter) # Eq. (3.3) + TDR = 1 - math.pow(self.cur_iter, (1 / p)) / math.pow(self.max_iter, (1 / p)) # Eq. (3.4) + + for i in range(self.num_agents): + black_hole_idx = i + for j in range(self.population.shape[1]): + + # Eq. (3.1) + r1 = np.random.random() + if r1 < normalized_fitness[i]: + white_hole_idx = self.roulette_wheel(-normalized_fitness) + self.population[black_hole_idx, j] = self.population[white_hole_idx, j] + + # Eq. (3.2) + r2 = np.random.random() + if r2 < WEP: + r3, r4 = np.random.random(2) + if r3 < 0.5: + self.population[i, j] = self.Leader_agent[j] + (TDR * r4) + else: + self.population[i, j] = self.Leader_agent[j] - (TDR * r4) + + # convert to binary using transfer function + self.transfer_to_binary(i, j) + + # increment current iteration + self.cur_iter += 1 + + +if __name__ == '__main__': + data = datasets.load_digits() + algo = MVO(num_agents=20, + max_iter=100, + train_data=data.data, + train_label=data.target, + trans_func_shape='s') + + solution = algo.run() From 8ccb3ffef467a1f545b493a4140e689645ab8c67 Mon Sep 17 00:00:00 2001 From: soumitri2001 <56516406+soumitri2001@users.noreply.github.com> Date: Sat, 14 Aug 2021 22:46:48 +0530 Subject: [PATCH 4/8] init refactorings --- Py_FS/wrapper/nature_inspired/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Py_FS/wrapper/nature_inspired/__init__.py b/Py_FS/wrapper/nature_inspired/__init__.py index 4c8b07b..8f377c8 100644 --- a/Py_FS/wrapper/nature_inspired/__init__.py +++ b/Py_FS/wrapper/nature_inspired/__init__.py @@ -10,9 +10,14 @@ from Py_FS.wrapper.nature_inspired.RDA import RDA from Py_FS.wrapper.nature_inspired.SCA import SCA from Py_FS.wrapper.nature_inspired.WOA import WOA +from Py_FS.wrapper.nature_inspired.AOA import AOA +from Py_FS.wrapper.nature_inspired.AROA import AROA +from Py_FS.wrapper.nature_inspired.MVO import MVO __all__ = [ + 'AOA', + 'AROA', 'BBA', 'CS', 'EO', @@ -21,6 +26,7 @@ 'GWO', 'HS', 'MA', + 'MVO', 'PSO', 'RDA', 'SCA', From 083964100634d07717c176f928b00f497fd43db3 Mon Sep 17 00:00:00 2001 From: soumitri2001 Date: Sun, 15 Aug 2021 13:36:06 +0530 Subject: [PATCH 5/8] refactorings --- Py_FS/wrapper/nature_inspired/__init__.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Py_FS/wrapper/nature_inspired/__init__.py b/Py_FS/wrapper/nature_inspired/__init__.py index 8f377c8..4c8b07b 100644 --- a/Py_FS/wrapper/nature_inspired/__init__.py +++ b/Py_FS/wrapper/nature_inspired/__init__.py @@ -10,14 +10,9 @@ from Py_FS.wrapper.nature_inspired.RDA import RDA from Py_FS.wrapper.nature_inspired.SCA import SCA from Py_FS.wrapper.nature_inspired.WOA import WOA -from Py_FS.wrapper.nature_inspired.AOA import AOA -from Py_FS.wrapper.nature_inspired.AROA import AROA -from Py_FS.wrapper.nature_inspired.MVO import MVO __all__ = [ - 'AOA', - 'AROA', 'BBA', 'CS', 'EO', @@ -26,7 +21,6 @@ 'GWO', 'HS', 'MA', - 'MVO', 'PSO', 'RDA', 'SCA', From 2c6b6ff9e8c0249c893c63735391320f64e49f16 Mon Sep 17 00:00:00 2001 From: soumitri2001 Date: Sun, 22 Aug 2021 01:18:33 +0530 Subject: [PATCH 6/8] updated parameter input for AOA, AROA and MVO --- Py_FS/wrapper/nature_inspired/AOA.py | 10 +++++----- Py_FS/wrapper/nature_inspired/AROA.py | 12 ++++++------ Py_FS/wrapper/nature_inspired/MVO.py | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Py_FS/wrapper/nature_inspired/AOA.py b/Py_FS/wrapper/nature_inspired/AOA.py index 1f2361e..46be9e4 100644 --- a/Py_FS/wrapper/nature_inspired/AOA.py +++ b/Py_FS/wrapper/nature_inspired/AOA.py @@ -52,11 +52,11 @@ def __init__(self, def user_input(self): # initializing parameters - self.algo_params['Min'] = 0.1 - self.algo_params['Max'] = 0.9 - self.algo_params['EPS'] = 1e-6 - self.algo_params['alpha'] = 5 - self.algo_params['mu'] = 0.5 + self.algo_params['Min'] = float(input('Minimum value of the accelerated function [0-1]: ') or 0.1) + self.algo_params['Max'] = float(input('Maximum value of the accelerated function [0-1]: ') or 0.9) + self.algo_params['EPS'] = float(input('Value of epsilon [default: 1e-6]: ') or 1e-6) + self.algo_params['alpha'] = float(input('Exploitation accuracy parameter [1-10]:' ) or 5) + self.algo_params['mu'] = float(input('Control parameter to adjust the search process [0-1]:' ) or 0.5) def moa(self, Min, Max): return Min + (Max - Min) * self.cur_iter / self.max_iter diff --git a/Py_FS/wrapper/nature_inspired/AROA.py b/Py_FS/wrapper/nature_inspired/AROA.py index a0b3c41..2fade3e 100644 --- a/Py_FS/wrapper/nature_inspired/AROA.py +++ b/Py_FS/wrapper/nature_inspired/AROA.py @@ -50,12 +50,12 @@ def __init__(self, def user_input(self): # initializing parameters - self.algo_params['C1'] = 2 - self.algo_params['C2'] = 6 - self.algo_params['C3'] = 2 - self.algo_params['C4'] = 0.5 - self.algo_params['upper'] = 0.9 - self.algo_params['lower'] = 0.1 + self.algo_params['C1'] = float(input('Control variable C1 [1,2]: ') or 2) + self.algo_params['C2'] = float(input('Control variable C2 [2,4,6]: ') or 6) + self.algo_params['C3'] = float(input('Control variable C3 [1,2]: ') or 2) + self.algo_params['C4'] = float(input('Control variable C4 [0-1]: ') or 0.5) + self.algo_params['upper'] = float(input('upper limit for normalization [0-1]: ') or 0.9) + self.algo_params['lower'] = float(input('lower limit for normalization [0-1]: ') or 0.1) def initialize(self): super(AROA, self).initialize() diff --git a/Py_FS/wrapper/nature_inspired/MVO.py b/Py_FS/wrapper/nature_inspired/MVO.py index 458f95e..7337b5d 100644 --- a/Py_FS/wrapper/nature_inspired/MVO.py +++ b/Py_FS/wrapper/nature_inspired/MVO.py @@ -52,9 +52,9 @@ def __init__(self, self.algo_params = {} def user_input(self): - self.algo_params['Min'] = 0.2 - self.algo_params['Max'] = 1.0 - self.algo_params['p'] = 0.6 + self.algo_params['Min'] = float(input('Minimum wormhole existence probability [0-1]: ') or 0.2) + self.algo_params['Max'] = float(input('Maximum wormhole existence probability [0-1]: ') or 1.0) + self.algo_params['p'] = float(input('Exploitation accuracy factor [1-10]: ') or 6) def normalize(self, fitness): # normalize the fitness values From 85a69f61d5cb44aca4f21864b0deacbf55e856c6 Mon Sep 17 00:00:00 2001 From: soumitri2001 Date: Sun, 12 Sep 2021 15:53:36 +0530 Subject: [PATCH 7/8] necessary updates --- Py_FS/wrapper/nature_inspired/AOA.py | 14 ++++++++------ Py_FS/wrapper/nature_inspired/AROA.py | 15 ++++++++------- Py_FS/wrapper/nature_inspired/MVO.py | 14 +++++++++----- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Py_FS/wrapper/nature_inspired/AOA.py b/Py_FS/wrapper/nature_inspired/AOA.py index 46be9e4..cae039b 100644 --- a/Py_FS/wrapper/nature_inspired/AOA.py +++ b/Py_FS/wrapper/nature_inspired/AOA.py @@ -12,6 +12,7 @@ from sklearn import datasets from Py_FS.wrapper.nature_inspired.algorithm import Algorithm +from Py_FS.wrapper.nature_inspired._transfer_functions import get_trans_function class AOA(Algorithm): @@ -35,7 +36,6 @@ def __init__(self, train_data, train_label, save_conv_graph=False, - trans_func_shape='s', seed=0): super().__init__(num_agents=num_agents, @@ -43,11 +43,11 @@ def __init__(self, train_data=train_data, train_label=train_label, save_conv_graph=save_conv_graph, - trans_func_shape=trans_func_shape, seed=seed) self.algo_name = 'AOA' self.agent_name = 'Agent' + self.trans_function = None self.algo_params = {} def user_input(self): @@ -58,11 +58,15 @@ def user_input(self): self.algo_params['alpha'] = float(input('Exploitation accuracy parameter [1-10]:' ) or 5) self.algo_params['mu'] = float(input('Control parameter to adjust the search process [0-1]:' ) or 0.5) + # initializing transfer function + self.algo_params['trans_function'] = input('Shape of Transfer Function [s/v/u] (default=s): ').lower() or 's' + self.trans_function = get_trans_function(self.algo_params['trans_function']) + def moa(self, Min, Max): return Min + (Max - Min) * self.cur_iter / self.max_iter def mop(self, alpha=5): - return 1 - (math.pow(self.cur_iter, (1 / alpha)) / math.pow(self.max_iter, (1 / alpha))) + return 1 - math.pow((self.cur_iter/self.max_iter), (1 / alpha)) def exploration(self, i, j, MoP): eps = self.algo_params['EPS'] @@ -76,7 +80,6 @@ def exploration(self, i, j, MoP): self.population[i][j] = self.Leader_agent[j] / (MoP + eps) * mu def exploitation(self, i, j, MoP): - eps = self.algo_params['EPS'] mu = self.algo_params['mu'] # Eq. (5) @@ -128,7 +131,6 @@ def next(self): algo = AOA(num_agents=20, max_iter=100, train_data=data.data, - train_label=data.target, - trans_func_shape='s') + train_label=data.target) solution = algo.run() diff --git a/Py_FS/wrapper/nature_inspired/AROA.py b/Py_FS/wrapper/nature_inspired/AROA.py index 2fade3e..8b7d893 100644 --- a/Py_FS/wrapper/nature_inspired/AROA.py +++ b/Py_FS/wrapper/nature_inspired/AROA.py @@ -10,6 +10,7 @@ from sklearn import datasets from Py_FS.wrapper.nature_inspired.algorithm import Algorithm +from Py_FS.wrapper.nature_inspired._transfer_functions import get_trans_function class AROA(Algorithm): @@ -33,7 +34,6 @@ def __init__(self, train_data, train_label, save_conv_graph=False, - trans_func_shape='s', seed=0): super().__init__(num_agents=num_agents, @@ -41,22 +41,26 @@ def __init__(self, train_data=train_data, train_label=train_label, save_conv_graph=save_conv_graph, - trans_func_shape=trans_func_shape, seed=seed) self.algo_name = 'AROA' self.agent_name = 'Particle' + self.trans_function = None self.algo_params = {} def user_input(self): # initializing parameters self.algo_params['C1'] = float(input('Control variable C1 [1,2]: ') or 2) - self.algo_params['C2'] = float(input('Control variable C2 [2,4,6]: ') or 6) + self.algo_params['C2'] = float(input('Control variable C2 [2/4/6]: ') or 6) self.algo_params['C3'] = float(input('Control variable C3 [1,2]: ') or 2) self.algo_params['C4'] = float(input('Control variable C4 [0-1]: ') or 0.5) self.algo_params['upper'] = float(input('upper limit for normalization [0-1]: ') or 0.9) self.algo_params['lower'] = float(input('lower limit for normalization [0-1]: ') or 0.1) + # initializing transfer function + self.algo_params['trans_function'] = input('Shape of Transfer Function [s/v/u] (default=s): ').lower() or 's' + self.trans_function = get_trans_function(self.algo_params['trans_function']) + def initialize(self): super(AROA, self).initialize() # initializing agent attributes @@ -146,8 +150,6 @@ def post_processing(self): super(AROA, self).post_processing() # update other leader attributes if self.fitness[0] > self.Leader_fitness: - self.Leader_agent = self.population[0].copy() - self.Leader_fitness = self.fitness[0].copy() self.Leader_position = self.position[0].copy() self.Leader_volume = self.volume[0].copy() self.Leader_density = self.density[0].copy() @@ -193,7 +195,6 @@ def next(self): algo = AROA(num_agents=20, max_iter=100, train_data=data.data, - train_label=data.target, - trans_func_shape='s') + train_label=data.target) solution = algo.run() diff --git a/Py_FS/wrapper/nature_inspired/MVO.py b/Py_FS/wrapper/nature_inspired/MVO.py index 7337b5d..366b298 100644 --- a/Py_FS/wrapper/nature_inspired/MVO.py +++ b/Py_FS/wrapper/nature_inspired/MVO.py @@ -13,6 +13,7 @@ from sklearn import preprocessing from Py_FS.wrapper.nature_inspired.algorithm import Algorithm +from Py_FS.wrapper.nature_inspired._transfer_functions import get_trans_function class MVO(Algorithm): @@ -35,7 +36,6 @@ def __init__(self, max_iter, train_data, train_label, - trans_func_shape='s', save_conv_graph=False, seed=0): @@ -43,19 +43,24 @@ def __init__(self, max_iter=max_iter, train_data=train_data, train_label=train_label, - trans_func_shape=trans_func_shape, save_conv_graph=save_conv_graph, seed=seed) self.algo_name = 'MVO' self.agent_name = 'Universe' + self.trans_function = None self.algo_params = {} def user_input(self): + # initializing parameters self.algo_params['Min'] = float(input('Minimum wormhole existence probability [0-1]: ') or 0.2) self.algo_params['Max'] = float(input('Maximum wormhole existence probability [0-1]: ') or 1.0) self.algo_params['p'] = float(input('Exploitation accuracy factor [1-10]: ') or 6) + # initializing transfer function + self.algo_params['trans_function'] = input('Shape of Transfer Function [s/v/u] (default=s): ').lower() or 's' + self.trans_function = get_trans_function(self.algo_params['trans_function']) + def normalize(self, fitness): # normalize the fitness values fitness = np.asarray(fitness.reshape(1, -1), dtype=float) @@ -86,7 +91,7 @@ def next(self): normalized_fitness = self.normalize(self.fitness) WEP = Min + (self.cur_iter * (Max - Min) / self.max_iter) # Eq. (3.3) - TDR = 1 - math.pow(self.cur_iter, (1 / p)) / math.pow(self.max_iter, (1 / p)) # Eq. (3.4) + TDR = 1 - math.pow((self.cur_iter / self.max_iter), (1 / p)) # Eq. (3.4) for i in range(self.num_agents): black_hole_idx = i @@ -119,7 +124,6 @@ def next(self): algo = MVO(num_agents=20, max_iter=100, train_data=data.data, - train_label=data.target, - trans_func_shape='s') + train_label=data.target) solution = algo.run() From 172c861d8a5783435d3f8582ad8a1bebf641ee4c Mon Sep 17 00:00:00 2001 From: soumitri2001 Date: Wed, 15 Sep 2021 02:32:50 +0530 Subject: [PATCH 8/8] code updated --- Py_FS/wrapper/nature_inspired/AOA.py | 21 ++++++--------------- Py_FS/wrapper/nature_inspired/AROA.py | 23 +++++++---------------- Py_FS/wrapper/nature_inspired/MVO.py | 8 ++------ 3 files changed, 15 insertions(+), 37 deletions(-) diff --git a/Py_FS/wrapper/nature_inspired/AOA.py b/Py_FS/wrapper/nature_inspired/AOA.py index cae039b..69a21d5 100644 --- a/Py_FS/wrapper/nature_inspired/AOA.py +++ b/Py_FS/wrapper/nature_inspired/AOA.py @@ -69,25 +69,20 @@ def mop(self, alpha=5): return 1 - math.pow((self.cur_iter/self.max_iter), (1 / alpha)) def exploration(self, i, j, MoP): - eps = self.algo_params['EPS'] - mu = self.algo_params['mu'] - # Eq. (3) r2 = np.random.random() if r2 >= 0.5: - self.population[i][j] = self.Leader_agent[j] * (MoP + eps) * mu + self.population[i][j] = self.Leader_agent[j] * (MoP + self.algo_params['EPS']) * self.algo_params['mu'] else: - self.population[i][j] = self.Leader_agent[j] / (MoP + eps) * mu + self.population[i][j] = self.Leader_agent[j] / (MoP + self.algo_params['EPS']) * self.algo_params['mu'] def exploitation(self, i, j, MoP): - mu = self.algo_params['mu'] - # Eq. (5) r3 = np.random.random() if r3 >= 0.5: - self.population[i][j] = self.Leader_agent[j] + MoP * mu + self.population[i][j] = self.Leader_agent[j] + MoP * self.algo_params['mu'] else: - self.population[i][j] = self.Leader_agent[j] - MoP * mu + self.population[i][j] = self.Leader_agent[j] - MoP * self.algo_params['mu'] def transfer_to_binary(self, i, j): if np.random.random() < self.trans_function(self.population[i][j]): @@ -100,15 +95,11 @@ def next(self): print(' Iteration - {}'.format(self.cur_iter + 1)) print('================================================================================\n') - Min = self.algo_params['Min'] - Max = self.algo_params['Max'] - alpha = self.algo_params['alpha'] - # Eq. (2) - MoA = self.moa(Min, Max) + MoA = self.moa(self.algo_params['Min'], self.algo_params['Max']) # Eq. (4) - MoP = self.mop(alpha) + MoP = self.mop(self.algo_params['alpha']) for i in range(self.num_agents): for j in range(self.num_features): diff --git a/Py_FS/wrapper/nature_inspired/AROA.py b/Py_FS/wrapper/nature_inspired/AROA.py index 8b7d893..8eb50db 100644 --- a/Py_FS/wrapper/nature_inspired/AROA.py +++ b/Py_FS/wrapper/nature_inspired/AROA.py @@ -50,9 +50,9 @@ def __init__(self, def user_input(self): # initializing parameters - self.algo_params['C1'] = float(input('Control variable C1 [1,2]: ') or 2) + self.algo_params['C1'] = float(input('Control variable C1 [1/2]: ') or 2) self.algo_params['C2'] = float(input('Control variable C2 [2/4/6]: ') or 6) - self.algo_params['C3'] = float(input('Control variable C3 [1,2]: ') or 2) + self.algo_params['C3'] = float(input('Control variable C3 [1/2]: ') or 2) self.algo_params['C4'] = float(input('Control variable C4 [0-1]: ') or 0.5) self.algo_params['upper'] = float(input('upper limit for normalization [0-1]: ') or 0.9) self.algo_params['lower'] = float(input('lower limit for normalization [0-1]: ') or 0.1) @@ -100,44 +100,35 @@ def sort_agents_attr(self): self.Leader_acceleration = self.acceleration[0].copy() def exploration(self, i, j, Df): - C1 = self.algo_params['C1'] - # update acceleration rand_vol, rand_density, rand_accn = np.random.random(3) self.acceleration[i][j] = (rand_density + rand_vol * rand_accn) / (self.density[i][j] * self.volume[i][j]) # update position r1, rand_pos = np.random.random(2) # Eq. (13) - self.position[i][j] = self.position[i][j] + C1 * r1 * Df * (rand_pos - self.position[i][j]) + self.position[i][j] = self.position[i][j] + self.algo_params['C1'] * r1 * Df * (rand_pos - self.position[i][j]) def exploitation(self, i, j, Tf, Df): - C2 = self.algo_params['C2'] - C3 = self.algo_params['C3'] - C4 = self.algo_params['C4'] - # update acceleration self.acceleration[i][j] = (self.Leader_density[j] + self.Leader_volume[j] * self.Leader_acceleration[j]) / ( self.density[i][j] * self.volume[i][j]) # update position r2, r3 = np.random.random(2) - T_ = C3 * Tf - P = 2 * r3 - C4 + T_ = self.algo_params['C3'] * Tf + P = 2 * r3 - self.algo_params['C4'] # Eq. (15) F = 1 if P <= 0.5 else -1 # Eq. (14) - self.position[i][j] = self.position[i][j] + F * C2 * r2 * self.acceleration[i][j] * Df * ( + self.position[i][j] = self.position[i][j] + F * self.algo_params['C2'] * r2 * self.acceleration[i][j] * Df * ( (T_ * self.Leader_position[j]) - self.position[i][j]) def normalize_accn(self, i, j): - upper = self.algo_params['upper'] - lower = self.algo_params['lower'] - # Normalize accelerations max_accn = np.amax(self.acceleration[i]) min_accn = np.amin(self.acceleration[i]) # Eq. (12) - self.acceleration[i][j] = lower + (self.acceleration[i][j] - min_accn) / (max_accn - min_accn) * upper + self.acceleration[i][j] = self.algo_params['lower'] + (self.acceleration[i][j] - min_accn) / (max_accn - min_accn) * self.algo_params['upper'] def transfer_to_binary(self, i, j): # lower acceleration => closer to equilibrium diff --git a/Py_FS/wrapper/nature_inspired/MVO.py b/Py_FS/wrapper/nature_inspired/MVO.py index 366b298..95829f6 100644 --- a/Py_FS/wrapper/nature_inspired/MVO.py +++ b/Py_FS/wrapper/nature_inspired/MVO.py @@ -85,13 +85,9 @@ def next(self): print(' Iteration - {}'.format(self.cur_iter + 1)) print('================================================================================\n') - Min = self.algo_params['Min'] - Max = self.algo_params['Max'] - p = self.algo_params['p'] - normalized_fitness = self.normalize(self.fitness) - WEP = Min + (self.cur_iter * (Max - Min) / self.max_iter) # Eq. (3.3) - TDR = 1 - math.pow((self.cur_iter / self.max_iter), (1 / p)) # Eq. (3.4) + WEP = self.algo_params['Min'] + (self.cur_iter * (self.algo_params['Max'] - self.algo_params['Min']) / self.max_iter) # Eq. (3.3) + TDR = 1 - math.pow((self.cur_iter / self.max_iter), (1 / self.algo_params['p'])) # Eq. (3.4) for i in range(self.num_agents): black_hole_idx = i