当前位置: 首页>>代码示例>>Python>>正文


Python Function.make_float_function方法代码示例

本文整理汇总了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
开发者ID:mlberkeley,项目名称:genetic-algs,代码行数:56,代码来源:mutations.py


注:本文中的function.Function.make_float_function方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。