#algorithm #search
Вопрос:
Привет, я хочу изменить начальную ячейку как верхнюю левую ячейку, а ячейка цели находится внизу справа. но в кодах начальная ячейка находится справа внизу, а ячейка цели-слева вверху. Другими словами, в противоположном направлении. Как я меняюсь в коде. Спасибо.
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 lt; 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
Я хочу изменить начальную ячейку как верхнюю левую ячейку, а ячейку цели-в правом нижнем углу. но в кодах начальная ячейка находится справа внизу, а ячейка цели-слева вверху. Другими словами, в противоположном направлении. Как я меняюсь в коде. Спасибо.
Ответ №1:
не знаю, как это связано с алгоритмами, но вот, пожалуйста:
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 lt; 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 (), вы можете начинать/заканчивать лабиринт везде, где вам нужно.