eckity.fitness.gp_fitness
This module implements the GPFitness
class
1""" 2This module implements the `GPFitness` class 3""" 4 5from overrides import overrides 6 7from eckity.fitness.simple_fitness import SimpleFitness 8 9 10class GPFitness(SimpleFitness): 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 GPFitness also adds bloat control to the fitness score, by "punishing" the fitness score of large trees 17 18 fitness: float 19 the fitness score of an individual 20 21 higher_is_better: bool 22 declares the fitness direction. 23 i.e., if it should be minimized or maximized 24 25 bloat_weight: float 26 the weight of the bloat control fitness reduction 27 """ 28 def __init__(self, 29 fitness: float = None, 30 higher_is_better=False, 31 bloat_weight=0.1): 32 super().__init__(fitness=fitness, higher_is_better=higher_is_better) 33 self.bloat_weight = bloat_weight 34 35 @overrides 36 def get_augmented_fitness(self, individual): 37 """ 38 Returns the fixed fitness of a given individual, after including bloat control 39 40 Parameters 41 ---------- 42 individual: Individual 43 a GP Tree to apply bloat control on 44 45 Returns 46 ---------- 47 float 48 augmented fitness score after applying bloat control 49 """ 50 if not self.is_fitness_evaluated(): 51 raise ValueError('Fitness not evaluated yet') 52 53 # subtract bloat value from the fitness score if it should be maximized, 54 # otherwise add bloat value to fitness score 55 return self.fitness - self.bloat_weight * individual.size() \ 56 if self.higher_is_better \ 57 else self.fitness + self.bloat_weight * individual.size()
11class GPFitness(SimpleFitness): 12 """ 13 This class is responsible for handling the fitness score of some Individual 14 (checking if fitness is evaluated, comparing fitness scores with other individuals etc.) 15 16 In the simple case, each individual holds a float fitness score 17 GPFitness also adds bloat control to the fitness score, by "punishing" the fitness score of large trees 18 19 fitness: float 20 the fitness score of an individual 21 22 higher_is_better: bool 23 declares the fitness direction. 24 i.e., if it should be minimized or maximized 25 26 bloat_weight: float 27 the weight of the bloat control fitness reduction 28 """ 29 def __init__(self, 30 fitness: float = None, 31 higher_is_better=False, 32 bloat_weight=0.1): 33 super().__init__(fitness=fitness, higher_is_better=higher_is_better) 34 self.bloat_weight = bloat_weight 35 36 @overrides 37 def get_augmented_fitness(self, individual): 38 """ 39 Returns the fixed fitness of a given individual, after including bloat control 40 41 Parameters 42 ---------- 43 individual: Individual 44 a GP Tree to apply bloat control on 45 46 Returns 47 ---------- 48 float 49 augmented fitness score after applying bloat control 50 """ 51 if not self.is_fitness_evaluated(): 52 raise ValueError('Fitness not evaluated yet') 53 54 # subtract bloat value from the fitness score if it should be maximized, 55 # otherwise add bloat value to fitness score 56 return self.fitness - self.bloat_weight * individual.size() \ 57 if self.higher_is_better \ 58 else self.fitness + self.bloat_weight * individual.size()
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 GPFitness also adds bloat control to the fitness score, by "punishing" the fitness score of large trees
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
bloat_weight: float the weight of the bloat control fitness reduction
@overrides
def
get_augmented_fitness(self, individual):
36 @overrides 37 def get_augmented_fitness(self, individual): 38 """ 39 Returns the fixed fitness of a given individual, after including bloat control 40 41 Parameters 42 ---------- 43 individual: Individual 44 a GP Tree to apply bloat control on 45 46 Returns 47 ---------- 48 float 49 augmented fitness score after applying bloat control 50 """ 51 if not self.is_fitness_evaluated(): 52 raise ValueError('Fitness not evaluated yet') 53 54 # subtract bloat value from the fitness score if it should be maximized, 55 # otherwise add bloat value to fitness score 56 return self.fitness - self.bloat_weight * individual.size() \ 57 if self.higher_is_better \ 58 else self.fitness + self.bloat_weight * individual.size()
Returns the fixed fitness of a given individual, after including bloat control
Parameters
- individual (Individual): a GP Tree to apply bloat control on
Returns
- float: augmented fitness score after applying bloat control