Neatified ga.print stuff and altered run_testing

This commit is contained in:
SimpleArt
2020-10-22 19:05:43 -04:00
parent 50aa1587ae
commit c84758f8c5
3 changed files with 28 additions and 6 deletions

View File

@ -119,3 +119,19 @@ class GA(attributes):
max_fitness = self.population.get_chromosome(-1).get_fitness() max_fitness = self.population.get_chromosome(-1).get_fitness()
min_fitness = self.population.get_chromosome(0).get_fitness() min_fitness = self.population.get_chromosome(0).get_fitness()
return max_fitness - fitness_value + min_fitness return max_fitness - fitness_value + min_fitness
def print_generation(self):
"""Prints the current generation"""
print(f"Current Generation: {self.current_generation}")
def print_population(self):
"""Prints the entire population"""
self.population.print_all()
def print_best(self):
"""Prints the best chromosome and its fitness"""
print(f"Best Chromosome \t: {self.population.get_chromosome(0)}")
print(f"Best Fitness \t: {self.population.get_chromosome(0).get_fitness()}")

View File

@ -4,8 +4,12 @@ import EasyGA
# Create the Genetic algorithm # Create the Genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
# Mutate and reproduce frequently
ga.parent_ratio = 0.25
ga.mutation_rate = 0.25
# Create 25 chromosomes each with 10 genes # Create 25 chromosomes each with 10 genes
ga.population_size = 25 ga.population_size = 50
ga.chromosome_length = 10 ga.chromosome_length = 10
# Create random genes from 0 to 10 # Create random genes from 0 to 10
@ -19,7 +23,9 @@ ga.target_fitness_type = 'min'
ga.fitness_goal = 0 ga.fitness_goal = 0
ga.generation_goal = None ga.generation_goal = None
ga.evolve() while ga.active():
ga.evolve_generation(10)
print(f"Current Generation: {ga.current_generation}") ga.print_generation()
ga.population.print_all() ga.print_best()
#ga.print_population()
print('-'*75)

View File

@ -16,7 +16,7 @@ class Termination_Methods:
return False return False
# If maximum fitness goal reached, stop ga. # If maximum fitness goal reached, stop ga.
if ga.target_fitness_type == 'max' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal): elif ga.target_fitness_type == 'max' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal):
return False return False
# If generation goal is set, check it. # If generation goal is set, check it.