Ejemplo n.º 1
0
    def initialize(self):
        # set the objective function
        self.default_vals["val_size"] = 30
        self.default_vals["weight_acc"] = 0.9

        if self.default_mode:
            self.val_size = self.default_vals["val_size"]
            self.weight_acc = self.default_vals["weight_acc"]
        else:
            self.val_size = float(input(f'Percentage of data for valdiation [0-100] (default={self.default_vals["val_size"]}): ') or self.default_vals["val_size"])/100
            self.weight_acc = float(input(f'Weight for the classification accuracy [0-1] (default={self.default_vals["weight_acc"]}): ') or self.default_vals["weight_acc"])
        
        self.obj_function = call_counter(compute_fitness(self.weight_acc))

        # start timer
        self.start_time = time.time()
        np.random.seed(self.seed)

        # data preparation
        self.training_data = Data()
        self.train_data, self.train_label = np.array(self.train_data), np.array(self.train_label)
        self.train_label = self.int_encoding(self.train_label)
        self.training_data.train_X, self.training_data.val_X, self.training_data.train_Y, self.training_data.val_Y = train_test_split(self.train_data, self.train_label, stratify=self.train_label, test_size=self.val_size)

        # create initial population
        self.num_features = self.train_data.shape[1]
        self.population = initialize(num_agents=self.num_agents, num_features=self.num_features, seed=self.seed)
        self.fitness = self.obj_function(self.population, self.training_data)
        self.population, self.fitness = sort_agents(agents=self.population, fitness=self.fitness)
        self.accuracy = compute_accuracy(agents=self.population, data=self.training_data)
        self.Leader_agent, self.Leader_fitness = self.population[0], self.fitness[0]
Ejemplo n.º 2
0
 def initialize(self):
     #call the base class function
     super().initialize()
     
     # initialize position and velocities of male and female mayflies' and Leader (the agent with the max fitness)
     self.male_pos = initialize(self.num_agents, self.num_features)
     self.female_pos = initialize(self.num_agents, self.num_features)
     self.male_vel = np.random.uniform(low = -1, high = 1, size = (self.num_agents, self.num_features))
     self.female_vel = np.random.uniform(low = -1, high = 1, size = (self.num_agents, self.num_features))
     self.male_fitness = np.zeros(self.num_agents)
     self.male_accuracy = np.zeros(self.num_agents)
     self.female_fitness = np.zeros(self.num_agents)
     self.male_personal_best = np.zeros((self.num_agents, self.num_features))
     self.male_personal_best_fit = np.zeros(self.num_agents)
     self.male_offspring = np.zeros((self.num_agents, self.num_features))
     self.female_offspring = np.zeros((self.num_agents, self.num_features))
     self.vmax_male = np.zeros(self.num_features)
     self.vmax_female = np.zeros(self.num_features)
     
     # rank initial population
     self.male_fitness = self.obj_function(self.male_pos,self.training_data)
     self.female_fitness = self.obj_function(self.female_pos,self.training_data)
     self.male_pos, self.male_fitness = sort_agents(self.male_pos,self.male_fitness)
     self.female_pos, self.female_fitness = sort_agents(self.female_pos,self.female_fitness)
     
     # create initial population
     self.population = self.male_pos
     self.fitness = self.male_fitness
     self.population, self.fitness = sort_agents(agents=self.population, fitness=self.fitness)
     self.accuracy = compute_accuracy(agents=self.population, data=self.training_data)
     self.Leader_agent, self.Leader_fitness = self.population[0], self.fitness[0]
Ejemplo n.º 3
0
 def post_processing(self):
     # post processing steps
     self.fitness = self.obj_function(self.population, self.training_data)
     self.population, self.fitness = sort_agents(agents=self.population, fitness=self.fitness)
     self.accuracy = compute_accuracy(agents=self.population, data=self.training_data)
     
     if(self.fitness[0] > self.Leader_fitness):
         self.Leader_fitness = self.fitness[0]
         self.Leader_agent = self.population[0, :]
         self.Leader_accuracy = self.accuracy[0]
Ejemplo n.º 4
0
    def run(self):
        # the main algorithm run
        if not self.default_mode:
            self.print(
                '\n************    Please enter the values of the following paramters or press newline for using default values    ************\n'
            )
        self.user_input()  # take the user inputs
        self.initialize()  # initialize the algorithm
        self.save_details()  # save the initial details

        if not self.default_mode:
            self.print(
                '\n*****************************************************    Thank You    ******************************************************\n'
            )

        while (not self.check_end()):  # while the end criterion is not met
            self.next()  # do one step of the algorithm
            self.post_processing()  # do the post processing steps
            self.display()  # display the details of 1 iteration
            self.save_details()  # save the details

        self.end_time = time.time()
        self.exec_time = self.end_time - self.start_time

        if self.test_data:  # if there is a test data, test the final solution on that
            self.test_label = self.int_encoding(self.test_label)
            temp_data = Data()
            temp_data.train_X = self.train_data
            temp_data.train_Y = self.train_label
            temp_data.val_X = self.test_data
            temp_data.val_Y = self.test_label

            self.Leader_fitness = compute_fitness(self.Leader_agent, temp_data)
            self.Leader_accuracy = compute_accuracy(self.Leader_agent,
                                                    temp_data)

        self.save_solution()

        if (self.save_conv_graph):
            fig = self.plot()
            fig.savefig('convergence_curve_' + self.algo_name + '.jpg')

        self.print('\n------------- Leader Agent ---------------')
        self.print('Fitness: {}'.format(self.Leader_fitness))
        self.print('Number of Features: {}'.format(
            int(np.sum(self.Leader_agent))))
        self.print('----------------------------------------\n')

        return self