Beispiel #1
0
    def _search(self, planning_task, n):
        """
        Searches for a plan on the given task using breadth first search and
        duplicate detection.
        @param planning_task: The planning task to solve.
        @return: The solution as a list of operators or None if the task is
        unsolvable.

        Frontier: The states that are the end of a particular search expedition.
          no successor state which is still not closed
        Closed: The states that the BFS has visited.
        """
        # counts the number of loops (only for printing)
        iteration = 0
        # fifo-queue storing the nodes which are next to explore
        queue = deque()
        root_node = searchspace.make_root_node(planning_task.initial_state)
        queue.append(root_node)
        frontier = []
        # set storing the explored nodes, used for duplicate detection
        closed = {planning_task.initial_state}
        goals = []
        open_state_exists = True
        while queue:
            iteration += 1
            logging.debug(
                "breadth_first_search: Iteration %d, #unexplored=%d" %
                (iteration, len(queue)))
            # get the next node to explore
            node = queue.popleft()
            if planning_task.goal_reached(node.state):
                logging.info("Goal reached. Start extraction of solution.")
                logging.info("%d Nodes expanded" % iteration)
                goals.append((node.state, node.extract_solution))
                queue = deque()
                queue.append(root_node)
            is_frontier = True
            for operator, successor_state in planning_task.get_successor_states(
                    node.state):
                # duplicate detection
                if successor_state not in closed:
                    is_frontier = False
                    queue.append(
                        searchspace.make_child_node(node, operator,
                                                    successor_state))
                    # remember the successor state
                    closed.add(successor_state)
            if len(goals) > n:
                return goals
            if is_frontier:
                frontier.append(node)
        logging.info("No operators left. Task unsolvable.")
        logging.info("%d Nodes expanded" % iteration)
        return None, frontier, closed
Beispiel #2
0
 def _search(self, planning_task):
     """
     Searches for a plan on the given task using breadth first search and
     duplicate detection.
     @param planning_task: The planning task to solve.
     @return: The solution as a list of operators or None if the task is
     unsolvable.
     """
     # counts the number of loops (only for printing)
     iteration = 0
     # fifo-queue storing the nodes which are next to explore
     queue = deque()
     queue.append(searchspace.make_root_node(planning_task.initial_state))
     # set storing the explored nodes, used for duplicate detection
     closed = {planning_task.initial_state}
     while queue:
         iteration += 1
         logging.debug(
             "breadth_first_search: Iteration %d, #unexplored=%d" %
             (iteration, len(queue)))
         # get the next node to explore
         node = queue.popleft()
         # exploring the node or if it is a goal node extracting the plan
         if planning_task.goal_reached(node.state):
             logging.info("Goal reached. Start extraction of solution.")
             logging.info("%d Nodes expanded" % iteration)
             return node.extract_solution()
         for operator, successor_state in planning_task.get_successor_states(
                 node.state):
             # duplicate detection
             if successor_state not in closed:
                 queue.append(
                     searchspace.make_child_node(node, operator,
                                                 successor_state))
                 # remember the successor state
                 closed.add(successor_state)
     logging.info("No operators left. Task unsolvable.")
     logging.info("%d Nodes expanded" % iteration)
     return None
Beispiel #3
0
 def _search(self, planning_task):
     """
     Note here: we use the SearchNode class which has "state" but instead, we interpret here that
     it is actually just a condition.
     """
     import time
     start = time.time()
     # counts the number of loops (only for printing)
     iteration = 0
     # fifo-queue storing the nodes which are next to explore
     queue = deque()
     root_node = searchspace.make_root_node(planning_task.goals)
     queue.append(root_node)
     frontier = []
     # set storing the explored nodes, used for duplicate detection
     closed = {planning_task.goals}
     while queue:
         iteration += 1
         logging.debug(
             "breadth_first_search: Iteration %d, #unexplored=%d" %
             (iteration, len(queue)))
         # get the next node to explore
         node = queue.popleft()
         if planning_task.start_reached(node.state):
             logging.info("Goal reached. Start extraction of solution.")
             logging.info("%d Nodes expanded" % iteration)
             return node.extract_regression(), frontier, closed
         is_frontier = True
         for operator, regressor_condition in planning_task.get_regressor_conditions(
                 node.state):
             # duplicate detection
             duplicate = False
             for match_conditions in closed:
                 if regressor_condition[0] >= match_conditions[
                         0] and regressor_condition[1] >= match_conditions[
                             1]:
                     duplicate = True
             if not duplicate:
                 is_frontier = False
                 queue.append(
                     searchspace.make_child_node(node, operator,
                                                 regressor_condition))
                 # remember the successor state
                 closed.add(regressor_condition)
         if is_frontier:
             frontier.append(node)
     minimized = set(closed)
     for sub in closed:
         supersets = [
             sup for sup in minimized
             if sub != sup and sub[0] <= sup[0] and sub[1] <= sup[1]
         ]
         for sup in supersets:
             minimized.remove(sup)
     closed = minimized
     end = time.time()
     logging.info("Elapsed time: " + str(end - start))
     print("Elapsed time: " + str(end - start))
     logging.info("No operators left. Task unsolvable.")
     logging.info("%d Nodes expanded" % iteration)
     return None, frontier, closed