eckity.fitness.fitness

This module implements the class Fitness

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

context: list of Individuals individuals involved in calculating the fitness (co-evolution)

trials: list of floats fitness results for previous trials done to calculate fitness (co-evolution)

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

is_relative_fitness: bool declares whether the fitness score is absolute or relative

should_cache_between_gens: bool declares whether the fitness score should reset at the end of each generation

higher_is_better: bool declares the fitness direction. i.e., if it should be minimized or maximized

Fitness( context=None, trials=None, is_evaluated=False, is_relative_fitness=False, should_cache_between_gens=True, higher_is_better=False)
33    def __init__(self,
34                 context=None,
35                 trials=None,
36                 is_evaluated=False,
37                 is_relative_fitness=False,
38                 should_cache_between_gens=True,
39                 higher_is_better=False):
40        self.context = context
41        self.trials = trials
42        self._is_evaluated = is_evaluated
43        self.is_relative_fitness = is_relative_fitness
44        self.should_cache_between_gens = False if is_relative_fitness else should_cache_between_gens
45        self.higher_is_better = higher_is_better
46        self.optimal_fitness = 1 if higher_is_better else 0
context
trials
is_relative_fitness
should_cache_between_gens
higher_is_better
optimal_fitness
@abstractmethod
def get_pure_fitness(self):
48    @abstractmethod
49    def get_pure_fitness(self):
50        """
51        Returns the pure fitness score of the individual (before applying balancing methods like bloat control)
52        """
53        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):
55    def get_augmented_fitness(self, individual):
56        """
57        Returns the fixed fitness score of the individual (after applying balancing methods like bloat control)
58        By default, returns the pure fitness score
59
60        Parameters
61        ----------
62        individual: Individual
63            the individual instance that holds this Fitness instance
64
65        Returns
66        ----------
67        object
68            Fixed fitness value for the given individual
69        """
70        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):
72    @abstractmethod
73    def better_than(self, ind, other_fitness, other_ind):
74        """
75        Compares between the current fitness of the individual `ind` to the fitness score `other_fitness` of `other_ind`
76
77        Parameters
78        ----------
79        ind: Individual
80            the individual instance that holds this Fitness instance
81
82        other_fitness: Fitness
83            the Fitness instance of the `other` individual
84
85        other_ind: Individual
86            the `other` individual instance which is being compared to the individual `ind`
87
88        Returns
89        ----------
90        bool
91            True if this fitness is better than the `other` fitness, False otherwise
92        """
93        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):
 95    @abstractmethod
 96    def equal_to(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
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 is equal to the `other` fitness, False otherwise
115        """
116        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):
118    def set_not_evaluated(self):
119        """
120        Set this fitness score status to be not evaluated
121        """
122        self._is_evaluated = False

Set this fitness score status to be not evaluated

def is_fitness_evaluated(self):
124    def is_fitness_evaluated(self):
125        """
126        Check this fitness score status (if the fitness score is updated)
127
128        Returns
129        ----------
130        bool
131            True if this fitness is evaluated, False otherwise
132        """
133        if self.is_relative_fitness:
134            return True
135        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