eckity.creators.gp_creators.grow

  1import random
  2from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  3
  4from overrides import overrides
  5
  6from eckity.creators.gp_creators.tree_creator import GPTreeCreator
  7from eckity.genetic_encodings.gp import (
  8    FunctionNode,
  9    TerminalNode,
 10    TreeNode,
 11)
 12
 13
 14class GrowCreator(GPTreeCreator):
 15    def __init__(
 16        self,
 17        init_depth: Tuple[int, int] = None,
 18        function_set: List[Callable] = None,
 19        terminal_set: Union[Dict[Any, type], List[Any]] = None,
 20        bloat_weight: float = 0.0,
 21        erc_range: Union[Tuple[int, int], Tuple[float, float]] = None,
 22        p_prune: float = 0.5,
 23        events: List[str] = None,
 24        root_type: Optional[type] = None,
 25        update_parents: bool = False
 26    ):
 27        """
 28        Tree creator using the grow method
 29
 30        Parameters
 31        ----------
 32        init_depth : (int, int)
 33        Min and max depths of initial random trees. The default is None.
 34
 35        function_set : list, default=None
 36                List of functions used as internal nodes in the GP-tree.
 37
 38        terminal_set : list, default=None.
 39                List of terminals used in the GP-tree leaves.
 40
 41        bloat_weight : float, default=0.0
 42                Bloat control weight to punish large trees.
 43
 44        p_prune : float, default=0.5
 45                Probability of pruning the tree at each level.
 46
 47        events : list
 48                List of events related to this class
 49        """
 50        super().__init__(
 51            init_depth=init_depth,
 52            function_set=function_set,
 53            terminal_set=terminal_set,
 54            bloat_weight=bloat_weight,
 55            events=events,
 56            root_type=root_type,
 57            update_parents=update_parents,
 58            erc_range=erc_range,
 59        )
 60        self.p_prune = p_prune
 61
 62    @overrides
 63    def create_tree(
 64        self,
 65        tree: List[TreeNode],
 66        random_function: Callable[[type], Optional[FunctionNode]],
 67        random_terminal: Callable[[type], Optional[TerminalNode]],
 68        depth: int = 0,
 69        node_type: Optional[type] = None,
 70    ) -> None:
 71        """
 72        Generate a list of TreeNodes in-place.
 73
 74        Parameters
 75        ----------
 76        tree : List[TreeNode]
 77            List of tree nodes representing a tree of a Tree individual.
 78        random_function : Callable[[type], Optional[FunctionNode]]
 79            Random FunctionNode generator.
 80        random_terminal : Callable[[type], Optional[TerminalNode]]
 81            Random TerminalNode generator.
 82        depth : int, optional
 83            current depth of the tree, by default 0
 84        node_type : Optional[type], optional
 85            _description_, by default None
 86        """
 87        if tree is None:
 88            tree = []
 89
 90        min_depth, max_depth = self.init_depth
 91
 92        is_func = False
 93        if depth < min_depth:
 94            node = random_function(node_type)
 95
 96            is_func = True
 97        elif depth >= max_depth:
 98            node = random_terminal(node_type)
 99        else:  # intermediate depth, grow
