eckity.creators.gp_creators.full

  1from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  2
  3from overrides import overrides
  4
  5from eckity.creators.gp_creators.tree_creator import GPTreeCreator
  6from eckity.genetic_encodings.gp import (
  7    TerminalNode,
  8    FunctionNode,
  9)
 10from eckity.genetic_encodings.gp.tree.utils import get_func_types
 11
 12
 13class FullCreator(GPTreeCreator):
 14    def __init__(
 15        self,
 16        init_depth: Tuple[int, int] = None,
 17        function_set: List[Callable] = None,
 18        terminal_set: Union[Dict[Any, type], List[Any]] = None,
 19        erc_range: Union[Tuple[int, int], Tuple[float, float]] = None,
 20        bloat_weight: float = 0.0,
 21        events: List[str] = None,
 22        root_type: Optional[type] = None,
 23        update_parents: bool = False,
 24    ):
 25        """
 26        Tree creator using the full method
 27
 28        Parameters
 29        ----------
 30        init_depth : (int, int)
 31        Min and max depths of initial random trees. The default is None.
 32
 33        function_set : list
 34                List of functions used as internal nodes in the GP tree. The default is None.
 35
 36        terminal_set : list
 37                List of terminals used in the GP-tree leaves. The default is None.
 38
 39        bloat_weight : float
 40                Bloat control weight to punish large trees. Bigger values make a bigger punish.
 41
 42        events : list
 43                List of events related to this class
 44        """
 45        super().__init__(
 46            init_depth=init_depth,
 47            function_set=function_set,
 48            terminal_set=terminal_set,
 49            bloat_weight=bloat_weight,
 50            erc_range=erc_range,
 51            events=events,
 52            root_type=root_type,
 53            update_parents=update_parents,
 54        )
 55
 56    @overrides
 57    def create_tree(
 58        self,
 59        tree,
 60        random_function: Callable[[type], Optional[FunctionNode]],
 61        random_terminal: Callable[[type], Optional[TerminalNode]],
 62        depth: int = 0,
 63        node_type: Optional[type] = None,
 64    ) -> None:
 65        """
 66        Recursively create a random tree using the full method
 67
 68        Parameters
 69        ----------
 70        depth: int
 71                Current depth in recursive process.
 72
 73        Returns
 74        -------
 75        None.
 76
 77        """
 78        max_depth = self.init_depth[1]
 79
 80        if depth >= max_depth:
 81            node = random_terminal(node_type)
 82            self._assert_node_created(node)
 83
 84            # add the new node to the tree of the given individual
 85            tree.append(node)
 86        else:
 87            node = random_function(node_type)
 88            self._assert_node_created(node)
 89
 90            # add the new node to the tree of the given individual
 91            tree.append(node)
 92
 93            # recursively add argument nodes to the tree
 94            func_types = get_func_types(node.function)[:-1]
 95            for t in func_types:
 96                self.create_tree(
 97                    tree,
 98                    random_function,
 99                    random_terminal,
100                    depth=depth + 1,
101                    node_type=t,
102                )
 14class FullCreator(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        erc_range: Union[Tuple[int, int], Tuple[float, float]] = None,
 21        bloat_weight: float = 0.0,
 22        events: List[str] = None,
 23        root_type: Optional[type] = None,
 24        update_parents: bool = False,
 25    ):
 26        """
 27        Tree creator using the full method
 28
 29        Parameters
 30        ----------
 31        init_depth : (int, int)
 32        Min and max depths of initial random trees. The default is None.
 33
 34        function_set : list
 35                List of functions used as internal nodes in the GP tree. The default is None.
 36
 37        terminal_set : list
 38                List of terminals used in the GP-tree leaves. The default is None.
 39
 40        bloat_weight : float
 41                Bloat control weight to punish large trees. Bigger values make a bigger punish.
 42
 43        events : list
 44                List of events related to this class
 45        """
 46        super().__init__(
 47            init_depth=init_depth,
 48            function_set=function_set,
 49            terminal_set=terminal_set,
 50            bloat_weight=bloat_weight,
 51            erc_range=erc_range,
 52            events=events,
 53            root_type=root_type,
 54            update_parents=update_parents,
 55        )
 56
 57    @overrides
 58    def create_tree(
 59        self,
 60        tree,
 61        random_function: Callable[[type], Optional[FunctionNode]],
 62        random_terminal: Callable[[type], Optional[TerminalNode]],
 63        depth: int = 0,
 64        node_type: Optional[type] = None,
 65    ) -> None:
 66        """
 67        Recursively create a random tree using the full method
 68
 69        Parameters
 70        ----------
 71        depth: int
 72                Current depth in recursive process.
 73
 74        Returns
 75        -------
 76        None.
 77
 78        """
 79        max_depth = self.init_depth[1]
 80
 81        if depth >= max_depth:
 82            node = random_terminal(node_type)
 83            self._assert_node_created(node)
 84
 85            # add the new node to the tree of the given individual
 86            tree.append(node)
 87        else:
 88            node = random_function(node_type)
 89            self._assert_node_created(node)
 90
 91            # add the new node to the tree of the given individual
 92            tree.append(node)
 93
 94            # recursively add argument nodes to the tree
 95            func_types = get_func_types(node.function)[:-1]
 96            for t in func_types:
 97                self.create_tree(
 98                    tree,
 99                    random_function,
100                    random_terminal,
101                    depth=depth + 1,
102                    node_type=t,
103                )

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

