Esempio n. 1
0
    def initialize_model(self, task, model_dir, model_name):

        domain_filepath = pathlib.Path(task.domain_file)
        problem_filepath = pathlib.Path(task.problem_file)

        self._problem = Problem(domain_filepath.name,
                                problem_filepath.name,
                                problem_filepath.parent)

        from generalized_learning.search.heuristic.nn_plact import NNPLACT
        self._nnplact = NNPLACT(self._problem, model_dir, model_name)
Esempio n. 2
0
    def _get_training_data(self, domain_filepath, problem_file_list,
                           solver_name, abstract_domain):

        if abstract_domain is None:

            abstract_domain = AbstractDomain()

        nn_pkgs_list = []
        for problem_filepath in problem_file_list:

            solution_filepath = "%s.%s.%s" % (problem_filepath,
                                              solver_name,
                                              constants.SOLUTION_FILE_EXT)

            problem = Problem(domain_filepath.name, problem_filepath.name,
                              problem_filepath.parent)
            if not problem._is_relaxed_reachable:

                continue

            try:
                solution = Solution.parse(solution_filepath)
            except Exception:

                continue

            current_state = problem.get_initial_state()

            action_list = solution.get_action_list()
            plan_length = len(action_list)
            for i in range(plan_length):

                abstract_state = AbstractState(problem, current_state)

                action_name = action_list[i]
                action = problem.get_action(action_name)
                nn_pkg = abstract_domain.encode_nn_training_data(
                    abstract_state,
                    action,
                    plan_length - i)

                nn_pkgs_list.append(nn_pkg)

                assert action.is_applicable(current_state)
                current_state = action.apply(current_state)

        return [(abstract_domain, nn_pkgs_list)]
Esempio n. 3
0
    def search(self, domain_filepath, problem_filepath):

        self._reset()

        assert domain_filepath.parent == problem_filepath.parent
        problem = Problem(domain_filepath.name, problem_filepath.name,
                          problem_filepath.parent)

        heuristic = InformedSearch.get_heuristic(self._search_param_dict,
                                                 problem, self._parent_dir)

        initial_state = problem.get_initial_state()
        root_node = Node(initial_state, None, None, 0, 0)

        fringe_list = []
        self._push_to_fringe(fringe_list, root_node)

        progress_bar = tqdm.tqdm(unit=" nodes")
        progress_bar.update(1)
        goal_node = None
        while len(fringe_list) > 0:

            node = self._pop_from_fringe(fringe_list)
            action = node.get_action()
            print(action)
            concrete_state = node.get_concrete_state()

            if problem.is_goal_satisfied(concrete_state):

                goal_node = node
                break
            else:

                self._total_nodes_expanded += 1

                new_children = heuristic.expand(node)
                for child in new_children:

                    self._push_to_fringe(fringe_list, child)
                    progress_bar.update(1)

        progress_bar.close()
        return self._create_solution(problem, goal_node)
Esempio n. 4
0
class NNPlactHeuristic(Heuristic):
    """
    Implements a simple blind heuristic for convenience.
    It returns 0 if the goal was reached and 1 otherwise.
    """

    def __init__(self, task):
        super().__init__()
        self.goals = task.goals
        self._nnplact = True

    def initialize_model(self, task, model_dir, model_name):

        domain_filepath = pathlib.Path(task.domain_file)
        problem_filepath = pathlib.Path(task.problem_file)

        self._problem = Problem(domain_filepath.name,
                                problem_filepath.name,
                                problem_filepath.parent)

        from generalized_learning.search.heuristic.nn_plact import NNPLACT
        self._nnplact = NNPLACT(self._problem, model_dir, model_name)

    def encode_pyperplan_initial_state(self):

        return self._problem.encode_pyperplan_initial_state()

    def update_candidates(self, current_node, candidate_list):

        from generalized_learning.search.node import Node

        parent_state = self._problem.encode_pyperplan_state(current_node.state)
        parent_node = Node(parent_state, None, None, 0)
        parent_node.artificial_g = current_node.artificial_g

        candidates = []
        for candidate in candidate_list:

            candidate_state = self._problem.encode_pyperplan_state(
                candidate.state)
            candidate_node = Node(
                candidate_state,
                None,
                self._problem.get_action(candidate.action.name),
                0)

            candidates.append(candidate_node)

        self._nnplact.update_candidates(parent_node, candidates)

        for i in range(len(candidates)):

            candidate_list[i].artificial_g = candidates[i].artificial_g
            candidate_list[i].h = candidates[i].h + candidates[i].artificial_g

    def __call__(self, node):

        concretized_state = self._problem.encode_pyperplan_state(node.state)
        h = self._nnplact.compute_h(None, concretized_state, None)
        if node.parent is None:

            node.action_score = 0
        else:

            parent_state = self._problem.encode_pyperplan_state(
                node.parent.state)
            action_score = self._nnplact.compute_d(
                parent_state,
                None,
                self._problem.get_action(node.action.name))

            node.action_score = node.parent.action_score + action_score

        # Store the confidence value as the artificial g.
        # We will use to compute the f-score rather than the real_g.
        # The real_g is still used to determine if a node must be added to
        # the open list in A*.
        node.artificial_g = node.action_score
        node.h = h

        return h + node.artificial_g
