Added the termination features

This commit is contained in:
danielwilczak101
2020-09-30 00:05:39 -04:00
parent d531888d78
commit 625143da7d
10 changed files with 72 additions and 30 deletions

View File

@ -4,75 +4,91 @@ import random
from initialization import population as create_population
from initialization import chromosome as create_chromosome
from initialization import gene as create_gene
from fitness_function import default_fitness_example
# Import the default fitness function
from fitness_function import is_the_value_5
# Import default termination points
from termination_point import generation_based
from termination_point import fitness_based
# Import functionality defaults
from initialization import random_initialization
class GA:
def __init__(self):
"""Initialize the GA."""
# Default variables
# Initilization variables
self.chromosome_length = 3
self.population_size = 5
self.chromosome_impl = None
self.gene_impl = None
self.population = None
# Termination varibles
self.current_generation = 0
self.generations = 3
self.chromosome_length = 3
self.population_size = 5
self.max_generations = 3
self.current_fitness = 0
self.goal_fitness = 3
# Mutation variables
self.mutation_rate = 0.03
# Defualt EastGA implimentation structure
self.initialization_impl = random_initialization
self.fitness_funciton_impl = default_fitness_example
self.fitness_funciton_impl = is_the_value_5
#self.mutation_impl = PerGeneMutation(Mutation_rate)
#self.selection_impl = TournamentSelection()
#self.crossover_impl = FastSinglePointCrossover()
#self.termination_impl = GenerationTermination(Total_generations)
self.termination_impl = generation_based
#self.evaluation_impl = TestEvaluation()
# If we want the fitnesses to be updated by the computer
self.update_fitness = True
def initialize_population(self):
"""Initialize the population"""
"""Initialize the population using the initialization
implimentation that is currently set"""
self.population = self.initialization_impl(
self.population_size,
self.chromosome_length,
self.chromosome_impl,
self.gene_impl)
def get_population_fitness(self,population):
"""Will get and set the fitness of each chromosome in the population"""
# 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)
def evolve(self):
"""Runs the ga until the termination point has been satisfied."""
self.initialize_population()
#while(self.active()):
#if(self.current_generation == 0):
#initialize_population()
# While the termination point hasnt been reached keep running
while(self.active()):
# If its the first generation then initialize the population
if(self.current_generation == 0):
# Initialize the population
self.initialize_population()
# First get the fitness of the population
self.get_population_fitness(self.population.chromosomes)
print(f"Ive completed generation - {self.current_generation}")
self.current_generation += 1
#get_fitness(population)
# run one iteration while the ga is active
#while self.active():
#self.evolve_generation(1)
def active(self):
"""Returns if the ga should terminate base on the termination implimented"""
# Send termination_impl the whole ga class
return self.termination_impl(self)
def evolve_generation(self, 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."""
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):
#for n in range(number_of_generations):
# 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