Implemented the always get fitness = True / False feature

This commit is contained in:
danielwilczak101
2020-10-01 00:43:43 -04:00
parent 42f49c43ee
commit 7ed6e55e4c

View File

@ -28,6 +28,9 @@ class GA:
# Mutation variables
self.mutation_rate = 0.03
# Rerun already computed fitness
self.update_fitness = False
# Defualt EastGA implimentation structure
self.initialization_impl = initialization_examples.random_initialization
self.fitness_funciton_impl = fitness_examples.is_it_5
@ -46,11 +49,18 @@ class GA:
self.gene_impl)
def get_population_fitness(self,population):
"""Will get and set the fitness of each chromosome in the population"""
"""Will get and set the fitness of each chromosome in the population.
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."""
# Get each chromosome in the population
for chromosome in population:
# Set the chromosomes fitness using the fitness function
chromosome.fitness = self.fitness_funciton_impl(chromosome)
# If the fitness is not set then get its fitness or if allways getting
# fitness is turn on then always get the fitness of the chromosome.
if(chromosome.fitness == None or self.update_fitness == True):
# Set the chromosomes fitness using the fitness function
chromosome.fitness = self.fitness_funciton_impl(chromosome)
def evolve(self):
"""Runs the ga until the termination point has been satisfied."""
@ -58,7 +68,6 @@ class GA:
while(self.active()):
self.evolve_generation()
def active(self):
"""Returns if the ga should terminate base on the termination implimented"""
# Send termination_impl the whole ga class
@ -66,10 +75,7 @@ class GA:
def evolve_generation(self, number_of_generations = 1):
"""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."""
"""Evolves the ga the specified number of generations."""
while(number_of_generations > 0):
# If its the first generation then initialize the population
if(self.current_generation == 0):
@ -77,14 +83,16 @@ class GA:
self.initialize_population()
# First get the fitness of the population
self.get_population_fitness(self.population.chromosomes)
#selecion -> crossover -> mutation
# Selection - Triggers flags in the chromosome if its been selected
#self.selection_impl(self)
# Crossover - Takes the flagged chromosomes and crosses there genetic
# makup to make new offsprings.
#self.crossover_impl(self)
# Repopulate - Manipulates the population to some desired way
#self.repopulate_impl(self)
# Mutation - Manipulates the population very slightly
#self.mutation_impl(self)
#next_population.append(mutated_offspring)
# Counter for the local number of generations in evolve_generation
number_of_generations -= 1
# Add one to the current overall generation