微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以在DEAP的适应性评估功能中添加超时?

如何解决是否可以在DEAP的适应性评估功能中添加超时?

我正在尝试使用DEAP来演化正则表达式字符串。它适用于较小的输入,但是当我尝试较大的输入时,它便开始演化出具有灾难性回溯的正则表达式字符串。正则表达式搜索永远不会完成,这会阻止进化过程。

我尝试使用多处理,pebble和timeout_decorator为正则表达式匹配添加超时,但是我遇到了Python处理线程方式的问题。

也许我缺少明显的东西,但是据我了解,DEAP希望在主脚本中定义工具箱,其中包括适应性评估功能。这里的Python线程知识有限,但是我认为这会引起问题,因为需要通过__main__检查来保护线程功能

因此以下方法可以正常工作:

@concurrent.process
def function(a,b):
    return a + b


if __name__ == '__main__':
    future = function(1,1)

    print(future.result())

但这失败了,因为不能在__main__卫队之外宣布未来。

@concurrent.process
def function(a,b):
    return a + b

future = function(1,1)

if __name__ == '__main__':
    print(future.result())

给出此错误

RuntimeError:
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

我的代码的简化版显示了我为什么会遇到此问题(该工具箱需要DEAP注册为“评估”功能的evalSymbReg可以全局访问。我看不到在{ {1}}进行检查,同时使工具箱可用于需要使用它的evalSymbReg函数

__main__

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。