我有一个函数some_result = treesearch(node)
(montecarlo树搜索的一种变体),它递归地搜索一棵大树。它决定通过next_node = expensive_heuristic(node)
遍历树的顺序,然后在树的后面的叶子上传播结果。我必须执行许多这样的搜索,并且expensive_heuristic(...)
可以成批高效地计算,例如通过SIMD
因此,我的想法是创建一个包含所有搜索/根节点的列表,批量计算expensive_heuristic
以选择遍历的“方向”,并重复此操作,直到找到一个叶,并将结果传播回树
当然,我可以使用队列而不是递归来重新编写搜索(保留历史记录),但我很好奇是否可以保留递归样式。我是否可以“中断”递归并将其放入列表/队列中,以便在python的稍后阶段继续它
可以使用
yield
、yield from
和send
;感谢juanpa.arrivillaga在评论中的建议下面的代码在随机二叉树中搜索多达10个节点,并返回其中的最大值。每当它必须计算用于指导搜索的
heuristic
时,它就会中断生成/搜索相关问题 更多 >
编程相关推荐