eckity.fitness.fitness

This module implements the class Fitness

  1"""
  2This module implements the class `Fitness`
  3"""
  4
  5from abc import abstractmethod, ABC
  6
  7
  8class Fitness(ABC):
  9    """
 10    This class is responsible for handling the fitness score of some Individual
 11    (checking if fitness is evaluated, comparing fitness scores with other individuals etc.)
 12
 13    is_evaluated: bool
 14        declares if fitness score is evaluated and updated in the current generation
 15
 16    higher_is_better: bool
 17        declares the fitness direction.
 18        i.e., if it should be minimized or maximized
 19
 20    cache: bool
 21        declares whether the fitness score should reset at the end of each generation
 22
 23    is_relative_fitness: bool
 24        declares whether the fitness score is absolute or relative
 25    """
 26
 27    def __init__(
 28        self,
 29        is_evaluated: bool = False,
 30        higher_is_better: bool = None,
 31        is_relative_fitness: bool = False,
 32        cache: bool = False,
 33    ):
 34        self._is_evaluated = is_evaluated
 35        self.is_relative_fitness = is_relative_fitness
 36        self.cache = False if is_relative_fitness else cache
 37
 38        if not isinstance(higher_is_better, bool):
 39            raise ValueError("higher_is_better must be boolean")
 40        self.higher_is_better = higher_is_better
 41
 42    @abstractmethod
 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        raise ValueError("get_pure_fitness is an abstract method in class Fitness")
 48
 49    def get_augmented_fitness(self, individual):
 50        """
 51        Returns the fixed fitness score of the individual (after applying balancing methods like bloat control)
 52        By default, returns the pure fitness score
 53
 54        Parameters
 55        ----------
 56        individual: Individual
 57            the individual instance that holds this Fitness instance
 58
 59        Returns
 60        ----------
 61        object
 62            Fixed fitness value for the given individual
 63        """
 64        return self.get_pure_fitness()
 65
 66    @abstractmethod
 67    def better_than(self, ind, other_fitness, other_ind):
 68        """
 69        Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind`
 70
 71        Parameters
 72        ----------
 73        ind: Individual
 74            the individual instance that holds this Fitness instance
 75
 76        other_fitness: Fitness
 77            the Fitness instance of the `other` individual
 78
 79        other_ind: Individual
 80            the `other` individual instance which is being compared to the individual `ind`
 81
 82        Returns
 83        ----------
 84        bool
 85            True if this fitness is better than the `other` fitness, False otherwise
 86        """
 87        raise ValueError("better_than is an abstract method in class Fitness")
 88
 89    @abstractmethod
 90    def equal_to(self, ind, other_fitness, other_ind):
 91        """
 92        Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind`
 93
 94        Parameters
 95        ----------
 96        ind: Individual
 97            the individual instance that holds this Fitness instance
 98
 99        other_fitness: Fitness
100            the Fitness instance of the `other` individual
101
102        other_ind: Individual
103            the `other` individual instance which is being compared to the individual `ind`
104
105        Returns
106        ----------
107        bool
108            True if this fitness is equal to the `other` fitness, False otherwise
109        """
110        raise ValueError("better_than is an abstract method in class Fitness")
111
112    def set_not_evaluated(self):
113        """
114        Set this fitness score status to be not evaluated
115        """
116        self._is_evaluated = False
117
118    def is_fitness_evaluated(self):
119        """
120        Check this fitness score status (if the fitness score is updated)
121
122        Returns
123        ----------
124        bool
125            True if this fitness is evaluated, False otherwise
126        """
127        if self.is_relative_fitness:
128            return True
129        return self._is_evaluated
class Fitness(abc.ABC):
  9class Fitness(ABC):
 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    is_evaluated: bool
 15        declares if fitness score is evaluated and updated in the current generation
 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        is_evaluated: bool = False,
 31        higher_is_better: bool = None,
 32        is_relative_fitness: bool = False,
 33        cache: bool = False,
 34    ):
 35        self._is_evaluated = is_evaluated
 36        self.is_relative_fitness = is_relative_fitness
 37        self.cache = False if is_relative_fitness else cache
 38
 39        if not isinstance(higher_is_better, bool):
 40            raise ValueError("higher_is_better must be boolean")
 41        self.higher_is_better = higher_is_better
 42
 43    @abstractmethod
 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        raise ValueError("get_pure_fitness is an abstract method in class Fitness")
 49
 50    def get_augmented_fitness(self, individual):
 51        """
 52        Returns the fixed fitness score of the individual (after applying balancing methods like bloat control)
 53        By default, returns the pure fitness score
 54
 55        Parameters
 56        ----------
 57        individual: Individual
 58            the individual instance that holds this Fitness instance
 59
 60        Returns
 61        ----------
 62        object
 63            Fixed fitness value for the given individual
 64        """
 65        return self.get_pure_fitness()
 66
 67    @abstractmethod
 68    def better_than(self, ind, other_fitness, other_ind):
 69        """
 70        Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind`
 71
 72        Parameters
 73        ----------
 74        ind: Individual
 75            the individual instance that holds this Fitness instance
 76
 77        other_fitness: Fitness
 78            the Fitness instance of the `other` individual
 79
 80        other_ind: Individual
 81            the `other` individual instance which is being compared to the individual `ind`
 82
 83        Returns
 84        ----------
 85        bool
 86            True if this fitness is better than the `other` fitness, False otherwise
 87        """
 88        raise ValueError("better_than is an abstract method in class Fitness")
 89
 90    @abstractmethod
 91    def equal_to(self, ind, other_fitness, other_ind):
 92        """
 93        Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind`
 94
 95        Parameters
 96        ----------
 97        ind: Individual
 98            the individual instance that holds this Fitness instance
 99
100        other_fitness: Fitness
101            the Fitness instance of the `other` individual
102
103        other_ind: Individual
104            the `other` individual instance which is being compared to the individual `ind`
105
106        Returns
107        ----------
108        bool
109            True if this fitness is equal to the `other` fitness, False otherwise
110        """
111        raise ValueError("better_than is an abstract method in class Fitness")
112
113    def set_not_evaluated(self):
114        """
115        Set this fitness score status to be not evaluated
116        """
117        self._is_evaluated = False
118
119    def is_fitness_evaluated(self):
120        """
121        Check this fitness score status (if the fitness score is updated)
122
123        Returns
124        ----------
125        bool
126            True if this fitness is evaluated, False otherwise
127        """
128        if self.is_relative_fitness:
129            return True
130        return self._is_evaluated

