diff --git a/src/EasyGA.py b/src/EasyGA.py index 65a1688..6a3590d 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -5,7 +5,7 @@ from initialization import population as create_population from initialization import chromosome as create_chromosome from initialization import gene as create_gene # Import the default fitness function -from fitness_function import is_the_value_5 +from fitness_function import example_is_it_5 # Import default termination points from termination_point import generation_based from termination_point import fitness_based @@ -29,14 +29,14 @@ class GA: # Mutation variables self.mutation_rate = 0.03 + # Defualt EastGA implimentation structure self.initialization_impl = random_initialization - self.fitness_funciton_impl = is_the_value_5 + self.fitness_funciton_impl = example_is_it_5 #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() #self.crossover_impl = FastSinglePointCrossover() self.termination_impl = generation_based - #self.evaluation_impl = TestEvaluation() # If we want the fitnesses to be updated by the computer self.update_fitness = True @@ -61,17 +61,7 @@ class GA: """Runs the ga until the termination point has been satisfied.""" # While the termination point hasnt been reached keep running while(self.active()): - # If its the first generation then initialize the population - if(self.current_generation == 0): - # Initialize the population - self.initialize_population() - # First get the fitness of the population - self.get_population_fitness(self.population.chromosomes) - - print(f"Ive completed generation - {self.current_generation}") - - self.current_generation += 1 - + self.evolve_generation() def active(self): @@ -80,17 +70,30 @@ class GA: return self.termination_impl(self) - def evolve_generation(self, number_of_generations): + def evolve_generation(self, number_of_generations = 1): """Evolves the ga the specified number of generations. If update_fitness is set then all fitness values are updated. Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated.""" + while(number_of_generations > 0): + # If its the first generation then initialize the population + if(self.current_generation == 0): + # Initialize the population + self.initialize_population() + # First get the fitness of the population + self.get_population_fitness(self.population.chromosomes) - # run the specified number of times - #for n in range(number_of_generations): + #selecion -> crossover -> mutation + #self.selection_impl(self) + #self.crossover_impl(self) + #self.mutation_impl(self) + #next_population.append(mutated_offspring) - # apply selection, crossover, and mutation + # Counter for the local number of generations in evolve_generation + number_of_generations -= 1 + # Add one to the current overall generation + self.current_generation += 1 def make_gene(self,value): """Let's the user create a gene.""" diff --git a/src/fitness_function/__init__.py b/src/fitness_function/__init__.py index eb0a719..100b611 100644 --- a/src/fitness_function/__init__.py +++ b/src/fitness_function/__init__.py @@ -1,2 +1,2 @@ # FROM (. means local) file_name IMPORT function_name -from .is_the_value_5 import is_the_value_5 +from .example_is_it_5 import example_is_it_5 diff --git a/src/fitness_function/is_the_value_5.py b/src/fitness_function/example_is_it_5.py similarity index 93% rename from src/fitness_function/is_the_value_5.py rename to src/fitness_function/example_is_it_5.py index 61d5428..3546373 100644 --- a/src/fitness_function/is_the_value_5.py +++ b/src/fitness_function/example_is_it_5.py @@ -1,7 +1,6 @@ -def is_the_value_5(chromosome): +def example_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.""" - # Overall fitness value fitness = 0 # For each gene in the chromosome diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index e83c9fc..aaec88f 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,12 +1,15 @@ class chromosome: - + def __init__(self, genes = None): """Initialize the chromosome based on input gene list, defaulted to an empty list""" if genes is None: self.genes = [] else: self.genes = genes + # The fitness of the overal chromosome self.fitness = None + # If the chromosome has been selected then the flag would switch to true + self.selected = False def add_gene(self, gene, index = -1): """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" diff --git a/src/run_testing.py b/src/run_testing.py index 6bb59b9..acc5f86 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -8,6 +8,8 @@ ga.max_generations = 5 # If the user wants to use a domain ga.gene_impl = [random.randrange,1,10] +ga.generation = 36 + # Run Everyhting ga.evolve() diff --git a/src/termination_point/generation_based.py b/src/termination_point/generation_based.py index 9bf2005..11df411 100644 --- a/src/termination_point/generation_based.py +++ b/src/termination_point/generation_based.py @@ -1,4 +1,6 @@ def generation_based(ga): + """Generation based approach to termination - If the current generation is + less then the """ status = True if(ga.current_generation > ga.max_generations): status = False