Initialization for GA with arguments
This commit is contained in:
@ -22,8 +22,66 @@ from attributes import attributes
|
|||||||
class GA(attributes):
|
class GA(attributes):
|
||||||
|
|
||||||
# Inhert all the ga attributes from the attributes class.
|
# Inhert all the ga attributes from the attributes class.
|
||||||
def __init__(self):
|
def __init__(self,
|
||||||
super(GA, self).__init__()
|
chromosome_length = None,
|
||||||
|
population_size = None,
|
||||||
|
chromosome_impl = None,
|
||||||
|
gene_impl = None,
|
||||||
|
population = None,
|
||||||
|
target_fitness_type = None,
|
||||||
|
update_fitness = None,
|
||||||
|
parent_ratio = None,
|
||||||
|
selection_probability = None,
|
||||||
|
tournament_size_ratio = None,
|
||||||
|
current_generation = None,
|
||||||
|
current_fitness = None,
|
||||||
|
generation_goal = None,
|
||||||
|
fitness_goal = None,
|
||||||
|
chromosome_mutation_rate = None,
|
||||||
|
gene_mutation_rate = None,
|
||||||
|
initialization_impl = None,
|
||||||
|
fitness_function_impl = None,
|
||||||
|
make_population = None,
|
||||||
|
make_chromosome = None,
|
||||||
|
make_gene = None,
|
||||||
|
parent_selection_impl = None,
|
||||||
|
crossover_individual_impl = None,
|
||||||
|
crossover_population_impl = None,
|
||||||
|
survivor_selection_impl = None,
|
||||||
|
mutation_individual_impl = None,
|
||||||
|
mutation_population_impl = None,
|
||||||
|
termination_impl = None
|
||||||
|
):
|
||||||
|
super(GA, self).__init__(
|
||||||
|
chromosome_length,
|
||||||
|
population_size,
|
||||||
|
chromosome_impl,
|
||||||
|
gene_impl,
|
||||||
|
population,
|
||||||
|
target_fitness_type,
|
||||||
|
update_fitness,
|
||||||
|
parent_ratio,
|
||||||
|
selection_probability,
|
||||||
|
tournament_size_ratio,
|
||||||
|
current_generation,
|
||||||
|
current_fitness,
|
||||||
|
generation_goal,
|
||||||
|
fitness_goal,
|
||||||
|
chromosome_mutation_rate,
|
||||||
|
gene_mutation_rate,
|
||||||
|
initialization_impl,
|
||||||
|
fitness_function_impl,
|
||||||
|
make_population,
|
||||||
|
make_chromosome,
|
||||||
|
make_gene,
|
||||||
|
parent_selection_impl,
|
||||||
|
crossover_individual_impl,
|
||||||
|
crossover_population_impl,
|
||||||
|
survivor_selection_impl,
|
||||||
|
mutation_individual_impl,
|
||||||
|
mutation_population_impl,
|
||||||
|
termination_impl
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def evolve_generation(self, number_of_generations = 1, consider_termination = True):
|
def evolve_generation(self, number_of_generations = 1, consider_termination = True):
|
||||||
|
|||||||
@ -21,50 +21,79 @@ from crossover import Crossover_Methods
|
|||||||
class attributes:
|
class attributes:
|
||||||
"""SAMPLE TEXT"""
|
"""SAMPLE TEXT"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self,
|
||||||
|
chromosome_length,
|
||||||
|
population_size,
|
||||||
|
chromosome_impl,
|
||||||
|
gene_impl,
|
||||||
|
population,
|
||||||
|
target_fitness_type,
|
||||||
|
update_fitness,
|
||||||
|
parent_ratio,
|
||||||
|
selection_probability,
|
||||||
|
tournament_size_ratio,
|
||||||
|
current_generation,
|
||||||
|
current_fitness,
|
||||||
|
generation_goal,
|
||||||
|
fitness_goal,
|
||||||
|
chromosome_mutation_rate,
|
||||||
|
gene_mutation_rate,
|
||||||
|
initialization_impl,
|
||||||
|
fitness_function_impl,
|
||||||
|
make_population,
|
||||||
|
make_chromosome,
|
||||||
|
make_gene,
|
||||||
|
parent_selection_impl,
|
||||||
|
crossover_individual_impl,
|
||||||
|
crossover_population_impl,
|
||||||
|
survivor_selection_impl,
|
||||||
|
mutation_individual_impl,
|
||||||
|
mutation_population_impl,
|
||||||
|
termination_impl
|
||||||
|
):
|
||||||
"""Initialize the GA."""
|
"""Initialize the GA."""
|
||||||
|
|
||||||
# Initilization variables
|
# Initilization variables
|
||||||
self.chromosome_length = 10
|
self.chromosome_length = 10 if chromosome_length is None else chromosome_length
|
||||||
self.population_size = 10
|
self.population_size = 10 if population_size is None else population_size
|
||||||
self.chromosome_impl = None
|
self.chromosome_impl = chromosome_impl
|
||||||
self.gene_impl = lambda: random.randint(1, 10)
|
self.gene_impl = gene_impl
|
||||||
self.population = None
|
self.population = population
|
||||||
self.target_fitness_type = 'max'
|
self.target_fitness_type = 'max' if target_fitness_type is None else target_fitness_type
|
||||||
self.update_fitness = True
|
self.update_fitness = True if update_fitness is None else update_fitness
|
||||||
|
|
||||||
# Selection variables
|
# Selection variables
|
||||||
self.parent_ratio = 0.1
|
self.parent_ratio = 0.10 if parent_ratio is None else parent_ratio
|
||||||
self.selection_probability = 0.75
|
self.selection_probability = 0.75 if selection_probability is None else selection_probability
|
||||||
self.tournament_size_ratio = 0.1
|
self.tournament_size_ratio = 0.10 if tournament_size_ratio is None else tournament_size_ratio
|
||||||
|
|
||||||
# Termination variables
|
# Termination variables
|
||||||
self.current_generation = 0
|
self.current_generation = 0 if current_generation is None else current_generation
|
||||||
self.current_fitness = 0
|
self.current_fitness = 0 if current_fitness is None else current_fitness
|
||||||
self.generation_goal = 15
|
self.generation_goal = 15 if generation_goal is None else generation_goal
|
||||||
self.fitness_goal = None
|
self.fitness_goal = fitness_goal
|
||||||
|
|
||||||
# Mutation variables
|
# Mutation variables
|
||||||
self.chromosome_mutation_rate = 0.10
|
self.chromosome_mutation_rate = 0.15 if chromosome_mutation_rate is None else chromosome_mutation_rate
|
||||||
self.gene_mutation_rate = 0.01
|
self.gene_mutation_rate = 0.03 if gene_mutation_rate is None else gene_mutation_rate
|
||||||
|
|
||||||
# Default EasyGA implimentation structure
|
# Default EasyGA implimentation structure
|
||||||
self.initialization_impl = Initialization_Methods.random_initialization
|
self.initialization_impl = Initialization_Methods.random_initialization if initialization_impl is None else initialization_impl
|
||||||
self.fitness_function_impl = Fitness_Examples.is_it_5
|
self.fitness_function_impl = Fitness_Examples.is_it_5 if fitness_function_impl is None else fitness_function_impl
|
||||||
self.make_population = create_population
|
self.make_population = create_population if create_population is None else create_population
|
||||||
self.make_chromosome = create_chromosome
|
self.make_chromosome = create_chromosome if create_chromosome is None else create_chromosome
|
||||||
self.make_gene = create_gene
|
self.make_gene = create_gene if create_gene is None else create_gene
|
||||||
|
|
||||||
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
|
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
|
||||||
self.parent_selection_impl = Parent_Selection.Rank.tournament
|
self.parent_selection_impl = Parent_Selection.Rank.tournament if parent_selection_impl is None else parent_selection_impl
|
||||||
self.crossover_individual_impl = Crossover_Methods.Individual.single_point
|
self.crossover_individual_impl = Crossover_Methods.Individual.single_point if crossover_individual_impl is None else crossover_individual_impl
|
||||||
self.crossover_population_impl = Crossover_Methods.Population.sequential_selection
|
self.crossover_population_impl = Crossover_Methods.Population.sequential_selection if crossover_population_impl is None else crossover_population_impl
|
||||||
self.survivor_selection_impl = Survivor_Selection.fill_in_best
|
self.survivor_selection_impl = Survivor_Selection.fill_in_best if survivor_selection_impl is None else survivor_selection_impl
|
||||||
self.mutation_individual_impl = Mutation_Methods.Individual.single_gene
|
self.mutation_individual_impl = Mutation_Methods.Individual.single_gene if mutation_individual_impl is None else mutation_individual_impl
|
||||||
self.mutation_population_impl = Mutation_Methods.Population.random_selection
|
self.mutation_population_impl = Mutation_Methods.Population.random_selection if mutation_population_impl is None else mutation_population_impl
|
||||||
|
|
||||||
# The type of termination to impliment
|
# The type of termination to impliment
|
||||||
self.termination_impl = Termination_Methods.fitness_and_generation_based
|
self.termination_impl = Termination_Methods.fitness_and_generation_based if termination_impl is None else termination_impl
|
||||||
|
|
||||||
|
|
||||||
# Getter and setters for all varibles
|
# Getter and setters for all varibles
|
||||||
|
|||||||
@ -2,30 +2,31 @@ import random
|
|||||||
import EasyGA
|
import EasyGA
|
||||||
|
|
||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
|
||||||
|
|
||||||
# Reproduce 30% of the population.
|
# Reproduce 30% of the population.
|
||||||
# Mutate 20% of the population.
|
# Mutate 20% of the population.
|
||||||
# Mutate 3% of the genes in each mutated chromosome.
|
# Mutate 3% of the genes in each mutated chromosome.
|
||||||
ga.parent_ratio = 0.30
|
#
|
||||||
ga.chromosome_mutation_rate = 0.20
|
|
||||||
ga.gene_mutation_rate = 0.03
|
|
||||||
|
|
||||||
# Create 25 chromosomes each with 10 genes
|
# Create 25 chromosomes each with 10 genes
|
||||||
ga.population_size = 100
|
|
||||||
ga.chromosome_length = 25
|
|
||||||
|
|
||||||
# Create random genes from 0 to 10
|
# Create random genes from 0 to 10
|
||||||
ga.gene_impl = lambda: random.randint(0, 10)
|
#
|
||||||
|
|
||||||
# Minimize the sum of the genes
|
# 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'
|
|
||||||
|
|
||||||
# Terminate when a chromosome has all 0's
|
# Terminate when a chromosome has all 0's
|
||||||
ga.fitness_goal = 0
|
# or 150 generations pass
|
||||||
ga.generation_goal = 150
|
#
|
||||||
|
ga = EasyGA.GA(
|
||||||
|
parent_ratio = 0.30,
|
||||||
|
chromosome_mutation_rate = 0.20,
|
||||||
|
gene_mutation_rate = 0.03,
|
||||||
|
population_size = 100,
|
||||||
|
chromosome_length = 25,
|
||||||
|
gene_impl = lambda: random.randint(0, 10),
|
||||||
|
fitness_function_impl = lambda chromosome: sum(gene.get_value() for gene in chromosome.get_gene_list()),
|
||||||
|
target_fitness_type = 'min',
|
||||||
|
fitness_goal = 0,
|
||||||
|
generation_goal = 150
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run 10 generations at a time
|
||||||
while ga.active():
|
while ga.active():
|
||||||
ga.evolve_generation(10)
|
ga.evolve_generation(10)
|
||||||
ga.print_generation()
|
ga.print_generation()
|
||||||
|
|||||||
Reference in New Issue
Block a user