Example #1
0
    def __bounded_search(self, prob, strategy, max_depth):
        """
        Method required to do the bounded search. It uses a border, a queue of Nodes that
        have to be checked.

        :param prob: Problem Object, used to know if a Node is solution, and get successors.
        :param strategy: String, Strategy to follow, to get a solution
        :param max_depth: Integer, Maximum depth that the search tree must reach.
        :return: The solution, a list of successors to get the objective Node. If the algorithm
        doesn't find a solution, it returns None.
        """
        border = Border()
        closed_list = {}   # List needed to prune the tree
        initial_node = Node(prob.initial_state(), 0, None, 0, None, 0)
        border.InsertNode(initial_node)

        #closed_list[initial_node.get_state().__str__()] = initial_node.get_value()
        sol = False
        while not sol and not border.IsEmpty():
            actual_node = border.Delete()
            if strategy == 'BFS' or strategy == 'DFS':
                closed_list[actual_node.get_state().__str__()] = actual_node.get_cost()
            else:
                closed_list[actual_node.get_state().get_unique_representation()] = actual_node.get_value()

            if prob.goal_state(actual_node.get_state()):
                actual_node.get_state().print_terrain()
                sol = True
            else:
                if actual_node.get_depth() < max_depth:
                    successors_list = prob.successors(actual_node.get_state())
                    for successor in successors_list:
                        new_node = actual_node.create_node(successor, actual_node, strategy, max_depth,prob)
                        # Prune the tree
                        if strategy == 'BFS' or strategy == 'DFS':
                            if new_node.get_state().get_unique_representation() not in closed_list:
                                border.InsertNode(new_node)
                                closed_list[new_node.get_state().get_unique_representation()] = new_node.get_cost()
                            else:
                                if closed_list[new_node.get_state().get_unique_representation()] > new_node.get_cost():
                                    closed_list[new_node.get_state().get_unique_representation()] = new_node.get_cost()
                                    border.InsertNode(new_node)
                        else:
                            if new_node.get_state().get_unique_representation() not in closed_list:
                                border.InsertNode(new_node)
                                closed_list[new_node.get_state().get_unique_representation] = new_node.get_value()
                            else:
                                if closed_list[new_node.get_state().get_unique_representation()] > new_node.get_value():
                                    closed_list[new_node.get_state().get_unique_representation()] = new_node.get_value()
                                    border.InsertNode(new_node)



        if sol:
            print('El costo de la solución es: '+ str(actual_node.get_cost()))
            print('La profundidad es: ' + str(actual_node.get_depth()))
            return self.__create_solution(actual_node)
        else:
            return None