Using try except
This commit is contained in:
@ -3,8 +3,8 @@ def _add_by_fitness_goal(termination_impl):
|
|||||||
|
|
||||||
def new_method(ga):
|
def new_method(ga):
|
||||||
|
|
||||||
# If fitness goal is set, check it.
|
# Try to check the fitness goal
|
||||||
if ga.fitness_goal is not None and ga.population is not None:
|
try:
|
||||||
|
|
||||||
# If minimum fitness goal reached, stop ga.
|
# If minimum fitness goal reached, stop ga.
|
||||||
if ga.target_fitness_type == 'min' and ga.population[0].fitness <= ga.fitness_goal:
|
if ga.target_fitness_type == 'min' and ga.population[0].fitness <= ga.fitness_goal:
|
||||||
@ -14,6 +14,14 @@ def _add_by_fitness_goal(termination_impl):
|
|||||||
elif ga.target_fitness_type == 'max' and ga.population[0].fitness >= ga.fitness_goal:
|
elif ga.target_fitness_type == 'max' and ga.population[0].fitness >= ga.fitness_goal:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Fitness or fitness goals are None
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Population not initialized
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
# Check other termination methods
|
# Check other termination methods
|
||||||
return termination_impl(ga)
|
return termination_impl(ga)
|
||||||
|
|
||||||
@ -42,8 +50,8 @@ def _add_by_tolerance_goal(termination_impl):
|
|||||||
|
|
||||||
def new_method(ga):
|
def new_method(ga):
|
||||||
|
|
||||||
# If tolerance is set, check it.
|
# If tolerance is set, check it, if possible.
|
||||||
if ga.tolerance_goal is not None and ga.population is not None:
|
try:
|
||||||
best_fitness = ga.population[0].fitness
|
best_fitness = ga.population[0].fitness
|
||||||
threshhold_fitness = ga.population[round(ga.percent_converged*len(ga.population))].fitness
|
threshhold_fitness = ga.population[round(ga.percent_converged*len(ga.population))].fitness
|
||||||
tol = ga.tolerance_goal * (1 + abs(best_fitness))
|
tol = ga.tolerance_goal * (1 + abs(best_fitness))
|
||||||
@ -52,6 +60,14 @@ def _add_by_tolerance_goal(termination_impl):
|
|||||||
if abs(best_fitness - threshhold_fitness) < tol:
|
if abs(best_fitness - threshhold_fitness) < tol:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Fitness or tolerance goals are None
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Population not initialized
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
# Check other termination methods
|
# Check other termination methods
|
||||||
return termination_impl(ga)
|
return termination_impl(ga)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user