From dcc3684202a804615c1ae499a4fbcac22b31b0f1 Mon Sep 17 00:00:00 2001 From: RyleyGG Date: Thu, 8 Oct 2020 22:48:45 -0400 Subject: [PATCH] Updated tournament selection On small populations, there is now a lower bound on the tournament size. --- src/EasyGA.py | 3 ++- src/parent_selection/parent_selection_methods.py | 10 ++++------ src/run_testing.py | 13 +++++-------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index c984e67..5dba2b3 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -29,12 +29,13 @@ class GA: # Selection variables self.parent_ratio = 0.1 self.selection_probability = 0.95 + self.tournament_size_ratio = 0.1 # Termination variables self.current_generation = 0 self.current_fitness = 0 - self.generation_goal = 250 + self.generation_goal = 15 self.fitness_goal = 9 # Mutation variables diff --git a/src/parent_selection/parent_selection_methods.py b/src/parent_selection/parent_selection_methods.py index 8f010d6..3df7b76 100644 --- a/src/parent_selection/parent_selection_methods.py +++ b/src/parent_selection/parent_selection_methods.py @@ -7,16 +7,14 @@ from initialization.chromosome_structure.chromosome import Chromosome class Parent_Selection: class Tournament: def with_replacement(ga): - tournament_size = int(len(ga.population.get_all_chromosomes())*ga.parent_ratio/10) - if tournament_size < 3: - tournament_size = int(len(ga.population.get_all_chromosomes())*ga.parent_ratio/3) - + tournament_size = int(len(ga.population.get_all_chromosomes())*ga.parent_ratio*ga.tournament_size_ratio) + if tournament_size < 5: + tournament_size = 5 # Probability used for determining if a chromosome should enter the mating pool. selection_probability = ga.selection_probability # Repeat tournaments until the mating pool is large enough. while (len(ga.population.mating_pool) < len(ga.population.get_all_chromosomes())*ga.parent_ratio): - # Generate a random tournament group and sort by fitness. tournament_group = ga.sort_by_best_fitness([random.choice(ga.population.get_all_chromosomes()) for n in range(tournament_size)]) @@ -27,7 +25,7 @@ class Parent_Selection: # second ranked fitness has probability: selection_probability * (1-selection_probability) # third ranked fitness has probability: selection_probability * (1-selection_probability)^2 # etc. - if random.uniform(0, 1) < selection_probability * pow(1-selection_probability, index+1): + if random.uniform(0, 1) < selection_probability * pow(1-selection_probability, index): ga.population.mating_pool.append(tournament_group[index]) class Roulette: diff --git a/src/run_testing.py b/src/run_testing.py index 4647e4e..95252c7 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -1,17 +1,14 @@ import EasyGA import random - # Create the Genetic algorithm ga = EasyGA.GA() +ga.population_size = 15 +ga.chromosome_length = 10 +ga.generation_goal = 100 +ga.gene_impl = [random.randint,1,10] - -ga.gene_impl = [random.randrange,1,100] - -# Run Everything ga.evolve() -# Print the current population -print(f"Current Generation: {ga.current_generation}") -ga.population.print_all() +ga.population.print_all() \ No newline at end of file