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