A *搜索在python中不起作用

丹尼尔·史密斯

我正在从事在线AI课堂作业。作为作业的一部分,我必须在python中实现A *搜索。我的代码:

def aStarSearch(problem, heuristic=nullHeuristic):
"""Search the node that has the lowest combined cost and heuristic first."""
"*** YOUR CODE HERE ***"

    fringe = util.PriorityQueue()
    visited = {} # Visited nodes

    if problem.isGoalState(problem.getStartState()):
        return []

    fringe.push((problem.getStartState(),[]),0)

    while not fringe.isEmpty():
        currentState, pathToCurrent = fringe.pop()
        currentCost = problem.getCostOfActions(pathToCurrent)

        if problem.isGoalState(currentState):
            return pathToCurrent

        if currentState not in visited or currentCost<visited[currentState]:
            visited[currentState]=currentCost
            for successor,action,stepCost in problem.getSuccessors(currentState):
                currentTotalCost = currentCost + stepCost + heuristic(currentState, problem)
                fringe.push((successor, pathToCurrent+[action]),currentTotalCost)
    return []

它对我来说似乎是正确的,但是当我运行自动分级机时,它会输出以下内容:

*** FAIL: test_cases/q4/astar_1_graph_heuristic.test
***     graph:
***              2     3     2
***           S --- A --- C ---> G
***           | \       /       ^
***         3 |  \ 5   / 1     / 
***           |   \   /       / 
***           B --- D -------/
***              4         5  
***         
***         S is the start state, G is the goal.  Arrows mark possible state 
***         transitions.  The number next to the arrow is the cost of that transition.
***         
***         The heuristic value of each state is:
***             S 6.0
***             A 2.5
***             B 5.25
***             C 1.125
***             D 1.0625
***             G 0
***     student solution:       ['0', '0', '2']
***     student expanded_states:    ['S', 'A', 'C', 'D']
*** 
***     correct solution:       ['0', '0', '2']
***     correct expanded_states:    ['S', 'A', 'D', 'C']
***     correct rev_solution:       ['0', '0', '2']
***     correct rev_expanded_states:    ['S', 'A', 'D', 'C']

我对python不太了解,但是在我看来我的代码应该可以通过此测试。如何修复我的代码,使其能够通过测试?提前致谢!

快乐戴夫

在这行上:

currentTotalCost = currentCost + stepCost + heuristic(currentState, problem)

您试图找出后继节点上的成本:这应该是到当前节点的路径,加上步骤成本,再加上后继节点上的启发式预期成本。所以我认为你应该打电话heuristic(successor,problem),而不是heuristic(currentState,problem)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在元素内部搜索似乎在 python selenium 中不起作用

来自分类Dev

搜索在Codeigniter中不起作用

来自分类Dev

通配符搜索在Kibana中不起作用

来自分类Dev

搜索栏在prototypecell中不起作用

来自分类Dev

排序在弹性搜索中不起作用

来自分类Dev

Ajax搜索在Django中不起作用

来自分类Dev

Ajax搜索在Django中不起作用

来自分类Dev

搜索查询在PHP中不起作用

来自分类Dev

通配符搜索在Kibana中不起作用

来自分类Dev

搜索栏在prototypecell中不起作用

来自分类Dev

在PFQueryTableView中搜索不起作用

来自分类Dev

在ajax页面中搜索不起作用

来自分类Dev

ElasticSearch中的嵌套搜索不起作用?

来自分类Dev

搜索在Laravel 5.1中不起作用

来自分类Dev

搜索在 orocrm 2.0 中不起作用

来自分类Dev

在 PHP 中搜索不起作用

来自分类Dev

由于搜索输入中的空格,提交搜索不起作用

来自分类Dev

Thunar中的鱼搜索在Xfce中不起作用

来自分类Dev

jQuery搜索不起作用

来自分类Dev

搜索字段不起作用

来自分类Dev

Elasticsearch搜索不起作用

来自分类Dev

jQuery搜索不起作用

来自分类Dev

递归搜索不起作用

来自分类Dev

搜索字段不起作用

来自分类Dev

搜索条件不起作用

来自分类Dev

Codeigniter搜索不起作用

来自分类Dev

实时搜索不起作用

来自分类Dev

搜索栏不起作用

来自分类Dev

Rails应用程序中的搜索表单不起作用