This class is responsible for handling the fitness score of some Individual (checking if fitness is evaluated, comparing fitness scores with other individuals etc.)

is_evaluated: bool declares if fitness score is evaluated and updated in the current generation

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

is_relative_fitness
cache
higher_is_better
@abstractmethod
def get_pure_fitness(self):
43    @abstractmethod
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        raise ValueError("get_pure_fitness is an abstract method in class Fitness")

Returns the pure fitness score of the individual (before applying balancing methods like bloat control)

def get_augmented_fitness(self, individual):
50    def get_augmented_fitness(self, individual):
51        """
52        Returns the fixed fitness score of the individual (after applying balancing methods like bloat control)
53        By default, returns the pure fitness score
54
55        Parameters
56        ----------
57        individual: Individual
58            the individual instance that holds this Fitness instance
59
60        Returns
61        ----------
62        object
63            Fixed fitness value for the given individual
64        """
65        return self.get_pure_fitness()

Returns the fixed fitness score of the individual (after applying balancing methods like bloat control) By default, returns the pure fitness score

Parameters
  • individual (Individual): the individual instance that holds this Fitness instance
Returns
  • object: Fixed fitness value for the given individual
@abstractmethod
def better_than(self, ind, other_fitness, other_ind):
67    @abstractmethod
68    def better_than(self, ind, other_fitness, other_ind):
69        """
70        Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind`
71
72        Parameters
73        ----------
74        ind: Individual
75            the individual instance that holds this Fitness instance
76
77        other_fitness: Fitness
78            the Fitness instance of the `other` individual
79
80        other_ind: Individual
81            the `other` individual instance which is being compared to the individual `ind`
82
83        Returns
84        ----------
85        bool
86            True if this fitness is better than the `other` fitness, False otherwise
87        """
88        raise ValueError("better_than is an abstract method in class Fitness")

Compares between the current fitness of the individual ind to the fitness score other_fitness of other_ind

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 individual ind
Returns
  • bool: True if this fitness is better than the other fitness, False otherwise
@abstractmethod
def equal_to(self, ind, other_fitness, other_ind):
 90    @abstractmethod
 91    def equal_to(self, ind, other_fitness, other_ind):
 92        """
 93        Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind`
 94
 95        Parameters
 96        ----------
 97        ind: Individual
 98            the individual instance that holds this Fitness instance
 99
100        other_fitness: Fitness
101            the Fitness instance of the `other` individual
102
103        other_ind: Individual
104            the `other` individual instance which is being compared to the individual `ind`
105
106        Returns
107        ----------
108        bool
109            True if this fitness is equal to the `other` fitness, False otherwise
110        """
111        raise ValueError("better_than is an abstract method in class Fitness")

Compares between the current fitness of the individual ind to the fitness score other_fitness of other_ind

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 individual ind
Returns
  • bool: True if this fitness is equal to the other fitness, False otherwise
def set_not_evaluated(self):
113    def set_not_evaluated(self):
114        """
115        Set this fitness score status to be not evaluated
116        """
117        self._is_evaluated = False

Set this fitness score status to be not evaluated

def is_fitness_evaluated(self):
119    def is_fitness_evaluated(self):
120        """
121        Check this fitness score status (if the fitness score is updated)
122
123        Returns
124        ----------
125        bool
126            True if this fitness is evaluated, False otherwise
127        """
128        if self.is_relative_fitness:
129            return True
130        return self._is_evaluated

Check this fitness score status (if the fitness score is updated)

Returns
  • bool: True if this fitness is evaluated, False otherwise