search.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:Pac-Man-Search 作者: xuefengDevelop 项目源码 文件源码
def aStarSearch(problem, heuristic=nullHeuristic):

    """Search the node that has the lowest combined cost and heuristic first."""
    "*** YOUR CODE HERE ***"
    "it is very similar to UCS, just change the totalcost to the heuristic + cost  "
    nodePriorityQueue = util.PriorityQueue()
    visited = []
    path = []
    totalCost = heuristic(problem.getStartState(),problem)
    startNode =(problem.getStartState(),path)
    nodePriorityQueue.push((startNode),totalCost)

    "start while loop to find the correct path"

    while  nodePriorityQueue.isEmpty() is False:
        node,path = nodePriorityQueue.pop()

        visited.append(node)

        if problem.isGoalState(node):
            return path
        for successor, direction, cost in problem.getSuccessors(node) :
            if successor not in visited:
                visited.append(successor)
                newNode =(successor,path+[direction])
                totalCost = problem.getCostOfActions(path + [direction]) + heuristic(successor,problem)
                nodePriorityQueue.push(newNode,totalCost)
            if problem.isGoalState(successor):
                newNode =(successor,path + [direction])
                totalCost = problem.getCostOfActions(path + [direction]) + heuristic(successor,problem)
                nodePriorityQueue.push(newNode,totalCost)


    return None


    util.raiseNotDefined()


# Abbreviations
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号