【问题标题】:How these two lines work x2 = x+delta[i][0] , y2 = y+delta[i][1]?这两行如何工作 x2 = x+delta[i][0] , y2 = y+delta[i][1]?
【发布时间】:2019-06-29 03:52:06
【问题描述】:

我正在阅读下面关于 First Search Program - 机器人人工智能的代码,我对下面这两行的工作有点坚持:

x2 = x+delta[i][0]
y2 = y+delta[i][1]

我知道从节点到邻居的移动模式的增量,但我不明白这两条线在这里是如何工作的。谁能帮我解释一下?

以下代码:

#grid format
# 0 = navigable space
# 1 = occupied space

grid=[[0,0,1,0,0,0],
      [0,0,1,0,0,0],
      [0,0,0,0,1,0],
      [0,0,1,1,1,0],
      [0,0,0,0,1,0]]

init = [0,0]                           
goal = [len(grid)-1,len(grid[0])-1]   

#Below the four potential actions to the single field
delta=[[-1, 0],      #up 
       [ 0,-1],      #left
       [ 1, 0],      #down
       [ 0, 1]]      #right

delta_name = ['^','<','V','>']        
cost = 1

def search():
    #open list elements are of the type [g,x,y] 

    closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
    #We initialize the starting location as checked
    closed[init[0]][init[1]] = 1

    # we assigned the cordinates and g value
    x = init[0]
    y = init[1]
    g = 0
    #our open list will contain our initial value
    open = [[g,x,y]]


    found = False #flag that is set when search complete
    resign= False #Flag set if we can't find expand

    #print('initial open list:')
    #for i in range(len(open)):
            #print('  ', open[i])
    #print('----')


    while found is False and resign is False:
        #Check if we still have elements in the open list
        if len(open)==0: #If our open list is empty
            resign=True
            print('Fail')
            print('############# Search terminated without success')
        else: 
            #if there is still elements on our list
            #remove node from list
            open.sort()      
            open.reverse()    #reverse the list
            next = open.pop() 

            #print('list item')
            #print('next')

            #Then we assign the three values to x,y and g. 
            x = next[1]
            y = next[2]
            g = next[0]

            #Check if we are done

            if x == goal[0] and y == goal[1]:
                found = True
                print(next) #The three elements above this if
                print('############## Search is success')
            else:
                #expand winning element and add to new open list
                for i in range(len(delta)): 

                    x2 = x+delta[i][0]
                    y2 = y+delta[i][1]
                    #if x2 and y2 falls into the grid
                    if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 <= len(grid[0])-1:
                        #if x2 and y2 not checked yet and there is not obstacles
                        if closed[x2][y2] == 0 and grid[x2][y2] == 0:
                            g2 = g+cost #we increment the cose
                            open.append([g2,x2,y2])#we add them to our open list
                            #print('append list item')
                            #print([g2,x2,y2])
                            #Then we check them to never expand again
                            closed[x2][y2] = 1

search()

【问题讨论】:

    标签: python algorithm artificial-intelligence depth-first-search


    【解决方案1】:

    x2y2是delta中第i个潜在动作执行后的位置。

    delta[i][0]表示delta中第i个动作的第一个元素(第i个动作引起的x变化)

    delta[i][1]表示delta中第i个动作的第二个元素(第i个动作引起的y变化)

    所以x2=x+delta[i][0]y2=y+delta[i][1] 正在应用第i 个潜在操作并将生成的新x 和y 值存储在x2y2 中。

    在这个程序的上下文中,这些值是为每个潜在动作创建的(for i in range(len(delta)),如果结果位置落入网格中,尚未检查,并且没有障碍物,那么位置是已添加到 open 列表中。

    【讨论】:

    • @巴斯卡。非常感谢先生的解释。经过您的许可,我没有在 (x 和 delta) 和 (y 和 delta) 之间得到 (+) 符号。通过这个总结,它们在这里的确切含义是什么?。
    • (+) 号是将动作 (delta[i][0]) 引起的 x 的变化添加到原始位置 x。该 x 加上 x 的变化为您提供了由该特定操作引起的新 x。例如。如果您使用的是 [1, 0] 的 delta[2],则 x2 将是 x + 1,y2 将是 y + 0,有效地将 (x, y) 向右移动一个单位。
    猜你喜欢
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2017-01-12
    • 2013-02-01
    相关资源
    最近更新 更多