FullCreator( init_depth: Tuple[int, int] = None, function_set: List[Callable] = None, terminal_set: Union[Dict[Any, type], List[Any]] = None, erc_range: Union[Tuple[int, int], Tuple[float, float]] = None, bloat_weight: float = 0.0, events: List[str] = None, root_type: Optional[type] = None, update_parents: bool = False)
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        erc_range: Union[Tuple[int, int], Tuple[float, float]] = None,
21        bloat_weight: float = 0.0,
22        events: List[str] = None,
23        root_type: Optional[type] = None,
24        update_parents: bool = False,
25    ):
26        """
27        Tree creator using the full method
28
29        Parameters
30        ----------
31        init_depth : (int, int)
32        Min and max depths of initial random trees. The default is None.
33
34        function_set : list
35                List of functions used as internal nodes in the GP tree. The default is None.
36
37        terminal_set : list
38                List of terminals used in the GP-tree leaves. The default is None.
39
40        bloat_weight : float
41                Bloat control weight to punish large trees. Bigger values make a bigger punish.
42
43        events : list
44                List of events related to this class
45        """
46        super().__init__(
47            init_depth=init_depth,
48            function_set=function_set,
49            terminal_set=terminal_set,
50            bloat_weight=bloat_weight,
51            erc_range=erc_range,
52            events=events,
53            root_type=root_type,
54            update_parents=update_parents,
55        )

Tree creator using the full method

Parameters
  • init_depth ((int, int)):

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

  • function_set (list): List of functions used as internal nodes in the GP tree. The default is None.
  • terminal_set (list): List of terminals used in the GP-tree leaves. The default is None.
  • bloat_weight (float): Bloat control weight to punish large trees. Bigger values make a bigger punish.
  • events (list): List of events related to this class
@overrides
def create_tree( self, tree, 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:
 57    @overrides
 58    def create_tree(
 59        self,
 60        tree,
 61        random_function: Callable[[type], Optional[FunctionNode]],
 62        random_terminal: Callable[[type], Optional[TerminalNode]],
 63        depth: int = 0,
 64        node_type: Optional[type] = None,
 65    ) -> None:
 66        """
 67        Recursively create a random tree using the full method
 68
 69        Parameters
 70        ----------
 71        depth: int
 72                Current depth in recursive process.
 73
 74        Returns
 75        -------
 76        None.
 77
 78        """
 79        max_depth = self.init_depth[1]
 80
 81        if depth >= max_depth:
 82            node = random_terminal(node_type)
 83            self._assert_node_created(node)
 84
 85            # add the new node to the tree of the given individual
 86            tree.append(node)
 87        else:
 88            node = random_function(node_type)
 89            self._assert_node_created(node)
 90
 91            # add the new node to the tree of the given individual
 92            tree.append(node)
 93
 94            # recursively add argument nodes to the tree
 95            func_types = get_func_types(node.function)[:-1]
 96            for t in func_types:
 97                self.create_tree(
 98                    tree,
 99                    random_function,
100                    random_terminal,
101                    depth=depth + 1,
102                    node_type=t,
103                )

Recursively create a random tree using the full method

Parameters
  • depth (int): Current depth in recursive process.
Returns
  • None.