Updated fitness based termination and fixed some EasyGA stuff

This commit is contained in:
SimpleArt
2020-10-22 00:05:42 -04:00
parent 8e2698fc0d
commit 6c6c64e342
3 changed files with 43 additions and 25 deletions

View File

@ -3,22 +3,23 @@ import EasyGA
# Create the Genetic algorithm
ga = EasyGA.GA()
ga.target_fitness_type = 'min'
ga.chromosome_length = 10
# Create 25 chromosomes each with 10 genes
ga.population_size = 25
ga.generation_goal = 50
ga.chromosome_length = 10
# Create random genes from 0 to 10
ga.gene_impl = lambda: random.randint(0, 10)
def fitness_function(chromosome):
return sum(
gene.get_value()
for gene in chromosome.get_gene_list())
# Minimize the sum of the genes
ga.fitness_function_impl = lambda chromosome: sum(gene.get_value() for gene in chromosome.get_gene_list())
ga.target_fitness_type = 'min'
ga.fitness_function_impl = fitness_function
# Terminate when a chromosome has all 0's
ga.fitness_goal = 0
ga.termination_impl = EasyGA.Termination_Methods.fitness_based
ga.evolve()
ga.set_all_fitness()
ga.population.sort_by_best_fitness(ga)
print(f"Current Generation: {ga.current_generation}")
ga.population.print_all()