示例#1
0
    def run(self):
        time_start = time.time()
        next_generation_individuals = np.array([False])
        for generation in range(self.epochs_num):
            new_population = Population(self.chromosome_type, self.population_size, self.chromosomes_number,
                                        self.range_start, self.range_end, self.accuracy, self.fitness_function,
                                        self.searching_value, self.crossover_type, self.crossover_prob,
                                        self.elite_strategy_num,
                                        next_generation_individuals)
            self.population_set.add(new_population)
            if self.elite_strategy_num > 0:
                self.best_individuals = self.elite_strategy(new_population.best_individuals)
            new_population.select_individuals(self.selection_type, self.selection_args)
            new_individuals = new_population.crossover_selected_individuals()
            if self.mutation_prob > 0.0:
                Population.mutate_individuals(new_individuals, self.mutation_type, self.mutation_prob)
            if self.inversion_prob > 0.0:
                Population.inverse_individuals(new_individuals, self.inversion_prob)
            if self.elite_strategy_num > 0:
                next_generation_individuals = np.append(new_individuals, self.best_individuals)
            else:
                next_generation_individuals = new_individuals
            self.fill_values_for_charts(next_generation_individuals, generation)

        best = Population.get_n_best_individuals(1, self.searching_value, next_generation_individuals,
                                                 self.fitness_function)
        self.best_individual = best[0]
        time_end = time.time()
        self.time = time_end - time_start

        print("Final best:")
        print(best[0].get_decimal_value_of_chromosomes())
        print("value:")
        print(best[0].evaluate(self.fitness_function))
        print("evolution time: ")
        print(self.time)
        print("BESTS")
        print(self.bests_values)
        print("MEAN")
        print(self.mean_values)
        print("STD")
        print(self.sd_values)
示例#2
0
 def elite_strategy(self, new_best_candidates):
     individuals = np.asarray(list(set(np.append(self.best_individuals, new_best_candidates))))
     return Population.get_n_best_individuals(self.elite_strategy_num, self.searching_value, individuals,
                                              self.fitness_function)