Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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_Path_Artificial Intelligence_A Star_Shortest - Fatal编程技术网

Python 麻烦,目标永远找不到

Python 麻烦,目标永远找不到,python,path,artificial-intelligence,a-star,shortest,Python,Path,Artificial Intelligence,A Star,Shortest,首先,很抱歉剪掉了太长的代码,但我觉得所有的代码都与理解问题相关 我有一个grid.txt文件(参见以下链接) 它存储二维单元格网格,填充0、100或-1。0是空闲的,100和-1被占用 我的A*必须找到从给定起点到目标的路径 当我删除以下检查单元是否已被占用的邻居条件时,该条件可以正常工作: if (world[yy][xx]!=0): continue 然而,当试图计算一条考虑了占用单元格的路径时,我的代码似乎并没有产生结果 任何帮助都将不胜感激,因

首先,很抱歉剪掉了太长的代码,但我觉得所有的代码都与理解问题相关

我有一个grid.txt文件(参见以下链接) 它存储二维单元格网格,填充0、100或-1。0是空闲的,100和-1被占用

我的A*必须找到从给定起点到目标的路径

当我删除以下检查单元是否已被占用的邻居条件时,该条件可以正常工作:

if (world[yy][xx]!=0):
                    continue
然而,当试图计算一条考虑了占用单元格的路径时,我的代码似乎并没有产生结果

任何帮助都将不胜感激,因为我真的很想了解这个问题。我的代码如下:

#!/usr/bin/env python 
import math
import json
from time import time
t = time()

start = [1,1]
size = [600,600]

stuff = open('grid.txt','r')

world = json.loads(stuff.read())


size[0]=len(world[0])
size[1]=len(world)

goal = [600,600]

print("World size: %sx%s" % (size[0],size[1]))


def astar():

    pq = []
    pq.append(([start],0))

    print("Definitely getting here")
    hits = []

    while (pq[0][0][-1] != goal):

        currentpath = pq.pop(0)[0][:]
        hits.append(currentpath[-1])

        for n in neighbours(currentpath[-1]):
            if n in hits:
                continue

            newPath=currentpath[:]
            newPath.append(n)
            heur=len(currentpath) + heuristic(n)
            print("newPath: %s (%s)" % (newPath,heur))
            pq.append((newPath,heur))

        pq=sorted(pq, key=lambda path: path[1])

    print("Done!")

    return pq[0][0]

def neighbours(coords): # [4,5]
    x = coords[0]
    y = coords[1]
    maxx = size[0]
    maxy = size[1]
    n=[]
    for i in range (-1,2):
        for j in range(-1,2):
            if (i==0 and j==0):
                continue
            else:
                xx = x + i
                yy = y + j

                if (world[yy][xx]!=0):
                    continue

                if (xx >= 0 and yy >= 0):
                    if (xx <= maxx):
                        if (yy <= maxy):
                            n.append([xx,yy])
    return n


def heuristic(n):
    dx = abs(n[0] - goal[0])
    dy = abs(n[1] - goal[1])
    return math.sqrt(dx * dx + dy * dy) 


print(astar())

print (time() - t)
#/usr/bin/env python
输入数学
导入json
从时间导入时间
t=时间()
开始=[1,1]
尺寸=[600600]
stuff=open('grid.txt','r')
world=json.load(stuff.read())
大小[0]=len(世界[0])
尺寸[1]=透镜(世界)
目标=[600600]
打印(“世界大小:%sx%s”%(大小[0],大小[1]))
def astar():
pq=[]
pq.追加(([开始],0))
打印(“肯定到了这里”)
点击次数=[]
而(pq[0][0][1]!=目标):
currentpath=pq.pop(0)[0][:]
hits.append(当前路径[-1])
对于邻居中的n(当前路径[-1]):
如果点击次数为n:
持续
newPath=currentpath[:]
newPath.append(n)
heur=len(当前路径)+启发式(n)
打印(“新路径:%s(%s)”%(新路径,heur))
pq.append((新路径,heur))
pq=已排序(pq,键=lambda路径:路径[1])
打印(“完成!”)
返回pq[0][0]
def邻居(coords):#[4,5]
x=坐标[0]
y=坐标[1]
maxx=大小[0]
maxy=大小[1]
n=[]
对于范围(-1,2)内的i:
对于范围(-1,2)内的j:
如果(i==0和j==0):
持续
其他:
xx=x+i
yy=y+j
如果(世界[yy][xx]!=0):
持续
如果(xx>=0和yy>=0):

如果出现(xx,则在尝试访问世界列表中的那些元素后,您正在检查[xx]、[yy]是否在边界内。因此,当xx和yy超出边界时,您最终会遇到
索引器:列表索引超出范围

此外,您的maxx和maxy检查被一个关闭。如果您尝试访问
world[maxy][maxx]
,您最终总会遇到索引器

在访问列表元素之前,请确保一切正常:

xx = x + i
yy = y + j

if ( xx >= 0 and 
     yy >= 0 and 
     xx < maxx and 
     yy < maxy and 
     world[yy][xx] == 0 ):

     n.append([xx,yy])
xx=x+i
yy=y+j
如果(xx>=0且
yy>=0和
xx
我查看了您的文件。它不仅包含0和100,还包含-1?