Split into function decorators
This commit is contained in:
@ -1,16 +1,5 @@
|
|||||||
class Termination_Methods:
|
def add_by_fitness_goal(termination_impl):
|
||||||
"""Example functions that can be used to terminate the the algorithms loop"""
|
def helper(ga):
|
||||||
|
|
||||||
def fitness_generation_tolerance(ga):
|
|
||||||
"""Terminate GA when any of the
|
|
||||||
- fitness,
|
|
||||||
- generation, or
|
|
||||||
- tolerance
|
|
||||||
goals are met."""
|
|
||||||
|
|
||||||
# Need to start the algorithm if the population is None.
|
|
||||||
if ga.population == None:
|
|
||||||
return True
|
|
||||||
|
|
||||||
# If fitness goal is set, check it.
|
# If fitness goal is set, check it.
|
||||||
if ga.fitness_goal is not None:
|
if ga.fitness_goal is not None:
|
||||||
@ -23,13 +12,28 @@ class Termination_Methods:
|
|||||||
elif ga.target_fitness_type == 'max' and ga.population.get_chromosome(0).get_fitness() >= ga.fitness_goal:
|
elif ga.target_fitness_type == 'max' and ga.population.get_chromosome(0).get_fitness() >= ga.fitness_goal:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Check other termination methods
|
||||||
|
return termination_impl(ga)
|
||||||
|
return helper
|
||||||
|
|
||||||
|
|
||||||
|
def add_by_generation_goal(termination_impl):
|
||||||
|
def helper(ga):
|
||||||
|
|
||||||
# If generation goal is set, check it.
|
# If generation goal is set, check it.
|
||||||
if ga.generation_goal is not None and ga.current_generation >= ga.generation_goal:
|
if ga.fitness_goal is not None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Check other termination methods
|
||||||
|
return termination_impl(ga)
|
||||||
|
return helper
|
||||||
|
|
||||||
|
|
||||||
|
def add_by_tolerance_goal(termination_impl):
|
||||||
|
def helper(ga):
|
||||||
|
|
||||||
# If tolerance is set, check it.
|
# If tolerance is set, check it.
|
||||||
if ga.tolerance_goal is not None:
|
if ga.tolerance_goal is not None:
|
||||||
|
|
||||||
best_fitness = ga.population.get_chromosome(0).get_fitness()
|
best_fitness = ga.population.get_chromosome(0).get_fitness()
|
||||||
threshhold_fitness = ga.population.get_chromosome(int(ga.percent_converged*ga.population.size())).get_fitness()
|
threshhold_fitness = ga.population.get_chromosome(int(ga.percent_converged*ga.population.size())).get_fitness()
|
||||||
tol = ga.tolerance_goal * (1 + abs(best_fitness))
|
tol = ga.tolerance_goal * (1 + abs(best_fitness))
|
||||||
@ -38,4 +42,30 @@ class Termination_Methods:
|
|||||||
if abs(best_fitness - threshhold_fitness) < tol:
|
if abs(best_fitness - threshhold_fitness) < tol:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Check other termination methods
|
||||||
|
return termination_impl(ga)
|
||||||
|
return helper
|
||||||
|
|
||||||
|
|
||||||
|
class Termination_Methods:
|
||||||
|
"""Example functions that can be used to terminate the the algorithms loop"""
|
||||||
|
|
||||||
|
def __add_by_fitness_goal(termination_impl):
|
||||||
|
return add_by_fitness_goal(termination_impl)
|
||||||
|
def __add_by_generation_goal(termination_impl):
|
||||||
|
return add_by_generation_goal(termination_impl)
|
||||||
|
def __add_by_tolerance_goal(termination_impl):
|
||||||
|
return add_by_tolerance_goal(termination_impl)
|
||||||
|
|
||||||
|
|
||||||
|
@add_by_fitness_goal
|
||||||
|
@add_by_generation_goal
|
||||||
|
@add_by_tolerance_goal
|
||||||
|
def fitness_generation_tolerance(ga):
|
||||||
|
"""Terminate GA when any of the
|
||||||
|
- fitness,
|
||||||
|
- generation, or
|
||||||
|
- tolerance
|
||||||
|
goals are met."""
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user