Python 不知道如何修复*寻路算法
我在试这个。我遵循灰色方框中的大部分步骤,直到我在dii和diii上遇到路障。以下是寻路的一部分:Python 不知道如何修复*寻路算法,python,a-star,Python,A Star,我在试这个。我遵循灰色方框中的大部分步骤,直到我在dii和diii上遇到路障。以下是寻路的一部分: def pathfind(grid): sx, sy = 0, 0 # find start point and end point cood for y in range(len(grid)): for x in range(len(grid[y])): if grid[y][x] == "S":
def pathfind(grid):
sx, sy = 0, 0
# find start point and end point cood
for y in range(len(grid)):
for x in range(len(grid[y])):
if grid[y][x] == "S":
sx = x
sy = y
elif grid[y][x] == "E":
ex = x
ey = y
opensq = []
closedsq = []
successor = []
#add starting point to closed
opensq.append([sx, sy, gcost(sx, sy, sx, sy), hcost(sx, sy, ex, ey)])
grid[sy][sx] = "O"
while opensq:
# find the node with lowest fcost
q = opensq[0]
if len(opensq) == 1:
pass
else:
for sq in range(len(opensq)):
if sq == len(opensq) - 1:
pass
else:
if q[2] + q[3] < opensq[sq + 1][2] + opensq[sq + 1][3]:
pass
elif q[2] + q[3] == opensq[sq + 1][2] + opensq[sq + 1][3]:
# if f is same, check hcost
if q[3] < opensq[sq + 1][3]:
pass
elif q[3] == opensq[sq + 1][3]:
pass
else:
q = opensq[sq + 1]
else:
q = opensq[sq + 1]
opensq.pop(opensq.index(q))
# pick successors to q
successors = []
successors.append([q[0] + 1, q[1], gcost(q[0] + 1, q[1], sx, sy), hcost(q[0] + 1, q[1], ex, ey)])
successors.append([q[0] - 1, q[1], gcost(q[0] - 1, q[1], sx, sy), hcost(q[0] - 1, q[1], ex, ey)])
successors.append([q[0], q[1] + 1, gcost(q[0], q[1] + 1, sx, sy), hcost(q[0], q[1] + 1, ex, ey)])
successors.append([q[0], q[1] - 1, gcost(q[0], q[1] - 1, sx, sy), hcost(q[0], q[1] - 1, ex, ey)])
for s in successors:
# if successor is the goal, stop search
if s[0] == ex and s[1] == ey:
pass
# if a node with the same position as
# successor is in the OPEN list which has a
# lower f than successor, skip this successor
for sq in opensq:
if sq[2] + sq[3] < s[2] + s[3]:
successors.pop(successors.index(s))
# if a node with the same position as
# successor is in the CLOSED list which has
# a lower f than successor, skip this successor
# otherwise, add the node to the open list
for sq in closedsq:
if sq[2] + sq[3] < s[2] + s[3]:
successors.pop(successors.index(s))
for s in successors:
opensq.append(s)
grid[s[1]][s[0]] = "O"
closedsq.append(q)
grid[q[1]][q[0]] = "C"
def路径查找(网格):
sx,sy=0,0
#找到起点和终点坐标
对于范围内的y(透镜(网格)):
对于范围内的x(len(网格[y]):
如果网格[y][x]=“S”:
sx=x
sy=y
elif网格[y][x]=“E”:
ex=x
ey=y
opensq=[]
closedsq=[]
继任者=[]
#将起点添加到已关闭的
opensq.append([sx,sy,gcost(sx,sy,sx,sy),hcost(sx,sy,ex,ey)])
网格[sy][sx]=“O”
而opensq:
#查找fcost最低的节点
q=opensq[0]
如果len(opensq)==1:
通过
其他:
对于范围内的sq(len(opensq)):
如果sq==len(opensq)-1:
通过
其他:
如果q[2]+q[3]
sx和sy是起点,ex-ey是终点。我使用字母来确定一个节点是打开的还是关闭的,或者是开始还是结束,以及它们的第一个字母。运行时会弹出此错误:
Traceback (most recent call last):
File "D:/Bruh/Desktop/Codes and Scripts/AI/A_Pathfinding/Code.py", line 287, in <module>
main()
File "D:/Bruh/Desktop/Codes and Scripts/AI/A_Pathfinding/Code.py", line 274, in main
pathfind(grid)
File "D:/Bruh/Desktop/Codes and Scripts/AI/A_Pathfinding/Code.py", line 98, in pathfind
successors.pop(successors.index(s))
ValueError: [5, 12, 3, 14] is not in list
回溯(最近一次呼叫最后一次):
文件“D:/Bruh/Desktop/Codes and Scripts/AI/A_Pathfinding/Code.py”,第287行,在
main()
文件“D:/Bruh/Desktop/Codes and Scripts/AI/A_Pathfinding/Code.py”,第274行,在main中
路径查找(网格)
文件“D:/Bruh/Desktop/Codes and Scripts/AI/A_Pathfinding/Code.py”,第98行,在pathfind中
继承人.pop(继承人.索引)
ValueError:[5,12,3,14]不在列表中
这是整个脚本,我使用pygame进行可视化,但只关注pathfind方法,它目前可以正常工作,但是1。由于某种原因,闭合节点在每次循环后再次变为开放节点。如果我在后续对象面对墙时跳过它,它将陷入循环。
编辑:终于完成了代码!我会把它挂在那里,唯一的问题是我不知道该怎么走。。
错误发生在以下部分:
对于继承者中的:
如果s[0]==ex和s[1]==ey:
通过
对于opensq中的sq:
如果sq[2]+sq[3]
在“第1行”和“第2行”上,后续项。在s
在for
循环的上一个循环中弹出后,可以调用索引。然后,出现错误
而且,更重要的是,您的代码执行不正确。在对代码进行注释时,只应选中“与后续节点位置相同的节点”。您可以尝试以下代码而不是上面提到的部分来修复问题
#迭代列表的副本,
#因为我们在原始列表中弹出元素。
对于列表中的s(继任者):
如果s[0]==ex和s[1]==ey:
通过
对于opensq+closedsq中的sq:
如果sq[0]==s[0]和sq[1]==s[1]和sq[2]+sq[3]则错误发生在以下部分:
对于继承者中的:
如果s[0]==ex和s[1]==ey:
通过
对于opensq中的sq:
如果sq[2]+sq[3]
在“第1行”和“第2行”上,后续项。在s
在for
循环的上一个循环中弹出后,可以调用索引。然后,出现错误
而且,更重要的是,您的代码执行不正确。在对代码进行注释时,只应选中“与后续节点位置相同的节点”。您可以尝试以下代码而不是上面提到的部分来修复问题
#迭代列表的副本,
#自从我们
successors.append([q[0] - 1, q[1], q[2] + 1, hcost(q[0] - 1, q[1], ex, ey)])
successors.append([q[0], q[1] + 1, q[2] + 1, hcost(q[0], q[1] + 1, ex, ey)])
successors.append([q[0], q[1] - 1, q[2] + 1, hcost(q[0], q[1] - 1, ex, ey)])