def recursive_best_first_search(problem, h=None):
    "[Fig. 3.26]"
    h = memoize(h or problem.h, 'h')

    def RBFS(problem, node, flimit):
        if problem.goal_test(node.state):
            return node, 0   # (The second value is immaterial)
        successors = node.expand(problem)
        if len(successors) == 0:
            return None, infinity
        for s in successors:
            s.f = max(s.path_cost + h(s), node.f)
        while True:
            successors.sort(lambda x,y: cmp(x.f, y.f)) # Order by lowest f value
            best = successors[0]
            if best.f > flimit:
                return None, best.f
            if len(successors) > 1:
                alternative = successors[1].f
            else:
                alternative = infinity
            result, best.f = RBFS(problem, best, min(flimit, alternative))
            if result is not None:
                return result, best.f

    node = Node(problem.initial)
    node.f = h(node)
    result, bestf = RBFS(problem, node, infinity)
    return result
def recursive_best_first_search(problem, h=None):
    "[Fig. 3.26]"
    h = memoize(h or problem.h, 'h')

    def RBFS(problem, node, flimit):
        if problem.goal_test(node.state):
            return node, 0   # (The second value is immaterial)
        successors = node.expand(problem)
        if len(successors) == 0:
            return None, infinity
        for s in successors:
            s.f = max(s.path_cost + h(s), node.f)
        while True:
            successors.sort(lambda x,y: cmp(x.f, y.f)) # Order by lowest f value
            best = successors[0]
            if best.f > flimit:
                return None, best.f
            if len(successors) > 1:
                alternative = successors[1].f
            else:
                alternative = infinity
            result, best.f = RBFS(problem, best, min(flimit, alternative))
            if result is not None:
                return result, best.f

    node = Node(problem.initial)
    node.f = h(node)
    result, bestf = RBFS(problem, node, infinity)
    return result
def recursive_best_first_search ( problem , h = None ) : h = memoize ( h or problem . h , 'h' ) def RBFS ( problem , node , flimit ) : if problem . goal_test ( node . state ) : return node , 0 # (The second value is immaterial) successors = node . expand ( problem ) if len ( successors ) == 0 : return None , infinity for s in successors : s . f = max ( s . path_cost + h ( s ) , node . f ) while True : successors . sort ( lambda x , y : cmp ( x . f , y . f ) ) # Order by lowest f value best = successors [ 0 ] if best . f > flimit : return None , best . f if len ( successors ) > 1 : alternative = successors [ 1 ] . f else : alternative = infinity result , best . f = RBFS ( problem , best , min ( flimit , alternative ) ) if result is not None : return result , best . f node = Node ( problem . initial ) node . f = h ( node ) result , bestf = RBFS ( problem , node , infinity ) return result
[Fig. 3.26]
[ Fig . 3 . 26 ]