From 6c6c64e342c9ee76316e97c51c7052054ca41026 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 22 Oct 2020 00:05:42 -0400 Subject: [PATCH] Updated fitness based termination and fixed some EasyGA stuff --- src/EasyGA.py | 28 +++++++++++++++----- src/run_testing.py | 21 ++++++++------- src/termination_point/termination_methods.py | 19 ++++++------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 14d7ff2..5e9d274 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,4 +1,20 @@ -import random +# Import all the data structure prebuilt modules +from structure import Population as create_population +from structure import Chromosome as create_chromosome +from structure import Gene as create_gene + +# Structure Methods +from fitness_function import Fitness_Examples +from initialization import Initialization_Methods +from termination_point import Termination_Methods + +# Parent/Survivor Selection Methods +from parent_selection import Parent_Selection +from survivor_selection import Survivor_Selection + +# Genetic Operator Methods +from mutation import Mutation_Methods +from crossover import Crossover_Methods from attributes import attributes @@ -13,9 +29,9 @@ class GA(attributes): def evolve_generation(self, number_of_generations = 1, consider_termination = True): """Evolves the ga the specified number of generations.""" - while(number_of_generations > 0 # Evolve the specified number of generations - and (not consider_termination # and if consider_termination flag is set - or self.termination_impl(self))): # then also check if termination conditions reached + while(number_of_generations > 0 # Evolve the specified number of generations + and (not consider_termination # and if consider_termination flag is set + or self.active())): # then also check if termination conditions reached # If its the first generation then initialize the population if self.current_generation == 0: @@ -25,13 +41,13 @@ class GA(attributes): # Otherwise evolve the population else: - self.set_all_fitness() - self.population.sort_by_best_fitness(self) self.parent_selection_impl(self) self.crossover_population_impl(self) self.survivor_selection_impl(self) self.mutation_population_impl(self) self.population.update() + self.set_all_fitness() + self.population.sort_by_best_fitness(self) number_of_generations -= 1 self.current_generation += 1 diff --git a/src/run_testing.py b/src/run_testing.py index 56b8260..4e46a76 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -3,22 +3,23 @@ import EasyGA # Create the Genetic algorithm ga = EasyGA.GA() -ga.target_fitness_type = 'min' -ga.chromosome_length = 10 + +# Create 25 chromosomes each with 10 genes ga.population_size = 25 -ga.generation_goal = 50 +ga.chromosome_length = 10 + +# Create random genes from 0 to 10 ga.gene_impl = lambda: random.randint(0, 10) -def fitness_function(chromosome): - return sum( - gene.get_value() - for gene in chromosome.get_gene_list()) +# Minimize the sum of the genes +ga.fitness_function_impl = lambda chromosome: sum(gene.get_value() for gene in chromosome.get_gene_list()) +ga.target_fitness_type = 'min' -ga.fitness_function_impl = fitness_function +# Terminate when a chromosome has all 0's +ga.fitness_goal = 0 +ga.termination_impl = EasyGA.Termination_Methods.fitness_based ga.evolve() -ga.set_all_fitness() -ga.population.sort_by_best_fitness(ga) print(f"Current Generation: {ga.current_generation}") ga.population.print_all() diff --git a/src/termination_point/termination_methods.py b/src/termination_point/termination_methods.py index c6a016c..9a3beae 100644 --- a/src/termination_point/termination_methods.py +++ b/src/termination_point/termination_methods.py @@ -4,22 +4,23 @@ class Termination_Methods: def fitness_based(ga): """Fitness based approach to terminate when the goal fitness has been reached""" - # Need to start the algorithm if the population is None + # Need to start the algorithm if the population is None. if ga.population == None: return True - # Check all chromosomes - for chromosome in ga.population.get_all_chromosomes(): + # If minimum fitness goal reached, stop ga. + if ga.target_fitness_type == 'min' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal): + return False - # Stop if a chromosome has reached the fitness_goal - if(chromosome.fitness >= ga.fitness_goal): - return False - - # Continue if no chromosomes have reached the fitness goal + # If maximum fitness goal reached, stop ga. + if ga.target_fitness_type == 'max' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal): + return False + + # Otherwise continue ga. return True def generation_based(ga): - """Generation based approach to terminate when the goal generation has been reached""" + """Generation based approach to terminate when the goal generation has been reached.""" return ga.current_generation < ga.generation_goal