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] 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()