Neatified ga.print stuff and altered run_testing
This commit is contained in:
@ -119,3 +119,19 @@ class GA(attributes):
|
||||
max_fitness = self.population.get_chromosome(-1).get_fitness()
|
||||
min_fitness = self.population.get_chromosome(0).get_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()}")
|
||||
|
||||
@ -4,8 +4,12 @@ import EasyGA
|
||||
# Create the Genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
|
||||
# Mutate and reproduce frequently
|
||||
ga.parent_ratio = 0.25
|
||||
ga.mutation_rate = 0.25
|
||||
|
||||
# Create 25 chromosomes each with 10 genes
|
||||
ga.population_size = 25
|
||||
ga.population_size = 50
|
||||
ga.chromosome_length = 10
|
||||
|
||||
# Create random genes from 0 to 10
|
||||
@ -19,7 +23,9 @@ ga.target_fitness_type = 'min'
|
||||
ga.fitness_goal = 0
|
||||
ga.generation_goal = None
|
||||
|
||||
ga.evolve()
|
||||
|
||||
print(f"Current Generation: {ga.current_generation}")
|
||||
ga.population.print_all()
|
||||
while ga.active():
|
||||
ga.evolve_generation(10)
|
||||
ga.print_generation()
|
||||
ga.print_best()
|
||||
#ga.print_population()
|
||||
print('-'*75)
|
||||
|
||||
@ -16,7 +16,7 @@ class Termination_Methods:
|
||||
return False
|
||||
|
||||
# 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
|
||||
|
||||
# If generation goal is set, check it.
|
||||
|
||||
Reference in New Issue
Block a user