From df32eb47a33f858bd39d84fa3d07594a0ca91a41 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:19:13 -0400 Subject: [PATCH 1/5] Added comments to the initilization function --- src/initialization/random_initialization.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 6484e43..7c4a95d 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -4,6 +4,8 @@ from .chromosome_structure.chromosome import chromosome as create_chromosome from .gene_structure.gene import gene as create_gene def random_initialization(population_size, chromosome_length, chromosome_impl, gene_impl): + """Takes the initialization inputs and choregraphs them to output the type of population + with the given parameters.""" # Create the population object population = create_population() # Fill the population with chromosomes @@ -14,7 +16,7 @@ def random_initialization(population_size, chromosome_length, chromosome_impl, g if chromosome_impl != None: # Each chromosome location is specified with its own function chromosome.add_gene(create_gene(chromosome_impl(j))) - # Will break if chromosome_length != lists in domain + # Will break if chromosome_length != len(lists) in domain elif gene_impl != None: # gene_impl = [range function,lowerbound,upperbound] function = gene_impl[0] From e66b4d7fd0e2ce2065be4d0491198619fe24f683 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:26:56 -0400 Subject: [PATCH 2/5] Commented EasyGA.py --- src/EasyGA.py | 21 ++++++++++++++++----- src/run_testing.py | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index fabbd10..483ecd8 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,4 +1,5 @@ import random + # Import all the data prebuilt modules from initialization.population_structure.population import population as create_population from initialization.chromosome_structure.chromosome import chromosome as create_chromosome @@ -7,9 +8,10 @@ from initialization.gene_structure.gene import gene as create_gene # Import functionality defaults from initialization.random_initialization import random_initialization - class GA: def __init__(self): + """Initialize the GA.""" + # Default variables self.chromosome_impl = None self.gene_impl = None @@ -19,6 +21,7 @@ class GA: self.chromosome_length = 3 self.population_size = 5 self.mutation_rate = 0.03 + # Defualt EastGA implimentation structure self.initialization_impl = random_initialization self.update_fitness = True @@ -28,7 +31,8 @@ class GA: #self.termination_impl = GenerationTermination(Total_generations) #self.evaluation_impl = TestEvaluation() - def initialize(self): + def initialize_population(self): + """Initialize the population""" self.population = self.initialization_impl( self.population_size, self.chromosome_length, @@ -43,7 +47,11 @@ class GA: """Updates the ga to the next generation. If update_fitness is set then all fitness values are updated. Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated.""" + + # for each chromosome in the population for chromosome in self.population.get_all_chromosomes(): + + # if the fitness should be updated, update it if self.update_fitness or chromosome.get_fitness() is None: chromosome.set_fitness(self.fitness_impl(chromosome)) @@ -52,15 +60,18 @@ class GA: return self.current_generation < self.generations def evolve_generation(self, number_of_generations): - # If you want to evolve through a number of generations - # and be able to pause and output data based on that generation run. - pass + """Evolves the ga the specified number of generations.""" + for n in range(number_of_generations): + self.evolve() def make_gene(self,value): + """Let's the user create a gene.""" return create_gene(value) def make_chromosome(self): + """Let's the user create a chromosome.""" return create_chromosome() def make_population(self): + """Let's the user create a population.""" return create_population() diff --git a/src/run_testing.py b/src/run_testing.py index e05ac52..3ff4fc7 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -15,6 +15,6 @@ def user_gene_domain(gene_index): # If the user wants to use a domain ga.chromosome_impl = user_gene_domain -ga.initialize() +ga.initialize_population() ga.population.print_all() From 760ec152644d9297f508305ad2bbf88fb47af7e7 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:36:59 -0400 Subject: [PATCH 3/5] Added comments --- src/initialization/random_initialization.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 7c4a95d..2bd50ad 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -13,6 +13,7 @@ def random_initialization(population_size, chromosome_length, chromosome_impl, g chromosome = create_chromosome() #Fill the Chromosome with genes for j in range(chromosome_length): + # Using the chromosome_impl to set every index inside of the chromosome if chromosome_impl != None: # Each chromosome location is specified with its own function chromosome.add_gene(create_gene(chromosome_impl(j))) From 4b21dc45f6270b7bceeaf4134e5b72165d6db9e6 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:42:41 -0400 Subject: [PATCH 4/5] Update EasyGA.py --- src/EasyGA.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 483ecd8..946b18d 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -44,25 +44,32 @@ class GA: pass def evolve(self): - """Updates the ga to the next generation. - If update_fitness is set then all fitness values are updated. - Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated.""" + """Runs the ga until the ga is no longer active.""" - # for each chromosome in the population - for chromosome in self.population.get_all_chromosomes(): - - # if the fitness should be updated, update it - if self.update_fitness or chromosome.get_fitness() is None: - chromosome.set_fitness(self.fitness_impl(chromosome)) + # run one iteration while the ga is active + while self.active(): + self.evolve_generation(1) def active(self): """Returns if the ga should terminate or not""" return self.current_generation < self.generations def evolve_generation(self, number_of_generations): - """Evolves the ga the specified number of generations.""" + """Evolves the ga the specified number of generations. + If update_fitness is set then all fitness values are updated. + Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated.""" + + # run the specified number of times for n in range(number_of_generations): - self.evolve() + + # for each chromosome in the population + for chromosome in self.population.get_all_chromosomes(): + + # if the fitness should be updated, update it + if self.update_fitness or chromosome.get_fitness() is None: + chromosome.set_fitness(self.fitness_impl(chromosome)) + + # apply selection, crossover, and mutation def make_gene(self,value): """Let's the user create a gene.""" From a42d5970caf65f16328330826a236774c7aba6b0 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:46:40 -0400 Subject: [PATCH 5/5] Update README.md --- README.md | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/README.md b/README.md index a6b9de2..4624399 100644 --- a/README.md +++ b/README.md @@ -25,37 +25,7 @@ ga.evolve() Put the out here ``` -## Customized: -```python -import random -import EasyGA -# Create the Genetic algorithm -ga = EasyGA.GA() - -# Makes a new gene -new_gene = ga.make_gene("HelloWorld") -# Makes a chromosome to store genes in -new_chromosome = ga.make_chromosome() -# Makes a Population to store chromosomes in -new_population = ga.make_population() - -ga.initialize() - -print(ga.population) - -for chromosome in ga.population.chromosomes: - print(chromosome.genes[0].__dict__) -``` - -### Output: -```python - -{'fitness': None, 'value': 47} -{'fitness': None, 'value': 4} -{'fitness': None, 'value': 68} -{'fitness': None, 'value': 57} -``` # How Testing works