这种迭代深化搜索代码有什么问题?(Python)

2024-06-07 03:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在项目中实现了这一点(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

Tags: pathinnodereturnifdefresultcutoff
1条回答
网友
1楼 · 发布于 2024-06-07 03:25:12

您总是将1的限制传递给depthLimitedSearch

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

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

相关问题 更多 >