From 99542a76631c1b6dee769b5552ac9a39e698d884 Mon Sep 17 00:00:00 2001 From: Felipe Terrana Date: Mon, 21 May 2018 20:44:41 -0300 Subject: [PATCH 1/2] Adicionar classes Individual, Population e SingletonPrint --- ppa/individual.py | 23 +++++++++++++++++++++++ ppa/population.py | 25 +++++++++++++++++++++++++ ppa/singleton_print.py | 21 +++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 ppa/individual.py create mode 100644 ppa/population.py create mode 100644 ppa/singleton_print.py diff --git a/ppa/individual.py b/ppa/individual.py new file mode 100644 index 0000000..ea14994 --- /dev/null +++ b/ppa/individual.py @@ -0,0 +1,23 @@ +import random + +# TODO Mudar (talvez) representação de vetor de inteiros pra um único inteiro em binário + + +class Individual: + def __init__(self, paramId, size): + self.size = size + self.id = paramId + self.prey = [0] * self.size + + self.survival_value = 0.0 + + def generate_random_prey(self): + self.prey.clear() + for i in range(self.size): + self.prey.append(random.randint(0, 1)) + + def __str__(self): + returned = '' + for position in self.prey: + returned += str(position) + ' ' + return returned + '\nSurvival value: ' + str(self.survival_value) + '\n' diff --git a/ppa/population.py b/ppa/population.py new file mode 100644 index 0000000..96154e2 --- /dev/null +++ b/ppa/population.py @@ -0,0 +1,25 @@ +# TODO Mudar (talvez) representação de vetor de inteiros pra um único inteiro em binário + + +class Population: + def __init__(self, individuals=None): + self.individuals = individuals + + self.size_population = 0 + self.best_preys_id = None + self.predator_id = 0 + self.ordinary_preys_ids = None + + def __str__(self): + returned = '' + for individual in self.individuals: + returned += 'ID = ' + str(individual.id) + '\n' + for position in individual.prey: + returned += str(position) + ' ' + returned += '\n' + returned += 'Survival value = ' + str(individual.survival_value) + '\n' + returned += '\nBest prey = ' + str(self.best_preys_id) + '\n' + returned += 'Predator = ' + str(self.predator_id) + '\n' + returned += 'Ordinary preys = ' + str(self.ordinary_preys_ids) + '\n' + + return returned diff --git a/ppa/singleton_print.py b/ppa/singleton_print.py new file mode 100644 index 0000000..2164b5f --- /dev/null +++ b/ppa/singleton_print.py @@ -0,0 +1,21 @@ +class SingletonPrint: + class __SingletonPrint: + def __init__(self): + pass + + __instance = None + __out = [] + + def __init__(self): + if not SingletonPrint.__instance: + SingletonPrint.__instance = SingletonPrint.__SingletonPrint() + + def add_string(self, text): + self.__out.append(text) + + def out(self): + for text in self.__out: + print(text, end='') + + def free(self): + self.__out.clear() From 5ed0bf633e6f888e0856817a33b54b05d7e4b566 Mon Sep 17 00:00:00 2001 From: felipe Date: Tue, 12 Jun 2018 20:01:50 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Adicionar=20in=C3=ADcio=20do=20algoritmo=20?= =?UTF-8?q?gen=C3=A9tico?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ppa/config.py | 13 +++++++++++++ ppa/ga.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 ppa/ga.py diff --git a/ppa/config.py b/ppa/config.py index 1a6aec7..393c489 100644 --- a/ppa/config.py +++ b/ppa/config.py @@ -17,3 +17,16 @@ def get_config(): config['follow_chance'] = 0.8 return config + + +def get_config_ga(): + config = {} + + config['population_size'] = 20 + config['num_generations'] = 100 + + config['top_selection_ratio'] = 0.2 + config['bottom_selection_ratio'] = 0.1 + config['mutation_chance'] = 0.01 + + return config diff --git a/ppa/ga.py b/ppa/ga.py new file mode 100644 index 0000000..e472121 --- /dev/null +++ b/ppa/ga.py @@ -0,0 +1,49 @@ +import numpy as np +import random + +from objective import fitness, fitness_population +from config import get_config_ga +from instance import Instance, print_instance + + +# TODO(felipe:2018-06-04): Adicionar outros métodos de crossover além do two-point +def crossover(individuals): + cut_point1 = random.randint(0, individuals[0].size - 1) + cut_point2 = random.randint(0, individuals[0].size - 1) + + while cut_point2 == cut_point1: + if individuals[0].size == 1: + break + + cut_point2 = random.randint(0, individuals[0].size - 1) + + if cut_point1 > cut_point2: + cut_point1, cut_point2 = cut_point2, cut_point1 + + new_individual = np.empty(individuals[0]) + + for i in range(0, cut_point1): + new_individual[i] = individuals[0] + + for i in range(cut_point1, cut_point2): + new_individual[i] = individuals[1] + + for i in range(cut_point2, individuals[0].size): + new_individual[i] = individuals[0] + + return new_individual + + +instance_test = Instance.load_from_file("../instance_files/config.txt") +print_instance(instance_test) +print("") +config = get_config_ga() + +population_size = config['population_size'] + +population = np.random.randint(2, size=(population_size, instance_test.num_materials), dtype=bool) + +selection_quant = 2 + +for generation in range(config['num_generations']): + pass