Esempio n. 5
0
    def search(self, domain_filepath, problem_filepath):

        start_time = time.time()
        end_time = start_time + self._time_limit_in_sec

        self._reset()

        assert domain_filepath.parent == problem_filepath.parent
        problem = Problem(domain_filepath.name, problem_filepath.name,
                          problem_filepath.parent)

        heuristic = AStar.get_heuristic(self._search_param_dict, problem,
                                        self._parent_dir)

        initial_state = problem.get_initial_state()
        root_node = Node(initial_state, None, None, 0, 0)
        root_node.artificial_g = 0.0
        root_node._h = 0

        state_node_dict = {}
        fringe = PriorityQ()

        fringe.push(initial_state, 0.0)
        state_node_dict[initial_state] = root_node

        #         progress_bar = tqdm.tqdm(unit=" nodes")
        goal_node = None

        true_g = {}
        true_g[initial_state] = 0

        while not fringe.is_empty() \
                and time.time() < end_time \
                and self._total_nodes_expanded < self._nodes_expanded_limit:

            #             progress_bar.update(1)
            current_state = fringe.pop()
            current_node = state_node_dict[current_state]
            del state_node_dict[current_state]

            action = current_node.get_action()
            #             print("picked:", action)

            if problem.is_goal_satisfied(current_state):

                goal_node = current_node
                break
            else:

                self._total_nodes_expanded += 1

                applicable_actions = problem.get_applicable_actions(
                    current_state)

                candidates = []

                #                 print("***************************")
                for action in applicable_actions:

                    next_state = action.apply(current_state)

                    assert action.get_cost() == 1

                    # g_scores for the current state must always be available.
                    assert current_state in true_g
                    temp_g_score = true_g[current_state] + action.get_cost()

                    if temp_g_score < true_g.get(next_state, float("inf")):

                        # Better path found, update priority in fringe
                        # (invalidate the old value) and also update the
                        # node reference for this state in the open set.
                        h = heuristic.compute_h(current_state, next_state,
                                                action)

                        #                         print("a:", action, "g:", temp_g_score,
                        #                               "h:", h, "f:", f_score)

                        child_node = Node(next_state, current_node, action,
                                          temp_g_score, 0)
                        state_node_dict[next_state] = child_node
                        true_g[next_state] = temp_g_score

                        candidates.append(child_node)

                if len(candidates) > 0:

                    heuristic.update_candidates(current_node, candidates)

                for child_node in candidates:
                    fringe.push(child_node.get_concrete_state(),
                                (child_node.get_fscore(), child_node.get_h()))

#         progress_bar.close()
        solution = self._create_solution(problem, goal_node, start_time,
                                         heuristic)

        solution.write(self.get_solution_filepath(problem_filepath))
        return []
