def __init__(self, algorithm_configuration):
     self.__algorithm_configuration = algorithm_configuration
     self.__population_generator = PopulationGenerator(self.__algorithm_configuration)
     self.__function = Function(self.__algorithm_configuration)
     self.__elite_strategy = EliteStrategy(self.__algorithm_configuration)
     self.__selection_service = SelectionService(self.__algorithm_configuration)
     self.__cross_service = CrossService(self.__algorithm_configuration)
     self.__mutation_service = MutationService(self.__algorithm_configuration)
     self.__inversion_service = InversionService(self.__algorithm_configuration)
     self.__chromosome_decoder = ChromosomeDecoder(self.__algorithm_configuration)
Ejemplo n.º 2
0
class TournamentSelection:
    def __init__(self, algorithm_configuration):
        self.__algorithm_configuration = algorithm_configuration
        self.__group_members_amount = self.__algorithm_configuration.selection_amount
        self.__function = Function(self.__algorithm_configuration)

    def handle_selection(self, population):
        np.random.shuffle(population)
        groups = self.__split_into_groups(population,
                                          self.__group_members_amount)
        best_chromosomes = []
        [
            best_chromosomes.append(
                self.__function.get_best_chromosome_for_population(group)[0])
            for group in groups
        ]

        return np.array(best_chromosomes)

    @staticmethod
    def __split_into_groups(population, members_amount):
        groups = [
            population[i * members_amount:(i + 1) * members_amount]
            for i in range((len(population) + members_amount - 1) //
                           members_amount)
        ]

        return groups
class BestStrategyBase:

    def __init__(self, algorithm_configuration, number_of_best_chromosomes):
        self.__number_of_best_chromosomes = number_of_best_chromosomes
        self.__algorithm_configuration = algorithm_configuration
        self.__function = Function(self.__algorithm_configuration)

    def get_best_chromosomes(self, population):
        if self.__number_of_best_chromosomes is 0:
            return [], []

        evaluated_population = self.__function.evaluate_population(population)
        sorted_evaluated_population = evaluated_population.argsort()
        optimization_evaluated_population = \
            self.__get_sorted_evaluated_population_for_optimization(sorted_evaluated_population)

        best_indexes = optimization_evaluated_population[-self.__number_of_best_chromosomes:]
        best_chromosomes = np.take(population, best_indexes, 0)
        new_population_to_evaluate = np.delete(population, best_indexes, 0)

        return best_chromosomes, new_population_to_evaluate

    def __get_sorted_evaluated_population_for_optimization(self, evaluated_population):
        if self.__algorithm_configuration.is_maximization:
            return evaluated_population

        return evaluated_population[::-1]
class RouletteWheelSelection:

    def __init__(self, algorithm_configuration):
        self.__algorithm_configuration = algorithm_configuration
        self.__function = Function(self.__algorithm_configuration)
        self.__is_max_opt = self.__algorithm_configuration.is_maximization

    def get_population(self, old_pop):
        old_pop = np.sort(old_pop)
        eval_pop = self.__function.evaluate_population(old_pop)
        if not self.__is_max_opt:
            eval_pop = 1/eval_pop
        corrected_eval_pop = eval_pop + abs(eval_pop.min())
        wheel = np.cumsum(corrected_eval_pop)
        new_pop = []
        for el in np.random.rand(self.__algorithm_configuration.selection_amount):
            new_pop.append(old_pop[(wheel > el * wheel.max())][0])
        return np.array(new_pop)
Ejemplo n.º 5
0
 def __init__(self, algorithm_configuration):
     self.__algorithm_configuration = algorithm_configuration
     self.__group_members_amount = self.__algorithm_configuration.selection_amount
     self.__function = Function(self.__algorithm_configuration)
 def __init__(self, algorithm_configuration, number_of_best_chromosomes):
     self.__number_of_best_chromosomes = number_of_best_chromosomes
     self.__algorithm_configuration = algorithm_configuration
     self.__function = Function(self.__algorithm_configuration)
Ejemplo n.º 7
0
from libs.config.algorithm_configuration_provider import AlgorithmConfigurationProvider
from libs.generator.population_generator import PopulationGenerator
from libs.algorithm.function import Function
from libs.algorithm.genetic_algorithm import GeneticAlgorithm
from libs.plot.plot_drawer import PlotDrawer