100            if random.random() < self.p_prune:
101                node = random_terminal(node_type)
102            else:
103                node = random_function(node_type)
104                is_func = True
105
106        # add the new node to the tree of the given individual
107        tree.append(node)
108
109        self._assert_node_created(node)
110
111        if is_func:
112            self._add_children(
113                tree=tree,
114                fn_node=node,
115                random_function=random_function,
116                random_terminal=random_terminal,
117                depth=depth,
118            )
 15class GrowCreator(GPTreeCreator):
 16    def __init__(
 17        self,
 18        init_depth: Tuple[int, int] = None,
 19        function_set: List[Callable] = None,
 20        terminal_set: Union[Dict[Any, type], List[Any]] = None,
 21        bloat_weight: float = 0.0,
 22        erc_range: Union[Tuple[int, int], Tuple[float, float]] = None,
 23        p_prune: float = 0.5,
 24        events: List[str] = None,
 25        root_type: Optional[type] = None,
 26        update_parents: bool = False
 27    ):
 28        """
 29        Tree creator using the grow method
 30
 31        Parameters
 32        ----------
 33        init_depth : (int, int)
 34        Min and max depths of initial random trees. The default is None.
 35
 36        function_set : list, default=None
 37                List of functions used as internal nodes in the GP-tree.
 38
 39        terminal_set : list, default=None.
 40                List of terminals used in the GP-tree leaves.
 41
 42        bloat_weight : float, default=0.0
 43                Bloat control weight to punish large trees.
 44
 45        p_prune : float, default=0.5
 46                Probability of pruning the tree at each level.
 47
 48        events : list
 49                List of events related to this class
 50        """
 51        super().__init__(
 52            init_depth=init_depth,
 53            function_set=function_set,
 54            terminal_set=terminal_set,
 55            bloat_weight=bloat_weight,
 56            events=events,
 57            root_type=root_type,
 58            update_parents=update_parents,
 59            erc_range=erc_range,
 60        )
 61        self.p_prune = p_prune
 62
 63    @overrides
 64    def create_tree(
 65        self,
 66        tree: List[TreeNode],
 67        random_function: Callable[[type], Optional[FunctionNode]],
 68        random_terminal: Callable[[type], Optional[TerminalNode]],
 69        depth: int = 0,
 70        node_type: Optional[type] = None,
 71    ) -> None:
 72        """
 73        Generate a list of TreeNodes in-place.
 74
 75        Parameters
 76        ----------
 77        tree : List[TreeNode]
 78            List of tree nodes representing a tree of a Tree individual.
 79        random_function : Callable[[type], Optional[FunctionNode]]
 80            Random FunctionNode generator.
 81        random_terminal : Callable[[type], Optional[TerminalNode]]
 82            Random TerminalNode generator.
 83        depth : int, optional
 84            current depth of the tree, by default 0
 85        node_type : Optional[type], optional
 86            _description_, by default None
 87        """
 88        if tree is None:
 89            tree = []
 90
 91        min_depth, max_depth = self.init_depth
 92
 93        is_func = False
 94        if depth < min_depth:
 95            node = random_function(node_type)
 96
 97            is_func = True
 98        elif depth >= max_depth:
 99            node = random_terminal(node_type)
100        else:  # intermediate depth, grow
101            if random.random() < self.p_prune:
102                node = random_terminal(node_type)
103            else:
104                node = random_function(node_type)
105                is_func = True
106
107        # add the new node to the tree of the given individual
108        tree.append(node)
109
110        self._assert_node_created(node)
111
112        if is_func:
113            self._add_children(
114                tree=tree,
115                fn_node=node,
116                random_function=random_function,
117                random_terminal=random_terminal,
118                depth=depth,
119            )

Helper class that provides a standard way to create an ABC using inheritance.

GrowCreator( init_depth: Tuple[int, int] = None, function_set: List[Callable] = None, terminal_set: Union[Dict[Any, type], List[Any]] = None, bloat_weight: float = 0.0, erc_range: Union[Tuple[int, int], Tuple[float, float]] = None, p_prune: float = 0.5, events: List[str] = None, root_type: Optional[type] = None, update_parents: bool = False)
16    def __init__(
17        self,
18        init_depth: Tuple[int, int] = None,
19        function_set: List[Callable] = None,
20        terminal_set: Union[Dict[Any, type], List[Any]] = None,
21        bloat_weight: float = 0.0,
22        erc_range: Union[Tuple[int, int], Tuple[float, float]] = None,
23        p_prune: float = 0.5,
24        events: List[str] = None,
25        root_type: Optional[type] = None,
26        update_parents: bool = False
27    ):
28        """
29        Tree creator using the grow method
30
31        Parameters
32        ----------
33        init_depth : (int, int)
34        Min and max depths of initial random trees. The default is None.
35
36        function_set : list, default=None
37                List of functions used as internal nodes in the GP-tree.
38
39        terminal_set : list, default=None.
40                List of terminals used in the GP-tree leaves.
41
42        bloat_weight : float, default=0.0
43                Bloat control weight to punish large trees.
44
45        p_prune : float, default=0.5
46                Probability of pruning the tree at each level.
47
48        events : list
49                List of events related to this class
50        """
51        super().__init__(
52            init_depth=init_depth,
53            function_set=function_set,
54            terminal_set=terminal_set,
55            bloat_weight=bloat_weight,
56            events=events,
57            root_type=root_type,
58            update_parents=update_parents,
59            erc_range=erc_range,
60        )
61        self.p_prune = p_prune

