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."""
# 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()
#while(self.active()):
#if(self.current_generation == 0):
#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

View File

@ -0,0 +1 @@
# From file name import function name

View File

@ -1 +1,2 @@
from .default_fitness_example import default_fitness_example
# FROM (. means local) file_name IMPORT function_name
from .is_the_value_5 import is_the_value_5

View File

@ -1,2 +0,0 @@
def default_fitness_example():
pass

View File

@ -0,0 +1,14 @@
def is_the_value_5(chromosome):
"""A very simple case test function - If the chromosomes gene value is a 5 add one
to the chromosomes overall fitness value."""
# Overall fitness value
fitness = 0
# For each gene in the chromosome
for gene in chromosome.genes:
# Check if its value = 5
if(gene.value == 5):
# If its value is 5 then add one to
# the overal fitness of the chromosome.
fitness += 1
return fitness

View File

@ -1,4 +1,4 @@
# __init__.py
# FROM (. means local) file_name IMPORT function_name
from .random_initialization import random_initialization
from .population_structure.population import population
from .chromosome_structure.chromosome import chromosome

View File

@ -4,11 +4,10 @@ import random
ga = EasyGA.GA()
ga.chromosome_length = 3
ga.max_generations = 5
# If the user wants to use a domain
ga.gene_impl = [random.randrange,1,10]
# Run Everyhting
ga.evolve()

View File

@ -0,0 +1,3 @@
# FROM (. means local) file_name IMPORT function_name
from .generation_based import generation_based
from .fitness_based import fitness_based

View File

@ -0,0 +1,5 @@
def fitness_based(ga):
status = True
if(ga.current_fitness > ga.goal_fitness):
status = False
return status

View File

@ -0,0 +1,5 @@
def generation_based(ga):
status = True
if(ga.current_generation > ga.max_generations):
status = False
return status