コード例 #1
0
def main():
    raw_board = gen_board(int(argv[1]), int(argv[2]))
    timestamp = str(int(t()))

    for s, name in zip(search_algs, names):
        print(f'Usando algoritmo {name}...')
        board = deepcopy(raw_board)

        fig = figure()
        camera = Camera(fig)
        search_res = test_search(board, s.search, camera=camera)

        path = search_res['path']
        if path:
            for i, j in path[1:-1]:
                board[i][j] = 1

        # Show path for longer time
        for i in range(10):
            plot_board(board)
            camera.snap()

        animation = camera.animate()
        outname = f'{timestamp}_{name}_{argv[1]}x{argv[2]}.gif'
        animation.save('gifs/' + outname, writer='imagemagick')
        print(search_res)
コード例 #2
0
def search(board: list,
           origin: tuple,
           target: tuple,
           camera: Camera = None) -> list:
    """ Executa o algoritmo A* no tabuleiro da origem ao destino """
    def calc_path(parents: dict) -> list:
        """ Retorna o caminho desde destino até a origem """
        path = [target]
        while path[-1] != origin:
            path.append(parents[path[-1]])
        return path

    ######## inicializações ###########
    open_list = {origin: 0}
    closed_list = set()
    parents = {}
    calc_g.values = {origin: 0}
    u.trapezoidal_dist.values = {}
    ###################################

    while open_list:
        pos = min(open_list, key=lambda p: open_list[p])
        del (open_list[pos])
        if pos == target:
            return calc_path(parents)
        if pos != origin:
            # marca como visitado
            board[pos[0]][pos[1]] = .4
        for move in u.available_moves(board, pos):
            if move not in closed_list:
                if move != target:
                    # marca como tocado
                    board[move[0]][move[1]] = .2
                if move in open_list:
                    new_g = calc_g(pos, move)
                    if calc_g.values[move] > new_g:
                        calc_g.values[move] = new_g
                    h = u.trapezoidal_dist.values[move]
                    new_f = new_g + h
                    old_f = open_list[move]
                    if new_f < old_f:
                        open_list[move] = new_f
                        parents[move] = pos
                else:
                    g = calc_g(pos, move)
                    calc_g.values[move] = g
                    h = u.trapezoidal_dist(move, target)
                    f = g + h
                    open_list[move] = f
                    parents[move] = pos
        closed_list.add(pos)

        if camera is not None:
            plot_board(board)
            camera.snap()

    return None
コード例 #3
0
def search(board: list, origin: tuple,
           target: tuple, camera: Camera = None) -> list:
    """ Executa o algoritmo A* no tabuleiro da origem ao destino """
    def calc_path(parents: dict) -> list:
        """ Retorna o caminho desde destino até a origem """
        path = [target]
        while path[-1] != origin:
            path.append(parents[path[-1]])
        return path

    ######## inicializações ###########
    open_list = []
    heappush(open_list, [0, origin])
    closed_list = set()
    parents = {}
    calc_g.values = {origin: 0}
    u.trapezoidal_dist.values = {}
    ###################################

    while open_list:
        pos = heappop(open_list)[1]
        if pos == target:
            return calc_path(parents)
        if pos != origin:
            # marca como visitado
            board[pos[0]][pos[1]] = .4
        for move in u.available_moves(board, pos):
            if move not in closed_list:
                if move != target:
                    # marca como visitado
                    board[move[0]][move[1]] = .2
                f = calc_f(pos, move, target)
                try:
                    # i = posição de move em open_list
                    i = next(i for (i,p) in enumerate(open_list) if p[1] == move)
                    old_f = open_list[i][0]
                    if f < old_f:
                        open_list[i][0] = f
                        heapify(open_list)
                        parents[move] = pos
                except StopIteration:
                    # move não está em open_list
                    heappush(open_list, [f, move])
                    parents[move] = pos
        closed_list.add(pos)

        if camera is not None:
            plot_board(board)
            camera.snap()

    return None
コード例 #4
0
def search(board: list,
           origin: tuple,
           target: tuple,
           camera: Camera = None) -> list:
    def calc_path(parents: dict) -> list:
        """ Retorna o caminho desde a origem até o destino """
        path = [target]
        while path[-1] != origin:
            path.append(parents[path[-1]])
        return path

    visited = deque()
    visited.append(origin)
    parents = {}
    processed = set()
    processed.add(origin)

    while len(visited) != 0:
        pos = visited.popleft()
        if pos == target:
            return calc_path(parents)
        if pos != origin:
            # marca como visitado
            board[pos[0]][pos[1]] = .4

        # Invertido para ir nas diagonais por último.
        for move in u.available_moves(board, pos)[::-1]:
            if move not in processed:
                if move != target:
                    # marca como tocado
                    board[move[0]][move[1]] = .2
                visited.appendleft(move)
                processed.add(move)
                parents[move] = pos

        if camera is not None:
            plot_board(board)
            camera.snap()

    return None
コード例 #5
0
def search(board: list,
           origin: tuple,
           target: tuple,
           camera: Camera = None) -> list:
    queue = [(0, [origin])]
    u.trapezoidal_dist.values = {origin: 0}
    processed = {origin}
    path = [None]

    while path[-1] != target:
        if not queue:
            path = None
            break

        path = heappop(queue)[1]
        curr = path[-1]
        if curr not in (origin, target):
            # marca como visitado
            board[curr[0]][curr[1]] = .4

        # Append moves
        for move in u.available_moves(board, path[-1]):
            if move not in processed:
                heappush(queue,
                         (u.trapezoidal_dist(move, target), path + [move]))
                if move != target:
                    # marca como visitado
                    board[move[0]][move[1]] = .2
                processed.add(move)

        if camera is not None:
            plot_board(board)
            camera.snap()

    board[origin[0]][origin[1]] = u.str2n['#']
    return path