Updated Implementation Framework
Updated to cover changes made by Dan to Master regarding general design changes Also added remove_two_worst survivor selection method
This commit is contained in:
@ -3,25 +3,29 @@ import random
|
||||
from initialization import Population as create_population
|
||||
from initialization import Chromosome as create_chromosome
|
||||
from initialization import Gene as create_gene
|
||||
# Import example classes
|
||||
# Structure Methods
|
||||
from fitness_function import Fitness_Examples
|
||||
from initialization import Initialization_Types
|
||||
from termination_point import Termination_Types
|
||||
from selection import Selection_Types
|
||||
from crossover import Crossover_Types
|
||||
from mutation import Mutation_Types
|
||||
from initialization import Initialization_Methods
|
||||
from termination_point import Termination_Methods
|
||||
# Population Methods
|
||||
from selection import Selection_Methods
|
||||
# Manipulation Methods
|
||||
from mutation import Mutation_Methods
|
||||
from crossover import Crossover_Methods
|
||||
|
||||
class GA:
|
||||
def __init__(self):
|
||||
"""Initialize the GA."""
|
||||
# Initilization variables
|
||||
self.chromosome_length = 10
|
||||
self.population_size = 150
|
||||
self.population_size = 100
|
||||
self.chromosome_impl = None
|
||||
self.gene_impl = None
|
||||
self.population = None
|
||||
# Termination varibles
|
||||
self.current_generation = 0
|
||||
self.generation_goal = 50
|
||||
|
||||
self.current_fitness = 0
|
||||
self.generation_goal = 100
|
||||
self.fitness_goal = 3
|
||||
@ -32,19 +36,52 @@ class GA:
|
||||
self.update_fitness = True
|
||||
|
||||
# Defualt EastGA implimentation structure
|
||||
self.initialization_impl = Initialization_Types().random_initialization
|
||||
self.initialization_impl = Initialization_Methods().random_initialization
|
||||
self.fitness_function_impl = Fitness_Examples().is_it_5
|
||||
self.mutation_impl = Mutation_Types().per_gene_mutation
|
||||
self.parent_selection_impl = Selection_Types().Parent_Selection().Tournament().with_replacement
|
||||
self.survivor_selection_impl = Selection_Types().Survivor_Selection().repeated_crossover
|
||||
self.crossover_impl = Crossover_Types().single_point_crossover
|
||||
self.termination_impl = Termination_Types().generation_based
|
||||
# Selects which chromosomes should be automaticly moved to the next population
|
||||
self.survivor_selection_impl = Selection_Methods().Survivor_Selection().remove_two_worst
|
||||
# Methods for accomplishing parent-selection -> Crossover -> Mutation
|
||||
self.parent_selection_impl = Selection_Methods().Parent_Selection().Tournament().with_replacement
|
||||
self.crossover_impl = Crossover_Methods().single_point_crossover
|
||||
self.mutation_impl = Mutation_Methods().per_gene_mutation
|
||||
# The type of termination to impliment
|
||||
self.termination_impl = Termination_Methods().generation_based
|
||||
|
||||
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))):
|
||||
# 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.parent_selection_impl(self)
|
||||
next_population = self.crossover_impl(self)
|
||||
next_population = self.survivor_selection_impl(self, next_population)
|
||||
next_population.set_all_chromosomes(self.mutation_impl(self, next_population.get_all_chromosomes()))
|
||||
|
||||
self.population = next_population
|
||||
self.set_all_fitness(self.population.chromosome_list)
|
||||
|
||||
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
|
||||
return self.termination_impl(self)
|
||||
|
||||
def initialize_population(self):
|
||||
"""Initialize the population using the initialization
|
||||
implimentation that is currently set"""
|
||||
self.population = self.initialization_impl(self)
|
||||
|
||||
|
||||
def set_all_fitness(self,chromosome_set):
|
||||
"""Will get and set the fitness of each chromosome in the population.
|
||||
If update_fitness is set then all fitness values are updated.
|
||||
@ -69,7 +106,7 @@ class GA:
|
||||
# If its the first generation then initialize the population
|
||||
if self.current_generation == 0:
|
||||
self.initialize_population()
|
||||
self.set_all_fitness(self.population.chromosomes)
|
||||
self.set_all_fitness(self.population.chromosome_list)
|
||||
|
||||
self.parent_selection_impl(self)
|
||||
next_population = self.crossover_impl(self)
|
||||
@ -77,7 +114,7 @@ class GA:
|
||||
next_population.set_all_chromosomes(self.mutation_impl(self, next_population.get_all_chromosomes()))
|
||||
|
||||
self.population = next_population
|
||||
self.set_all_fitness(self.population.chromosomes)
|
||||
self.set_all_fitness(self.population.chromosome_list)
|
||||
|
||||
number_of_generations -= 1
|
||||
self.current_generation += 1
|
||||
|
||||
Reference in New Issue
Block a user