예제 #1
0
    def read_from_file(self, plan_file):
        """
        Parses a plan from file into a list of grounded actions.
        Expects the file to be in correct format, one action per line:
        time: (action) [duration]
        """
        if self.domain is None or self.problem is None:
            raise Exception("Domain and problem must be set before reading plan from file.")

        with open(plan_file, 'r') as f:
            for line in f:
                # parse line, ignoring temporal information
                action = line.split(":")[1].split("[")[0].strip()

                # get the action name and parameters
                tokens = action.replace("(","").replace(")","").split()
                op = self.domain.operators[tokens[0]]
                if not op: raise Exception("Action " + action + " not found in domain.")

                objects = tokens[1:]
                if len(objects) != len(op.formula.typed_parameters):
                    raise Exception("Action " + action + " has wrong number of parameters.")

                # create grounded action
                parameters = []
                for param, object in zip(op.formula.typed_parameters, objects):
                    parameters.append(TypedParameter(param.type, param.label, object))
                op = op.bind_parameters(parameters)
                self.action_list.append(op)
예제 #2
0
파일: parser.py 프로젝트: taskplanning/otpl
 def visitTyped_var_list(self, ctx: pddl22Parser.Typed_var_listContext):
     typed_parameters = []
     parent_type = ctx.pddl_type().getText()
     for param in ctx.variable():
         typed_parameters.append(
             TypedParameter(parent_type, param.getText()))
     return typed_parameters
예제 #3
0
 def visitGround_function_expression_function(self, ctx: pddl22Parser.Ground_function_expression_functionContext):
     expr = ExprBase(expr_type=ExprBase.ExprType.FUNCTION)
     name = ctx.name()[0].getText()
     params = []
     for param in ctx.name()[1:]:
         params.append(TypedParameter("TODO parse tables", "TODO parse tables", param.getText()))
     expr.function = AtomicFormula(name, params)
     return ExprComposite([expr])
예제 #4
0
 def get_parameters_from_id(self, id : int, params : list[TypedParameter]) -> list[TypedParameter]:
     ground_parameters = []
     for param in params:
         obj_count = self.type_counts[param.type]
         param_id = id % obj_count
         value  = self.type_symbol_tables[param.type].get_symbol(param_id)
         id = id // obj_count
         ground_parameters.append(TypedParameter(param.type, param.label, value))
     return ground_parameters
예제 #5
0
    def parse_actions(self, plan_file):
        # read actions and create nodes
        with open(plan_file, 'r') as f:
            for line in f:

                # parse line, ignoring temporal information
                time = float(line.split(':')[0])
                action = line.split(":")[1].split("[")[0].strip()
                duration = float(line.split("[")[1].split("]")[0])

                # get the action name and parameters
                tokens = action.replace("(", "").replace(")", "").split()
                op = self.domain.operators[tokens[0]]
                if not op:
                    raise Exception("Action " + action +
                                    " not found in domain.")

                objects = tokens[1:]
                if len(objects) != len(op.formula.typed_parameters):
                    raise Exception("Action " + action +
                                    " has wrong number of parameters.")

                # get grounded action ID
                parameters = []
                for param, object in zip(op.formula.typed_parameters, objects):
                    parameters.append(
                        TypedParameter(param.type, param.label, object))
                formula = AtomicFormula(tokens[0], parameters)
                action_id = self.grounding.get_id_from_action_formula(formula)

                # action start happening
                node_id = len(self.happenings)
                action_start = Happening(node_id, time,
                                         HappeningType.ACTION_START, action_id)
                self.temporal_network.add_node(node_id,
                                               label=formula.print_pddl() +
                                               "_start")
                self.happenings.append(action_start)

                # action end happening
                node_id = len(self.happenings)
                action_end = Happening(node_id, time + duration,
                                       HappeningType.ACTION_END, action_id)
                self.temporal_network.add_node(node_id,
                                               label=formula.print_pddl() +
                                               "_end")
                self.happenings.append(action_end)

                # create two edges for action duration
                self.temporal_network.add_edge(node_id - 1, node_id, duration)
                self.temporal_network.add_edge(node_id, node_id - 1, -duration)
예제 #6
0
 def add_proposition_from_str(self, predicate_name : str, params : list[str] = []):
     """
     Adds a new proposition to the initial state using AtomicFormula.from_string()
     param name: the name of the proposition to add.
     param params: list of parameter values.
     """
     if predicate_name not in self.domain.predicates:
         raise Exception("Predicate {.s} does not exist.".format(predicate_name))
     if len(self.domain.predicates[predicate_name].typed_parameters) != len(params):
         raise Exception("Proposition {.s} has wrong number of parameters.".format(predicate_name))
     typed_params = []
     for param, value in zip(self.domain.predicates[predicate_name].typed_parameters, params):
         typed_params.append(TypedParameter(param.type, param.label,value))
     self.add_proposition(AtomicFormula(predicate_name, typed_params))
예제 #7
0
 def add_assignment_from_str(self, value : float, function_name : str, params : list[str] = []):
     """
     Adds a new function assignment to the initial state using AtomicFormula.from_string()
     param value: the value to be assigned.
     param name: the name of the function to add.
     param params: list of parameter values.
     """
     if function_name not in self.domain.functions:
         raise Exception("Function {.s} does not exist.".format(function_name))
     if len(self.domain.functions[function_name].typed_parameters) != len(params):
         raise Exception("Function {.s} has wrong number of parameters.".format(function_name))
     typed_params = []
     for param, value in zip(self.domain.functions[function_name].typed_parameters, params):
         typed_params.append(TypedParameter(param.type, param.label,value))
     self.add_assignment(value, AtomicFormula(function_name, typed_params))
예제 #8
0
 def add_til_from_str(self, time : float, predicate_name : str, params : list[str] = [], negative : bool = False):
     """
     Adds a new TIL to the problem using AtomicFormula.from_string()
     param time: the time at which the TIL should occur.
     param name: the name of the timed proposition.
     param params: list of parameter values.
     param negative: True if the TIL is negative.
     """
     if predicate_name not in self.domain.predicates:
         raise Exception("Predicate {.s} does not exist.".format(predicate_name))
     if len(self.domain.predicates[predicate_name].typed_parameters) != len(params):
         raise Exception("Proposition {.s} has wrong number of parameters.".format(predicate_name))
     typed_params = []
     for param, value in zip(self.domain.predicates[predicate_name].typed_parameters, params):
         typed_params.append(TypedParameter(param.type, param.label,value))
     formula = AtomicFormula(predicate_name, typed_params)
     effect = EffectNegative(formula) if negative else EffectSimple(formula)
     self.timed_initial_literals.append(TimedInitialLiteral(time, effect))
예제 #9
0
 def visitTerm_name(self, ctx: pddl22Parser.Term_nameContext):
     return TypedParameter(type="TODO term types", label="TODO term name", value=ctx.getText())
예제 #10
0
 def visitTerm_var(self, ctx: pddl22Parser.Term_varContext):
     return TypedParameter(type="TODO term types", label=ctx.getText())
예제 #11
0
 def visitUntyped_var_list(self, ctx:pddl22Parser.Untyped_var_listContext):
     typed_parameters = []
     for param in ctx.variable():
         typed_parameters.append(TypedParameter("object", param.getText()))
     return typed_parameters