本文整理汇总了Python中function.Function.make_float_function方法的典型用法代码示例。如果您正苦于以下问题:Python Function.make_float_function方法的具体用法?Python Function.make_float_function怎么用?Python Function.make_float_function使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类function.Function
的用法示例。
在下文中一共展示了Function.make_float_function方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mutate_float
# 需要导入模块: from function import Function [as 别名]
# 或者: from function.Function import make_float_function [as 别名]
def mutate_float(node, score_tree, eps=1e-1):
""" Takes a Node object and optimizes floats greedily.
Returns a new tree.
Args:
node: Node object to operate on
score_tree: function that takes a tree
and returns a fitness (as float)
eps: learning rate (as float) (default=1e-1)
Returns:
Node object
"""
# Copy the tree
new_tree = node.deepcopy()
# Find all floating leaves
floats = new_tree.all_floats()
if floats == []:
return None
has_changed = False
for f in floats:
value = f.func.func()
left_value = value - eps
right_value = value + eps
func = f.func
left_func = Function.make_float_function(left_value)
left_func = Function(left_func, 0, str(left_value))
right_func = Function.make_float_function(right_value)
right_func = Function(right_func, 0, str(right_value))
score = score_tree(new_tree)
f.func = left_func
left_score = score_tree(new_tree)
f.func = right_func
right_score = score_tree(new_tree)
max_ = max(left_score, score, right_score)
if abs(max_ - left_score) < 1e-10:
f.func = left_func
has_changed = True
elif abs(max_ - right_score) < 1e-10:
f.func = right_func
has_changed = True
else:
f.func = func
if not has_changed:
return None
return new_tree