eckity.genetic_operators.mutations.subtree_mutation

 1from typing import Any, List, Optional, Tuple
 2
 3from overrides import override
 4
 5from eckity.creators.gp_creators.grow import GrowCreator
 6from eckity.genetic_encodings.gp import Tree, TreeNode
 7from eckity.genetic_operators import FailableOperator
 8
 9
10class SubtreeMutation(FailableOperator):
11    def __init__(
12        self,
13        arity=1,
14        probability: float = 1.0,
15        init_depth: Tuple[int, int] = (2, 4),
16        events=None,
17        attempts=1,
18    ):
19        super().__init__(
20            probability=probability, arity=1, events=events, attempts=attempts
21        )
22        self.init_depth = init_depth
23        self.tree_creator = None
24
25    @override
26    def attempt_operator(
27        self, payload: Any, attempt_num: int
28    ) -> Tuple[bool, Any]:
29        """
30        Perform subtree mutation: select a subtree at random
31        to be replaced by a new, randomly generated subtree.
32
33        Returns
34        -------
35        Tuple[bool, Any]
36            A tuple containing a boolean indicating whether the operator was
37            successful and a list of the individuals.
38        """
39        individuals: List[Tree] = payload
40
41        old_subtrees: List[Optional[List[TreeNode]]] = [
42            ind.random_subtree() for ind in individuals
43        ]
44
45        if None in old_subtrees:
46            return False, individuals
47
48        self._swap_subtrees(individuals, old_subtrees)
49
50        self.applied_individuals = individuals
51        return True, individuals
52
53    def _swap_subtrees(
54        self, individuals: List[Tree], old_subtrees: List[List[TreeNode]]
55    ) -> None:
56
57        if self.tree_creator is None:
58            self.tree_creator = GrowCreator(
59                init_depth=self.init_depth,
60                function_set=individuals[0].function_set,
61                terminal_set=individuals[0].terminal_set,
62            )
63
64        for ind, old_subtree in zip(individuals, old_subtrees):
65            # generate a random tree with the same root type
66            # of the old subtree to not cause type errors
67
68            new_subtree = []
69            self.tree_creator.create_tree(
70                new_subtree,
71                ind.random_function,
72                ind.random_terminal,
73                depth=0,
74                node_type=old_subtree[0].node_type,
75            )
76
77            # replace the old subtree with the newly generated one
78            ind.replace_subtree(
79                old_subtree=old_subtree, new_subtree=new_subtree
80            )
11class SubtreeMutation(FailableOperator):
12    def __init__(
13        self,
14        arity=1,
15        probability: float = 1.0,
16        init_depth: Tuple[int, int] = (2, 4),
17        events=None,
18        attempts=1,
19    ):
20        super().__init__(
21            probability=probability, arity=1, events=events, attempts=attempts
22        )
23        self.init_depth = init_depth
24        self.tree_creator = None
25
26    @override
27    def attempt_operator(
28        self, payload: Any, attempt_num: int
29    ) -> Tuple[bool, Any]:
30        """
31        Perform subtree mutation: select a subtree at random
32        to be replaced by a new, randomly generated subtree.
33
34        Returns
35        -------
36        Tuple[bool, Any]
37            A tuple containing a boolean indicating whether the operator was
38            successful and a list of the individuals.
39        """
40        individuals: List[Tree] = payload
41
42        old_subtrees: List[Optional[List[TreeNode]]] = [
43            ind.random_subtree() for ind in individuals
44        ]
45
46        if None in old_subtrees:
47            return False, individuals
48
49        self._swap_subtrees(individuals, old_subtrees)
50
51        self.applied_individuals = individuals
52        return True, individuals
53
54    def _swap_subtrees(
55        self, individuals: List[Tree], old_subtrees: List[List[TreeNode]]
56    ) -> None:
57
58        if self.tree_creator is None:
59            self.tree_creator = GrowCreator(
60                init_depth=self.init_depth,
61                function_set=individuals[0].function_set,
62                terminal_set=individuals[0].terminal_set,
63            )
64
65        for ind, old_subtree in zip(individuals, old_subtrees):
66            # generate a random tree with the same root type
67            # of the old subtree to not cause type errors
68
69            new_subtree = []
70            self.tree_creator.create_tree(
71                new_subtree,
72                ind.random_function,
73                ind.random_terminal,
74                depth=0,
75                node_type=old_subtree[0].node_type,
76            )
77
78            # replace the old subtree with the newly generated one
79            ind.replace_subtree(
80                old_subtree=old_subtree, new_subtree=new_subtree
81            )

Genetic operator that has a chance of failing.

For example, adding a gaussian noise to a FloatVector cell might exceed the legal bounds of the vector. In that case, the Gauss Mutation fails.

Parameters
  • probability (float): the probability of the operator to be applied
  • arity (int): number of individuals to be applied on
  • events (list of strings): events to be published before, after and during the operator
  • attempts (int): number of attempts to be made during the operator execution
SubtreeMutation( arity=1, probability: float = 1.0, init_depth: Tuple[int, int] = (2, 4), events=None, attempts=1)
12    def __init__(
13        self,
14        arity=1,
15        probability: float = 1.0,
16        init_depth: Tuple[int, int] = (2, 4),
17        events=None,
18        attempts=1,
19    ):
20        super().__init__(
21            probability=probability, arity=1, events=events, attempts=attempts
22        )
23        self.init_depth = init_depth
24        self.tree_creator = None
init_depth
tree_creator
@override
def attempt_operator(self, payload: Any, attempt_num: int) -> Tuple[bool, Any]:
26    @override
27    def attempt_operator(
28        self, payload: Any, attempt_num: int
29    ) -> Tuple[bool, Any]:
30        """
31        Perform subtree mutation: select a subtree at random
32        to be replaced by a new, randomly generated subtree.
33
34        Returns
35        -------
36        Tuple[bool, Any]
37            A tuple containing a boolean indicating whether the operator was
38            successful and a list of the individuals.
39        """
40        individuals: List[Tree] = payload
41
42        old_subtrees: List[Optional[List[TreeNode]]] = [
43            ind.random_subtree() for ind in individuals
44        ]
45
46        if None in old_subtrees:
47            return False, individuals
48
49        self._swap_subtrees(individuals, old_subtrees)
50
51        self.applied_individuals = individuals
52        return True, individuals

Perform subtree mutation: select a subtree at random to be replaced by a new, randomly generated subtree.

Returns
  • Tuple[bool, Any]: A tuple containing a boolean indicating whether the operator was successful and a list of the individuals.