diff --git a/src/EasyGA.py b/src/EasyGA.py index f11090e..af45f78 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,20 +1,25 @@ import random + # Import all the data structure prebuilt modules from initialization import Population as create_population from initialization import Chromosome as create_chromosome from initialization import Gene as create_gene + # Structure Methods from fitness_function import Fitness_Examples from initialization import Initialization_Methods from termination_point import Termination_Methods + # Population Methods from survivor_selection import Survivor_Selection from parent_selection import Parent_Selection + # Manipulation Methods from mutation import Mutation_Methods from crossover import Crossover_Methods class GA: + def __init__(self): """Initialize the GA.""" # Initilization variables @@ -44,12 +49,15 @@ class GA: # Default EasyGA implimentation structure self.initialization_impl = Initialization_Methods.random_initialization self.fitness_function_impl = Fitness_Examples.index_dependent_values + # Selects which chromosomes should be automaticly moved to the next population self.survivor_selection_impl = Survivor_Selection.fill_in_best + # Methods for accomplishing parent-selection -> Crossover -> Mutation self.parent_selection_impl = Parent_Selection.Tournament.with_replacement self.crossover_impl = Crossover_Methods.single_point_crossover self.mutation_impl = Mutation_Methods.per_gene_mutation + # The type of termination to impliment self.termination_impl = Termination_Methods.generation_based @@ -76,22 +84,26 @@ class GA: self.current_generation += 1 + def evolve(self): """Runs the ga until the termination point has been satisfied.""" # While the termination point hasnt been reached keep running while(self.active()): self.evolve_generation() + def active(self): """Returns if the ga should terminate base on the termination implimented""" # Send termination_impl the whole ga class return self.termination_impl(self) + def initialize_population(self): """Initialize the population using the initialization implimentation that is currently set""" self.population = self.initialization_impl(self) + def set_all_fitness(self,chromosome_set): """Will get and set the fitness of each chromosome in the population. If update_fitness is set then all fitness values are updated. @@ -104,6 +116,7 @@ class GA: # Set the chromosomes fitness using the fitness function chromosome.set_fitness(self.fitness_function_impl(chromosome)) + def sort_by_best_fitness(self, chromosome_set): chromosome_set_temp = chromosome_set @@ -120,14 +133,17 @@ class GA: return chromosome_set + def make_gene(self,value): """Let's the user create a gene.""" return create_gene(value) + def make_chromosome(self): """Let's the user create a chromosome.""" return create_chromosome() + def make_population(self): """Let's the user create a population.""" return create_population() diff --git a/src/fitness_function/fitness_examples.py b/src/fitness_function/fitness_examples.py index a5bbd83..08b78db 100644 --- a/src/fitness_function/fitness_examples.py +++ b/src/fitness_function/fitness_examples.py @@ -1,5 +1,6 @@ class Fitness_Examples: """Fitness function examples used""" + def is_it_5(chromosome): """A very simple case test function - If the chromosomes gene value is a 5 add one to the chromosomes overall fitness value.""" @@ -15,6 +16,7 @@ class Fitness_Examples: return fitness + def index_dependent_values(chromosome): """Test of the GA's ability to improve fitness when the value is index-dependent""" """If a gene is equal to its index in the chromosome + 1, fitness is incremented""" diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index 6e418a1..2f20935 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,4 +1,5 @@ class Chromosome: + def __init__(self, gene_list = None): if gene_list is None: self.gene_list = [] @@ -9,36 +10,45 @@ class Chromosome: # If the chromosome has been selected then the flag would switch to true self.selected = False + def size(self): """Returns the number of genes in the chromosome""" return len(self.gene_list) + def add_gene(self, gene, index = -1): """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" if index == -1: index = len(self.gene_list) self.gene_list.insert(index, gene) + def remove_gene(self, index): del self.gene_list[index] + def get_genes(self): return self.gene_list + def get_fitness(self): """Return the fitness of the chromosome""" return self.fitness + def set_gene(self, gene, index): self.gene_list[index] = gene + def set_genes(self, genes): self.gene_list = genes + def set_fitness(self, fitness): """Set the fitness value of the chromosome""" self.fitness = fitness + def __repr__(self): """Format the repr() output for the chromosome""" output_str = '' diff --git a/src/initialization/gene_structure/gene.py b/src/initialization/gene_structure/gene.py index 04ecb53..3729d50 100644 --- a/src/initialization/gene_structure/gene.py +++ b/src/initialization/gene_structure/gene.py @@ -3,6 +3,7 @@ def check_gene(value): assert value != "" , "Gene can not be empty" return value + class Gene: def __init__(self, value): @@ -10,22 +11,27 @@ class Gene: self.fitness = None self.value = check_gene(value) + def get_fitness(self): """Return fitness of the gene""" return self.fitness + def get_value(self): """Return value of the gene""" return self.value + def set_fitness(self, fitness): """Set fitness of the gene""" self.fitness = fitness + def set_value(self, value): """Set value of the gene""" self.value = value + def __repr__(self): """Format the repr() output value""" return f'[{self.value}]' diff --git a/src/initialization/initialization_methods.py b/src/initialization/initialization_methods.py index 05768b6..3cf3e35 100644 --- a/src/initialization/initialization_methods.py +++ b/src/initialization/initialization_methods.py @@ -5,7 +5,7 @@ from .gene_structure.gene import Gene as create_gene class Initialization_Methods: """Initialization examples that are used as defaults and examples""" - + def random_initialization(ga): """Takes the initialization inputs and choregraphs them to output the type of population with the given parameters.""" @@ -28,5 +28,6 @@ class Initialization_Methods: else: #Exit because either were not specified print("You did not specify any initialization constraints.") + break population.add_chromosome(chromosome) return population diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index 435af04..65fad47 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -1,60 +1,79 @@ class Population: + def __init__(self, chromosome_list = None): """Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter""" + if chromosome_list is None: self.chromosome_list = [] else: self.chromosome_list = chromosome_list + self.fitness = None self.mating_pool = [] + def size(self): """Returns the size of the population""" return len(self.chromosome_list) + def get_closet_fitness(self,value): """Get the chomosome that has the closets fitness to the value defined""" pass + def add_chromosome(self, chromosome, index = -1): """Adds a chromosome to the population at the input index, defaulted to the end of the chromosome set""" if index == -1: index = len(self.chromosome_list) self.chromosome_list.insert(index, chromosome) + def remove_chromosome(self, index): """removes a chromosome from the indicated index""" del self.chromosome_list[index] + def get_all_chromosomes(self): """returns all chromosomes in the population""" return self.chromosome_list + def get_fitness(self): """returns the population's fitness""" return self.fitness + def set_all_chromosomes(self, chromosomes): self.chromosome_list = chromosomes + def set_chromosome(self, chromosome, index = -1): if index == -1: index = len(self.chromosomes)-1 self.chromosome_list[index] = chromosome + def set_fitness(self, fitness): """Sets the fitness value of the population""" self.fitness = fitness + def __repr__(self): - for index in range(len(self.chromosomes)): - return f'{self.chromosome_list[index]}' + """Returns a string representation of the entire population""" + pass + def print_all(self): - """Prints information about the population in the following format:""" - """Ex .Current population""" - """Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - """ + """Prints information about the population in the following format: + Current population + Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - + Chromosome 2 - [gene][gene][gene][.etc] / Chromosome fitness - + etc. + """ + print("Current population:") - for index in range(len(self.chromosome_list)): + + for index in range(self.size()): print(f'Chromosome - {index} {self.chromosome_list[index]}', end = "") - print(f' / Fitness = {self.chromosome_list[index].fitness}') + print(f' / Fitness = {self.chromosome_list[index].get_fitness()}') diff --git a/src/parent_selection/parent_selection_methods.py b/src/parent_selection/parent_selection_methods.py index 14b348e..5288c72 100644 --- a/src/parent_selection/parent_selection_methods.py +++ b/src/parent_selection/parent_selection_methods.py @@ -5,7 +5,9 @@ from initialization.population_structure.population import Population from initialization.chromosome_structure.chromosome import Chromosome class Parent_Selection: + class Tournament: + def with_replacement(ga): """ Will make tournaments of size tournament_size and choose the winner (best fitness) from the tournament and use it as a parent for the next generation @@ -33,7 +35,9 @@ class Parent_Selection: if random.uniform(0, 1) < selection_probability * pow(1-selection_probability, index): ga.population.mating_pool.append(tournament_group[index]) + class Roulette: + def roulette_selection(ga): """Roulette selection works based off of how strong the fitness is of the chromosomes in the population. The stronger the fitness the higher the probability @@ -41,22 +45,23 @@ class Parent_Selection: Where the chromosomes are the numbers to be selected and the board size for those numbers are directly proportional to the chromosome's current fitness. Where the ball falls is a randomly generated number between 0 and 1""" + total_fitness = sum(ga.population.chromosome_list[i].get_fitness() for i in range(ga.population.size())) rel_fitnesses = [] - + for chromosome in ga.population.chromosome_list: if (total_fitness != 0): rel_fitnesses.append(float(chromosome.fitness)/total_fitness) probability = [sum(rel_fitnesses[:i+1]) for i in range(len(rel_fitnesses))] - + while (len(ga.population.mating_pool) < ga.population.size()*ga.parent_ratio): rand_number = random.random() - + # Loop through the list of probabilities for i in range(len(probability)): # If the probability is greater than the random_number, then select that chromosome if (probability[i] >= rand_number): ga.population.mating_pool.append(ga.population.chromosome_list[i]) # print (f'Selected chromosome : {i}') - break \ No newline at end of file + break diff --git a/src/run_testing.py b/src/run_testing.py index 79b6599..b4abb47 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -1,5 +1,5 @@ -import EasyGA import random +import EasyGA # Create the Genetic algorithm ga = EasyGA.GA() @@ -8,7 +8,8 @@ ga.population_size = 100 ga.chromosome_length = 10 ga.generation_goal = 100 ga.gene_impl = [random.randint,1,10] +ga.parent_selection_impl = EasyGA.Parent_Selection.Roulette.roulette_selection ga.evolve() -ga.population.print_all() \ No newline at end of file +ga.population.print_all() diff --git a/src/termination_point/termination_methods.py b/src/termination_point/termination_methods.py index f382dd4..c6a016c 100644 --- a/src/termination_point/termination_methods.py +++ b/src/termination_point/termination_methods.py @@ -3,15 +3,23 @@ class Termination_Methods: def fitness_based(ga): """Fitness based approach to terminate when the goal fitness has been reached""" - + + # Need to start the algorithm if the population is None if ga.population == None: return True - for i in range(ga.population.size()): - if(ga.population.get_all_chromosomes()[i].fitness >= ga.fitness_goal): + + # Check all chromosomes + for chromosome in ga.population.get_all_chromosomes(): + + # Stop if a chromosome has reached the fitness_goal + if(chromosome.fitness >= ga.fitness_goal): return False + + # Continue if no chromosomes have reached the fitness goal return True + def generation_based(ga): """Generation based approach to terminate when the goal generation has been reached""" - + return ga.current_generation < ga.generation_goal