-1

我正在研究来自人工智能项目的 python 程序。这是关于深度优先搜索的。我有一个运行良好的python代码:

def depthFirst(problem,start,path,result,vis):
    next=problem.getSuccessors(start)
    if problem.isGoalState(start):
        result.append(path)
        print path
    for I in next:
        if I[0] not in vis:
            vis.append(I[0])
            depthFirst(problem,I[0],path+[I[1]],result,vis)
            vis.pop()

    def depthFirstSearch(problem):
        start=problem.getStartState()
        result=[]
        depthFirst(problem,start,[],result,[])
        return result[-1]

我知道对于数字,python 不能返回它的引用,但对于列表,它传递它的引用而不是它的值。

我写了一个类似的程序;在这里我使用变量path而不是result

def depthFirst(problem,start,path,vis):
    if problem.isGoalState(start[0]):
        print 'Start1',start[1]

        print 'Path',path
        l=start[1]
        path.append(l)
        path=path[0]
        print 'FindPath',path

        return

    #vis.add(start[0])
    fringe=problem.getSuccessors(start[0])
    for child in fringe:
        '''if problem.isGoalState(child):
            path.apend(child[1])

            break
            return'''
        print child
        if child[0] not in vis:
            tmppath=start[1][:]
            tmppath.append(child[1])

            vis.add(child[0])
            print 'vis=',vis
            print path
            depthFirst(problem,(child[0],tmppath),path,vis)
        print 'path=',path

def depthFirstSearch(problem):
    start=problem.getStartState()
    path=[]
    vis=set([])

    depthFirst(problem,[start,[]],path,vis)
    print path[0]
    return path[0]

但它的路径与第一个路径截然不同。我认为这两个程序是相同的,但我只是想更改resultpath. 怎么来的区别。

4

2 回答 2

0

我已尽力为您代码中的一些问题添加注释,希望它能解决您的问题:

def depthFirst(problem,start,path,vis):
    if problem.isGoalState(start[0]):
        l=start[1]      # just use path.append(start[1]) here
        path.append(l)  # AND why do you even need path here at all???
        path=path[0]    # if you assign path=path[0], then don't call path[0]
                        # in depthFirstSearch
        return

    # vis.add(start[0]) # Why comment this out? Move this to the top!
    fringe=problem.getSuccessors(start[0])
    for child in fringe:
        if child[0] not in vis:
            tmppath=start[1][:]
            tmppath.append(child[1])
            vis.add(child[0])  # you AREN'T discovering it yet, don't add it here!
            depthFirst(problem,(child[0],tmppath),path,vis)

def depthFirstSearch(problem):
    start=problem.getStartState()
    path=[]
    vis=set([])
    depthFirst(problem,[start,[]],path,vis)
    return path[0]  # should be path instead here since you assigned path=path[0]

这是我认为应该可以工作的固定版本:

def depthFirst(problem,start,path,vis):
    vis.add(start[0])

    if problem.isGoalState(start[0]):
        path.append(start[1])  # still convoluted..
        path = path[0]
        return True

    fringe=problem.getSuccessors(start[0])
    for child in fringe:
        if child[0] not in vis:
            tmppath = start[1][:]
            tmppath.append(child[1])
            depthFirst(problem, (child[0], tmppath), path, vis)

def depthFirstSearch(problem):
    start=problem.getStartState()
    path=[]
    vis=set([])
    if depthFirst(problem, [start, []], path, vis):
        return path
于 2012-10-22T07:27:38.357 回答
-2

我对 python 不是很熟悉,但是如果您需要通过引用传递 var,也许这会对您有所帮助:如何通过引用传递变量?

于 2012-10-22T05:47:36.220 回答