Added more structure methods and some quality of life changes

Overall cleaned up a lot of comments.

EasyGA:
- Code cleanup.

Population:
- Added sort_by_best_fitness
- Added parent/mating pool methods.
- Renamed some methods for consistency.

Chromosome:
- Added get_gene(index).

Parent Selection:
- Improved selection methods to use the ga.selection_probability so that the roulette selection actually works well.
- Added stochastic selection.

Survivor Selection:
- Added fill_in_random and fill_in_parents_then_random.

Crossover/Mutation:
- Cleaned up code.
This commit is contained in:
SimpleArt
2020-10-13 12:48:20 -04:00
parent 5e6f9b0427
commit fb213f04dd
8 changed files with 223 additions and 77 deletions

View File

@ -10,11 +10,11 @@ from fitness_function import Fitness_Examples
from initialization import Initialization_Methods
from termination_point import Termination_Methods
# Population Methods
from survivor_selection import Survivor_Selection
# Parent/Survivor Selection Methods
from parent_selection import Parent_Selection
from survivor_selection import Survivor_Selection
# Manipulation Methods
# Genetic Operator Methods
from mutation import Mutation_Methods
from crossover import Crossover_Methods
@ -23,25 +23,24 @@ class GA:
def __init__(self):
"""Initialize the GA."""
# Initilization variables
self.chromosome_length = 10
self.population_size = 10
self.chromosome_impl = None
self.gene_impl = [random.randint,1,10]
self.population = None
self.chromosome_length = 10
self.population_size = 10
self.chromosome_impl = None
self.gene_impl = [random.randint,1,10]
self.population = None
self.target_fitness_type = 'maximum'
self.update_fitness = True
self.update_fitness = True
# Selection variables
self.parent_ratio = 0.1
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 = 15
self.fitness_goal = 9
self.current_fitness = 0
self.generation_goal = 15
self.fitness_goal = 9
# Mutation variables
self.mutation_rate = 0.10
@ -53,13 +52,11 @@ class GA:
self.make_chromosome = create_chromosome
self.make_gene = create_gene
# Selects which chromosomes should be automaticly moved to the next population
self.survivor_selection_impl = Survivor_Selection.fill_in_best
# Methods for accomplishing parent-selection -> Crossover -> Mutation
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
self.parent_selection_impl = Parent_Selection.Tournament.with_replacement
self.crossover_individual_impl = Crossover_Methods.Individual.single_point_crossover
self.crossover_population_impl = Crossover_Methods.Population.random_selection
self.survivor_selection_impl = Survivor_Selection.fill_in_best
self.mutation_individual_impl = Mutation_Methods.Individual.single_gene
self.mutation_population_impl = Mutation_Methods.Population.random_selection
@ -69,57 +66,63 @@ class GA:
def evolve_generation(self, number_of_generations = 1, consider_termination = True):
"""Evolves the ga the specified number of generations."""
while(number_of_generations > 0 and (consider_termination == False or self.termination_impl(self))):
# Evolve the specified number of generations
# and if consider_termination flag is set then
# also check if termination conditions reached
while(number_of_generations > 0 and (not consider_termination or self.termination_impl(self))):
# If its the first generation then initialize the population
if self.current_generation == 0:
self.initialize_population()
self.set_all_fitness(self.population.chromosome_list)
self.population.set_all_chromosomes(self.sort_by_best_fitness(self.population.get_all_chromosomes()))
self.set_all_fitness()
self.population.sort_by_best_fitness(self)
# Otherwise evolve the population
else:
self.set_all_fitness(self.population.chromosome_list)
self.population.reset_mating_pool()
self.set_all_fitness()
self.population.set_all_chromosomes(self.sort_by_best_fitness(self.population.get_all_chromosomes()))
self.parent_selection_impl(self)
next_population = self.crossover_population_impl(self)
next_population = self.survivor_selection_impl(self, next_population)
self.population = next_population
self.survivor_selection_impl(self, next_population)
self.mutation_population_impl(self)
self.set_all_fitness(self.population.chromosome_list)
self.population.set_all_chromosomes(self.sort_by_best_fitness(self.population.get_all_chromosomes()))
number_of_generations -= 1
self.current_generation += 1
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()):
self.evolve_generation()
def active(self):
"""Returns if the ga should terminate base on the termination implimented"""
# Send termination_impl the whole ga class
"""Returns if the ga should terminate based on the termination implimented."""
return self.termination_impl(self)
def initialize_population(self):
"""Initialize the population using the initialization
implimentation that is currently set
"""Initialize the population using
the initialization implimentation
that is currently set.
"""
self.population = self.initialization_impl(self)
def set_all_fitness(self, chromosome_set):
def set_all_fitness(self):
"""Will get and set the fitness of each chromosome in the population.
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."""
# Get each chromosome in the population
fitness values) are updated.
"""
for chromosome in chromosome_set:
if(chromosome.fitness == None or self.update_fitness == True):
# Set the chromosomes fitness using the fitness function
# Check each chromosome
for chromosome in self.population.get_all_chromosomes():
# Update fitness if needed or asked by the user
if(chromosome.get_fitness() is None or self.update_fitness):
chromosome.set_fitness(self.fitness_function_impl(chromosome))