eckity.termination_checkers.threshold_from_target_termination_checker
1from eckity.termination_checkers.termination_checker import TerminationChecker 2 3 4class ThresholdFromTargetTerminationChecker(TerminationChecker): 5 """ 6 Concrete Termination Checker that checks the distance from best existing fitness value to target fitness value. 7 8 Parameters 9 ---------- 10 optimal: float, default=0. 11 Target fitness value. 12 This termination checker checks if the currently best fitness is "close enough" to the optimal value. 13 14 threshold: float, default=0. 15 How close should the current best fitness be to the target fitness. 16 17 higher_is_better: bool, default=False 18 Determines if higher fitness values are better. 19 """ 20 def __init__(self, optimal=0., threshold=0., higher_is_better=False): 21 super().__init__() 22 self.optimal = optimal 23 self.threshold = threshold 24 self.higher_is_better = higher_is_better 25 26 def should_terminate(self, population, best_individual, gen_number): 27 """ 28 Determines if the currently best fitness is close enough to the target fitness. 29 If so, recommends the algorithm to terminate early. 30 31 Parameters 32 ---------- 33 population: Population 34 The evolutionary experiment population of individuals. 35 36 best_individual: Individual 37 The individual that has the best fitness of the current generation. 38 39 gen_number: int 40 Current generation number. 41 42 Returns 43 ------- 44 bool 45 True if the algorithm should terminate early, False otherwise. 46 """ 47 return abs(best_individual.get_pure_fitness() - self.optimal) <= self.threshold
class
ThresholdFromTargetTerminationChecker(eckity.termination_checkers.termination_checker.TerminationChecker):
5class ThresholdFromTargetTerminationChecker(TerminationChecker): 6 """ 7 Concrete Termination Checker that checks the distance from best existing fitness value to target fitness value. 8 9 Parameters 10 ---------- 11 optimal: float, default=0. 12 Target fitness value. 13 This termination checker checks if the currently best fitness is "close enough" to the optimal value. 14 15 threshold: float, default=0. 16 How close should the current best fitness be to the target fitness. 17 18 higher_is_better: bool, default=False 19 Determines if higher fitness values are better. 20 """ 21 def __init__(self, optimal=0., threshold=0., higher_is_better=False): 22 super().__init__() 23 self.optimal = optimal 24 self.threshold = threshold 25 self.higher_is_better = higher_is_better 26 27 def should_terminate(self, population, best_individual, gen_number): 28 """ 29 Determines if the currently best fitness is close enough to the target fitness. 30 If so, recommends the algorithm to terminate early. 31 32 Parameters 33 ---------- 34 population: Population 35 The evolutionary experiment population of individuals. 36 37 best_individual: Individual 38 The individual that has the best fitness of the current generation. 39 40 gen_number: int 41 Current generation number. 42 43 Returns 44 ------- 45 bool 46 True if the algorithm should terminate early, False otherwise. 47 """ 48 return abs(best_individual.get_pure_fitness() - self.optimal) <= self.threshold
Concrete Termination Checker that checks the distance from best existing fitness value to target fitness value.
Parameters
- optimal (float, default=0.): Target fitness value. This termination checker checks if the currently best fitness is "close enough" to the optimal value.
- threshold (float, default=0.): How close should the current best fitness be to the target fitness.
- higher_is_better (bool, default=False): Determines if higher fitness values are better.
def
should_terminate(self, population, best_individual, gen_number):
27 def should_terminate(self, population, best_individual, gen_number): 28 """ 29 Determines if the currently best fitness is close enough to the target fitness. 30 If so, recommends the algorithm to terminate early. 31 32 Parameters 33 ---------- 34 population: Population 35 The evolutionary experiment population of individuals. 36 37 best_individual: Individual 38 The individual that has the best fitness of the current generation. 39 40 gen_number: int 41 Current generation number. 42 43 Returns 44 ------- 45 bool 46 True if the algorithm should terminate early, False otherwise. 47 """ 48 return abs(best_individual.get_pure_fitness() - self.optimal) <= self.threshold
Determines if the currently best fitness is close enough to the target fitness. If so, recommends the algorithm to terminate early.
Parameters
- population (Population): The evolutionary experiment population of individuals.
- best_individual (Individual): The individual that has the best fitness of the current generation.
- gen_number (int): Current generation number.
Returns
- bool: True if the algorithm should terminate early, False otherwise.