eckity.fitness.simple_fitness
1from overrides import override 2 3from eckity.fitness.fitness import Fitness 4 5 6class SimpleFitness(Fitness): 7 """ 8 This class is responsible for handling the fitness score of some Individual 9 (checking if fitness is evaluated, comparing fitness scores with other individuals etc.) 10 11 All simple classes assume only one sub-population. 12 In the simple case, each individual holds a float fitness score 13 14 fitness: float 15 the fitness score of an individual 16 17 higher_is_better: bool 18 declares the fitness direction. 19 i.e., if it should be minimized or maximized 20 21 cache: bool 22 declares whether the fitness score should reset at the end of each generation 23 24 is_relative_fitness: bool 25 declares whether the fitness score is absolute or relative 26 """ 27 28 def __init__( 29 self, 30 fitness: float = None, 31 higher_is_better: bool = False, 32 cache: bool = False, 33 is_relative_fitness: bool = False, 34 ): 35 is_evaluated = fitness is not None 36 super().__init__( 37 higher_is_better=higher_is_better, 38 is_evaluated=is_evaluated, 39 cache=cache, 40 is_relative_fitness=is_relative_fitness, 41 ) 42 self.fitness: float = fitness 43 44 def set_fitness(self, fitness): 45 """ 46 Updates the fitness score to `fitness` 47 48 Parameters 49 ---------- 50 fitness: float 51 the fitness score to be updated 52 """ 53 self.fitness = fitness 54 self._is_evaluated = True 55 56 @override 57 def get_pure_fitness(self): 58 """ 59 Returns the pure fitness score of the individual (before applying balancing methods like bloat control) 60 61 Returns 62 ---------- 63 float 64 fitness score of the individual 65 """ 66 if not self._is_evaluated: 67 raise ValueError("Fitness not evaluated yet") 68 return self.fitness 69 70 @override 71 def set_not_evaluated(self): 72 """ 73 Set this fitness score status to be not evaluated 74 """ 75 super().set_not_evaluated() 76 self.fitness = None 77 78 def check_comparable_fitness_scores(self, other_fitness): 79 """ 80 Check if `this` fitness score is comparable to `other_fitness` 81 82 Returns 83 ---------- 84 bool 85 True if fitness scores are comparable, False otherwise 86 """ 87 if not isinstance(other_fitness, SimpleFitness): 88 raise TypeError( 89 "Expected SimpleFitness object in better_than, got", type(other_fitness) 90 ) 91 if not self.is_fitness_evaluated() or not other_fitness.is_fitness_evaluated(): 92 raise ValueError("Fitness scores must be evaluated before comparison") 93 94 @override 95 def better_than(self, ind, other_fitness, other_ind): 96 """ 97 Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind` 98 In the simple case, compares the float fitness scores of the two individuals 99 100 Parameters 101 ---------- 102 ind: Individual 103 the individual instance that holds this Fitness instance 104 105 other_fitness: Fitness 106 the Fitness instance of the `other` individual 107 108 other_ind: Individual 109 the `other` individual instance which is being compared to the individual `ind` 110 111 Returns 112 ---------- 113 bool 114 True if this fitness score is better than the `other` fitness score, False otherwise 115 """ 116 self.check_comparable_fitness_scores(other_fitness) 117 return ( 118 self.get_augmented_fitness(ind) 119 > other_fitness.get_augmented_fitness(other_ind) 120 if self.higher_is_better 121 else self.get_augmented_fitness(ind) 122 < other_fitness.get_augmented_fitness(other_ind) 123 ) 124 125 @override 126 def equal_to(self, ind, other_fitness, other_ind): 127 """ 128 Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind` 129 In the simple case, compares the float fitness scores of the two individuals 130 131 Parameters 132 ---------- 133 ind: Individual 134 the individual instance that holds this Fitness instance 135 136 other_fitness: Fitness 137 the Fitness instance of the `other` individual 138 139 other_ind: Individual 140 the `other` individual instance which is being compared to the individual `ind` 141 142 Returns 143 ---------- 144 bool 145 True if this fitness score is equal to the `other` fitness score, False otherwise 146 """ 147 self.check_comparable_fitness_scores(other_fitness) 148 return self.get_augmented_fitness(ind) == other_fitness.get_augmented_fitness( 149 other_ind 150 ) 151 152 def __getstate__(self): 153 state = self.__dict__.copy() 154 if not self.cache: 155 state["_is_evaluated"] = False 156 state["fitness"] = None 157 return state
7class SimpleFitness(Fitness): 8 """ 9 This class is responsible for handling the fitness score of some Individual 10 (checking if fitness is evaluated, comparing fitness scores with other individuals etc.) 11 12 All simple classes assume only one sub-population. 13 In the simple case, each individual holds a float fitness score 14 15 fitness: float 16 the fitness score of an individual 17 18 higher_is_better: bool 19 declares the fitness direction. 20 i.e., if it should be minimized or maximized 21 22 cache: bool 23 declares whether the fitness score should reset at the end of each generation 24 25 is_relative_fitness: bool 26 declares whether the fitness score is absolute or relative 27 """ 28 29 def __init__( 30 self, 31 fitness: float = None, 32 higher_is_better: bool = False, 33 cache: bool = False, 34 is_relative_fitness: bool = False, 35 ): 36 is_evaluated = fitness is not None 37 super().__init__( 38 higher_is_better=higher_is_better, 39 is_evaluated=is_evaluated, 40 cache=cache, 41 is_relative_fitness=is_relative_fitness, 42 ) 43 self.fitness: float = fitness 44 45 def set_fitness(self, fitness): 46 """ 47 Updates the fitness score to `fitness` 48 49 Parameters 50 ---------- 51 fitness: float 52 the fitness score to be updated 53 """ 54 self.fitness = fitness 55 self._is_evaluated = True 56 57 @override 58 def get_pure_fitness(self): 59 """ 60 Returns the pure fitness score of the individual (before applying balancing methods like bloat control) 61 62 Returns 63 ---------- 64 float 65 fitness score of the individual 66 """ 67 if not self._is_evaluated: 68 raise ValueError("Fitness not evaluated yet") 69 return self.fitness 70 71 @override 72 def set_not_evaluated(self): 73 """ 74 Set this fitness score status to be not evaluated 75 """ 76 super().set_not_evaluated() 77 self.fitness = None 78 79 def check_comparable_fitness_scores(self, other_fitness): 80 """ 81 Check if `this` fitness score is comparable to `other_fitness` 82 83 Returns 84 ---------- 85 bool 86 True if fitness scores are comparable, False otherwise 87 """ 88 if not isinstance(other_fitness, SimpleFitness): 89 raise TypeError( 90 "Expected SimpleFitness object in better_than, got", type(other_fitness) 91 ) 92 if not self.is_fitness_evaluated() or not other_fitness.is_fitness_evaluated(): 93 raise ValueError("Fitness scores must be evaluated before comparison") 94 95 @override 96 def better_than(self, ind, other_fitness, other_ind): 97 """ 98 Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind` 99 In the simple case, compares the float fitness scores of the two individuals 100 101 Parameters 102 ---------- 103 ind: Individual 104 the individual instance that holds this Fitness instance 105 106 other_fitness: Fitness 107 the Fitness instance of the `other` individual 108 109 other_ind: Individual 110 the `other` individual instance which is being compared to the individual `ind` 111 112 Returns 113 ---------- 114 bool 115 True if this fitness score is better than the `other` fitness score, False otherwise 116 """ 117 self.check_comparable_fitness_scores(other_fitness) 118 return ( 119 self.get_augmented_fitness(ind) 120 > other_fitness.get_augmented_fitness(other_ind) 121 if self.higher_is_better 122 else self.get_augmented_fitness(ind) 123 < other_fitness.get_augmented_fitness(other_ind) 124 ) 125 126 @override 127 def equal_to(self, ind, other_fitness, other_ind): 128 """ 129 Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind` 130 In the simple case, compares the float fitness scores of the two individuals 131 132 Parameters 133 ---------- 134 ind: Individual 135 the individual instance that holds this Fitness instance 136 137 other_fitness: Fitness 138 the Fitness instance of the `other` individual 139 140 other_ind: Individual 141 the `other` individual instance which is being compared to the individual `ind` 142 143 Returns 144 ---------- 145 bool 146 True if this fitness score is equal to the `other` fitness score, False otherwise 147 """ 148 self.check_comparable_fitness_scores(other_fitness) 149 return self.get_augmented_fitness(ind) == other_fitness.get_augmented_fitness( 150 other_ind 151 ) 152 153 def __getstate__(self): 154 state = self.__dict__.copy() 155 if not self.cache: 156 state["_is_evaluated"] = False 157 state["fitness"] = None 158 return state
This class is responsible for handling the fitness score of some Individual (checking if fitness is evaluated, comparing fitness scores with other individuals etc.)
All simple classes assume only one sub-population. In the simple case, each individual holds a float fitness score
fitness: float the fitness score of an individual
higher_is_better: bool declares the fitness direction. i.e., if it should be minimized or maximized
cache: bool declares whether the fitness score should reset at the end of each generation
is_relative_fitness: bool declares whether the fitness score is absolute or relative
29 def __init__( 30 self, 31 fitness: float = None, 32 higher_is_better: bool = False, 33 cache: bool = False, 34 is_relative_fitness: bool = False, 35 ): 36 is_evaluated = fitness is not None 37 super().__init__( 38 higher_is_better=higher_is_better, 39 is_evaluated=is_evaluated, 40 cache=cache, 41 is_relative_fitness=is_relative_fitness, 42 ) 43 self.fitness: float = fitness
57 @override 58 def get_pure_fitness(self): 59 """ 60 Returns the pure fitness score of the individual (before applying balancing methods like bloat control) 61 62 Returns 63 ---------- 64 float 65 fitness score of the individual 66 """ 67 if not self._is_evaluated: 68 raise ValueError("Fitness not evaluated yet") 69 return self.fitness
Returns the pure fitness score of the individual (before applying balancing methods like bloat control)
Returns
- float: fitness score of the individual
71 @override 72 def set_not_evaluated(self): 73 """ 74 Set this fitness score status to be not evaluated 75 """ 76 super().set_not_evaluated() 77 self.fitness = None
Set this fitness score status to be not evaluated
79 def check_comparable_fitness_scores(self, other_fitness): 80 """ 81 Check if `this` fitness score is comparable to `other_fitness` 82 83 Returns 84 ---------- 85 bool 86 True if fitness scores are comparable, False otherwise 87 """ 88 if not isinstance(other_fitness, SimpleFitness): 89 raise TypeError( 90 "Expected SimpleFitness object in better_than, got", type(other_fitness) 91 ) 92 if not self.is_fitness_evaluated() or not other_fitness.is_fitness_evaluated(): 93 raise ValueError("Fitness scores must be evaluated before comparison")
Check if this fitness score is comparable to other_fitness
Returns
- bool: True if fitness scores are comparable, False otherwise
95 @override 96 def better_than(self, ind, other_fitness, other_ind): 97 """ 98 Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind` 99 In the simple case, compares the float fitness scores of the two individuals 100 101 Parameters 102 ---------- 103 ind: Individual 104 the individual instance that holds this Fitness instance 105 106 other_fitness: Fitness 107 the Fitness instance of the `other` individual 108 109 other_ind: Individual 110 the `other` individual instance which is being compared to the individual `ind` 111 112 Returns 113 ---------- 114 bool 115 True if this fitness score is better than the `other` fitness score, False otherwise 116 """ 117 self.check_comparable_fitness_scores(other_fitness) 118 return ( 119 self.get_augmented_fitness(ind) 120 > other_fitness.get_augmented_fitness(other_ind) 121 if self.higher_is_better 122 else self.get_augmented_fitness(ind) 123 < other_fitness.get_augmented_fitness(other_ind) 124 )
Compares between the current fitness of the individual ind to the fitness score other_fitness of other_ind
In the simple case, compares the float fitness scores of the two individuals
Parameters
- ind (Individual): the individual instance that holds this Fitness instance
- other_fitness (Fitness):
the Fitness instance of the
otherindividual - other_ind (Individual):
the
otherindividual instance which is being compared to the individualind
Returns
- bool: True if this fitness score is better than the
otherfitness score, False otherwise
126 @override 127 def equal_to(self, ind, other_fitness, other_ind): 128 """ 129 Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind` 130 In the simple case, compares the float fitness scores of the two individuals 131 132 Parameters 133 ---------- 134 ind: Individual 135 the individual instance that holds this Fitness instance 136 137 other_fitness: Fitness 138 the Fitness instance of the `other` individual 139 140 other_ind: Individual 141 the `other` individual instance which is being compared to the individual `ind` 142 143 Returns 144 ---------- 145 bool 146 True if this fitness score is equal to the `other` fitness score, False otherwise 147 """ 148 self.check_comparable_fitness_scores(other_fitness) 149 return self.get_augmented_fitness(ind) == other_fitness.get_augmented_fitness( 150 other_ind 151 )
Compares between the current fitness of the individual ind to the fitness score other_fitness of other_ind
In the simple case, compares the float fitness scores of the two individuals
Parameters
- ind (Individual): the individual instance that holds this Fitness instance
- other_fitness (Fitness):
the Fitness instance of the
otherindividual - other_ind (Individual):
the
otherindividual instance which is being compared to the individualind
Returns
- bool: True if this fitness score is equal to the
otherfitness score, False otherwise