Updated tournament selection

On small populations, there is now a lower bound on the tournament size.
This commit is contained in:
RyleyGG
2020-10-08 22:48:45 -04:00
parent 88927f7415
commit dcc3684202
3 changed files with 11 additions and 15 deletions

View File

@ -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

View File

@ -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:

View File

@ -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()