Exemplo n.º 1
0
 def __init__(self, file_path, maze_number):
     self.maze = Maze(file_path, maze_number)
     self.player = None
     self.closed_list = []
     self.opened_list = []
     self.root_node = None
     self.success_way = []
     self.maze_number = maze_number
Exemplo n.º 2
0
                product_locations.append(Coordinate(x, y))
            spec = PathSpecification.read_coordinates(coordinates)
            return TSPData(product_locations, spec)
        except FileNotFoundError:
            print("Error reading file " + product_file)
            traceback.print_exc()
            sys.exit()

# Assignment 2.a
if __name__ == "__main__":
    #parameters
    gen = 1
    no_gen = 1
    q = 1000
    evap = 0.1
    persist_file = "./../tmp/productMatrixDist"
    tsp_path = "./../data/tsp products.txt"
    coordinates = "./../data/hard coordinates.txt"
        
    #construct optimization
    maze = Maze.create_maze("./../data/hard maze.txt")
    pd = TSPData.read_specification(coordinates, tsp_path)
    aco = AntColonyOptimization(maze, gen, no_gen, q, evap)
        
    #run optimization and write to file
    pd.calculate_routes(aco)
    pd.write_to_file(persist_file)
        
    #read from file and print
    pd2 = TSPData.read_from_file(persist_file)
    print(pd == pd2)
Exemplo n.º 3
0
class Search:
    def __init__(self, file_path, maze_number):
        self.maze = Maze(file_path, maze_number)
        self.player = None
        self.closed_list = []
        self.opened_list = []
        self.root_node = None
        self.success_way = []
        self.maze_number = maze_number

    def start_search(self):
        if self.maze.create_maze(
        ):  # Verifica se conseguiu ler o arquivo e criar a matriz do labirinto
            self.player = Player(self.maze.get_start(
            ))  # Posiciona o jogador no inicio do labirinto
            self.root_node = Node(self.player.get_position(), None, 0,
                                  0)  # Cria o no raiz da árvore
            self.calculate_cost(self.root_node)
            self.opened_list.append(
                self.root_node)  # Adiciona o nó raiz na lista de abertos
            success = False
            while len(self.opened_list) != 0:
                self.print_fluxograma()
                current_node = self.opened_list.pop(
                    0)  # Pega o primeiro nó da lista de abertos
                self.player.set_position(current_node.get_position())
                if not self.is_success(
                        current_node):  # Se não for sucesso, explora esse nó
                    self.explore_node(current_node)
                else:
                    success = True
                    print("------------- Final ---------------")
                    self.create_success_way(current_node)
                    print("\n O custo real eh: {}".format(
                        current_node.get_real_cost()))
                    print("\n A arvore gerada eh: ")
                    self.pprint_tree(self.root_node, "", True)
                    print("\n O caminho no labirinto sera: \n")
                    self.maze_solution()
                    break
            if not success:
                print('Não encontrou solução')
        else:
            print('Não foi possível criar o labirinto')

    def is_success(self,
                   node):  # Função que verifica se o nó é o final do labirinto
        # print('função que verifica se o nó passado como param é o nó final')
        position = node.get_position()
        if position == self.maze.get_ending():
            return True
        return False

    def explore_node(
            self,
            node):  #Função que explora o nó e adiciona na lista de abertos
        # print('função para explorar e adicionar possiveis caminhos a lista de abertos')
        moves = self.player.get_moves()
        for m in moves:
            new_position = m()
            if self.can_move_to_position(new_position) and not self.is_on_way(
                    new_position, node):
                new_node = Node(new_position, node, 0, 0)
                node.add_child(new_node)
                self.calculate_cost(new_node)
                self.add_to_opened_list(new_node)
        self.closed_list.append(node)

    def is_on_way(self, position, current_node):
        # print('Função que verifica se o nó está no caminho')
        aux_node = current_node
        while aux_node is not None:
            if aux_node.get_position() == position:
                return True
            aux_node = aux_node.get_father()
        return False

    def can_move_to_position(self, position):
        width = self.maze.get_width()
        height = self.maze.get_height()
        if (0 <= position[0] < height) and (0 <= position[1] < width):
            if self.maze.get_info_position(position) != '#':
                return True

    @abstractmethod
    def calculate_cost(self, node):
        pass

    def create_success_way(self, node):
        current_node = node
        self.success_way = []
        i = 0
        while current_node is not None:
            self.success_way.append(current_node.get_position())
            current_node = current_node.get_father()
            i += 1
        self.success_way.reverse()
        print('\n Caminho do sucesso: {}'.format(self.success_way))
        print("\n O nivel da solucao eh: {}".format(len(self.success_way) - 1))

    # Funcoes para Printar Informacoes
    def pprint_tree(self, node, _prefix, _last):
        print(_prefix,
              "`- " if _last else "|- ",
              node.get_information(),
              sep="")
        _prefix += "   " if _last else "|  "
        child_count = len(node.children)
        for i, child in enumerate(node.children):
            _last = i == (child_count - 1)
            self.pprint_tree(child, _prefix, _last)

    def print_opened_list(self):
        print("Lista de Abertos: ")
        for i in self.opened_list:
            print(" - {}({:.2f})".format(i.get_position(), i.get_total_cost()),
                  end="")
        print("\n")

    def print_closed_list(self):
        print("Lista de Fechados: ")
        for j in self.closed_list:
            print(" - {}({:.2f})".format(j.get_position(), j.get_total_cost()),
                  end="")
        print("\n")

    def print_fluxograma(self):
        print("------------ Fluxograma ---------------")
        self.print_opened_list()
        self.print_closed_list()
        print("Arvore Atual")
        self.pprint_tree(self.root_node, "", True)
        print("\n\n")

    def maze_solution(self):
        maze = self.maze.get_maze()
        for i in self.success_way:
            maze[i[0]][i[1]] = '*'
        for i in maze:
            for j in i:
                print(j, end=' ')
            print()

    def add_to_opened_list(self, node):
        got_to_end = True
        for i in range(len(self.opened_list)):
            if node.get_total_cost() < self.opened_list[i].get_total_cost():
                got_to_end = False
                self.opened_list.insert(i, node)
                break
        if got_to_end:
            self.opened_list.append(node)
Exemplo n.º 4
0
        result.reverse()
        return result

    def print_track(self):
        track = copy.deepcopy(self.track)
        s1 = track.pop(0)
        while track:
            s2 = track.pop(0)
            if s2[0] == s1[0]:
                print(f"L {s2[1]}      // Lucky to {self.maze.nodes.get(s2[1]).name}")
            else:
                print(f"R {s2[0]}      // Rocket to {self.maze.nodes.get(s2[0]).name}")
            s1 = s2

    def animate_route(self):
        g = self.maze.get_graph()
        counter = 1
        for i in self.track:
            self.maze.draw_graph(g, nx.kamada_kawai_layout(g), i, counter)
            counter += 1
            time.sleep(1)


if __name__ == "__main__":
    model = Maze()          # Creating our model
    model.get_data()        # Initiating out model
    maze_solver = MazeSolver(model)
    maze_solver.solve()     # Solve our model