Exemple #1
0
def calculate(grid: DistanceGrid) -> Tuple[int, int, int, int]:
    start_cell = grid.cell_at(0, 0)
    if start_cell is None:
        raise IndexError("Invalid start cell row {} column {}".format(0, 0))

    new_start_cell, distance = start_cell.distances.max
    goal_cell, distance = new_start_cell.distances.max

    return new_start_cell.row, new_start_cell.column, goal_cell.row, goal_cell.column
Exemple #2
0
def calculate_distances(grid: DistanceGrid, start: Point, end: Point) -> None:
    """
    Calculate the distances in the grid using Dijkstra's algorithm
    """
    if grid[start] is None:
        raise IndexError("Invalid start cell {} column {}".format(*start))
    if grid[end] is None:
        raise IndexError("Invalid destination cell row {} column {}".format(*end))

    grid.distances = grid[start].distances.path_to(grid[end])  # type: ignore
def calculate_distances(grid: DistanceGrid, start: Point, end: Point) -> None:
    ''' Calculate the distances in the grid using Dijkstra's algorithm '''

    start_cell = grid[start]
    if start_cell is None:
        raise IndexError('Invalid start cell row {} column {}'.format(*start))

    end_cell = grid[end]
    if end_cell is None:
        raise IndexError(
            'Invalid destination cell row {} column {}'.format(*end))

    grid.distances = start_cell.distances.pathTo(end_cell)
        print("Valid algorithms: {}".format("|".join([algorithm.__name__ for algorithm in ALGORITHMS])))
        print("Valid exporters: {}".format("|".join(AVAILABLE_EXPORTERS)))
        print("Rotations is an integer value measuring number of 90 degree clockwise rotations to perform")
        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)
Exemple #5
0
                        default=False,
                        help="whether solve the maze")
    args = parser.parse_args()

    rows = args.rows
    columns = args.columns
    algorithm = get_algorithm(args.algorithm, AVAILABLE_ALGORITHMS)
    exporter = get_exporter(args.exporter, AVAILABLE_EXPORTERS)
    rotations = args.rotations
    pathfinding = args.pathfinding
    print("Algorithm: {}\nRows: {}\ncolumns: {}\nExporter: {}".format(
        args.algorithm, rows, columns, args.exporter))
    print("90deg Rotations: {}\nPathfinding: {}".format(
        rotations, pathfinding))

    grid = DistanceGrid(rows, columns)
    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)))
Exemple #6
0
    algorithm_averages = {}
    algorithm_benchmarks = {}
    pathfinding_benchmarks = {}

    print("Rows: {}\ncolumns: {}\nTotal cells: {}\nRuns per algorithm: {}".
          format(rows, columns, size, tries))
    print("Pathfinding: {}".format(pathfinding))
    for algorithm in ALGORITHMS:
        print("> running {}".format(algorithm.__name__))

        pathfinding_timings = []
        timings = []
        deadend_counts = []
        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)