diff --git a/src/EasyGA.py b/src/EasyGA.py index edb60f2..0b105b5 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,69 +1,11 @@ import random -# Import all the data structure prebuilt modules -from structure import Population as create_population -from structure import Chromosome as create_chromosome -from structure import Gene as create_gene +from attributes import attributes -# Structure Methods -from fitness_function import Fitness_Examples -from initialization import Initialization_Methods -from termination_point import Termination_Methods - -# Parent/Survivor Selection Methods -from parent_selection import Parent_Selection -from survivor_selection import Survivor_Selection - -# Genetic Operator 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 = 10 - self.chromosome_impl = None - self.gene_impl = lambda: random.randint(1, 10) - self.population = None - self.target_fitness_type = 'maximum' - self.update_fitness = True - - # Selection variables - self.parent_ratio = 0.1 - self.selection_probability = 0.75 - self.tournament_size_ratio = 0.1 - - # Termination variables - self.current_generation = 0 - self.current_fitness = 0 - self.generation_goal = 15 - self.fitness_goal = 9 - - # Mutation variables - self.mutation_rate = 0.10 - - # Default EasyGA implimentation structure - self.initialization_impl = Initialization_Methods.random_initialization - self.fitness_function_impl = Fitness_Examples.is_it_5 - self.make_population = create_population - self.make_chromosome = create_chromosome - self.make_gene = create_gene - - # Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation - self.parent_selection_impl = Parent_Selection.Tournament.with_replacement - self.crossover_individual_impl = Crossover_Methods.Individual.single_point - self.crossover_population_impl = Crossover_Methods.Population.random_selection - self.survivor_selection_impl = Survivor_Selection.fill_in_best - self.mutation_individual_impl = Mutation_Methods.Individual.single_gene - self.mutation_population_impl = Mutation_Methods.Population.random_selection - - # The type of termination to impliment - self.termination_impl = Termination_Methods.generation_based +class GA(attributes): + def __init__(self): # Inhert all the ga attributes + super(GA, self).__init__() # from the attributes class def evolve_generation(self, number_of_generations = 1, consider_termination = True): """Evolves the ga the specified number of generations.""" @@ -136,14 +78,3 @@ class GA: return sorted(chromosome_set, # list to be sorted key = lambda chromosome: chromosome.get_fitness(), # by fitness reverse = True) # from highest to lowest fitness - - # Example of how the setter error checking will look like - @property - def chromosome_length(self): - return self._chromosome_length - - @chromosome_length.setter - def chromosome_length(self, value_input): - if(value_input == 0): - raise ValueError("Sorry your chromosome length must be greater then 0") - self._chromosome_length = value_input diff --git a/src/attributes.py b/src/attributes.py new file mode 100644 index 0000000..7d4e02b --- /dev/null +++ b/src/attributes.py @@ -0,0 +1,75 @@ +import random + +# Import all the data structure prebuilt modules +from structure import Population as create_population +from structure import Chromosome as create_chromosome +from structure import Gene as create_gene + +# Structure Methods +from fitness_function import Fitness_Examples +from initialization import Initialization_Methods +from termination_point import Termination_Methods + +# Parent/Survivor Selection Methods +from parent_selection import Parent_Selection +from survivor_selection import Survivor_Selection + +# Genetic Operator Methods +from mutation import Mutation_Methods +from crossover import Crossover_Methods + +class attributes: + def __init__(self): + """Initialize the GA.""" + + # Initilization variables + self.chromosome_length = 10 + self.population_size = 10 + self.chromosome_impl = None + self.gene_impl = lambda: random.randint(1, 10) + self.population = None + self.target_fitness_type = 'maximum' + self.update_fitness = True + + # Selection variables + self.parent_ratio = 0.1 + self.selection_probability = 0.75 + self.tournament_size_ratio = 0.1 + + # Termination variables + self.current_generation = 0 + self.current_fitness = 0 + self.generation_goal = 15 + self.fitness_goal = 9 + + # Mutation variables + self.mutation_rate = 0.10 + + # Default EasyGA implimentation structure + self.initialization_impl = Initialization_Methods.random_initialization + self.fitness_function_impl = Fitness_Examples.is_it_5 + self.make_population = create_population + self.make_chromosome = create_chromosome + self.make_gene = create_gene + + # Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation + self.parent_selection_impl = Parent_Selection.Tournament.with_replacement + self.crossover_individual_impl = Crossover_Methods.Individual.single_point + self.crossover_population_impl = Crossover_Methods.Population.random_selection + self.survivor_selection_impl = Survivor_Selection.fill_in_best + self.mutation_individual_impl = Mutation_Methods.Individual.single_gene + self.mutation_population_impl = Mutation_Methods.Population.random_selection + + # The type of termination to impliment + self.termination_impl = Termination_Methods.generation_based + + # Example of how the setter error checking will look like + @property + def chromosome_length(self): + return self._chromosome_length + + @chromosome_length.setter + def chromosome_length(self, value_input): + if(value_input == 0): + raise ValueError("Sorry your chromosome length must be greater then 0") + self._chromosome_length = value_input diff --git a/src/run_testing.py b/src/run_testing.py index 96827e5..78765f0 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -4,7 +4,7 @@ import EasyGA # Create the Genetic algorithm ga = EasyGA.GA() -ga.chromosome_length = 0 +ga.chromosome_length = 100 ga.evolve()