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