def store_solution(grid: ColoredGrid) -> ColoredGrid: start_row, start_column, end_row, end_column = LongestPath.calculate(grid) solved_grid = cast( ColoredGrid, Dijkstra.calculate_distances(grid, start_row, start_column, end_row, end_column)) return solved_grid
def test_basic_pathfinding(algorithm: Type[Algorithm]) -> None: for size in TEST_SIZES: grid = DistanceGrid(*size) algorithm().on(grid) try: start, end = LongestPath.calculate(grid) Dijkstra.calculate_distances(grid, start, end) except Exception as ex: raise ex
def is_valid(grid: ColoredGrid) -> bool: ''' Wolf3D exit wall cell is a switch that only gets rendered east and west ''' _, end = LongestPath.calculate(grid) cell = grid[end[0], end[1]] if cell is None: raise ValueError( 'Ending row not found at row {} column {}'.format(*end)) linked_neighbor = cell.links[0] # assume exactly one path to the exit return (cell.east is not None and cell.east == linked_neighbor) or \ (cell.west is not None and cell.west == linked_neighbor)
def is_valid(grid: ColoredGrid) -> bool: """ Wolf3D exit wall cell is a switch that only gets rendered east and west """ _, _, end_row, end_column = LongestPath.calculate(grid) cell = grid.cell_at(end_row, end_column) if cell is None: raise ValueError("Ending row not found at row {} column {}".format( end_row, end_column)) linked_neighbor = cell.links[0] # assume exactly one path to the exit return (cell.east is not None and cell.east == linked_neighbor) or \ (cell.west is not None and cell.west == linked_neighbor)
help='whether to find the path through the maze') args = parser.parse_args() algorithm = avalible_algorithm(args.algorithm, AVAILABLE_ALGORITHMS) exporter = avalible_exporter(args.exporter, AVAILABLE_EXPORTERS) rotations = args.rotations pathfinding = args.pathfinding rows = args.rows cols = args.cols print('Algorithm: {}\nRows: {}\ncolumns: {}\nExporter: {}'.format( args.algorithm, rows, cols, args.exporter)) print('90deg Rotations: {}\nPathfinding: {}'.format( rotations, pathfinding)) grid = DistanceGrid(rows, cols) algorithm.on(grid) for num in range(rotations): grid = cast(DistanceGrid, Rotator().on(grid)) if pathfinding: start, end = LongestPath.calculate(grid) print('Solving maze from row {} column {} to row {} column {}'.format( *start, *end)) Dijkstra.calculate_distances(grid, start, end) exporter.render(grid) print('Maze has {} dead-ends'.format(len(grid.deadends)))
print("Pathfinding flag shows distances between cells") exit(1) exporter, exporter_name = get_exporter(AVAILABLE_EXPORTERS, DEFAULT_EXPORTER) rotations = get_rotations() pathfinding = get_pathfinding() rows = int(args.all[0]) columns = int(args.all[1]) algorithm = get_algorithm() print("Algorithm: {}\nRows: {}\ncolumns: {}\nExporter: {}".format(algorithm.__name__, rows, columns, exporter_name)) print("90deg Rotations: {}\nPathfinding: {}".format(rotations, pathfinding)) if pathfinding: grid = DistanceGrid(rows, columns) # type: Union[Grid, DistanceGrid] else: grid = Grid(rows, columns) grid = algorithm.on(grid) for num in range(rotations): grid = Rotator.on(grid) if pathfinding: start_row, start_column, end_row, end_column = LongestPath.calculate(cast(DistanceGrid, grid)) print("Solving maze from row {} column {} to row {} column {}".format( start_row, start_column, end_row, end_column)) grid = Dijkstra.calculate_distances(cast(DistanceGrid, grid), start_row, start_column, end_row, end_column) exporter.render(grid) print("Maze has {} dead-ends".format(len(grid.deadends)))
for _ in range(tries): if pathfinding: grid: Union[Grid, DistanceGrid] = DistanceGrid(rows, columns) else: grid = Grid(rows, columns) time_start = time.perf_counter() algorithm().on(grid) time_end = time.perf_counter() deadend_counts.append(len(grid.deadends)) timings.append(time_end - time_start) if pathfinding: time_start = time.perf_counter() start, end = LongestPath.calculate(cast(DistanceGrid, grid)) Dijkstra.calculate_distances(cast(DistanceGrid, grid), start, end) time_end = time.perf_counter() pathfinding_timings.append(time_end - time_start) total_deadends = sum(deadend_counts) algorithm_averages[algorithm] = total_deadends / len(deadend_counts) timings = sorted(timings) algorithm_benchmarks[algorithm] = { "min": timings[0], "max": timings[-1], "average": sum(timings) / tries } if pathfinding: pathfinding_timings = sorted(pathfinding_timings)
def store_solution(grid: ColoredGrid) -> ColoredGrid: start, end = LongestPath.calculate(grid) Dijkstra.calculate_distances(grid, start, end) return grid