def MA(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_function_shape='s', prob_mut=0.2, save_conv_graph=False): # Mayfly Algorithm ############################### Parameters #################################### # # # num_agents: number of mayflies # # 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 # # prob_mut: probability of mutation # # trans_function_shape: shape of the transfer function used # # save_conv_graph: boolean value for saving convergence graph # # # ############################################################################### short_name = 'MA' agent_name = 'Mayfly' train_data, train_label = np.array(train_data), np.array(train_label) num_features = train_data.shape[1] trans_function = get_trans_function(trans_function_shape) # control parameters a1 = 1 a2 = 1.5 d = 0.1 fl = 0.1 g = 0.8 beta = 2 delta = 0.9 # initialize position and velocities of male and female mayflies' and Leader (the agent with the max fitness) male_pos = initialize(num_agents, num_features) female_pos = initialize(num_agents, num_features) male_vel = np.random.uniform(low = -1, high = -1, size = (num_agents, num_features)) female_vel = np.random.uniform(low = -1, high = -1, size = (num_agents, num_features)) male_fitness = np.zeros((num_agents)) male_accuracy = np.zeros(num_agents) female_fitness = np.zeros((num_agents)) Leader_agent = np.zeros((num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") male_personal_best = np.zeros((num_agents, num_features)) male_offspring = np.zeros((num_agents, num_features)) female_offspring = np.zeros((num_agents, num_features)) vmax_male = np.zeros((num_features)) vmax_female = np.zeros((num_features)) # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) convergence_curve['feature_count'] = np.zeros(max_iter) data = Data() data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split(train_data, train_label, stratify=train_label, test_size=0.2) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial population male_pos, male_fitness = sort_agents(male_pos, obj_function, data) female_pos, female_fitness = sort_agents(female_pos, obj_function, data) # start timer start_time = time.time() # main loop for iter_no in range(max_iter): print('\n================================================================================') print(' Iteration - {}'.format(iter_no+1)) print('================================================================================\n') #updating velocity limits vmax_male, vmax_female = update_max_velocity(male_pos, female_pos) for agent in range(num_agents): #updating Leader fitness and personal best fitnesses if male_fitness[agent] > Leader_fitness: Leader_fitness = male_fitness[agent] Leader_agent = male_pos[agent] if male_fitness[agent] > obj_function(male_personal_best[agent], data.train_X, data.val_X, data.train_Y, data.val_Y): male_personal_best[agent] = male_pos[agent] #update velocities of male and female mayflies male_vel[agent], female_vel[agent] = update_velocity(male_pos[agent], female_pos[agent], male_vel[agent], female_vel[agent], Leader_agent, male_personal_best[agent], a1, a2, d, fl, g, beta, agent, data, obj_function) #check boundary condition of velocities of male and female mayflies male_vel[agent], female_vel[agent] = check_velocity_limits(male_vel[agent], female_vel[agent], vmax_male, vmax_female) #applying transfer functions to update positions of male and female mayflies #the updation is done based on their respective velocity values for j in range(num_features): trans_value = trans_function(male_vel[agent][j]) if trans_value > np.random.normal(0,1): male_pos[agent][j]=1 else: male_pos[agent][j]=0 trans_value = trans_function(female_vel[agent][j]) if trans_value > np.random.random(): female_pos[agent][j]=1 else: female_pos[agent][j]=0 #sorting male_pos, male_fitness = sort_agents(male_pos, obj_function, data) female_pos, female_fitness = sort_agents(female_pos, obj_function, data) for agent in range(num_agents): #generation of offsprings by crossover and mutation between male and female parent mayflies male_offspring[agent], female_offspring[agent] = cross_mut(male_pos[agent], female_pos[agent]) #comparing parents and offsprings and replacing parents wherever necessary male_pos = compare_and_replace(male_pos, male_offspring, male_fitness, data, obj_function) female_pos = compare_and_replace(female_pos, female_offspring, female_fitness, data, obj_function) #updating fitness values male_pos, male_fitness = sort_agents(male_pos, obj_function, data) female_pos, female_fitness = sort_agents(female_pos, obj_function, data) #updating values of nuptial dance d = d * delta fl = fl * delta #update final information display(male_pos, male_fitness, agent_name) if(male_fitness[0] > Leader_fitness): Leader_agent = male_pos[0].copy() Leader_fitness = male_fitness[0].copy() convergence_curve['fitness'][iter_no] = Leader_fitness convergence_curve['feature_count'][iter_no] = int(np.sum(Leader_agent)) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) male_pos, male_accuracy = sort_agents(male_pos, compute_accuracy, data) print('\n================================================================================') print(' Final Result ') print('================================================================================\n') print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print('\n================================================================================\n') # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence curves iters = np.arange(max_iter)+1 fig, axes = plt.subplots(2, 1) fig.tight_layout(pad = 5) fig.suptitle('Convergence Curves') axes[0].set_title('Convergence of Fitness over Iterations') axes[0].set_xlabel('Iteration') axes[0].set_ylabel('Fitness') axes[0].plot(iters, convergence_curve['fitness']) axes[1].set_title('Convergence of Feature Count over Iterations') axes[1].set_xlabel('Iteration') axes[1].set_ylabel('Number of Selected Features') axes[1].plot(iters, convergence_curve['feature_count']) if(save_conv_graph): plt.savefig('convergence_graph_'+ short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_population = male_pos solution.final_fitness = male_fitness solution.final_accuracy = male_accuracy solution.execution_time = exec_time return solution
def PSO(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_func_shape='s', save_conv_graph=False): # Particle Swarm Optimizer ############################### Parameters #################################### # # # num_agents: number of particles # # 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 # # # ############################################################################### short_name = 'PSO' agent_name = 'Particle' train_data, train_label = np.array(train_data), np.array(train_label) num_features = train_data.shape[1] trans_function = get_trans_function(trans_func_shape) # setting up the objectives weight_acc = None if (obj_function == compute_fitness): weight_acc = float( input('Weight for the classification accuracy [0-1]: ')) obj = (obj_function, weight_acc) compute_accuracy = ( compute_fitness, 1 ) # compute_accuracy is just compute_fitness with accuracy weight as 1 # initialize particles and Leader (the agent with the max fitness) particles = initialize(num_agents, num_features) fitness = np.zeros(num_agents) accuracy = np.zeros(num_agents) Leader_agent = np.zeros((1, num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) # initialize data class data = Data() val_size = float( input('Enter the percentage of data wanted for valdiation [0, 100]: ') ) / 100 data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split( train_data, train_label, stratify=train_label, test_size=val_size) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial particles particles, fitness = sort_agents(particles, obj, data) # start timer start_time = time.time() # initialize global and local best particles globalBestParticle = [0 for i in range(num_features)] globalBestFitness = float("-inf") localBestParticle = [[0 for i in range(num_features)] for j in range(num_agents)] localBestFitness = [float("-inf") for i in range(num_agents)] weight = 1.0 velocity = [[0.0 for i in range(num_features)] for j in range(num_agents)] for iter_no in range(max_iter): print( '\n================================================================================' ) print(' Iteration - {}'.format(iter_no + 1)) print( '================================================================================\n' ) # update weight weight = 1.0 - (iter_no / max_iter) # update the velocity for i in range(num_agents): for j in range(num_features): velocity[i][j] = (weight * velocity[i][j]) r1, r2 = np.random.random(2) velocity[i][j] = velocity[i][j] + ( r1 * (localBestParticle[i][j] - particles[i][j])) velocity[i][j] = velocity[i][j] + ( r2 * (globalBestParticle[j] - particles[i][j])) # updating position of particles for i in range(num_agents): for j in range(num_features): trans_value = trans_function(velocity[i][j]) if (np.random.random() < trans_value): particles[i][j] = 1 else: particles[i][j] = 0 # updating fitness of particles particles, fitness = sort_agents(particles, obj, data) display(particles, fitness, agent_name) # updating the global best and local best particles for i in range(num_agents): if fitness[i] > localBestFitness[i]: localBestFitness[i] = fitness[i] localBestParticle[i] = particles[i][:] if fitness[i] > globalBestFitness: globalBestFitness = fitness[i] globalBestParticle = particles[i][:] # update Leader (best agent) if globalBestFitness > Leader_fitness: Leader_agent = globalBestParticle.copy() Leader_fitness = globalBestFitness.copy() convergence_curve['fitness'][iter_no] = np.mean(fitness) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) particles, accuracy = sort_agents(particles, compute_accuracy, data) print( '\n================================================================================' ) print( ' Final Result ' ) print( '================================================================================\n' ) print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print( '\n================================================================================\n' ) # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence graph fig, axes = Conv_plot(convergence_curve) if (save_conv_graph): plt.savefig('convergence_graph_' + short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_particles = particles solution.final_fitness = fitness solution.final_accuracy = accuracy solution.execution_time = exec_time return solution
def WOA(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_function_shape='s', save_conv_graph=False): # Whale Optimization Algorithm ############################### Parameters #################################### # # # num_agents: number of whales # # 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 # # # ############################################################################### short_name = 'WOA' agent_name = 'Whale' train_data, train_label = np.array(train_data), np.array(train_label) num_features = train_data.shape[1] cross_limit = 5 trans_function = get_trans_function(trans_function_shape) # setting up the objectives weight_acc = None if (obj_function == compute_fitness): weight_acc = float( input('Weight for the classification accuracy [0-1]: ')) obj = (obj_function, weight_acc) compute_accuracy = ( compute_fitness, 1 ) # compute_accuracy is just compute_fitness with accuracy weight as 1 # initialize whales and Leader (the agent with the max fitness) whales = initialize(num_agents, num_features) fitness = np.zeros(num_agents) accuracy = np.zeros(num_agents) Leader_agent = np.zeros((1, num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) convergence_curve['feature_count'] = np.zeros(max_iter) # format the data data = Data() data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split( train_data, train_label, stratify=train_label, test_size=0.2) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial population whales, fitness = sort_agents(whales, obj, data) # start timer start_time = time.time() # main loop for iter_no in range(max_iter): print( '\n================================================================================' ) print(' Iteration - {}'.format(iter_no + 1)) print( '================================================================================\n' ) a = 2 - iter_no * (2 / max_iter) # a decreases linearly fron 2 to 0 # update the position of each whale for i in range(num_agents): # update the parameters r = np.random.random() # r is a random number in [0, 1] A = (2 * a * r) - a # Eq. (3) C = 2 * r # Eq. (4) l = -1 + (np.random.random() * 2 ) # l is a random number in [-1, 1] p = np.random.random() # p is a random number in [0, 1] b = 1 # defines shape of the spiral if p < 0.5: # Shrinking Encircling mechanism if abs(A) >= 1: rand_agent_index = np.random.randint(0, num_agents) rand_agent = whales[rand_agent_index, :] mod_dist_rand_agent = abs(C * rand_agent - whales[i, :]) whales[i, :] = rand_agent - (A * mod_dist_rand_agent ) # Eq. (9) else: mod_dist_Leader = abs(C * Leader_agent - whales[i, :]) whales[i, :] = Leader_agent - (A * mod_dist_Leader ) # Eq. (2) else: # Spiral-Shaped Attack mechanism dist_Leader = abs(Leader_agent - whales[i, :]) whales[i, :] = dist_Leader * np.exp(b * l) * np.cos( l * 2 * np.pi) + Leader_agent # Apply transformation function on the updated whale for j in range(num_features): trans_value = trans_function(whales[i, j]) if (np.random.random() < trans_value): whales[i, j] = 1 else: whales[i, j] = 0 # update final information whales, fitness = sort_agents(whales, obj, data) display(whales, fitness, agent_name) if fitness[0] > Leader_fitness: Leader_agent = whales[0].copy() Leader_fitness = fitness[0].copy() convergence_curve['fitness'][iter_no] = Leader_fitness convergence_curve['feature_count'][iter_no] = int(np.sum(Leader_agent)) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) whales, accuracy = sort_agents(whales, compute_accuracy, data) print( '\n================================================================================' ) print( ' Final Result ' ) print( '================================================================================\n' ) print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print( '\n================================================================================\n' ) # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence curves iters = np.arange(max_iter) + 1 fig, axes = plt.subplots(2, 1) fig.tight_layout(pad=5) fig.suptitle('Convergence Curves') axes[0].set_title('Convergence of Fitness over Iterations') axes[0].set_xlabel('Iteration') axes[0].set_ylabel('Fitness') axes[0].plot(iters, convergence_curve['fitness']) axes[1].set_title('Convergence of Feature Count over Iterations') axes[1].set_xlabel('Iteration') axes[1].set_ylabel('Number of Selected Features') axes[1].plot(iters, convergence_curve['feature_count']) if (save_conv_graph): plt.savefig('convergence_graph_' + short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_population = whales solution.final_fitness = fitness solution.final_accuracy = accuracy solution.execution_time = exec_time return solution
def GWO(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_func_shape='s', save_conv_graph=False): # Grey Wolf Optimizer ############################### Parameters #################################### # # # num_agents: number of greywolves # # 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 # # # ############################################################################### short_name = 'GWO' agent_name = 'Greywolf' train_data, train_label = np.array(train_data), np.array(train_label) num_features = train_data.shape[1] trans_function = get_trans_function(trans_func_shape) # setting up the objectives weight_acc = None if (obj_function == compute_fitness): weight_acc = float( input('Weight for the classification accuracy [0-1]: ')) obj = (obj_function, weight_acc) compute_accuracy = ( compute_fitness, 1 ) # compute_accuracy is just compute_fitness with accuracy weight as 1 # initialize greywolves and Leader (the agent with the max fitness) greywolves = initialize(num_agents, num_features) fitness = np.zeros(num_agents) accuracy = np.zeros(num_agents) Leader_agent = np.zeros((1, num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) convergence_curve['feature_count'] = np.zeros(max_iter) # initialize data class data = Data() val_size = float( input('Enter the percentage of data wanted for valdiation [0, 100]: ') ) / 100 data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split( train_data, train_label, stratify=train_label, test_size=val_size) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial greywolves greywolves, fitness = sort_agents(greywolves, obj, data) # start timer start_time = time.time() # initialize the alpha, beta and delta grey wolves and their fitness alpha, beta, delta = np.zeros((1, num_features)), np.zeros( (1, num_features)), np.zeros((1, num_features)) alpha_fit, beta_fit, delta_fit = float("-inf"), float("-inf"), float( "-inf") for iter_no in range(max_iter): print( '\n================================================================================' ) print(' Iteration - {}'.format(iter_no + 1)) print( '================================================================================\n' ) # update the alpha, beta and delta grey wolves for i in range(num_agents): # update alpha, beta, delta if fitness[i] > alpha_fit: delta_fit = beta_fit delta = beta.copy() beta_fit = alpha_fit beta = alpha.copy() alpha_fit = fitness[i] alpha = greywolves[i, :].copy() # update beta, delta elif fitness[i] > beta_fit: delta_fit = beta_fit delta = beta.copy() beta_fit = fitness[i] beta = greywolves[i, :].copy() # update delta elif fitness[i] > delta_fit: delta_fit = fitness[i] delta = greywolves[i, :].copy() # a decreases linearly fron 2 to 0 a = 2 - iter_no * ((2) / max_iter) for i in range(num_agents): for j in range(num_features): # calculate distance between alpha and current agent r1 = np.random.random() # r1 is a random number in [0,1] r2 = np.random.random() # r2 is a random number in [0,1] A1 = (2 * a * r1) - a # calculate A1 C1 = 2 * r2 # calculate C1 D_alpha = abs(C1 * alpha[j] - greywolves[i, j]) # find distance from alpha X1 = alpha[j] - (A1 * D_alpha) # Eq. (3.6) # calculate distance between beta and current agent r1 = np.random.random() # r1 is a random number in [0,1] r2 = np.random.random() # r2 is a random number in [0,1] A2 = (2 * a * r1) - a # calculate A2 C2 = 2 * r2 # calculate C2 D_beta = abs(C2 * beta[j] - greywolves[i, j]) # find distance from beta X2 = beta[j] - (A2 * D_beta) # Eq. (3.6) # calculate distance between delta and current agent r1 = np.random.random() # r1 is a random number in [0,1] r2 = np.random.random() # r2 is a random number in [0,1] A3 = (2 * a * r1) - a # calculate A3 C3 = 2 * r2 # calculate C3 D_delta = abs(C3 * delta[j] - greywolves[i, j]) # find distance from delta X3 = delta[j] - A3 * D_delta # Eq. (3.6) # update the position of current agent greywolves[i, j] = (X1 + X2 + X3) / 3 # Eq. (3.7) # Apply transformation function on the updated greywolf for j in range(num_features): trans_value = trans_function(greywolves[i, j]) if (np.random.random() < trans_value): greywolves[i, j] = 1 else: greywolves[i, j] = 0 # update final information greywolves, fitness = sort_agents(greywolves, obj, data) display(greywolves, fitness, agent_name) # update Leader (best agent) if fitness[0] > Leader_fitness: Leader_agent = greywolves[0].copy() Leader_fitness = fitness[0].copy() if alpha_fit > Leader_fitness: Leader_fitness = alpha_fit Leader_agent = alpha.copy() convergence_curve['fitness'][iter_no] = Leader_fitness convergence_curve['feature_count'][iter_no] = int(np.sum(Leader_agent)) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) greywolves, accuracy = sort_agents(greywolves, compute_accuracy, data) print( '\n================================================================================' ) print( ' Final Result ' ) print( '================================================================================\n' ) print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print( '\n================================================================================\n' ) # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence curves iters = np.arange(max_iter) + 1 fig, axes = plt.subplots(2, 1) fig.tight_layout(pad=5) fig.suptitle('Convergence Curves') axes[0].set_title('Convergence of Fitness over Iterations') axes[0].set_xlabel('Iteration') axes[0].set_ylabel('Fitness') axes[0].plot(iters, convergence_curve['fitness']) axes[1].set_title('Convergence of Feature Count over Iterations') axes[1].set_xlabel('Iteration') axes[1].set_ylabel('Number of Selected Features') axes[1].plot(iters, convergence_curve['feature_count']) if (save_conv_graph): plt.savefig('convergence_graph_' + short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_greywolves = greywolves solution.final_fitness = fitness solution.final_accuracy = accuracy solution.execution_time = exec_time return solution
def CS (num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_function_shape='s', save_conv_graph=False): # Cuckoo Search 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 # # # ############################################################################### short_name = 'CS' agent_name = 'Agent' num_features = train_data.shape[1] trans_function = get_trans_function(trans_function_shape) # setting up the objectives weight_acc = None if(obj_function==compute_fitness): weight_acc = float(input('Weight for the classification accuracy [0-1]: ')) obj = (obj_function, weight_acc) compute_accuracy = (compute_fitness, 1) # compute_accuracy is just compute_fitness with accuracy weight as 1 # initializing cuckoo and host nests levy_flight = np.random.uniform(low=-2, high=2, size=(num_features)) cuckoo = np.random.randint(low=0, high=2, size=(num_features)) nest = initialize(num_agents, num_features) fitness = np.zeros(num_agents) nest_accuracy = np.zeros(num_agents) cuckoo_fitness = float("-inf") Leader_agent = np.zeros((num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") p_a=0.25 # fraction of nests to be replaced # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) # initialize data class data = Data() val_size = float(input('Enter the percentage of data wanted for valdiation [0, 100]: '))/100 data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split(train_data, train_label, stratify=train_label, test_size=val_size) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial nests nest, fitness = sort_agents(nest, obj, data) cuckoo,cuckoo_fitness = sort_agents(cuckoo,obj,data) # start timer start_time = time.time() # main loop for iter_no in range(max_iter): print('\n================================================================================') print(' Iteration - {}'.format(iter_no+1)) print('================================================================================\n') # updating leader nest if fitness[0] > Leader_fitness: Leader_agent = nest[0].copy() Leader_fitness = fitness[0] # get new cuckoo levy_flight = get_cuckoo(levy_flight) for j in range(num_features): if trans_function(levy_flight[j]) > np.random.random(): cuckoo[j]=1 else: cuckoo[j]=0 cuckoo,cuckoo_fitness = sort_agents(cuckoo,obj,data) # check if a nest needs to be replaced j = np.random.randint(0,num_agents) if cuckoo_fitness > fitness[j]: nest[j] = cuckoo.copy() fitness[j] = cuckoo_fitness nest, fitness = sort_agents(nest, obj, data) # eliminate worse nests and generate new ones nest = replace_worst(nest, p_a) nest, fitness = sort_agents(nest, obj, data) # update final information display(nest, fitness, agent_name) if fitness[0]>Leader_fitness: Leader_agent = nest[0].copy() Leader_fitness = fitness[0].copy() convergence_curve['fitness'][iter_no] = np.mean(fitness) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) nest, nest_accuracy = sort_agents(nest, compute_accuracy, data) print('\n================================================================================') print(' Final Result ') print('================================================================================\n') print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print('\n================================================================================\n') # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence graph fig, axes = Conv_plot(convergence_curve) if(save_conv_graph): plt.savefig('convergence_graph_'+ short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_population = nest solution.final_fitness = fitness solution.final_accuracy = nest_accuracy solution.execution_time = exec_time return solution
def CS(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_function_shape='s', save_conv_graph=False): # Cuckoo Search 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 # # # ############################################################################### short_name = 'CS' agent_name = 'Agent' num_features = train_data.shape[1] trans_function = get_trans_function(trans_function_shape) # initializing cuckoo and host nests levy_flight = np.random.uniform(low=-2, high=2, size=(num_features)) cuckoo = np.random.randint(low=0, high=2, size=(num_features)) nest = initialize(num_agents, num_features) nest_fitness = np.zeros(num_agents) nest_accuracy = np.zeros(num_agents) cuckoo_fitness = float("-inf") Leader_agent = np.zeros((num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") p_a = 0.25 # fraction of nests to be replaced # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) convergence_curve['feature_count'] = np.zeros(max_iter) data = Data() data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split( train_data, train_label, stratify=train_label, test_size=0.2) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial nests nest, nest_fitness = sort_agents(nest, obj_function, data) # start timer start_time = time.time() # main loop for iter_no in range(max_iter): print( '\n================================================================================' ) print(' Iteration - {}'.format(iter_no + 1)) print( '================================================================================\n' ) # updating leader nest if nest_fitness[0] > Leader_fitness: Leader_agent = nest[0].copy() Leader_fitness = nest_fitness[0] # get new cuckoo levy_flight = get_cuckoo(levy_flight) for j in range(num_features): if trans_function(levy_flight[j]) > np.random.random(): cuckoo[j] = 1 else: cuckoo[j] = 0 # check if a nest needs to be replaced j = np.random.randint(0, num_agents) if cuckoo_fitness > nest_fitness[j]: nest[j] = cuckoo.copy() nest_fitness[j] = cuckoo_fitness nest, nest_fitness = sort_agents(nest, obj_function, data) # eliminate worse nests and generate new ones nest = replace_worst(nest, p_a) nest, nest_fitness = sort_agents(nest, obj_function, data) # update final information display(nest, nest_fitness, agent_name) if nest_fitness[0] > Leader_fitness: Leader_agent = nest[0].copy() Leader_fitness = nest_fitness[0].copy() convergence_curve['fitness'][iter_no] = Leader_fitness convergence_curve['feature_count'][iter_no] = int(np.sum(Leader_agent)) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) nest, nest_accuracy = sort_agents(nest, compute_accuracy, data) print( '\n================================================================================' ) print( ' Final Result ' ) print( '================================================================================\n' ) print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print( '\n================================================================================\n' ) # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence curves iters = np.arange(max_iter) + 1 fig, axes = plt.subplots(2, 1) fig.tight_layout(pad=5) fig.suptitle('Convergence Curves') axes[0].set_title('Convergence of Fitness over Iterations') axes[0].set_xlabel('Iteration') axes[0].set_ylabel('Fitness') axes[0].plot(iters, convergence_curve['fitness']) axes[1].set_title('Convergence of Feature Count over Iterations') axes[1].set_xlabel('Iteration') axes[1].set_ylabel('Number of Selected Features') axes[1].plot(iters, convergence_curve['feature_count']) if (save_conv_graph): plt.savefig('convergence_graph_' + short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_population = nest solution.final_fitness = nest_fitness solution.final_accuracy = nest_accuracy solution.execution_time = exec_time return solution
def GA(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, prob_cross=0.4, prob_mut=0.3, save_conv_graph=False, seed=0): # Genetic Algorithm ############################### Parameters #################################### # # # num_agents: number of chromosomes # # 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 # # prob_cross: probability of crossover # # prob_mut: probability of mutation # # save_conv_graph: boolean value for saving convergence graph # # # ############################################################################### short_name = 'GA' agent_name = 'Chromosome' train_data, train_label = np.array(train_data), np.array(train_label) num_features = train_data.shape[1] cross_limit = 5 np.random.seed(seed) # setting up the objectives weight_acc = None if (obj_function == compute_fitness): weight_acc = float( input('Weight for the classification accuracy [0-1]: ')) obj = (obj_function, weight_acc) compute_accuracy = ( compute_fitness, 1 ) # compute_accuracy is just compute_fitness with accuracy weight as 1 # initialize chromosomes and Leader (the agent with the max fitness) chromosomes = initialize(num_agents, num_features) fitness = np.zeros(num_agents) accuracy = np.zeros(num_agents) Leader_agent = np.zeros((1, num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) # initialize data class data = Data() val_size = float( input('Enter the percentage of data wanted for valdiation [0, 100]: ') ) / 100 data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split( train_data, train_label, stratify=train_label, test_size=val_size) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial population chromosomes, fitness = sort_agents(chromosomes, obj, data) # start timer start_time = time.time() # main loop for iter_no in range(max_iter): print( '\n================================================================================' ) print(' Iteration - {}'.format(iter_no + 1)) print( '================================================================================\n' ) # perform crossover, mutation and replacement chromosomes, fitness = cross_mut(chromosomes, fitness, obj, data, prob_cross, cross_limit, prob_mut) # update final information chromosomes, fitness = sort_agents(chromosomes, obj, data, fitness) display(chromosomes, fitness, agent_name) if fitness[0] > Leader_fitness: Leader_agent = chromosomes[0].copy() Leader_fitness = fitness[0].copy() convergence_curve['fitness'][iter_no] = np.mean(fitness) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) chromosomes, accuracy = sort_agents(chromosomes, compute_accuracy, data) print( '\n================================================================================' ) print( ' Final Result ' ) print( '================================================================================\n' ) print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print( '\n================================================================================\n' ) # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence graph fig, axes = Conv_plot(convergence_curve) if (save_conv_graph): plt.savefig('convergence_graph_' + short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_population = chromosomes solution.final_fitness = fitness solution.final_accuracy = accuracy solution.execution_time = exec_time return solution
def GA(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, prob_cross=0.4, prob_mut=0.3, save_conv_graph=False): # Genetic Algorithm ############################### Parameters #################################### # # # num_agents: number of chromosomes # # 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 # # prob_cross: probability of crossover # # prob_mut: probability of mutation # # save_conv_graph: boolean value for saving convergence graph # # # ############################################################################### short_name = 'GA' agent_name = 'Chromosome' train_data, train_label = np.array(train_data), np.array(train_label) num_features = train_data.shape[1] cross_limit = 5 # initialize chromosomes and Leader (the agent with the max fitness) chromosomes = initialize(num_agents, num_features) fitness = np.zeros(num_agents) accuracy = np.zeros(num_agents) Leader_agent = np.zeros((1, num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) convergence_curve['feature_count'] = np.zeros(max_iter) # format the data data = Data() data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split(train_data, train_label, stratify=train_label, test_size=0.2) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial population chromosomes, fitness = sort_agents(chromosomes, obj_function, data) # start timer start_time = time.time() # main loop for iter_no in range(max_iter): print('\n================================================================================') print(' Iteration - {}'.format(iter_no+1)) print('================================================================================\n') # perform crossover, mutation and replacement cross_mut(chromosomes, fitness, obj_function, data, prob_cross, cross_limit, prob_mut) # update final information chromosomes, fitness = sort_agents(chromosomes, obj_function, data) display(chromosomes, fitness, agent_name) if fitness[0]>Leader_fitness: Leader_agent = chromosomes[0].copy() Leader_fitness = fitness[0].copy() convergence_curve['fitness'][iter_no] = Leader_fitness convergence_curve['feature_count'][iter_no] = int(np.sum(Leader_agent)) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) chromosomes, accuracy = sort_agents(chromosomes, compute_accuracy, data) print('\n================================================================================') print(' Final Result ') print('================================================================================\n') print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print('\n================================================================================\n') # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence curves iters = np.arange(max_iter)+1 fig, axes = plt.subplots(2, 1) fig.tight_layout(pad = 5) fig.suptitle('Convergence Curves') axes[0].set_title('Convergence of Fitness over Iterations') axes[0].set_xlabel('Iteration') axes[0].set_ylabel('Fitness') axes[0].plot(iters, convergence_curve['fitness']) axes[1].set_title('Convergence of Feature Count over Iterations') axes[1].set_xlabel('Iteration') axes[1].set_ylabel('Number of Selected Features') axes[1].plot(iters, convergence_curve['feature_count']) if(save_conv_graph): plt.savefig('convergence_graph_'+ short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_population = chromosomes solution.final_fitness = fitness solution.final_accuracy = accuracy solution.execution_time = exec_time return solution
def Name_of_the_wrapper(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_func_shape='s', save_conv_graph=False): # Name of the 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 # # # ############################################################################### short_name = '' agent_name = '' train_data, train_label = np.array(train_data), np.array(train_label) num_features = train_data.shape[1] trans_function = get_trans_function(trans_func_shape) # setting up the objectives weight_acc = None if (obj_function == compute_fitness): weight_acc = float( input('Weight for the classification accuracy [0-1]: ')) obj = (obj_function, weight_acc) compute_accuracy = ( compute_fitness, 1 ) # compute_accuracy is just compute_fitness with accuracy weight as 1 # initialize agents and Leader (the agent with the max fitness) agents = initialize(num_agents, num_features) fitness = np.zeros(num_agents) accuracy = np.zeros(num_agents) Leader_agent = np.zeros((1, num_features)) Leader_fitness = float("-inf") Leader_accuracy = float("-inf") # initialize convergence curves convergence_curve = {} convergence_curve['fitness'] = np.zeros(max_iter) convergence_curve['feature_count'] = np.zeros(max_iter) # format the data data = Data() data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split( train_data, train_label, stratify=train_label, test_size=0.2) # create a solution object solution = Solution() solution.num_agents = num_agents solution.max_iter = max_iter solution.num_features = num_features solution.obj_function = obj_function # rank initial agents agents, fitness = sort_agents(agents, obj, data) # start timer start_time = time.time() for iter_no in range(max_iter): print( '\n================================================================================' ) print(' Iteration - {}'.format(iter_no + 1)) print( '================================================================================\n' ) ################ write your main position update code here ################ ########################################################################### # update final information agents, fitness = sort_agents(agents, obj, data) display(agents, fitness, agent_name) # update Leader (best agent) if fitness[0] > Leader_fitness: Leader_agent = agents[0].copy() Leader_fitness = fitness[0].copy() convergence_curve['fitness'][iter_no] = Leader_fitness convergence_curve['feature_count'][iter_no] = int(np.sum(Leader_agent)) # compute final accuracy Leader_agent, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data) agents, accuracy = sort_agents(agents, compute_accuracy, data) print( '\n================================================================================' ) print( ' Final Result ' ) print( '================================================================================\n' ) print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent)))) print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness)) print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy)) print( '\n================================================================================\n' ) # stop timer end_time = time.time() exec_time = end_time - start_time # plot convergence graph fig, axes = Conv_plot(convergence_curve) if (save_conv_graph): plt.savefig('convergence_graph_' + short_name + '.jpg') plt.show() # update attributes of solution solution.best_agent = Leader_agent solution.best_fitness = Leader_fitness solution.best_accuracy = Leader_accuracy solution.convergence_curve = convergence_curve solution.final_agents = agents solution.final_fitness = fitness solution.final_accuracy = accuracy solution.execution_time = exec_time return solution