Added tolerance-based termination
This commit is contained in:
@ -49,6 +49,7 @@ class Attributes:
|
||||
current_fitness = 0,
|
||||
generation_goal = 15,
|
||||
fitness_goal = None,
|
||||
tolerance_goal = 1e-3,
|
||||
chromosome_mutation_rate = 0.15,
|
||||
gene_mutation_rate = 0.03,
|
||||
initialization_impl = Initialization_Methods.random_initialization,
|
||||
@ -93,6 +94,7 @@ class Attributes:
|
||||
self.current_fitness = deepcopy(current_fitness)
|
||||
self.generation_goal = deepcopy(generation_goal)
|
||||
self.fitness_goal = deepcopy(fitness_goal)
|
||||
self.tolerance_goal = deepcopy(tolerance_goal)
|
||||
|
||||
# Mutation variables
|
||||
self.chromosome_mutation_rate = deepcopy(chromosome_mutation_rate)
|
||||
|
||||
@ -12,12 +12,31 @@ class Termination_Methods:
|
||||
if ga.fitness_goal is not None:
|
||||
|
||||
# If minimum fitness goal reached, stop ga.
|
||||
if ga.target_fitness_type == 'min' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal):
|
||||
if ga.target_fitness_type == 'min' and ga.population.get_chromosome(0).get_fitness() <= ga.fitness_goal:
|
||||
return False
|
||||
|
||||
# If maximum fitness goal reached, stop ga.
|
||||
elif 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.population.get_chromosome(0).get_fitness() >= ga.fitness_goal:
|
||||
return False
|
||||
|
||||
# If generation goal is set, check it.
|
||||
return ga.generation_goal is None or ga.current_generation < ga.generation_goal
|
||||
if ga.generation_goal is not None and ga.current_generation >= ga.generation_goal:
|
||||
return False
|
||||
|
||||
# If tolerance is set, check it.
|
||||
if ga.tolerance_goal is not None:
|
||||
|
||||
best_fitness = ga.population.get_chromosome(0).get_fitness()
|
||||
convergence_count = 0
|
||||
tol = ga.tolerance_goal * (1 + abs(best_fitness))
|
||||
|
||||
# Find out how many chromosomes have converged
|
||||
for chromosome in ga.population.get_chromosome_list():
|
||||
if abs(best_fitness - chromosome.get_fitness()) < tol:
|
||||
convergence_count += 1
|
||||
|
||||
# Terminate if 10% of the population has converged
|
||||
if convergence_count > 0.1*ga.population.size():
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user