From 994bdb164cde891a526748fb0502cdcc16a0c5a5 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Wed, 23 Sep 2020 21:58:48 -0400 Subject: [PATCH 1/4] Fixed all jacks code --- src/EasyGA.py | 4 +- .../chromosome_structure/chromosome.py | 35 +++++++++------ src/initialization/gene_structure/gene.py | 15 +++---- .../population_structure/population.py | 45 ++++++++++++++++--- .../population_structure/test_population.py | 14 ------ src/initialization/random_initialization.py | 31 +++++++------ src/run_testing.py | 1 + 7 files changed, 87 insertions(+), 58 deletions(-) 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) From 9c5092525aa9029e3af5515185e8379a8fca8b69 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Wed, 23 Sep 2020 22:12:42 -0400 Subject: [PATCH 2/4] Fixed file and everything --- src/EasyGA.py | 2 +- .../{gene_creation => gene_function}/gene_random.py | 0 src/initialization/random_initialization.py | 4 ---- 3 files changed, 1 insertion(+), 5 deletions(-) rename src/initialization/{gene_creation => gene_function}/gene_random.py (100%) diff --git a/src/EasyGA.py b/src/EasyGA.py index 2a3c25c..6967527 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -4,7 +4,7 @@ from initialization.chromosome_structure.chromosome import chromosome as create_ from initialization.gene_structure.gene import gene as create_gene # Import functions for defaults -from initialization.gene_creation.gene_random import random_gene +from initialization.gene_function.gene_random import random_gene # Import functionality defaults from initialization.random_initialization import random_initialization diff --git a/src/initialization/gene_creation/gene_random.py b/src/initialization/gene_function/gene_random.py similarity index 100% rename from src/initialization/gene_creation/gene_random.py rename to src/initialization/gene_function/gene_random.py diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index aeb78d5..cdef3ef 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -4,10 +4,6 @@ from .chromosome_structure.chromosome import chromosome as create_chromosome from .gene_structure.gene import gene as create_gene 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 From c4ead43d6d185e457a4e14a2ea5ba57b44c53788 Mon Sep 17 00:00:00 2001 From: Daniel Wilczak Date: Thu, 24 Sep 2020 15:02:58 -0400 Subject: [PATCH 3/4] Updated genes,chromosme,population prints --- src/initialization/chromosome_structure/chromosome.py | 5 ++++- src/initialization/gene_structure/gene.py | 2 +- src/initialization/population_structure/population.py | 10 +++++++++- src/run_testing.py | 7 +++---- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index 02fe0b4..1880f54 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -28,4 +28,7 @@ class chromosome: self.fitness = fitness def __repr__(self): - return f"chromosome({self.genes.__repr__()})" + output_str = '' + for gene in self.genes: + output_str += gene.__repr__() + return output_str diff --git a/src/initialization/gene_structure/gene.py b/src/initialization/gene_structure/gene.py index e309237..ea3b547 100644 --- a/src/initialization/gene_structure/gene.py +++ b/src/initialization/gene_structure/gene.py @@ -21,4 +21,4 @@ class gene: self.value = value def __repr__(self): - return f"Gene({self.value} - Fitness{self.fitness})" + return f'[{self.value}]' diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index 643a6a2..fd80c28 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -32,7 +32,15 @@ class population: self.fitness = fitness def __repr__(self): - return f"population({self.chromosomes.__repr__()})" + pass + + def print_all(self): + # Ex .Current population + # Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - # + print("Current population:") + for index in range(len(self.chromosomes)): + print(f'Chromosome - {index} {self.chromosomes[index]}', end = "") + print(f' / Fitness = {self.chromosomes[index].fitness}') def generate_first_chromosomes(self, chromosome_count, chromosome_length, gene_lower_bound, gene_upper_bound): #Creating the chromosomes with Genes of random size diff --git a/src/run_testing.py b/src/run_testing.py index 63107b6..1d5b785 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -14,7 +14,6 @@ new_population = ga.make_population() # Creating population ga.initialize() -print(ga.population) - -for chromosome in ga.population.chromosomes: - print(chromosome.genes[0].__dict__) +ga.population.print_all() +print("") +print(ga.population.chromosomes[0].__repr__()) From 45638ad4eb295cb95918b78b3447ecf3fedc5532 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 24 Sep 2020 18:13:44 -0400 Subject: [PATCH 4/4] Fixed data structures Fixed constructors with default arguments as well as the adders with default arguments. --- .../chromosome_structure/chromosome.py | 12 ++++++++---- .../population_structure/population.py | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index 1880f54..fecc278 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,12 +1,16 @@ class chromosome: - # fitness = Empty, genes = [gene,gene,gene,etc] - def __init__(self, genes = []): - self.genes = genes + + # fitness = Empty; genes = [gene, gene, gene, etc.] + def __init__(self, genes = None): + if genes is None: + self.genes = [] + else: + self.genes = genes self.fitness = None def add_gene(self, gene, index = -1): if index == -1: - index = len(self.genes) - 1 + index = len(self.genes) self.genes.insert(index, gene) def remove_gene(self, index): diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index fd80c28..7973218 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -1,7 +1,11 @@ class population: - # population = [chromosome,chromosome,etc] - def __init__(self, chromosomes = []): - self.chromosomes = chromosomes + + # fitness = Empty; population = [chromosome, chromosome, etc.] + def __init__(self, chromosomes = None): + if chromosomes is None: + self.chromosomes = [] + else: + self.chromosomes = chromosomes self.fitness = None def get_closet_fitness(self,value): @@ -10,7 +14,7 @@ class population: def add_chromosome(self, chromosome, index = -1): if index == -1: - index = len(self.chromosomes) - 1 + index = len(self.chromosomes) self.chromosomes.insert(index, chromosome) def remove_chromosome(self, index):