BagFix and add main file
This commit is contained in:
parent
afa3c146b0
commit
b1b22678a2
|
|
@ -15,9 +15,7 @@ class TextFileMazeBuilder(MazeBuilders):
|
|||
for y, line in enumerate(lines):
|
||||
row = []
|
||||
for x, ch in enumerate(line):
|
||||
|
||||
cell = Cell(x, y, is_wall = (ch == "#"), is_start = (ch == "S"), is_exit = (ch == "E"))
|
||||
|
||||
cell = Cell(x, y, isWall = (ch == "#"), isStart = (ch == "S"), isExit = (ch == "E"))
|
||||
if (ch == "S"):
|
||||
start = cell
|
||||
if (ch == "E"):
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ class Maze:
|
|||
|
||||
# Создание новой ячейки
|
||||
def getCell(self, x, y):
|
||||
if 0 <= y < self.height and 0 <= x < self.width:
|
||||
if 0 <= x < self.height and 0 <= y < self.width:
|
||||
return self.grid[x][y]
|
||||
return None
|
||||
|
||||
|
|
@ -19,7 +19,7 @@ class Maze:
|
|||
|
||||
for dx, dy in directions:
|
||||
nx, ny = cell.x + dx, cell.y + dy
|
||||
neighbor = self.getСell(nx, ny)
|
||||
neighbor = self.getCell(nx, ny)
|
||||
if neighbor and neighbor.isPassable():
|
||||
result.append(neighbor)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,34 +1,38 @@
|
|||
from Strategies.strat import PathFindingStrategy
|
||||
from path import restore
|
||||
from Strategies.path import restore
|
||||
import heapq
|
||||
|
||||
class AStar(PathFindingStrategy):
|
||||
|
||||
def heuristic(self, a, b):
|
||||
return abs(a.x - b.x)+ abs(a.y - b.y)
|
||||
return abs(a.x - b.x) + abs(a.y - b.y)
|
||||
|
||||
def findPath(self, maze, start, exit):
|
||||
heap = [(0, start)]
|
||||
heap = []
|
||||
counter = 0
|
||||
heapq.heappush(heap, (0, counter, start))
|
||||
counter += 1
|
||||
|
||||
parent = {}
|
||||
g = {start: 0}
|
||||
visited = set()
|
||||
|
||||
while heap:
|
||||
_, current = heapq.heappop(heap)
|
||||
_, _, current = heapq.heappop(heap) # распаковка трёх элементов
|
||||
|
||||
if current == exit:
|
||||
break
|
||||
|
||||
visited.add(current)
|
||||
|
||||
for n in maze.get_neigbors(current):
|
||||
for n in maze.getNeighbors(current):
|
||||
tentative = g[current] + 1
|
||||
|
||||
if n not in g or tentative < g[n]:
|
||||
g[n] = tentative
|
||||
priority = tentative + self.heuristic(n, exit)
|
||||
heapq.heappush(heap, (priority, n))
|
||||
heapq.heappush(heap, (priority, counter, n))
|
||||
counter += 1
|
||||
parent[n] = current
|
||||
|
||||
return self.restore(parent, start, exit), len(visited)
|
||||
|
||||
return restore(parent, start, exit), len(visited)
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
from Strategies.strat import PathFindingStrategy
|
||||
from path import restore
|
||||
from Strategies.path import restore
|
||||
from collections import deque
|
||||
|
||||
class BFS(PathFindingStrategy):
|
||||
|
|
@ -14,10 +14,10 @@ class BFS(PathFindingStrategy):
|
|||
if current == exit:
|
||||
break
|
||||
|
||||
for n in maze.get_neigbors(current):
|
||||
for n in maze.getNeighbors(current):
|
||||
if n not in visited:
|
||||
visited.add(n)
|
||||
parent[n] = current
|
||||
queue.append(n)
|
||||
|
||||
return self.restore(parent, start, exit), len(visited)
|
||||
return restore(parent, start, exit), len(visited)
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
from Strategies.strat import PathFindingStrategy
|
||||
from path import restore
|
||||
from Strategies.path import restore
|
||||
|
||||
|
||||
class DFS(PathFindingStrategy):
|
||||
|
|
@ -8,16 +8,16 @@ class DFS(PathFindingStrategy):
|
|||
visited = {start}
|
||||
parent = {}
|
||||
|
||||
while start:
|
||||
while stack:
|
||||
current = stack.pop()
|
||||
|
||||
if current == exit:
|
||||
break
|
||||
|
||||
for n in maze.get_neighbors(current):
|
||||
for n in maze.getNeighbors(current):
|
||||
if n not in visited:
|
||||
visited.add(n)
|
||||
parent[n] = current
|
||||
stack.append(n)
|
||||
|
||||
return self.restore(parent, start, exit), len(visited)
|
||||
return restore(parent, start, exit), len(visited)
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
def restore(self, parent, start, exit):
|
||||
def restore(parent, start, exit):
|
||||
if exit not in parent and start != exit:
|
||||
return[]
|
||||
|
||||
|
|
|
|||
BIN
pomelovsd/ExitMaze/analysis.png
Normal file
BIN
pomelovsd/ExitMaze/analysis.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.3 KiB |
File diff suppressed because one or more lines are too long
16
pomelovsd/ExitMaze/results.csv
Normal file
16
pomelovsd/ExitMaze/results.csv
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
maze,strategy,time_ms,visited_cells
|
||||
small,BFS,0.051,8
|
||||
small,DFS,0.027,8
|
||||
small,A*,0.049,8
|
||||
medium,BFS,0.039,10
|
||||
medium,DFS,0.031,10
|
||||
medium,A*,0.042,10
|
||||
large,BFS,0.962,197
|
||||
large,DFS,0.84,197
|
||||
large,A*,1.042,197
|
||||
empty,BFS,0.01,2
|
||||
empty,DFS,0.008,2
|
||||
empty,A*,0.01,2
|
||||
no_exit,BFS,0.007,1
|
||||
no_exit,DFS,0.005,1
|
||||
no_exit,A*,0.006,1
|
||||
|
Loading…
Reference in New Issue
Block a user