Reverted back to old style of attributes.

This commit is contained in:
danielwilczak101
2020-11-03 11:47:56 -05:00
parent cae4314581
commit 5b6dc8348a
4 changed files with 45 additions and 153 deletions

View File

@ -29,66 +29,9 @@ class GA(attributes):
https://github.com/danielwilczak101/EasyGA/wiki https://github.com/danielwilczak101/EasyGA/wiki
""" """
def __init__(self, # Inhert all the ga attributes from the attributes class.
chromosome_length = None, def __init__(self):
population_size = None, super(GA, self).__init__()
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):

View File

@ -19,85 +19,56 @@ from mutation import Mutation_Methods
from crossover import Crossover_Methods from crossover import Crossover_Methods
class attributes: class attributes:
"""Default GA attributes can be found here""" """Default GA attributes can be found here. If any attributes have not
been set then they will fall back onto the default attribute. All
attributes have been catigorized to explain sections in the ga process."""
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
):
"""Default GA attributes can be found here. If any attributes have not
been set then they will fall back onto the default attribute. All
attributes have been catigorized to explain sections in the ga process."""
# Initilization variables # Initilization variables
self.chromosome_length = 10 if chromosome_length is None else chromosome_length self.chromosome_length = 10
self.population_size = 10 if population_size is None else population_size self.population_size = 10
self.chromosome_impl = chromosome_impl self.chromosome_impl = None
self.gene_impl = gene_impl self.gene_impl = lambda: random.randint(1, 10)
self.population = population self.population = None
self.target_fitness_type = 'max' if target_fitness_type is None else target_fitness_type self.target_fitness_type = 'max'
self.update_fitness = True if update_fitness is None else update_fitness self.update_fitness = True
# Selection variables # Selection variables
self.parent_ratio = 0.10 if parent_ratio is None else parent_ratio self.parent_ratio = 0.1
self.selection_probability = 0.75 if selection_probability is None else selection_probability self.selection_probability = 0.75
self.tournament_size_ratio = 0.10 if tournament_size_ratio is None else tournament_size_ratio self.tournament_size_ratio = 0.1
# Termination variables # Termination variables
self.current_generation = 0 if current_generation is None else current_generation self.current_generation = 0
self.current_fitness = 0 if current_fitness is None else current_fitness self.current_fitness = 0
self.generation_goal = 15 if generation_goal is None else generation_goal self.generation_goal = 15
self.fitness_goal = fitness_goal self.fitness_goal = None
# Mutation variables # Mutation variables
self.chromosome_mutation_rate = 0.15 if chromosome_mutation_rate is None else chromosome_mutation_rate self.gene_mutation_rate = 0.03
self.gene_mutation_rate = 0.03 if gene_mutation_rate is None else gene_mutation_rate self.chromosome_mutation_rate = 0.15
# Default EasyGA implimentation structure # Default EasyGA implimentation structure
self.initialization_impl = Initialization_Methods.random_initialization if initialization_impl is None else initialization_impl self.initialization_impl = Initialization_Methods.random_initialization
self.fitness_function_impl = Fitness_Examples.is_it_5 if fitness_function_impl is None else fitness_function_impl self.fitness_function_impl = Fitness_Examples.is_it_5
self.make_population = create_population if create_population is None else create_population self.make_population = create_population
self.make_chromosome = create_chromosome if create_chromosome is None else create_chromosome self.make_chromosome = create_chromosome
self.make_gene = create_gene if create_gene is None else create_gene self.make_gene = 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 if parent_selection_impl is None else parent_selection_impl self.parent_selection_impl = Parent_Selection.Rank.tournament
self.crossover_individual_impl = Crossover_Methods.Individual.single_point if crossover_individual_impl is None else crossover_individual_impl self.crossover_individual_impl = Crossover_Methods.Individual.single_point
self.crossover_population_impl = Crossover_Methods.Population.sequential_selection if crossover_population_impl is None else crossover_population_impl self.crossover_population_impl = Crossover_Methods.Population.sequential_selection
self.survivor_selection_impl = Survivor_Selection.fill_in_best if survivor_selection_impl is None else survivor_selection_impl self.survivor_selection_impl = Survivor_Selection.fill_in_best
self.mutation_individual_impl = Mutation_Methods.Individual.single_gene if mutation_individual_impl is None else mutation_individual_impl self.mutation_individual_impl = Mutation_Methods.Individual.single_gene
self.mutation_population_impl = Mutation_Methods.Population.random_selection if mutation_population_impl is None else mutation_population_impl self.mutation_population_impl = Mutation_Methods.Population.random_selection
# The type of termination to impliment # The type of termination to impliment
self.termination_impl = Termination_Methods.fitness_and_generation_based if termination_impl is None else termination_impl self.termination_impl = Termination_Methods.fitness_and_generation_based
# Database varibles
self.database_name = "database.db"
# Getter and setters for all required varibles # Getter and setters for all required varibles
@property @property

View File

@ -45,6 +45,7 @@ def create_chromosome(conn, chromosome):
conn.commit() conn.commit()
return cur.lastrowid return cur.lastrowid
def main(): def main():
database = r"database.db" database = r"database.db"

View File

@ -2,34 +2,11 @@ import random
import EasyGA import EasyGA
# Create the Genetic algorithm # Create the Genetic algorithm
# Reproduce 30% of the population. ga = EasyGA.GA()
# Mutate 20% of the population.
# Mutate 3% of the genes in each mutated chromosome.
#
# Create 100 chromosomes each with 25 genes
# Create random genes from 0 to 10
#
# Minimize the sum of the genes
# Terminate when a chromosome has all 0's
# or 150 generations pass
#
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 # Evolve the genetic algorithm
while ga.active(): ga.evolve()
ga.evolve_generation(10)
ga.print_generation() # Print your default genetic algorithm
ga.print_best() ga.print_generation()
#ga.print_population() ga.print_population()
print('-'*75)