Tree creator using the grow method

Parameters
  • init_depth ((int, int)):

  • Min and max depths of initial random trees. The default is None.

  • function_set (list, default=None): List of functions used as internal nodes in the GP-tree.
  • terminal_set (list, default=None.): List of terminals used in the GP-tree leaves.
  • bloat_weight (float, default=0.0): Bloat control weight to punish large trees.
  • p_prune (float, default=0.5): Probability of pruning the tree at each level.
  • events (list): List of events related to this class
p_prune
@overrides
def create_tree( self, tree: List[eckity.genetic_encodings.gp.tree.tree_node.TreeNode], random_function: Callable[[type], Optional[eckity.genetic_encodings.gp.tree.tree_node.FunctionNode]], random_terminal: Callable[[type], Optional[eckity.genetic_encodings.gp.tree.tree_node.TerminalNode]], depth: int = 0, node_type: Optional[type] = None) -> None:
 63    @overrides
 64    def create_tree(
 65        self,
 66        tree: List[TreeNode],
 67        random_function: Callable[[type], Optional[FunctionNode]],
 68        random_terminal: Callable[[type], Optional[TerminalNode]],
 69        depth: int = 0,
 70        node_type: Optional[type] = None,
 71    ) -> None:
 72        """
 73        Generate a list of TreeNodes in-place.
 74
 75        Parameters
 76        ----------
 77        tree : List[TreeNode]
 78            List of tree nodes representing a tree of a Tree individual.
 79        random_function : Callable[[type], Optional[FunctionNode]]
 80            Random FunctionNode generator.
 81        random_terminal : Callable[[type], Optional[TerminalNode]]
 82            Random TerminalNode generator.
 83        depth : int, optional
 84            current depth of the tree, by default 0
 85        node_type : Optional[type], optional
 86            _description_, by default None
 87        """
 88        if tree is None:
 89            tree = []
 90
 91        min_depth, max_depth = self.init_depth
 92
 93        is_func = False
 94        if depth < min_depth:
 95            node = random_function(node_type)
 96
 97            is_func = True
 98        elif depth >= max_depth:
 99            node = random_terminal(node_type)
100        else:  # intermediate depth, grow
101            if random.random() < self.p_prune:
102                node = random_terminal(node_type)
103            else:
104                node = random_function(node_type)
105                is_func = True
106
107        # add the new node to the tree of the given individual
108        tree.append(node)
109
110        self._assert_node_created(node)
111
112        if is_func:
113            self._add_children(
114                tree=tree,
115                fn_node=node,
116                random_function=random_function,
117                random_terminal=random_terminal,
118                depth=depth,
119            )

Generate a list of TreeNodes in-place.

Parameters
  • tree (List[TreeNode]): List of tree nodes representing a tree of a Tree individual.
  • random_function (Callable[[type], Optional[FunctionNode]]): Random FunctionNode generator.
  • random_terminal (Callable[[type], Optional[TerminalNode]]): Random TerminalNode generator.
  • depth (int, optional): current depth of the tree, by default 0
  • node_type (Optional[type], optional): _description_, by default None