algorithm_configuration = AlgorithmConfigurationProvider(2, 3, 3, 8, 10, True)
population_generator = PopulationGenerator(algorithm_configuration)
population = population_generator.generate_population()
#print(population)
#
#print(chromosome_decoder.decode_chromosome(population[0]))
#print(chromosome_decoder.decode_chromosome(population[1]))
#print(chromosome_decoder.decode_chromosome(population[2]))

function = Function(algorithm_configuration)

#print(function.evaluate_population(population))
#print(function.get_best_chromosome_for_population(population))

#elite_strategy = EliteStrategy(algorithm_configuration, 1)
#print(elite_strategy.get_best_chromosomes(population))

#best_strategy = BestStrategy(algorithm_configuration, 10)
#print(best_strategy.get_best_chromosomes(population))

#tournament_selection = TournamentSelection(algorithm_configuration, 2)
#print(tournament_selection.handle_selection(population))

genetic_algorithm = GeneticAlgorithm(algorithm_configuration)
#print(genetic_algorithm.evolve())
 def __init__(self, algorithm_configuration):
     self.__algorithm_configuration = algorithm_configuration
     self.__function = Function(self.__algorithm_configuration)
     self.__is_max_opt = self.__algorithm_configuration.is_maximization
class GeneticAlgorithm:

    def __init__(self, algorithm_configuration):
        self.__algorithm_configuration = algorithm_configuration
        self.__population_generator = PopulationGenerator(self.__algorithm_configuration)
        self.__function = Function(self.__algorithm_configuration)
        self.__elite_strategy = EliteStrategy(self.__algorithm_configuration)
        self.__selection_service = SelectionService(self.__algorithm_configuration)
        self.__cross_service = CrossService(self.__algorithm_configuration)
        self.__mutation_service = MutationService(self.__algorithm_configuration)
        self.__inversion_service = InversionService(self.__algorithm_configuration)
        self.__chromosome_decoder = ChromosomeDecoder(self.__algorithm_configuration)

    def evolve(self):
        population = self.__population_generator.generate_population()

        solution_best_value = self.__initialize_solution_best_value()
        solution_best_chromosome = []
        list_best = []
        list_mean = []
        list_std = []

        start = time.time()
        for i in range(self.__algorithm_configuration.epochs_number):
            best_chromosomes, new_population_to_evaluate = self.__elite_strategy.get_best_chromosomes(population)
            population = self.__selection_service.handle_selection(new_population_to_evaluate)
            population = self.__cross_service.handle_cross(population)
            population = self.__mutation_service.handle_mut(population)
            population = self.__inversion_service.handle_inv(population)

            population = np.concatenate((population, best_chromosomes), axis=0)

            evaluated_population = self.__function.evaluate_population(population)
            current_best_chromosome, current_best_chromosome_function_value = \
                self.__function.get_best_chromosome_for_population(population)

            solution_best_chromosome, solution_best_value = \
                self.__update_solution_best_value(solution_best_value, current_best_chromosome_function_value,
                                                  solution_best_chromosome, current_best_chromosome)

            list_best.append(current_best_chromosome_function_value)
            list_mean.append(np.mean(evaluated_population))
            list_std.append(np.std(evaluated_population))

        end = time.time()
        elapsed_time = end - start
        decoded_best_chromosome = self.__chromosome_decoder.decode_chromosome(solution_best_chromosome)

        return decoded_best_chromosome, solution_best_value, list_best, list_mean, list_std, elapsed_time

    def __update_solution_best_value(self, solution_best_value, current_best_chromosome_function_value,
                                     solution_best_chromosome, current_best_chromosome):
        if self.__algorithm_configuration.is_maximization:
            if solution_best_value < current_best_chromosome_function_value:
                solution_best_chromosome = current_best_chromosome
                return solution_best_chromosome, current_best_chromosome_function_value
        else:
            if solution_best_value > current_best_chromosome_function_value:
                solution_best_chromosome = current_best_chromosome
                solution_best_value = current_best_chromosome_function_value

        return solution_best_chromosome, solution_best_value

    def __initialize_solution_best_value(self):
        if self.__algorithm_configuration.is_maximization:
            return -float("inf")

        return float("inf")