这个迭代深化的搜索代码有什么问题?(python)

时间:2015-09-24 15:45:45

标签: python search recursion graph iteration

我把它实现到项目中(Pacman),但它根本就没有移动,它似乎进入了一个无限循环或某种东西....有人可以告诉我它有什么问题吗? ???我被困在这里好几天.....

def depthLimitedSearch(problem, limit ):

    explored = set()                                         
    node = problem.getStartState()
    path = []

    def recursive_DLS(node, problem, limit, path):

        if node not in explored :    
            explored.add(node)      
            if problem.goalTest(node):      
                return path     
            elif limit == 0 :       
                return 'cutoff'
            else:            
                cutoff_occurred = False
                for successor in problem.getActions(node):   
                    child = problem.getResult(node,successor) #next state
                    result = recursive_DLS(child ,problem, limit - 1, path)  
                    if result == 'cutoff':
                    cutoff_occurred = True
                    elif result != None:
                    path.append(successor)
                if cutoff_occurred:
                    return 'cutoff'
                else:
                    return None

    return recursive_DLS(node, problem, limit, path)    


def iterativeDeepeningSearch(problem):

    for depth in xrange(sys.maxint):
        result = depthLimitedSearch(problem, 1)
        if result is not 'cutoff':
            return result

1 个答案:

答案 0 :(得分:0)

您总是将1限制为depthLimitedSearch

for depth in xrange(sys.maxint):
    result = depthLimitedSearch(problem, 1)

未使用depth。如果使用限制1的搜索导致截止,这将迭代很长时间而不会产生结果。