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    cache: bool
26        declares whether the fitness score should reset at the end of each generation
27
28    is_relative_fitness: bool
29        declares whether the fitness score is absolute or relative
30
31    bloat_weight: float
32        the weight of the bloat control fitness reduction
33    """
34
35    def __init__(
36        self,
37        fitness: float = None,
38        higher_is_better: bool = False,
39        cache: bool = False,
40        is_relative_fitness: bool = False,
41        bloat_weight: float = 0.1,
42    ):
43        super().__init__(
44            fitness=fitness,
45            higher_is_better=higher_is_better,
46            cache=cache,
47            is_relative_fitness=is_relative_fitness,
48        )
49        self.bloat_weight = bloat_weight
50
51    @overrides
52    def get_augmented_fitness(self, individual):
53        """
54        Returns the fixed fitness of a given individual, after including bloat control
55
56        Parameters
57        ----------
58        individual: Individual
59            a GP Tree to apply bloat control on
60
61        Returns
62        ----------
63        float
64            augmented fitness score after applying bloat control
65        """
66        score = self.get_pure_fitness()
67
68        if self.bloat_weight == 0:
69            # no bloat
70            return score
71
72        # subtract bloat value from the fitness score if it should be maximized,
73        # otherwise add bloat value to fitness score
74        return (
75            score - self.bloat_weight * individual.size()
76            if self.higher_is_better
77            else score + self.bloat_weight * individual.size()
78        )
class GPFitness(eckity.fitness.simple_fitness.SimpleFitness):
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    cache: bool
27        declares whether the fitness score should reset at the end of each generation
28
29    is_relative_fitness: bool
30        declares whether the fitness score is absolute or relative
31
32    bloat_weight: float
33        the weight of the bloat control fitness reduction
34    """
35
36    def __init__(
37        self,
38        fitness: float = None,
39        higher_is_better: bool = False,
40        cache: bool = False,
41        is_relative_fitness: bool = False,
42        bloat_weight: float = 0.1,
43    ):
44        super().__init__(
45            fitness=fitness,
46            higher_is_better=higher_is_better,
47            cache=cache,
48            is_relative_fitness=is_relative_fitness,
49        )
50        self.bloat_weight = bloat_weight
51
52    @overrides
53    def get_augmented_fitness(self, individual):
54        """
55        Returns the fixed fitness of a given individual, after including bloat control
56
57        Parameters
58        ----------
59        individual: Individual
60            a GP Tree to apply bloat control on
61
62        Returns
63        ----------
64        float
65            augmented fitness score after applying bloat control
66        """
67        score = self.get_pure_fitness()
68
69        if self.bloat_weight == 0:
70            # no bloat
71            return score
72
73        # subtract bloat value from the fitness score if it should be maximized,
74        # otherwise add bloat value to fitness score
75        return (
76            score - self.bloat_weight * individual.size()
77            if self.higher_is_better
78            else score + self.bloat_weight * individual.size()
79        )

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

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

bloat_weight: float the weight of the bloat control fitness reduction

GPFitness( fitness: float = None, higher_is_better: bool = False, cache: bool = False, is_relative_fitness: bool = False, bloat_weight: float = 0.1)
36    def __init__(
37        self,
38        fitness: float = None,
39        higher_is_better: bool = False,
40        cache: bool = False,
41        is_relative_fitness: bool = False,
42        bloat_weight: float = 0.1,
43    ):
44        super().__init__(
45            fitness=fitness,
46            higher_is_better=higher_is_better,
47            cache=cache,
48            is_relative_fitness=is_relative_fitness,
49        )
50        self.bloat_weight = bloat_weight
bloat_weight
@overrides
def get_augmented_fitness(self, individual):
52    @overrides
53    def get_augmented_fitness(self, individual):
54        """
55        Returns the fixed fitness of a given individual, after including bloat control
56
57        Parameters
58        ----------
59        individual: Individual
60            a GP Tree to apply bloat control on
61
62        Returns
63        ----------
64        float
65            augmented fitness score after applying bloat control
66        """
67        score = self.get_pure_fitness()
68
69        if self.bloat_weight == 0:
70            # no bloat
71            return score
72
73        # subtract bloat value from the fitness score if it should be maximized,
74        # otherwise add bloat value to fitness score
75        return (
76            score - self.bloat_weight * individual.size()
77            if self.higher_is_better
78            else score + self.bloat_weight * individual.size()
79        )

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