def _parse_domain(self, f_domain): """ Extract information from the domain file. The following will be extracted: * types * predicates * actions """ parse_tree = PDDL_Tree.create(f_domain) assert "domain" in parse_tree, "Domain must have a name" self.domain_name = parse_tree ["domain"].named_children ()[0] # must read types before constants if ":types" in parse_tree: if "-" in parse_tree[":types"].named_children(): type_hierarchy = PDDL_Utils.read_type(parse_tree[":types"]) self.parent_types = {subtype: parent for subtype, parent in type_hierarchy} self.types = set(parse_tree[":types"].named_children()) self.types.discard("-") else: self.types = set(parse_tree[":types"].named_children()) self.parent_types = {t: None for t in self.types} else: self.types = set([Predicate.OBJECT]) self.parent_types = {Predicate.OBJECT: None} # must read in constants before actions or predicates if ":constants" in parse_tree: object_list = PDDL_Utils.read_type(parse_tree[":constants"]) self._add_objects(object_list) #TODO this may not be correct, depending on the type hierchy const_map = {const: list(self.obj_to_type[const])[0] for const in self.objects} self.predicates = [self.to_predicate(c, map=const_map) for c in parse_tree[":predicates"].children] # some predicates have this property: they are untyped. for predicate in self.predicates: if Predicate.OBJECT not in self.types and any([arg[1] == Predicate.OBJECT for arg in predicate.args]): for t in self.types: if self.parent_types[t] is None: self.parent_types[t] = Predicate.OBJECT self.parent_types[Predicate.OBJECT] = None self.types.add(Predicate.OBJECT) self.type_to_obj[Predicate.OBJECT] = set([]) for obj, type_list in self.obj_to_type.iteritems(): type_list.add(Predicate.OBJECT) self.type_to_obj[Predicate.OBJECT].add(obj) # only need to do this once, obviously break self.actions = [self.to_action(c) for c in parse_tree.find_all(":action")]
def _parse_problem(self, f_problem): """ Extract information from the problem file. The following will be extracted: * problem name * objects * initial state * goal state * type_to_obj * obj_to_type """ parse_tree = PDDL_Tree.create(f_problem) assert "problem" in parse_tree, "Problem must have a name" self.problem_name = parse_tree["problem"].named_children()[0] # objects must be parsed first if ":objects" in parse_tree: object_list = PDDL_Utils.read_type(parse_tree[":objects"]) self._add_objects(object_list) #TODO this may not be valid with a non-flat type hierchy obj_map = {obj: list(self.obj_to_type[obj])[0] for obj in self.objects} # the goal can be expressed in either a formula form, or a direct form if len(parse_tree[":goal"].children ) == 1 and parse_tree[":goal"].children[0].name == "and": self.goal = And([ Primitive(self.to_fluents(c)) for c in parse_tree[":goal"].children[0].children ]) else: self.goal = And([ Primitive(self.to_fluents(c)) for c in parse_tree[":goal"].children ]) # it is critical that the formula here be checked against the objects if len(parse_tree[":init"].children) == 1 and \ parse_tree[":init"].children[0].name == "and": self.init = self.to_formula(parse_tree[":init"].children[0], obj_map) else: # initial condition is one big AND # print "```", self.init self.init = And([ self.to_formula(c, obj_map) for c in parse_tree[":init"].children ])
def _parse_problem(self, f_problem): """ Extract information from the problem file. The following will be extracted: * problem name * objects * initial state * goal state * type_to_obj * obj_to_type """ parse_tree = PDDL_Tree.create(f_problem) assert "problem" in parse_tree, "Problem must have a name" self.problem_name = parse_tree ["problem"].named_children ()[0] # objects must be parsed first if ":objects" in parse_tree: object_list = PDDL_Utils.read_type(parse_tree[":objects"]) self._add_objects(object_list) #TODO this may not be valid with a non-flat type hierchy obj_map = {obj: list(self.obj_to_type[obj])[0] for obj in self.objects} # the goal can be expressed in either a formula form, or a direct form if len(parse_tree[":goal"].children) == 1 and parse_tree[":goal"].children[0].name == "and": self.goal = And([Primitive(self.to_fluents(c)) for c in parse_tree[":goal"].children[0].children]) else: self.goal = And([Primitive(self.to_fluents(c)) for c in parse_tree[":goal"].children]) # it is critical that the formula here be checked against the objects if len(parse_tree[":init"].children) == 1 and \ parse_tree[":init"].children[0].name == "and": self.init = self.to_formula(parse_tree[":init"].children[0], obj_map) else: # initial condition is one big AND self.init = And([self.to_formula(c, obj_map) for c in parse_tree[":init"].children])
def _parse_domain(self, f_domain): """ Extract information from the domain file. The following will be extracted: * types * predicates * actions """ parse_tree = PDDL_Tree.create(f_domain) assert "domain" in parse_tree, "Domain must have a name" self.domain_name = parse_tree["domain"].named_children()[0] # must read types before constants if ":types" in parse_tree: if "-" in parse_tree[":types"].named_children(): type_hierarchy = PDDL_Utils.read_type(parse_tree[":types"]) self.parent_types = { subtype: parent for subtype, parent in type_hierarchy } self.types = set(parse_tree[":types"].named_children()) self.types.discard("-") else: self.types = set(parse_tree[":types"].named_children()) self.parent_types = {t: None for t in self.types} else: self.types = set([Predicate.OBJECT]) self.parent_types = {Predicate.OBJECT: None} # must read in constants before actions or predicates if ":constants" in parse_tree: object_list = PDDL_Utils.read_type(parse_tree[":constants"]) self._add_objects(object_list) #TODO this may not be correct, depending on the type hierchy const_map = { const: list(self.obj_to_type[const])[0] for const in self.objects } self.predicates = [ self.to_predicate(c, map=const_map) for c in parse_tree[":predicates"].children ] # some predicates have this property: they are untyped. for predicate in self.predicates: if Predicate.OBJECT not in self.types and any( [arg[1] == Predicate.OBJECT for arg in predicate.args]): for t in self.types: if self.parent_types[t] is None: self.parent_types[t] = Predicate.OBJECT self.parent_types[Predicate.OBJECT] = None self.types.add(Predicate.OBJECT) self.type_to_obj[Predicate.OBJECT] = set([]) for obj, type_list in self.obj_to_type.iteritems(): type_list.add(Predicate.OBJECT) self.type_to_obj[Predicate.OBJECT].add(obj) # only need to do this once, obviously break self.actions = [ self.to_action(c) for c in parse_tree.find_all(":action") ]
def _parse_problem(self, f_problem): """ Extract information from the problem file. The following will be extracted: * problem name * objects * initial state * goal state * type_to_obj * obj_to_type """ parse_tree = PDDL_Tree.create(f_problem) assert "problem" in parse_tree, "Problem must have a name" self.problem_name = parse_tree["problem"].named_children()[0] # objects must be parsed first if ":objects" in parse_tree: object_list = PDDL_Utils.read_type(parse_tree[":objects"]) self._add_objects(object_list) #TODO this may not be valid with a non-flat type hierchy obj_map = {obj: list(self.obj_to_type[obj])[0] for obj in self.objects} # the goal can be expressed in either a formula form, or a direct form if len(parse_tree[":goal"].children ) == 1 and parse_tree[":goal"].children[0].name == "and": self.goal = And([ self.to_formula(c, obj_map) for c in parse_tree[":goal"].children[0].children ]) else: self.goal = And([ self.to_formula(c, obj_map) for c in parse_tree[":goal"].children ]) # it is critical that the formula here be checked against the objects if len(parse_tree[":init"].children) == 1 and \ parse_tree[":init"].children[0].name == "and": self.init = self.to_formula(parse_tree[":init"].children[0], obj_map) else: # initial condition is one big AND self.init = And([ self.to_formula(c, obj_map) for c in parse_tree[":init"].children ]) # Parse the multiagent stuff self.task = parse_tree[":task"].children[0].name self.depth = int(parse_tree[":depth"].children[0].name) self.projection = [a.name for a in parse_tree[":projection"].children] self.init_type = parse_tree[":init-type"].children[0].name self.plan = [] if ':plan' in parse_tree: self.plan = map( lambda x: '_'.join( map(str, [x.name] + [y.name for y in x.children])), parse_tree[":plan"].children)