Python 我如何使用欧几里德距离公式来计算这里的启发式?让算法在八个方向上搜索?

Python 我如何使用欧几里德距离公式来计算这里的启发式?让算法在八个方向上搜索?,python,path-finding,a-star,heuristics,Python,Path Finding,A Star,Heuristics,我正在研究星型算法,我的启发式设置如下: heuristic = [[9, 8, 7, 6, 5, 4], [8, 7, 6, 5, 4, 3], [7, 6, 5, 4, 3, 2], [6, 5, 4, 3, 2, 1], [5, 4, 3, 2, 1, 0]] 算法在四个方向的移动如下: delta = [[-1, 0 ], # go up [ 0, -1], # g

我正在研究星型算法,我的启发式设置如下:

heuristic = [[9, 8, 7, 6, 5, 4],
             [8, 7, 6, 5, 4, 3],
             [7, 6, 5, 4, 3, 2],
             [6, 5, 4, 3, 2, 1],
             [5, 4, 3, 2, 1, 0]]
算法在四个方向的移动如下:

delta = [[-1, 0 ], # go up
         [ 0, -1], # go left
         [ 1, 0 ], # go down
         [ 0, 1 ]] # go right    
delta_name = ['^','<','V','>']
我如何通过欧几里德距离公式计算启发式,并使算法朝八个方向移动,而不仅仅是四个方向

这是我的代码:

import random
grid = [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]]
heuristic = [[9, 8, 7, 6, 5, 4],
             [8, 7, 6, 5, 4, 3],
             [7, 6, 5, 4, 3, 2],
             [6, 5, 4, 3, 2, 1],
             [5, 4, 3, 2, 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','>']  #The name of above actions
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))]
    action = [[-1 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
    expand=[[-1 for row in range(len(grid[0]))] for col in range(len(grid))]

    # we assigned the cordinates and g value
    x = init[0]
    y = init[1]
    g = 0
    h = heuristic[x][y]
    f = g + h 

    #our open list will contain our initial value
    open = [[f, g, h, x, y]]
    found  = False   #flag that is set when search complete
    resign = False   #Flag set if we can't find expand
    count = 0

    #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, there is nothing to expand.
            resign = True
            print('Fail')
            print('############# Search terminated without success')
            print()
        else: 
            #if there is still elements on our list
            #remove node from list
            open.sort()             #sort elements in an increasing order from the smallest g value up
            open.reverse()          #reverse the list
            next = open.pop()       #remove the element with the smallest g value from the list
            #print('list item')
            #print('next')

            #Then we assign the three values to x,y and g. Which is our expantion.
            x = next[3]
            y = next[4]
            g = next[1]

            expand[x][y] = count
            count+=1

            #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')
                print()

            else:
                #expand winning element and add to new open list
                for i in range(len(delta)):       #going through all our actions the four actions
                    #We apply the actions to x and y with additional delta to construct x2 and y2
                    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 closed[x2][y2] == 0 and grid[x2][y2] == 0:
                            g2 = g + cost            
                            h2 = heuristic[x2][y2]
                            f2 = g2 + h2 

                            open.append([f2,g2,h2,x2,y2])   
                            #print('append list item')
                            #print([g2,x2,y2])
                            #Then we check them to never expand again
                            closed[x2][y2] = 1
                            action[x2][y2] = i

    for i in range(len(expand)):
        print(expand[i])
    print()

    policy=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
    x=goal[0]
    y=goal[1]
    policy[x][y]='*'
    while x !=init[0] or y !=init[1]:
        x2=x-delta[action[x][y]][0]
        y2=y-delta[action[x][y]][1]
        policy[x2][y2]= delta_name[action[x][y]]
        x=x2
        y=y2
    for i in range(len(policy)):
        print(policy[i])

search()
随机导入
网格=[[0,1,0,0,0,0,0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0]]
启发式=[[9,8,7,6,5,4],
[8, 7, 6, 5, 4, 3],
[7, 6, 5, 4, 3, 2],
[6, 5, 4, 3, 2, 1],
[5, 4, 3, 2, 1, 0]]
init=[0,0]
目标=[len(网格)-1,len(网格[0])-1]
#下面四个潜在的行动,以单一领域
增量=[-1,0],#向上
[0,-1],#左
[1,0],#向下
[0,1]#对
delta_name=['^','']#上述操作的名称
成本=1
def search():
#开放列表元素的类型为[g,x,y]
闭合=[[0表示范围内的行(len(网格[0]))]表示范围内的列(len(网格))]
action=[-1表示范围内的行(len(网格[0]))]表示范围内的列(len(网格))]
#我们将起始位置初始化为选中状态
已关闭[init[0]][init[1]]=1
expand=[-1表示范围内的行(len(网格[0])),expand=[-1表示范围内的列(len(网格))]
#我们分配了cordinates和g值
x=init[0]
y=init[1]
g=0
h=启发式[x][y]
f=g+h
#我们的开放列表将包含我们的初始值
开放=[[f,g,h,x,y]]
found=False#搜索完成时设置的标志
如果找不到expand,则设置dedict=False#标志
计数=0
#打印('初始打开列表:')
#对于范围内的i(len(open)):
#打印(“”,打开[i])
#打印('---')
当发现错误且辞职错误时:
#检查开放列表中是否仍有元素
如果len(open)=0:#如果我们的open列表为空,则没有可扩展的内容。
辞职=真
打印('失败')
打印(“搜索终止但未成功”)
打印()
其他:
#如果我们的列表中还有元素
#从列表中删除节点
open.sort()#按从最小g值开始的递增顺序对元素进行排序
open.reverse()#反转列表
next=open.pop()#从列表中删除g值最小的元素
#打印('列表项')
#打印(‘下一个’)
#然后我们将这三个值赋给x、y和g。这是我们的解释。
x=下一个[3]
y=下一个[4]
g=下一个[1]
展开[x][y]=计数
计数+=1
#检查我们是否完成了
如果x==目标[0]和y==目标[1]:
找到=真
打印(下一页)#此“如果”上方的三个元素。
印刷品(‘搜索就是成功’)
打印()
其他:
#展开获胜元素并添加到新的开放列表
对于范围内的i(len(delta)):#经历我们所有的动作,四个动作
#我们将动作应用于x和y,并附加delta来构造x2和y2
x2=x+delta[i][0]
y2=y+delta[i][1]
#如果x2和y2落入网格中

如果x2>=0和x2=0和y2,你不应该使用图表:?@Dan说实话,亲爱的,这是我第一次知道,Matplotlib旁边有这样的东西。不,不是绘图。该算法寻找连接图上两个节点的最短路径。所以现在,你的图在上下邻域和左右邻域之间有边。要得到对角线移动,您不需要更改算法,只需在图形中添加连接(边)。@Dan您能帮我解决这个问题吗,先生?我感到困惑。。我怎么办?这有点太开放了,看看这个。也许你不应该用图表:?@Dan说实话,亲爱的,这是我第一次知道,Matplotlib旁边有这样的东西。不,不是情节。该算法寻找连接图上两个节点的最短路径。所以现在,你的图在上下邻域和左右邻域之间有边。要得到对角线移动,您不需要更改算法,只需在图形中添加连接(边)。@Dan您能帮我解决这个问题吗,先生?我感到困惑。。我怎么办?这有点太开放了,看看这个。也可能
import random
grid = [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]]
heuristic = [[9, 8, 7, 6, 5, 4],
             [8, 7, 6, 5, 4, 3],
             [7, 6, 5, 4, 3, 2],
             [6, 5, 4, 3, 2, 1],
             [5, 4, 3, 2, 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','>']  #The name of above actions
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))]
    action = [[-1 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
    expand=[[-1 for row in range(len(grid[0]))] for col in range(len(grid))]

    # we assigned the cordinates and g value
    x = init[0]
    y = init[1]
    g = 0
    h = heuristic[x][y]
    f = g + h 

    #our open list will contain our initial value
    open = [[f, g, h, x, y]]
    found  = False   #flag that is set when search complete
    resign = False   #Flag set if we can't find expand
    count = 0

    #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, there is nothing to expand.
            resign = True
            print('Fail')
            print('############# Search terminated without success')
            print()
        else: 
            #if there is still elements on our list
            #remove node from list
            open.sort()             #sort elements in an increasing order from the smallest g value up
            open.reverse()          #reverse the list
            next = open.pop()       #remove the element with the smallest g value from the list
            #print('list item')
            #print('next')

            #Then we assign the three values to x,y and g. Which is our expantion.
            x = next[3]
            y = next[4]
            g = next[1]

            expand[x][y] = count
            count+=1

            #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')
                print()

            else:
                #expand winning element and add to new open list
                for i in range(len(delta)):       #going through all our actions the four actions
                    #We apply the actions to x and y with additional delta to construct x2 and y2
                    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 closed[x2][y2] == 0 and grid[x2][y2] == 0:
                            g2 = g + cost            
                            h2 = heuristic[x2][y2]
                            f2 = g2 + h2 

                            open.append([f2,g2,h2,x2,y2])   
                            #print('append list item')
                            #print([g2,x2,y2])
                            #Then we check them to never expand again
                            closed[x2][y2] = 1
                            action[x2][y2] = i

    for i in range(len(expand)):
        print(expand[i])
    print()

    policy=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
    x=goal[0]
    y=goal[1]
    policy[x][y]='*'
    while x !=init[0] or y !=init[1]:
        x2=x-delta[action[x][y]][0]
        y2=y-delta[action[x][y]][1]
        policy[x2][y2]= delta_name[action[x][y]]
        x=x2
        y=y2
    for i in range(len(policy)):
        print(policy[i])

search()