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
|
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):
|
def evolve_generation(self, number_of_generations = 1, consider_termination = True):
|
||||||
"""Evolves the ga the specified number of generations."""
|
"""Evolves the ga the specified number of generations."""
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import random
|
import random
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
# Import all the data structure prebuilt modules
|
# Import all the data structure prebuilt modules
|
||||||
from structure import Population as create_population
|
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
|
been set then they will fall back onto the default attribute. All
|
||||||
attributes have been catigorized to explain sections in the ga process."""
|
attributes have been catigorized to explain sections in the ga process."""
|
||||||
|
|
||||||
def __init__(self, attributes):
|
def __init__(self,
|
||||||
|
chromosome_length = 10,
|
||||||
# Default settings for the GA
|
population_size = 10,
|
||||||
default_attributes = {
|
chromosome_impl = None,
|
||||||
'chromosome_length' : 10,
|
gene_impl = lambda: random.randint(1, 10),
|
||||||
'population_size' : 10,
|
population = None,
|
||||||
'chromosome_impl' : None,
|
target_fitness_type = 'max',
|
||||||
'gene_impl' : lambda: random.randint(1, 10),
|
update_fitness = True,
|
||||||
'population' : None,
|
parent_ratio = 0.10,
|
||||||
'target_fitness_type' : 'max',
|
selection_probability = 0.50,
|
||||||
'update_fitness' : True,
|
tournament_size_ratio = 0.10,
|
||||||
'parent_ratio' : 0.10,
|
current_generation = 0,
|
||||||
'selection_probability' : 0.50,
|
current_fitness = 0,
|
||||||
'tournament_size_ratio' : 0.10,
|
generation_goal = 15,
|
||||||
'current_generation' : 0,
|
fitness_goal = None,
|
||||||
'current_fitness' : 0,
|
chromosome_mutation_rate = 0.15,
|
||||||
'generation_goal' : 15,
|
gene_mutation_rate = 0.03,
|
||||||
'fitness_goal' : None,
|
initialization_impl = Initialization_Methods.random_initialization,
|
||||||
'chromosome_mutation_rate' : 0.15,
|
fitness_function_impl = Fitness_Examples.is_it_5,
|
||||||
'gene_mutation_rate' : 0.03,
|
make_population = create_population,
|
||||||
'initialization_impl' : Initialization_Methods.random_initialization,
|
make_chromosome = create_chromosome,
|
||||||
'fitness_function_impl' : Fitness_Examples.is_it_5,
|
make_gene = create_gene,
|
||||||
'make_population' : create_population,
|
parent_selection_impl = Parent_Selection.Rank.tournament,
|
||||||
'make_chromosome' : create_chromosome,
|
crossover_individual_impl = Crossover_Methods.Individual.single_point,
|
||||||
'make_gene' : create_gene,
|
crossover_population_impl = Crossover_Methods.Population.sequential_selection,
|
||||||
'parent_selection_impl' : Parent_Selection.Rank.tournament,
|
survivor_selection_impl = Survivor_Selection.fill_in_best,
|
||||||
'crossover_individual_impl' : Crossover_Methods.Individual.single_point,
|
mutation_individual_impl = Mutation_Methods.Individual.single_gene,
|
||||||
'crossover_population_impl' : Crossover_Methods.Population.sequential_selection,
|
mutation_population_impl = Mutation_Methods.Population.random_selection,
|
||||||
'survivor_selection_impl' : Survivor_Selection.fill_in_best,
|
termination_impl = Termination_Methods.fitness_and_generation_based,
|
||||||
'mutation_individual_impl' : Mutation_Methods.Individual.single_gene,
|
database = None,
|
||||||
'mutation_population_impl' : Mutation_Methods.Population.random_selection,
|
database_name = 'database.db',
|
||||||
'termination_impl' : Termination_Methods.fitness_and_generation_based,
|
'sql_create_data_structure' = """ CREATE TABLE IF NOT EXISTS data (
|
||||||
'database' : None,
|
|
||||||
'database_name' : r"database.db",
|
|
||||||
'sql_create_data_structure' : """ CREATE TABLE IF NOT EXISTS data (
|
|
||||||
id integer PRIMARY KEY,
|
id integer PRIMARY KEY,
|
||||||
generation integer NOT NULL,
|
generation integer NOT NULL,
|
||||||
fitness DOUBLE,
|
fitness DOUBLE,
|
||||||
chromosome text
|
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
|
# Initilization variables
|
||||||
self.chromosome_length = attributes['chromosome_length']
|
self.chromosome_length = deepcopy(chromosome_length)
|
||||||
self.population_size = attributes['population_size']
|
self.population_size = deepcopy(population_size)
|
||||||
self.chromosome_impl = attributes['chromosome_impl']
|
self.chromosome_impl = deepcopy(chromosome_impl)
|
||||||
self.gene_impl = attributes['gene_impl']
|
self.gene_impl = deepcopy(gene_impl)
|
||||||
self.population = attributes['population']
|
self.population = deepcopy(population)
|
||||||
self.target_fitness_type = attributes['target_fitness_type']
|
self.target_fitness_type = deepcopy(target_fitness_type)
|
||||||
self.update_fitness = attributes['update_fitness']
|
self.update_fitness = deepcopy(update_fitness)
|
||||||
|
|
||||||
# Selection variables
|
# Selection variables
|
||||||
self.parent_ratio = attributes['parent_ratio']
|
self.parent_ratio = deepcopy(parent_ratio)
|
||||||
self.selection_probability = attributes['selection_probability']
|
self.selection_probability = deepcopy(selection_probability)
|
||||||
self.tournament_size_ratio = attributes['tournament_size_ratio']
|
self.tournament_size_ratio = deepcopy(tournament_size_ratio)
|
||||||
|
|
||||||
# Termination variables
|
# Termination variables
|
||||||
self.current_generation = attributes['current_generation']
|
self.current_generation = deepcopy(current_generation)
|
||||||
self.current_fitness = attributes['current_fitness']
|
self.current_fitness = deepcopy(current_fitness)
|
||||||
self.generation_goal = attributes['generation_goal']
|
self.generation_goal = deepcopy(generation_goal)
|
||||||
self.fitness_goal = attributes['fitness_goal']
|
self.fitness_goal = deepcopy(fitness_goal)
|
||||||
|
|
||||||
# Mutation variables
|
# Mutation variables
|
||||||
self.chromosome_mutation_rate = attributes['chromosome_mutation_rate']
|
self.chromosome_mutation_rate = deepcopy(chromosome_mutation_rate)
|
||||||
self.gene_mutation_rate = attributes['gene_mutation_rate']
|
self.gene_mutation_rate = deepcopy(gene_mutation_rate)
|
||||||
|
|
||||||
# Default EasyGA implimentation structure
|
# Default EasyGA implimentation structure
|
||||||
self.initialization_impl = attributes['initialization_impl']
|
self.initialization_impl = deepcopy(initialization_impl)
|
||||||
self.fitness_function_impl = attributes['fitness_function_impl']
|
self.fitness_function_impl = deepcopy(fitness_function_impl)
|
||||||
self.make_population = attributes['make_population']
|
self.make_population = deepcopy(make_population)
|
||||||
self.make_chromosome = attributes['make_chromosome']
|
self.make_chromosome = deepcopy(make_chromosome)
|
||||||
self.make_gene = attributes['make_gene']
|
self.make_gene = deepcopy(make_gene)
|
||||||
|
|
||||||
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
|
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
|
||||||
self.parent_selection_impl = attributes['parent_selection_impl']
|
self.parent_selection_impl = deepcopy(parent_selection_impl)
|
||||||
self.crossover_individual_impl = attributes['crossover_individual_impl']
|
self.crossover_individual_impl = deepcopy(crossover_individual_impl)
|
||||||
self.crossover_population_impl = attributes['crossover_population_impl']
|
self.crossover_population_impl = deepcopy(crossover_population_impl)
|
||||||
self.survivor_selection_impl = attributes['survivor_selection_impl']
|
self.survivor_selection_impl = deepcopy(survivor_selection_impl)
|
||||||
self.mutation_individual_impl = attributes['mutation_individual_impl']
|
self.mutation_individual_impl = deepcopy(mutation_individual_impl)
|
||||||
self.mutation_population_impl = attributes['mutation_population_impl']
|
self.mutation_population_impl = deepcopy(mutation_population_impl)
|
||||||
|
|
||||||
# The type of termination to impliment
|
# The type of termination to impliment
|
||||||
self.termination_impl = attributes['termination_impl']
|
self.termination_impl = deepcopy(termination_impl)
|
||||||
|
|
||||||
# Database varibles
|
# Database varibles
|
||||||
self.database_name = attributes['database_name']
|
self.database = deepcopy(database)
|
||||||
|
self.database_name = deepcopy(database_name)
|
||||||
# Database varibles
|
self.sql_create_data_structure = deepcopy(sql_create_data_structure)
|
||||||
self.database = attributes['database']
|
|
||||||
self.database_name = attributes['database_name']
|
|
||||||
self.sql_create_data_structure = attributes['sql_create_data_structure']
|
|
||||||
|
|
||||||
|
|
||||||
# Getter and setters for all required varibles
|
# Getter and setters for all required varibles
|
||||||
|
|||||||
Reference in New Issue
Block a user