I have a function some_result = treesearch(node)
(a variant of Monte-Carlo Tree Search) that recursively searches a large tree. It decides the order in which to traverse the tree via next_node = expensive_heuristic(node)
and then propagates the result at the leaf back up the tree. I have to perform many such searches and expensive_heuristic(...)
can be efficiently computed in batches, e.g., via SIMD.
So my idea is to create a list with all the searches/root nodes, batch compute expensive_heuristic
to select the "direction" of traversal, and repeat this until I find a leaf and can propagate the result back up the tree.
Of course, I can re-write my search using a queue instead of a recursion (keeping the history around), but I am curious if it is possible to keep the recursive style. Can I "interrupt" a recursion and put it into a list/queue to continue it at a later stage in python?