Fixed default arguments
Reverted usage format:
GA(
chromosome_length = 20,
population_size = 25
)
This commit is contained in:
@ -33,9 +33,6 @@ class GA(Attributes):
|
||||
https://github.com/danielwilczak101/EasyGA/wiki
|
||||
"""
|
||||
|
||||
def __init__(self, attributes = None):
|
||||
super(GA, self).__init__({} if attributes is None else attributes)
|
||||
|
||||
|
||||
def evolve_generation(self, number_of_generations = 1, consider_termination = True):
|
||||
"""Evolves the ga the specified number of generations."""
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import random
|
||||
import sqlite3
|
||||
from copy import deepcopy
|
||||
|
||||
# Import all the data structure prebuilt modules
|
||||
from structure import Population as create_population
|
||||
@ -28,103 +29,91 @@ class Attributes:
|
||||
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, attributes):
|
||||
|
||||
# Default settings for the GA
|
||||
default_attributes = {
|
||||
'chromosome_length' : 10,
|
||||
'population_size' : 10,
|
||||
'chromosome_impl' : None,
|
||||
'gene_impl' : lambda: random.randint(1, 10),
|
||||
'population' : None,
|
||||
'target_fitness_type' : 'max',
|
||||
'update_fitness' : True,
|
||||
'parent_ratio' : 0.10,
|
||||
'selection_probability' : 0.50,
|
||||
'tournament_size_ratio' : 0.10,
|
||||
'current_generation' : 0,
|
||||
'current_fitness' : 0,
|
||||
'generation_goal' : 15,
|
||||
'fitness_goal' : None,
|
||||
'chromosome_mutation_rate' : 0.15,
|
||||
'gene_mutation_rate' : 0.03,
|
||||
'initialization_impl' : Initialization_Methods.random_initialization,
|
||||
'fitness_function_impl' : Fitness_Examples.is_it_5,
|
||||
'make_population' : create_population,
|
||||
'make_chromosome' : create_chromosome,
|
||||
'make_gene' : create_gene,
|
||||
'parent_selection_impl' : Parent_Selection.Rank.tournament,
|
||||
'crossover_individual_impl' : Crossover_Methods.Individual.single_point,
|
||||
'crossover_population_impl' : Crossover_Methods.Population.sequential_selection,
|
||||
'survivor_selection_impl' : Survivor_Selection.fill_in_best,
|
||||
'mutation_individual_impl' : Mutation_Methods.Individual.single_gene,
|
||||
'mutation_population_impl' : Mutation_Methods.Population.random_selection,
|
||||
'termination_impl' : Termination_Methods.fitness_and_generation_based,
|
||||
'database' : None,
|
||||
'database_name' : r"database.db",
|
||||
'sql_create_data_structure' : """ CREATE TABLE IF NOT EXISTS data (
|
||||
def __init__(self,
|
||||
chromosome_length = 10,
|
||||
population_size = 10,
|
||||
chromosome_impl = None,
|
||||
gene_impl = lambda: random.randint(1, 10),
|
||||
population = None,
|
||||
target_fitness_type = 'max',
|
||||
update_fitness = True,
|
||||
parent_ratio = 0.10,
|
||||
selection_probability = 0.50,
|
||||
tournament_size_ratio = 0.10,
|
||||
current_generation = 0,
|
||||
current_fitness = 0,
|
||||
generation_goal = 15,
|
||||
fitness_goal = None,
|
||||
chromosome_mutation_rate = 0.15,
|
||||
gene_mutation_rate = 0.03,
|
||||
initialization_impl = Initialization_Methods.random_initialization,
|
||||
fitness_function_impl = Fitness_Examples.is_it_5,
|
||||
make_population = create_population,
|
||||
make_chromosome = create_chromosome,
|
||||
make_gene = create_gene,
|
||||
parent_selection_impl = Parent_Selection.Rank.tournament,
|
||||
crossover_individual_impl = Crossover_Methods.Individual.single_point,
|
||||
crossover_population_impl = Crossover_Methods.Population.sequential_selection,
|
||||
survivor_selection_impl = Survivor_Selection.fill_in_best,
|
||||
mutation_individual_impl = Mutation_Methods.Individual.single_gene,
|
||||
mutation_population_impl = Mutation_Methods.Population.random_selection,
|
||||
termination_impl = Termination_Methods.fitness_and_generation_based,
|
||||
database = None,
|
||||
database_name = 'database.db',
|
||||
'sql_create_data_structure' = """ CREATE TABLE IF NOT EXISTS data (
|
||||
id integer PRIMARY KEY,
|
||||
generation integer NOT NULL,
|
||||
fitness DOUBLE,
|
||||
chromosome text
|
||||
); """
|
||||
|
||||
}
|
||||
|
||||
# Filling in the default settings
|
||||
for attribute in default_attributes.keys():
|
||||
if attribute not in attributes.keys():
|
||||
attributes[attribute] = default_attributes[attribute]
|
||||
):
|
||||
|
||||
# Initilization variables
|
||||
self.chromosome_length = attributes['chromosome_length']
|
||||
self.population_size = attributes['population_size']
|
||||
self.chromosome_impl = attributes['chromosome_impl']
|
||||
self.gene_impl = attributes['gene_impl']
|
||||
self.population = attributes['population']
|
||||
self.target_fitness_type = attributes['target_fitness_type']
|
||||
self.update_fitness = attributes['update_fitness']
|
||||
self.chromosome_length = deepcopy(chromosome_length)
|
||||
self.population_size = deepcopy(population_size)
|
||||
self.chromosome_impl = deepcopy(chromosome_impl)
|
||||
self.gene_impl = deepcopy(gene_impl)
|
||||
self.population = deepcopy(population)
|
||||
self.target_fitness_type = deepcopy(target_fitness_type)
|
||||
self.update_fitness = deepcopy(update_fitness)
|
||||
|
||||
# Selection variables
|
||||
self.parent_ratio = attributes['parent_ratio']
|
||||
self.selection_probability = attributes['selection_probability']
|
||||
self.tournament_size_ratio = attributes['tournament_size_ratio']
|
||||
self.parent_ratio = deepcopy(parent_ratio)
|
||||
self.selection_probability = deepcopy(selection_probability)
|
||||
self.tournament_size_ratio = deepcopy(tournament_size_ratio)
|
||||
|
||||
# Termination variables
|
||||
self.current_generation = attributes['current_generation']
|
||||
self.current_fitness = attributes['current_fitness']
|
||||
self.generation_goal = attributes['generation_goal']
|
||||
self.fitness_goal = attributes['fitness_goal']
|
||||
self.current_generation = deepcopy(current_generation)
|
||||
self.current_fitness = deepcopy(current_fitness)
|
||||
self.generation_goal = deepcopy(generation_goal)
|
||||
self.fitness_goal = deepcopy(fitness_goal)
|
||||
|
||||
# Mutation variables
|
||||
self.chromosome_mutation_rate = attributes['chromosome_mutation_rate']
|
||||
self.gene_mutation_rate = attributes['gene_mutation_rate']
|
||||
self.chromosome_mutation_rate = deepcopy(chromosome_mutation_rate)
|
||||
self.gene_mutation_rate = deepcopy(gene_mutation_rate)
|
||||
|
||||
# Default EasyGA implimentation structure
|
||||
self.initialization_impl = attributes['initialization_impl']
|
||||
self.fitness_function_impl = attributes['fitness_function_impl']
|
||||
self.make_population = attributes['make_population']
|
||||
self.make_chromosome = attributes['make_chromosome']
|
||||
self.make_gene = attributes['make_gene']
|
||||
self.initialization_impl = deepcopy(initialization_impl)
|
||||
self.fitness_function_impl = deepcopy(fitness_function_impl)
|
||||
self.make_population = deepcopy(make_population)
|
||||
self.make_chromosome = deepcopy(make_chromosome)
|
||||
self.make_gene = deepcopy(make_gene)
|
||||
|
||||
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
|
||||
self.parent_selection_impl = attributes['parent_selection_impl']
|
||||
self.crossover_individual_impl = attributes['crossover_individual_impl']
|
||||
self.crossover_population_impl = attributes['crossover_population_impl']
|
||||
self.survivor_selection_impl = attributes['survivor_selection_impl']
|
||||
self.mutation_individual_impl = attributes['mutation_individual_impl']
|
||||
self.mutation_population_impl = attributes['mutation_population_impl']
|
||||
self.parent_selection_impl = deepcopy(parent_selection_impl)
|
||||
self.crossover_individual_impl = deepcopy(crossover_individual_impl)
|
||||
self.crossover_population_impl = deepcopy(crossover_population_impl)
|
||||
self.survivor_selection_impl = deepcopy(survivor_selection_impl)
|
||||
self.mutation_individual_impl = deepcopy(mutation_individual_impl)
|
||||
self.mutation_population_impl = deepcopy(mutation_population_impl)
|
||||
|
||||
# The type of termination to impliment
|
||||
self.termination_impl = attributes['termination_impl']
|
||||
self.termination_impl = deepcopy(termination_impl)
|
||||
|
||||
# Database varibles
|
||||
self.database_name = attributes['database_name']
|
||||
|
||||
# Database varibles
|
||||
self.database = attributes['database']
|
||||
self.database_name = attributes['database_name']
|
||||
self.sql_create_data_structure = attributes['sql_create_data_structure']
|
||||
self.database = deepcopy(database)
|
||||
self.database_name = deepcopy(database_name)
|
||||
self.sql_create_data_structure = deepcopy(sql_create_data_structure)
|
||||
|
||||
|
||||
# Getter and setters for all required varibles
|
||||
|
||||
Reference in New Issue
Block a user