Пример #1
0
def read_problem_from_file(filename):

    # Auxiliary function to parse a string of the form (prefix + "rest")
    # If string is of this form, it returns True,"rest"
    # Otherwise, it returns False,""
    def remove_prefix_if_possible(line, prefix):
        if line[0:len(prefix)] == prefix:
            return True,line[len(prefix):];
        else:
            return False,"";

    try:
        file = open(filename, "r");
        # Initialize
        initial_str = "";
        goals_str = "";
        t_max_str = "20"; # default value is 20
        actions_list = [];
        # Read the file line by line
        for line in file.readlines():
            stripped_line = line.strip();
            if stripped_line != "" and stripped_line[0] != '#':
                # Lines specifying initial state
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"initial: ");
                if match:
                    initial_str = rest_of_line.strip();
                    if expr(initial_str) == True or expr(initial_str) == None:
                        initial_str = "[]";
                # Lines specifying goals
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"goals: ");
                if match:
                    goals_str = rest_of_line.strip();
                    if expr(goals_str) == True or expr(goals_str) == None:
                        goals_str = "[]";
                # Lines specifying t_max
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"t_max: ");
                if match:
                    t_max_str = rest_of_line.strip();
                # Lines specifying an action
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"action: ");
                if match:
                    action_strs = rest_of_line.split(";");
                    action = Action(action_strs[0], precond=action_strs[1], effect=action_strs[2]);
                    if expr(action.precond) == None or expr(action.precond) == True:
                        action.precond = [];
                    if expr(action.effect) == None or expr(action.effect) == True:
                        action.effect = [];
                    actions_list.append(action);
        # Create planning_problem and t_max from the data stored after reading, and return them
        planning_problem = PlanningProblem(initial=initial_str, goals=goals_str, actions=actions_list);
        t_max = int(t_max_str);
        return planning_problem, t_max;

    # If exception occurs, print error message and return None,None
    except Exception as e:
        print("Something went wrong while reading from " + filename + " (" + str(e) + ")");
        return None, None;
Пример #2
0
def read_problem_from_file(filename):
    """
    Reads a planning problem (together with an upper bound t_max on the length
    of plans for this problem) from a file.

    Parameters:
        filename (str): Name of the file that is to be read from.

    Returns:
        ((PlanningProblem,int)): Pair with the planning problem and the
        bound t_max that are read from the file.
    """

    # Auxiliary function to parse a string of the form (prefix + "rest")
    # If string is of this form, it returns True,"rest"
    # Otherwise, it returns False,""
    def remove_prefix_if_possible(line, prefix):
        if line[0:len(prefix)] == prefix:
            return True,line[len(prefix):]
        else:
            return False,""

    try:
        file = open(filename, "r")
        # Initialize
        initial_str = ""
        goals_str = ""
        t_max_str = "20" # default value is 20
        actions_list = []
        # Read the file line by line
        for line in file.readlines():
            stripped_line = line.strip()
            if stripped_line != "" and stripped_line[0] != '#':
                # Lines specifying initial state
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"initial: ")
                if match:
                    initial_str = rest_of_line.strip()
                    if expr(initial_str) == True or expr(initial_str) == None:
                        initial_str = "[]"
                # Lines specifying goals
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"goals: ")
                if match:
                    goals_str = rest_of_line.strip()
                    if expr(goals_str) == True or expr(goals_str) == None:
                        goals_str = "[]"
                # Lines specifying t_max
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"t_max: ")
                if match:
                    t_max_str = rest_of_line.strip()
                # Lines specifying an action
                match,rest_of_line = remove_prefix_if_possible(stripped_line,"action: ")
                if match:
                    action_strs = rest_of_line.split(";")
                    action = Action(action_strs[0], precond=action_strs[1], effect=action_strs[2])
                    if expr(action.precond) == None or expr(action.precond) == True:
                        action.precond = []
                    if expr(action.effect) == None or expr(action.effect) == True:
                        action.effect = []
                    actions_list.append(action)
        # Create planning_problem and t_max from the data stored after reading, and return them
        planning_problem = PlanningProblem(initial=initial_str, goals=goals_str, actions=actions_list)
        t_max = int(t_max_str)
        return planning_problem, t_max

    # If exception occurs, print error message and return None,None
    except Exception as e:
        print("Something went wrong while reading from " + filename + " (" + str(e) + ")")
        return None, None