eckity.genetic_operators.genetic_operator

 1import random
 2from abc import ABC, abstractmethod
 3from overrides import override
 4
 5from eckity.event_based_operator import Operator
 6
 7
 8class GeneticOperator(Operator, ABC):
 9    """
10    Abstract class for genetic operators.
11    Each operator has a probability of being applied each generation.
12
13    Genetic operators are applied in-place.
14
15
16    Parameters
17    ----------
18    probability : float, optional
19        probability of being applied each generation, by default 1.0
20    arity : int, optional
21        number of individuals required for the operator, by default 0
22    events : List[str], optional
23        custom events that the operator publishes, by default None
24    """
25
26    def __init__(self, probability=1.0, arity=0, events=None):
27        super().__init__(events=events, arity=arity)
28        self.probability = probability
29
30    @override
31    def apply_operator(self, payload):
32        """
33        Apply the genetic operator with a certain probability.
34        The individuals are modified in-place, so it is not mandatory
35        to return them.
36
37        Parameters
38        ----------
39        payload : List[Individual]
40            Individuals to apply the operator to.
41
42        Returns
43        -------
44        List[Individual]
45            The individuals after applying the operator.
46        """
47        if random.random() <= self.probability:
48            # Fitness is irrelevant once the operator is applied
49            for individual in payload:
50                individual.set_fitness_not_evaluated()
51            op_res = self.apply(payload)
52
53            # Add the operator to the applied operators list
54            for ind in op_res:
55                ind.applied_operators.append(type(self).__name__)
56                
57                if ind.update_parents:
58                    parents = [p.id for p in payload]
59                    ind.parents.extend(parents)
60            return op_res
61        return payload
62
63    @abstractmethod
64    def apply(self, individuals):
65        """
66        Apply the genetic operator to the individuals.
67        This method should be implemented by the subclasses.
68
69        Parameters
70        ----------
71        individuals : List[Individual]
72            Individuals to apply the operator to.
73        """
74        pass
class GeneticOperator(eckity.event_based_operator.Operator, abc.ABC):
 9class GeneticOperator(Operator, ABC):
10    """
11    Abstract class for genetic operators.
12    Each operator has a probability of being applied each generation.
13
14    Genetic operators are applied in-place.
15
16
17    Parameters
18    ----------
19    probability : float, optional
20        probability of being applied each generation, by default 1.0
21    arity : int, optional
22        number of individuals required for the operator, by default 0
23    events : List[str], optional
24        custom events that the operator publishes, by default None
25    """
26
27    def __init__(self, probability=1.0, arity=0, events=None):
28        super().__init__(events=events, arity=arity)
29        self.probability = probability
30
31    @override
32    def apply_operator(self, payload):
33        """
34        Apply the genetic operator with a certain probability.
35        The individuals are modified in-place, so it is not mandatory
36        to return them.
37
38        Parameters
39        ----------
40        payload : List[Individual]
41            Individuals to apply the operator to.
42
43        Returns
44        -------
45        List[Individual]
46            The individuals after applying the operator.
47        """
48        if random.random() <= self.probability:
49            # Fitness is irrelevant once the operator is applied
50            for individual in payload:
51                individual.set_fitness_not_evaluated()
52            op_res = self.apply(payload)
53
54            # Add the operator to the applied operators list
55            for ind in op_res:
56                ind.applied_operators.append(type(self).__name__)
57                
58                if ind.update_parents:
59                    parents = [p.id for p in payload]
60                    ind.parents.extend(parents)
61            return op_res
62        return payload
63
64    @abstractmethod
65    def apply(self, individuals):
66        """
67        Apply the genetic operator to the individuals.
68        This method should be implemented by the subclasses.
69
70        Parameters
71        ----------
72        individuals : List[Individual]
73            Individuals to apply the operator to.
74        """
75        pass

Abstract class for genetic operators. Each operator has a probability of being applied each generation.

Genetic operators are applied in-place.

Parameters
  • probability (float, optional): probability of being applied each generation, by default 1.0
  • arity (int, optional): number of individuals required for the operator, by default 0
  • events (List[str], optional): custom events that the operator publishes, by default None
probability
@override
def apply_operator(self, payload):
31    @override
32    def apply_operator(self, payload):
33        """
34        Apply the genetic operator with a certain probability.
35        The individuals are modified in-place, so it is not mandatory
36        to return them.
37
38        Parameters
39        ----------
40        payload : List[Individual]
41            Individuals to apply the operator to.
42
43        Returns
44        -------
45        List[Individual]
46            The individuals after applying the operator.
47        """
48        if random.random() <= self.probability:
49            # Fitness is irrelevant once the operator is applied
50            for individual in payload:
51                individual.set_fitness_not_evaluated()
52            op_res = self.apply(payload)
53
54            # Add the operator to the applied operators list
55            for ind in op_res:
56                ind.applied_operators.append(type(self).__name__)
57                
58                if ind.update_parents:
59                    parents = [p.id for p in payload]
60                    ind.parents.extend(parents)
61            return op_res
62        return payload

Apply the genetic operator with a certain probability. The individuals are modified in-place, so it is not mandatory to return them.

Parameters
  • payload (List[Individual]): Individuals to apply the operator to.
Returns
  • List[Individual]: The individuals after applying the operator.
@abstractmethod
def apply(self, individuals):
64    @abstractmethod
65    def apply(self, individuals):
66        """
67        Apply the genetic operator to the individuals.
68        This method should be implemented by the subclasses.
69
70        Parameters
71        ----------
72        individuals : List[Individual]
73            Individuals to apply the operator to.
74        """
75        pass

Apply the genetic operator to the individuals. This method should be implemented by the subclasses.

Parameters
  • individuals (List[Individual]): Individuals to apply the operator to.