嗨,我想將起始單元格更改為左上角的單元格,目標單元格在右下角。但在代碼中,起始單元格在右下角,目標單元格在左上角。換句話說,它的方向相反。我如何更改代碼。謝謝。
from pyamaze import maze,agent,COLOR,textLabel
from timeit import timeit
def UCS(m,*h,start=None):
if start is None:
start=(m.rows,m.cols)
hurdles=[(i.position,i.cost) for i in h]
unvisited={n:float('inf') for n in m.grid}
unvisited[start]=0
visited={}
revPath={}
while unvisited:
currCell=min(unvisited,key=unvisited.get)
visited[currCell]=unvisited[currCell]
if currCell==m._goal:
break
for d in 'EWNS':
if m.maze_map[currCell][d]==True:
if d=='E':
childCell=(currCell[0],currCell[1] 1)
elif d=='W':
childCell=(currCell[0],currCell[1]-1)
elif d=='S':
childCell=(currCell[0] 1,currCell[1])
elif d=='N':
childCell=(currCell[0]-1,currCell[1])
if childCell in visited:
continue
tempDist= unvisited[currCell] 1
for hurdle in hurdles:
if hurdle[0]==currCell:
tempDist =hurdle[1]
if tempDist < unvisited[childCell]:
unvisited[childCell]=tempDist
revPath[childCell]=currCell
unvisited.pop(currCell)
fwdPath={}
cell=m._goal
while cell!=start:
fwdPath[revPath[cell]]=cell
cell=revPath[cell]
return fwdPath,visited[m._goal]
if __name__=='__main__':
myMaze=maze(10,10)
myMaze.CreateMaze(loadMaze='maze--2021-11-12--21-36-39.csv')
# myMaze.CreateMaze(loadMaze='dijkMaze.csv')
# h1=agent(myMaze,4,4,color=COLOR.red)
# h2=agent(myMaze,4,6,color=COLOR.red)
# h3=agent(myMaze,4,1,color=COLOR.red)
# h4=agent(myMaze,4,2,color=COLOR.red)
# h5=agent(myMaze,4,3,color=COLOR.red)
# h1.cost=100
# h2.cost=100
# h3.cost=100
# h4.cost=100
# h5.cost=100
# path,c=dijstra(myMaze,h1,h2,h2,h3,h4,h5)
# path,c=dijkstra(myMaze,h1,h2,h3,h4,h5,start=(6,1))
path,c=UCS(myMaze)
textLabel(myMaze,'Total Cost',c)
a=agent(myMaze,color=COLOR.cyan,filled=True,footprints=True,)
# a=agent(myMaze,6,1,color=COLOR.cyan,filled=True,footprints=True)
myMaze.tracePath({a:path},delay=20)
t1=timeit(stmt='UCS(myMaze)',number=100,globals=globals())
textLabel(myMaze,'UCS Time',t1)
myMaze.run()
迷宮路徑(csv)
cell ,E,W,N,S
"(1, 1)",0,0,0,1
"(2, 1)",1,0,1,1
"(3, 1)",1,0,1,1
"(4, 1)",1,0,1,1
"(5, 1)",1,0,1,1
"(6, 1)",1,0,1,0
"(7, 1)",1,0,0,1
"(8, 1)",1,0,1,1
"(9, 1)",1,0,1,0
"(10, 1)",1,0,0,0
"(1, 2)",1,0,0,1
"(2, 2)",1,1,1,0
"(3, 2)",1,1,0,0
"(4, 2)",1,1,0,0
"(5, 2)",1,1,0,0
"(6, 2)",1,1,0,1
"(7, 2)",1,1,1,0
"(8, 2)",1,1,0,0
"(9, 2)",0,1,0,1
"(10, 2)",1,1,1,0
"(1, 3)",1,1,0,0
"(2, 3)",0,1,0,1
"(3, 3)",1,1,1,1
"(4, 3)",0,1,1,1
"(5, 3)",1,1,1,1
"(6, 3)",1,1,1,0
"(7, 3)",1,1,0,1
"(8, 3)",1,1,1,1
"(9, 3)",0,0,1,1
"(10, 3)",1,1,1,0
"(1, 4)",1,1,0,1
"(2, 4)",1,0,1,1
"(3, 4)",1,1,1,1
"(4, 4)",0,0,1,1
"(5, 4)",1,1,1,0
"(6, 4)",1,1,0,1
"(7, 4)",1,1,1,0
"(8, 4)",1,1,0,1
"(9, 4)",0,0,1,1
"(10, 4)",1,1,1,0
"(1, 5)",1,1,0,0
"(2, 5)",1,1,0,0
"(3, 5)",1,1,0,1
"(4, 5)",0,0,1,1
"(5, 5)",1,1,1,1
"(6, 5)",1,1,1,0
"(7, 5)",1,1,0,1
"(8, 5)",0,1,1,1
"(9, 5)",0,0,1,1
"(10, 5)",1,1,1,0
"(1, 6)",1,1,0,1
"(2, 6)",1,1,1,1
"(3, 6)",0,1,1,1
"(4, 6)",0,0,1,1
"(5, 6)",1,1,1,0
"(6, 6)",1,1,0,1
"(7, 6)",0,1,1,1
"(8, 6)",0,0,1,1
"(9, 6)",0,0,1,1
"(10, 6)",1,1,1,0
"(1, 7)",1,1,0,0
"(2, 7)",1,1,0,1
"(3, 7)",1,0,1,0
"(4, 7)",1,0,0,1
"(5, 7)",0,1,1,1
"(6, 7)",1,1,1,1
"(7, 7)",0,0,1,1
"(8, 7)",0,0,1,1
"(9, 7)",1,0,1,1
"(10, 7)",1,1,1,0
"(1, 8)",1,1,0,1
"(2, 8)",1,1,1,0
"(3, 8)",0,1,0,1
"(4, 8)",1,1,1,1
"(5, 8)",0,0,1,1
"(6, 8)",1,1,1,1
"(7, 8)",1,0,1,1
"(8, 8)",0,0,1,1
"(9, 8)",1,1,1,0
"(10, 8)",1,1,0,0
"(1, 9)",1,1,0,0
"(2, 9)",1,1,0,1
"(3, 9)",1,0,1,1
"(4, 9)",0,1,1,1
"(5, 9)",1,0,1,1
"(6, 9)",0,1,1,0
"(7, 9)",1,1,0,1
"(8, 9)",1,0,1,1
"(9, 9)",0,1,1,1
"(10, 9)",1,1,1,0
"(1, 10)",0,1,0,1
"(2, 10)",0,1,1,0
"(3, 10)",0,1,0,1
"(4, 10)",0,0,1,1
"(5, 10)",0,1,1,1
"(6, 10)",0,0,1,1
"(7, 10)",0,1,1,0
"(8, 10)",0,1,0,1
"(9, 10)",0,0,1,1
"(10, 10)",0,1,1,0
我想將起始單元格更改為左上角單元格,目標單元格在右下角。但在代碼中,起始單元格在右下角,目標單元格在左上角。換句話說,它的方向相反。我如何更改代碼。謝謝。
uj5u.com熱心網友回復:
不知道這與演算法有什么關系,但你去吧:
from pyamaze import maze, agent, COLOR, textLabel
from timeit import timeit
def UCS(m, *h, start=None):
if start is None:
start=(1,1)
hurdles = [(i.position, i.cost) for i in h]
unvisited = {n: float('inf') for n in m.grid}
unvisited[start] = 0
visited = {}
revPath = {}
while unvisited:
currCell = min(unvisited, key=unvisited.get)
visited[currCell] = unvisited[currCell]
if currCell == m._goal:
break
for d in 'EWNS':
if m.maze_map[currCell][d] == True:
if d == 'E':
childCell = (currCell[0], currCell[1] 1)
elif d == 'W':
childCell = (currCell[0], currCell[1] - 1)
elif d == 'S':
childCell = (currCell[0] 1, currCell[1])
elif d == 'N':
childCell = (currCell[0] - 1, currCell[1])
if childCell in visited:
continue
tempDist = unvisited[currCell] 1
for hurdle in hurdles:
if hurdle[0] == currCell:
tempDist = hurdle[1]
if tempDist < unvisited[childCell]:
unvisited[childCell] = tempDist
revPath[childCell] = currCell
unvisited.pop(currCell)
fwdPath = {}
cell = m._goal
while cell != start:
fwdPath[revPath[cell]] = cell
cell = revPath[cell]
return fwdPath, visited[m._goal]
if __name__ == '__main__':
myMaze = maze(10, 10)
myMaze.CreateMaze(x=10,y=10,loadMaze='maze--2021-11-12--21-36-39.csv')
# myMaze.CreateMaze(loadMaze='dijkMaze.csv')
# h1=agent(myMaze,4,4,color=COLOR.red)
# h2=agent(myMaze,4,6,color=COLOR.red)
# h3=agent(myMaze,4,1,color=COLOR.red)
# h4=agent(myMaze,4,2,color=COLOR.red)
# h5=agent(myMaze,4,3,color=COLOR.red)
# h1.cost=100
# h2.cost=100
# h3.cost=100
# h4.cost=100
# h5.cost=100
# path,c=dijstra(myMaze,h1,h2,h2,h3,h4,h5)
# path,c=dijkstra(myMaze,h1,h2,h3,h4,h5,start=(6,1))
path, c = UCS(myMaze)
textLabel(myMaze, 'Total Cost', c)
a = agent(x=1,y=1, parentMaze=myMaze ,color=COLOR.cyan, filled=True, footprints=True, )
# a=agent(myMaze,6,1,color=COLOR.cyan,filled=True,footprints=True)
myMaze.tracePath({a: path}, delay=20)
t1 = timeit(stmt='UCS(myMaze)', number=100, globals=globals())
textLabel(myMaze, 'UCS Time', t1)
myMaze.run()
只是為了讓你知道:
當您創建迷宮或將代理放入迷宮時,您可以使用 (x,y) 屬性來控制起始和目標位置,因此通過在 createMaze() 和 gent() 初始化時更改屬性,您可以開始/在任何需要的地方結束迷宮。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/359131.html
上一篇:如何合并滿足堆順序的兩棵樹?
下一篇:排列演算法的復雜度分析
