本文整理匯總了Python中function.Function.random_function方法的典型用法代碼示例。如果您正苦於以下問題:Python Function.random_function方法的具體用法?Python Function.random_function怎麽用?Python Function.random_function使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類function.Function
的用法示例。
在下文中一共展示了Function.random_function方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: grow
# 需要導入模塊: from function import Function [as 別名]
# 或者: from function.Function import random_function [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_grow_tree
# 需要導入模塊: from function import Function [as 別名]
# 或者: from function.Function import random_function [as 別名]
def create_grow_tree(depth):
""" Creates a tree using the grow method with depth `depth`. Note
that since grow ends when all leaf nodes are 0-arity, it is
possible that the tree generated by this method has a depth
less than `depth`.
Args:
depth: int
Returns:
Node instance
"""
func = Function.random_function()
root = Node(func)
while True:
if root.grow(depth) is None:
break
return root
示例3: create_full_tree
# 需要導入模塊: from function import Function [as 別名]
# 或者: from function.Function import random_function [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