eckity.genetic_encodings.gp.tree.tree_node

  1from abc import ABC, abstractmethod
  2from typing import Any, Callable, Optional
  3
  4from overrides import override
  5
  6from eckity.base.utils import arity
  7
  8from .utils import get_func_types
  9
 10
 11class TreeNode(ABC):
 12    """
 13    GP Node
 14
 15    Attributes
 16    ----------
 17    node_type : type
 18        node type
 19    """
 20
 21    def __init__(self, node_type: Optional[type] = None) -> None:
 22        self.node_type = node_type
 23
 24    @abstractmethod
 25    def __str__(self) -> str:
 26        pass
 27
 28    def __eq__(self, other):
 29        return (
 30            isinstance(other, TreeNode) and self.node_type is other.node_type
 31        )
 32
 33    def __repr__(self):
 34        return str(self)
 35
 36
 37class FunctionNode(TreeNode):
 38    def __init__(
 39        self,
 40        function: Callable,
 41    ) -> None:
 42        # infer the return type of the function
 43        func_types = get_func_types(function)
 44        return_type = func_types[-1] if func_types else None
 45        self.n_args = arity(function)
 46
 47        if 0 < len(func_types) < self.n_args + 1:
 48            raise ValueError(
 49                f"Function {function.__name__} has missing type hints."
 50                f"Please provide type hints for all arguments and return type."
 51            )
 52
 53        super().__init__(return_type)
 54
 55        self.function = function
 56
 57    @override
 58    def __eq__(self, other: object) -> bool:
 59        """
 60        Compare two FunctionNodes for equality.
 61        Function nodes are equal if they have the same function.
 62
 63        Parameters
 64        ----------
 65        other : object
 66            The object to compare to
 67
 68        Returns
 69        -------
 70        bool
 71            True if the two FunctionNodes are equal, False otherwise
 72        """
 73        return (
 74            super().__eq__(other)
 75            and isinstance(other, FunctionNode)
 76            and self.function == other.function
 77        )
 78
 79    @override
 80    def __str__(self) -> str:
 81        return self.function.__name__
 82
 83
 84class TerminalNode(TreeNode):
 85    def __init__(
 86        self,
 87        value: Any,
 88        node_type: Optional[type] = None
 89    ) -> None:
 90        super().__init__(node_type)
 91        self.value = value
 92
 93    @override
 94    def __eq__(self, other):
 95        return (
 96            super().__eq__(other)
 97            and isinstance(other, TerminalNode)
 98            and self.value is other.value
 99        )
100
101    def __str__(self):
102        return str(self.value)
class TreeNode(abc.ABC):
12class TreeNode(ABC):
13    """
14    GP Node
15
16    Attributes
17    ----------
18    node_type : type
19        node type
20    """
21
22    def __init__(self, node_type: Optional[type] = None) -> None:
23        self.node_type = node_type
24
25    @abstractmethod
26    def __str__(self) -> str:
27        pass
28
29    def __eq__(self, other):
30        return (
31            isinstance(other, TreeNode) and self.node_type is other.node_type
32        )
33
34    def __repr__(self):
35        return str(self)

GP Node

Attributes
  • node_type (type): node type
node_type
class FunctionNode(TreeNode):
38class FunctionNode(TreeNode):
39    def __init__(
40        self,
41        function: Callable,
42    ) -> None:
43        # infer the return type of the function
44        func_types = get_func_types(function)
45        return_type = func_types[-1] if func_types else None
46        self.n_args = arity(function)
47
48        if 0 < len(func_types) < self.n_args + 1:
49            raise ValueError(
50                f"Function {function.__name__} has missing type hints."
51                f"Please provide type hints for all arguments and return type."
52            )
53
54        super().__init__(return_type)
55
56        self.function = function
57
58    @override
59    def __eq__(self, other: object) -> bool:
60        """
61        Compare two FunctionNodes for equality.
62        Function nodes are equal if they have the same function.
63
64        Parameters
65        ----------
66        other : object
67            The object to compare to
68
69        Returns
70        -------
71        bool
72            True if the two FunctionNodes are equal, False otherwise
73        """
74        return (
75            super().__eq__(other)
76            and isinstance(other, FunctionNode)
77            and self.function == other.function
78        )
79
80    @override
81    def __str__(self) -> str:
82        return self.function.__name__

GP Node

Attributes
  • node_type (type): node type
FunctionNode(function: Callable)
39    def __init__(
40        self,
41        function: Callable,
42    ) -> None:
43        # infer the return type of the function
44        func_types = get_func_types(function)
45        return_type = func_types[-1] if func_types else None
46        self.n_args = arity(function)
47
48        if 0 < len(func_types) < self.n_args + 1:
49            raise ValueError(
50                f"Function {function.__name__} has missing type hints."
51                f"Please provide type hints for all arguments and return type."
52            )
53
54        super().__init__(return_type)
55
56        self.function = function
n_args
function
Inherited Members
TreeNode
node_type
class TerminalNode(TreeNode):
 85class TerminalNode(TreeNode):
 86    def __init__(
 87        self,
 88        value: Any,
 89        node_type: Optional[type] = None
 90    ) -> None:
 91        super().__init__(node_type)
 92        self.value = value
 93
 94    @override
 95    def __eq__(self, other):
 96        return (
 97            super().__eq__(other)
 98            and isinstance(other, TerminalNode)
 99            and self.value is other.value
100        )
101
102    def __str__(self):
103        return str(self.value)

GP Node

Attributes
  • node_type (type): node type
TerminalNode(value: Any, node_type: Optional[type] = None)
86    def __init__(
87        self,
88        value: Any,
89        node_type: Optional[type] = None
90    ) -> None:
91        super().__init__(node_type)
92        self.value = value
value
Inherited Members
TreeNode
node_type