本文整理汇总了Python中function.Function.random_terminal方法的典型用法代码示例。如果您正苦于以下问题:Python Function.random_terminal方法的具体用法?Python Function.random_terminal怎么用?Python Function.random_terminal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类function.Function
的用法示例。
在下文中一共展示了Function.random_terminal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: grow
# 需要导入模块: from function import Function [as 别名]
# 或者: from function.Function import random_terminal [as 别名]
def grow(self, depth=None):
""" Grows a random child node by 1, limited by `depth` (if provided)
and arity restrictions. Returns the new node (or None if no
node can be expanded).
Args:
depth: int (default=None)
Returns:
Node instance (or None)
"""
# Creates a random permutation of child nodes
nodes_depths = list(self.descendants_and_self_with_depths())
shuffle(nodes_depths)
for node, d in nodes_depths:
if len(node.children) >= node.func.arity:
continue
if d >= depth:
continue
if d == depth - 1:
func = Function.random_terminal()
else:
func = Function.random_function()
child = Node(func)
node.add_child(child)
return child
# If no children can be expanded, return None
return None
示例2: create_full_tree
# 需要导入模块: from function import Function [as 别名]
# 或者: from function.Function import random_terminal [as 别名]
def create_full_tree(depth):
""" Creates a tree using the full method with depth `depth`.
Returns the root node.
Args:
depth: int
Returns:
Node instance
"""
if depth == 0:
# Generate a leaf node
terminal = Function.random_terminal()
node = Node(terminal)
return node
else:
# Generate an intermediate node
func = Function.random_function()
node = Node(func)
for _ in range(func.arity):
node.add_child(TreeMethods.create_full_tree(depth - 1))
return node