Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python宽度优先搜索类型错误_Python_Breadth First Search - Fatal编程技术网

Python宽度优先搜索类型错误

Python宽度优先搜索类型错误,python,breadth-first-search,Python,Breadth First Search,在宽度优先搜索的Python脚本中,出现以下错误: TypeError:“列表”对象不可调用 代码如下: initialState = sys.argv[2] initialState = [i for i in initialState.split(',')] goalTest = ['0','1','2','3','4','5','6','7','8'] def bfs(initialState, goalTest): q = deque([]) for item in i

在宽度优先搜索的Python脚本中,出现以下错误:

TypeError:“列表”对象不可调用

代码如下:

initialState = sys.argv[2]
initialState = [i for i in initialState.split(',')]
goalTest = ['0','1','2','3','4','5','6','7','8']

def bfs(initialState, goalTest):
    q = deque([])
    for item in initialState:
        q.appendleft(item)
        print q

    frontier = q
    explored = set()

    while not frontier <= len(frontier):
        state = frontier.pop()
        print state
        explored.add(state)

        if goalTest(state):
            return state

        for neighbor in state.neighbors():
            if neighbor not in frontier or explored:
                frontier.append(neighbor)

    return "FAILURE"
initialState=sys.argv[2]
initialState=[initialState.split(',')中的i代表i]
goalTest=['0'、'1'、'2'、'3'、'4'、'5'、'6'、'7'、'8']
def bfs(初始状态、目标测试):
q=deque([])
对于处于初始状态的项目:
q、 左(项)
打印q
前沿=q
explored=set()

虽然不是frontier,但您的代码有很多问题:

  • 如果要将状态添加到访问状态的
    集合
    ,它们必须是
    元组
    ,因为
    列表
    不可散列
  • 不要将状态中的单个项目添加到
    deque
    ,只将状态作为一个整体
  • 如果这应该是一个BFS,您必须使用
    deque.popleft
    ,这样它的行为就像一个FIFO队列;使用
    pop
    您有一个后进先出队列或堆栈,即DFS(深度优先搜索)
  • 而不是frontier 0
    或只是
    而frontier
  • goalTest
    只是一个参考状态,所以您应该只比较:
    if state==goalTest
  • neights
    不是
    state
    (它只是一个列表或元组)的方法,而是一个函数,因此像
    neights(state)
    一样调用它,而不是
    state.neights()
  • 邻居不在边界或已探测
    不检查
    邻居
    是否不在这两个列表中,而是检查
    (邻居不在边界)或已探测
    ;此外,如果您立即将其添加到
    已探索的
    中,则无需检查它是否位于
    前沿
  • 与其
    返回“FAILURE”
    ,不如
    返回False
    返回None
以下是一个固定(但未经过彻底测试)版本:

实现
邻居
留给读者作为练习。如果是,那么
0
应该是拼图中的“空白”空间。只需检查与之相邻的其他部分(提示:除法和模是您的朋友)并交换这些部分,然后返回交换状态的列表


另外,请注意,BFS可能不是解决此问题的最佳算法,因为存在许多状态,而诸如BFS之类的无信息搜索可能需要非常长的时间。更好地使用知情搜索,例如,使用错位磁贴的数量作为启发。

错误是不言自明的,
goalTest
是一个列表,您试图将参数像函数一样传递给它;我是一名web前端开发人员,也是python的新手。我刚开始一门人工智能课程,这只是编程课的一个单元。所以我每次都会遇到问题,我不明白到底发生了什么。我希望能得到帮助。也许当我得到一个可运行的sccipt时,我能更好地理解它。而前沿是因为“而非前沿”不起作用。我试着去理解邻居的想法。你应该加上你的输入是什么样子的,一个给定状态的“beighbor”是什么,然后我们可能会提供帮助。这是一个8puzzel解决方案单元。因此,我用python driver.py bfs 1,2,3,4,5,8,7,0,6开始脚本,关于我不知道的邻居,它来自演示文稿中的伪代码。line
而不是frontier 0:
while len(frontier):
甚至
对于frontier中的state:
而不是
while
initialState = tuple(initialState.split(','))
goalTest = ('0','1','2','3','4','5','6','7','8')

def bfs(initialState, goalTest):
    frontier = collections.deque([initialState])
    explored = set()

    while frontier:
        state = frontier.popleft()
        print(repr(state), len(frontier), len(explored))

        if state == goalTest:
            return state

        for neighbor in neighbors(state):
            if neighbor not in explored:
                frontier.append(neighbor)
                explored.add(neighbor)
    return False

def neighbors(state):
    ...