diff --git a/src/EasyGA.py b/src/EasyGA.py index c5fb804..2a3c25c 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -25,7 +25,7 @@ class GA: # Defualt EastGA implimentation structure self.gene_function_impl = random_gene # Set the GA Configuration - self.initialization_impl = random_initialization() + self.initialization_impl = random_initialization #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() #self.crossover_impl = FastSinglePointCrossover() @@ -35,7 +35,7 @@ class GA: def initialize(self): # Create the first population - self.population = self.initialization_impl.initialize( + self.population = self.initialization_impl( self.population_size, self.chromosome_length, self.gene_function_impl) diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index 0f20b3e..02fe0b4 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,22 +1,31 @@ class chromosome: # fitness = Empty, genes = [gene,gene,gene,etc] - def __init__(self): + def __init__(self, genes = []): + self.genes = genes self.fitness = None - self.genes = [] - def add_gene(self,gene): - self.genes.append(gene) + def add_gene(self, gene, index = -1): + if index == -1: + index = len(self.genes) - 1 + self.genes.insert(index, gene) + + def remove_gene(self, index): + del self.genes[index] + + def get_genes(self): + return self.genes def get_fitness(self): return self.fitness - def get_chromosome(self): - return self.genes + def set_gene(self, gene, index): + self.genes[index] = gene - def print_chromosome(self): - for i in range(len(self.genes)): - # Print the gene one by one. - if(i == len(self.genes) - 1): - print(f"[{self.genes[i].get_value()}]") - else: - print(f"[{self.genes[i].get_value()}],", end = '') + def set_genes(self, genes): + self.genes = genes + + def set_fitness(self, fitness): + self.fitness = fitness + + def __repr__(self): + return f"chromosome({self.genes.__repr__()})" diff --git a/src/initialization/gene_structure/gene.py b/src/initialization/gene_structure/gene.py index a956c9c..e309237 100644 --- a/src/initialization/gene_structure/gene.py +++ b/src/initialization/gene_structure/gene.py @@ -4,22 +4,21 @@ def check_gene(value): return value class gene: - # fitness = Empty, value = Define by gene function def __init__(self, value): self.fitness = None self.value = check_gene(value) - def set_fitness(self,fitness): - self.fitness = fitness - def get_fitness(self): return self.fitness def get_value(self): return self.value - def print_value(self): - print(self.value) + def set_fitness(self, fitness): + self.fitness = fitness - def print_fitness(self): - print(self.fitness) + def set_value(self): + self.value = value + + def __repr__(self): + return f"Gene({self.value} - Fitness{self.fitness})" diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index dc3068e..643a6a2 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -1,8 +1,43 @@ class population: # population = [chromosome,chromosome,etc] - def __init__(self): + def __init__(self, chromosomes = []): + self.chromosomes = chromosomes self.fitness = None - self.chromosomes = [] - - def add_chromosome(self,chromosome): - self.chromosomes.append(chromosome) + + def get_closet_fitness(self,value): + # Get the chomosome that has the closets fitness to the value defined + pass + + def add_chromosome(self, chromosome, index = -1): + if index == -1: + index = len(self.chromosomes) - 1 + self.chromosomes.insert(index, chromosome) + + def remove_chromosome(self, index): + del self.chromosomes[index] + + def get_all_chromosomes(self): + return chromosomes + + def get_fitness(self): + return self.fitness + + def set_all_chromosomes(self, chromosomes): + self.chromosomes = chromosomes + + def set_chromosome(self, chromosomes, index): + self.chromosome[index] = chromosome + + def set_fitness(self, fitness): + self.fitness = fitness + + def __repr__(self): + return f"population({self.chromosomes.__repr__()})" + + def generate_first_chromosomes(self, chromosome_count, chromosome_length, gene_lower_bound, gene_upper_bound): + #Creating the chromosomes with Genes of random size + for x in range(chromosome_count): + chromosome = Chromosome(chromosome_length) + for y in range(chromosome_length): + chromosome.gene_set[y] = Gene(random.randint(gene_lower_bound[y], gene_upper_bound[y])) + self.chromosome_set.append(chromosome) diff --git a/src/initialization/population_structure/test_population.py b/src/initialization/population_structure/test_population.py index b187bb7..e69de29 100644 --- a/src/initialization/population_structure/test_population.py +++ b/src/initialization/population_structure/test_population.py @@ -1,14 +0,0 @@ -from population import population -from chromosome import chromosome -from gene import gene - -population = population() -# Fill the population with chromosomes -for i in range(population_size): - chromosome = chromosome() - #Fill the Chromosome with genes - for j in range(chromosome_length): - gene = gene(gene_function) - chromosome.add_gene(gene) - - population.add_chromosome(chromosome) diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 40da542..aeb78d5 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -3,19 +3,18 @@ from .population_structure.population import population as create_population from .chromosome_structure.chromosome import chromosome as create_chromosome from .gene_structure.gene import gene as create_gene -class random_initialization: - def initialize(self,chromosome_length,population_size,gene_function): - # I dont understand why python needs this in its scope but it does. - global population - global chromosome - global gene - # Create the population object - population = create_population() - # Fill the population with chromosomes - for i in range(population_size): - chromosome = create_chromosome() - #Fill the Chromosome with genes - for j in range(chromosome_length): - chromosome.add_gene(create_gene(gene_function())) - population.add_chromosome(chromosome) - return population +def random_initialization(chromosome_length,population_size,gene_function): + # I dont understand why python needs this in its scope but it does. + global population + global chromosome + global gene + # Create the population object + population = create_population() + # Fill the population with chromosomes + for i in range(population_size): + chromosome = create_chromosome() + #Fill the Chromosome with genes + for j in range(chromosome_length): + chromosome.add_gene(create_gene(gene_function())) + population.add_chromosome(chromosome) + return population diff --git a/src/run_testing.py b/src/run_testing.py index 7d9db70..63107b6 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -11,6 +11,7 @@ new_chromosome = ga.make_chromosome() # Makes a Population to store chromosomes in new_population = ga.make_population() +# Creating population ga.initialize() print(ga.population)