Example #1
0
def bfs(draw, start, end, grid):
    came_from = {start: 0}
    q = deque()
    q.append(start)

    while q:
        for event in pg.event.get():  # can quit while algorithm is running
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()

        curr = q.popleft()

        if curr == end:
            check_reset(grid)
            draw_path(end, start, came_from, draw)
            return True

        for neighbor in curr.neighbors:
            if not neighbor.is_visited() and not neighbor.is_barrier() and not neighbor.is_check():
                if neighbor != start and neighbor != end:
                    neighbor.make_check()
                came_from[neighbor] = curr

                q.append(neighbor)

        draw()
        if curr != start:
            curr.make_visited()
    return False
def init():
    initial_grid = gh.generate_grid_weighted()
    start, end = (4, 0), (4, 6)
    came_from = find_path_dijkstra(initial_grid, start, end)
    path = gh.find_path(start, end, came_from)
    gh.draw_path(path, initial_grid)

    pprint(initial_grid)
def init():
    initial_grid = gh.generate_grid_empty()
    start, end = (0, 0), (8, 8)
    came_from = find_path_greedy(initial_grid, start, end)
    path = gh.find_path(start, end, came_from)
    gh.draw_path(path, initial_grid)

    pprint(initial_grid)
def init():
    initial_grid = gh.generate_grid_obstacle_for_b_star()
    start, end = (4, 0), (4, 8)
    came_from = find_path_a_star(initial_grid, start, end)
    path = gh.find_path(start, end, came_from)
    gh.draw_path(path, initial_grid)

    pprint(initial_grid)
Example #5
0
def init():
    initial_grid = gh.generate_grid_obstacle()
    start_pos = (3, 0)
    directions = scan_grid(initial_grid, start_pos)

    path1 = gh.find_path(start_pos, (8, 8), directions)
    # need copy as we modify the grid
    grid_with_path1 = gh.draw_path(path1, copy.deepcopy(initial_grid))
    pprint(grid_with_path1)
    print(f"steps: {len(path1)}")

    path2 = gh.find_path(start_pos, (4, 7), directions)
    grid_with_path2 = gh.draw_path(path2, copy.deepcopy(initial_grid))
    pprint(grid_with_path2)
    print(f"steps: {len(path2)}")
Example #6
0
def a_star(draw, start, end, grid):
    count = 0
    came_from = {start: 0}
    open_set = PriorityQueue()
    open_set_members = {start}
    g_score = {node: float('inf') for row in grid for node in row}
    f_score = {node: float('inf') for row in grid for node in row}
    g_score[start] = 0
    f_score[start] = g_score[start] + h(start.get_pos(), end.get_pos())
    open_set.put((f_score[start], count, start))

    while not open_set.empty():
        for event in pg.event.get():  # can quit while algorithm is running
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()

        curr = open_set.get()[2]
        open_set_members.remove(curr)

        if curr == end:
            check_reset(grid)
            draw_path(end, start, came_from, draw)
            return True

        for neighbor in curr.neighbors:
            if not neighbor.is_barrier():
                tmp_g_score = g_score[curr] + 1

                if tmp_g_score < g_score[neighbor]:
                    came_from[neighbor] = curr
                    g_score[neighbor] = tmp_g_score
                    f_score[neighbor] = g_score[neighbor] + h(
                        neighbor.get_pos(), end.get_pos())

                    if neighbor not in open_set_members:
                        count += 1
                        open_set.put((f_score[neighbor], count, neighbor))
                        open_set_members.add(neighbor)
                        if neighbor != start and neighbor != end:
                            neighbor.make_check()

        draw()
        if curr != start:
            curr.make_visited()
    return False
Example #7
0
def best_first_search(draw, start, end, grid):
    count = 0
    came_from = {start: 0}
    pq = PriorityQueue()
    score = {
        node: h(node.get_pos(), end.get_pos())
        for row in grid for node in row
    }
    pq.put((score[start], count, start))

    while not pq.empty():
        for event in pg.event.get():  # can quit while algorithm is running
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()

        curr = pq.get()[2]

        if curr == end:
            check_reset(grid)
            draw_path(end, start, came_from, draw)
            return True

        for neighbor in curr.neighbors:
            if not neighbor.is_visited() and not neighbor.is_barrier(
            ) and not neighbor.is_check():
                if neighbor != start and neighbor != end:
                    count += 1
                    neighbor.make_check()
                came_from[neighbor] = curr
                pq.put((score[neighbor], count, neighbor))

        draw()
        if curr != start:
            curr.make_visited()
    return False