Esempio n. 6
0
    def search(self, domain_filepath, problem_filepath):

        temp_logfile_handle = tempfile.NamedTemporaryFile("w+", delete=False)

        ff_cmd = "%s -o %s -f %s" % (self._FF_EXECUTABLE, domain_filepath,
                                     problem_filepath)

        logger.debug("Running ff_cmd=%s" % (ff_cmd))
        ff_cmd = ff_cmd.split(" ")

        start_time = time.time()

        timeout = False
        failed = False

        try:

            completed_process = subprocess.Popen(ff_cmd,
                                                 stdout=temp_logfile_handle)

            completed_process.wait(timeout=self._time_limit_in_sec)

            # If we did not timeout, then the command must have run
            # successfully.
            #
            # Currently, we do not support internal errors.
            if completed_process.returncode != 0:

                failed = True
        except subprocess.TimeoutExpired:

            self._terminate(completed_process)
            self._kill(completed_process)
            timeout = True
            pass

        total_time = time.time() - start_time
        total_time = min(total_time, self._time_limit_in_sec)

        # Get the problem parsed out.
        assert domain_filepath.parent == problem_filepath.parent
        problem = Problem(domain_filepath.name, problem_filepath.name,
                          problem_filepath.parent)

        # Parse the solution.
        solution = self._parse_log(temp_logfile_handle, problem, timeout,
                                   failed)

        solution.set_solution_property("time_taken", "%.2f" % (total_time))

        # Set the algorithm properties.
        algorithm_properties = {
            "time_limit": self.get_time_limit_in_sec(),
            "nodes_expanded_limit": self.get_nodes_expanded_limit(),
        }

        for key in algorithm_properties:

            solution.set_algorithm_property(key, algorithm_properties[key])

        # Finally, check if we need to save the logfiile.
        temp_logfile_handle.close()
        if self._save_logs:

            log_filename = "%s.%s.%s" % (problem_filepath.name, self._name,
                                         constants.LOG_FILE_EXT)
            log_filepath = pathlib.Path(problem_filepath.parent, log_filename)
            shutil.move(temp_logfile_handle.name, log_filepath)
        else:

            try:

                os.remove(temp_logfile_handle.name)
            except FileNotFoundError:

                pass

        try:

            # ff also creates a <problem>.soln file.
            # delete that file.
            #
            # TODO: What if a solution could not be found and consequently this
            #     file does not exist?
            os.remove("%s/%s.%s" %
                      (problem_filepath.parent, problem_filepath.name,
                       constants.SOLUTION_FILE_EXT))
        except FileNotFoundError:

            pass

        # Save the solution to a compliant file.
        solution_filename = "%s.%s.%s" % (problem_filepath.name, self._name,
                                          constants.SOLUTION_FILE_EXT)
        solution_filepath = pathlib.Path(problem_filepath.parent,
                                         solution_filename)

        solution.set_name(self._name)
        solution.write(solution_filepath)

        # Return nothing since the solution has already been written to the
        # disk.
        #
        # Returning nothing ensures that the phase system does not rely upon
        # results from this phase.
        return []
Esempio n. 7
0
    def _search(self, domain_filepath, problem_filepath):

        pyperplan_cmd = self._get_pyperplan_cmd(domain_filepath,
                                                problem_filepath)

        temp_logfile_handle = tempfile.NamedTemporaryFile("w+", delete=False)

        logger.debug("Running pyperplan_cmd=%s" % (pyperplan_cmd))

        pyperplan_cmd = pyperplan_cmd.split(" ")

        start_time = time.time()
        timeout = False
        failed = False

        try:

            completed_process = subprocess.Popen(pyperplan_cmd,
                                                 stdout=temp_logfile_handle)

            completed_process.wait(timeout=self._time_limit_in_sec)

        except subprocess.TimeoutExpired:

            self._terminate(completed_process)
            self._kill(completed_process)
            timeout = True
            failed = True
            pass

        total_time = time.time() - start_time
        total_time = min(total_time, self._time_limit_in_sec)

        # Get the problem parsed out.
        assert domain_filepath.parent == problem_filepath.parent
        problem = Problem(domain_filepath.name, problem_filepath.name,
                          problem_filepath.parent)

        # Parse the solution.
        solution = self._parse_log(temp_logfile_handle, problem, self._mode,
                                   timeout, failed)

        solution.set_solution_property("time_taken", "%.2f" % (total_time))

        # Set the algorithm properties.
        algorithm_properties = {
            "time_limit": self.get_time_limit_in_sec(),
            "nodes_expanded_limit": self.get_nodes_expanded_limit(),
        }

        for key in algorithm_properties:

            solution.set_algorithm_property(key, algorithm_properties[key])

        # Finally, check if we need to save the logfiile.
        temp_logfile_handle.close()
        if self._save_logs:

            log_filename = "%s.%s.%s.%s" % (problem_filepath.name, self._mode,
                                            self._name, constants.LOG_FILE_EXT)
            log_filepath = pathlib.Path(problem_filepath.parent, log_filename)
            shutil.move(temp_logfile_handle.name, log_filepath)
        else:

            os.remove(temp_logfile_handle.name)

        # Save the solution to a compliant file.
        solution_filename = "%s.%s.%s" % (problem_filepath.name, self._name,
                                          constants.SOLUTION_FILE_EXT)
        solution_filepath = pathlib.Path(problem_filepath.parent,
                                         solution_filename)

        solution.set_name(self._name)
        solution.write(solution_filepath)

        # Return nothing since the solution has already been written to the
        # disk.
        #
        # Returning nothing ensures that the phase system does not rely upon
        # results from this phase.
        return []