Updated fitness based termination and fixed some EasyGA stuff
This commit is contained in:
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user