Beispiel #1
0
def parse(domain_file, problem_file):
    # Parsing
    parser = Parser(domain_file, problem_file)
    domain = parser.parse_domain()
    problem = parser.parse_problem(domain)

    return problem
Beispiel #2
0
def gen_heuristic_test(dom,
                       prob,
                       search_class,
                       heuristic_class,
                       h_values_plan,
                       plan_length=None):
    parser = Parser('')
    parser.domInput = dom
    parser.probInput = prob

    domain = parser.parse_domain(False)
    problem = parser.parse_problem(domain, False)

    task = grounding.ground(problem)

    heuristic = heuristic_class(task)
    plan = search_class(task, heuristic)
    if plan_length:
        assert len(plan) == plan_length
    # run through plan and validate heuristic value
    # the true_h_values are taken from fast downward with astar and lm cut
    # heuristic
    computed_h_values = list(_gen_h_values(task.initial_state, plan,
                                           heuristic))
    assert h_values_plan == computed_h_values
Beispiel #3
0
def parse(domain_file, problem_file):
	# Parsing
	parser = Parser(domain_file, problem_file)
	domain = parser.parse_domain()
	problem = parser.parse_problem(domain)

	return problem
Beispiel #4
0
def get_ground_task(domain_filepath, problem_filepath):
    parser = Parser(domain_filepath, problem_filepath)
    domain = parser.parse_domain()
    problem = parser.parse_problem(domain)
    task = grounding.ground(problem)

    return task
Beispiel #5
0
 def __init__(self, domain_file, problem_file):
     """
         domain - domain pddl file
         problem - problem pddl file
     """
     #parse the domain and problem
     parser = Parser(domain_file, problem_file)
     domain = parser.parse_domain()
     self.problem = parser.parse_problem(domain)
class PlanRecognitionProblemParser:
    def __init__(self):
        self.parser = Parser(None)
        self.problem = None

    def _parse_domain(self,domain_file):
        self.parser.domFile = domain_file
        return self.parser.parse_domain()

    def _parse_observations(self,domain,observations_file):
        observations = []
        with open(observations_file, encoding='utf-8') as file:
            for obs in file:
                iter = parse_lisp_iterator(obs)
                act = parse_predicate_instance(self.parser._read_input(obs))
                observations.append(act)

            # iter = self.parser._read_input(file)
            # while not iter.empty():
            #     observations.append()
            # observations = parse_predicate_instance_list(iter)
        # TODO validate parsed actions

        return observations

    def _parse_hypotheses(self,domain,hypotheses_file):
        hypotheses = []
        with open(hypotheses_file, encoding="utf-8") as file:
            for hyp in file:
                preds = hyp.split(",")
                hyp_literals = [parse_predicate_instance(self.parser._read_input(p)) for p in preds]
                hypotheses.append(hyp_literals)
        # TODO validate hypotheses with domain
        return hypotheses

    def _parse_template(self,domain,template_file):
        self.parser.probFile = template_file
        template_content = ""
        with open(template_file, encoding="utf-8") as file:
            for line in file:
                if line.find("<HYPOTHESIS>")>=0:
                    line = ""
                template_content+=line+"\n"
        self.parser.probInput = template_content
        return self.parser.parse_problem(domain,read_from_file=False)

    def parse_pr_problem(self, domain_file, observations_file, hypotheses_file, template_file):
        parser = Parser(domain_file,template_file)
        domain = self._parse_domain(domain_file)
        observations = self._parse_observations(domain, observations_file)
        hypotheses = self._parse_hypotheses(domain,hypotheses_file)
        template = self._parse_template(domain, template_file)
        return PlanRecognitionProblem(domain,observations, hypotheses, template)
def _parse(domain_file, problem_file):
    # Parsing
    parser = Parser(domain_file, problem_file)
    #logging.info('Parsing Domain {0}'.format(domain_file))
    domain = parser.parse_domain()
    #logging.info('Parsing Problem {0}'.format(problem_file))
    problem = parser.parse_problem(domain)
    #logging.debug(domain)
    #logging.info('{0} Predicates parsed'.format(len(domain.predicates)))
    #logging.info('{0} Actions parsed'.format(len(domain.actions)))
    #logging.info('{0} Objects parsed'.format(len(problem.objects)))
    #logging.info('{0} Constants parsed'.format(len(domain.constants)))
    return problem
Beispiel #8
0
def _parse(domain_file, problem_file):
    # Parsing
    parser = Parser(domain_file, problem_file)
    logging.info('Parsing Domain {0}'.format(domain_file))
    domain = parser.parse_domain()
    logging.info('Parsing Problem {0}'.format(problem_file))
    problem = parser.parse_problem(domain)
    logging.debug(domain)
    logging.info('{0} Predicates parsed'.format(len(domain.predicates)))
    logging.info('{0} Actions parsed'.format(len(domain.actions)))
    logging.info('{0} Objects parsed'.format(len(problem.objects)))
    logging.info('{0} Constants parsed'.format(len(domain.constants)))
    return problem
Beispiel #9
0
def _parse(domain_file, problem_file):
    # Parsing
    parser = Parser(domain_file, problem_file)
    logging.info("Parsing Domain {}".format(domain_file))
    domain = parser.parse_domain()
    logging.info("Parsing Problem {}".format(problem_file))
    problem = parser.parse_problem(domain)
    logging.debug(domain)
    logging.info("{} Predicates parsed".format(len(domain.predicates)))
    logging.info("{} Actions parsed".format(len(domain.actions)))
    logging.info("{} Objects parsed".format(len(problem.objects)))
    logging.info("{} Constants parsed".format(len(domain.constants)))
    return problem
Beispiel #10
0
def test_lm_cut_blocksworld_initial_state():
    parser = Parser("")
    parser.domInput = blocks_dom
    parser.probInput = blocks_problem_1

    domain = parser.parse_domain(False)
    problem = parser.parse_problem(domain, False)

    task = grounding.ground(problem)

    heuristic = LmCutHeuristic(task)
    h_val = heuristic(make_root_node(task.initial_state))
    assert h_val == 6.0
Beispiel #11
0
def test_lm_cut_blocksworld_initial_state():
    parser = Parser('')
    parser.domInput = blocks_dom
    parser.probInput = blocks_problem_1

    domain = parser.parse_domain(False)
    problem = parser.parse_problem(domain, False)

    task = grounding.ground(problem)

    heuristic = LmCutHeuristic(task)
    h_val = heuristic(make_root_node(task.initial_state))
    assert h_val == 6.
Beispiel #12
0
def parse(domain_file, problem_file):
    """
    unmodified pyperplan function
    """
    parser = Parser(domain_file, problem_file)
    logging.info('Parsing Domain {0}'.format(domain_file))
    domain = parser.parse_domain()
    logging.info('Parsing Problem {0}'.format(problem_file))
    problem = parser.parse_problem(domain)
    logging.debug(domain)
    logging.info('{0} Predicates parsed'.format(len(domain.predicates)))
    logging.info('{0} Actions parsed'.format(len(domain.actions)))
    logging.info('{0} Objects parsed'.format(len(problem.objects)))
    logging.info('{0} Constants parsed'.format(len(domain.constants)))
    return problem
Beispiel #13
0
 def __init__(self, domain_file, problem_file):
     """
         domain - domain pddl file
         problem - problem pddl file
     """
     #parse the domain and problem
     with open(DOM_TEMPL) as d_fd:
         self.domain_template = '\n'.join(
             [line.strip() for line in d_fd.readlines()])
     with open(PROB_TEMPL) as p_fd:
         self.prob_template = '\n'.join(
             [line.strip() for line in p_fd.readlines()])
     parser = Parser(domain_file, problem_file)
     domain = parser.parse_domain()
     problem = parser.parse_problem(domain)
     self.task = grounding.ground(problem)
def gen_heuristic_test(dom, prob, search_class, heuristic_class, h_values_plan,
                       plan_length=None):
    parser = Parser('')
    parser.domInput = dom
    parser.probInput = prob

    domain = parser.parse_domain(False)
    problem = parser.parse_problem(domain, False)

    task = grounding.ground(problem)

    heuristic = heuristic_class(task)
    plan = search_class(task, heuristic)
    if plan_length:
        assert len(plan) == plan_length
    # run through plan and validate heuristic value
    # the true_h_values are taken from fast downward with astar and lm cut
    # heuristic
    computed_h_values = list(_gen_h_values(task.initial_state, plan, heuristic))
    assert h_values_plan == computed_h_values
Beispiel #15
0
    logging.basicConfig(level=getattr(logging, args.loglevel.upper()),
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        stream=sys.stdout)

    args.problem = os.path.abspath(args.problem)
    if args.domain is None:
        args.domain = find_domain(args.problem)
    else:
        args.domain = os.path.abspath(args.domain)

    problem = parse(args.domain, args.problem)
    task = ground(problem)

    if args.graphtype == 'relatedness' and args.diameter == 'true':
        build_graph_related(task, static=True, draw=True, diameter=True)
    elif args.graphtype == 'relatedness' and args.diameter == 'false':
        build_graph_related(task, static=True, draw=True, diameter=False)
    elif args.graphtype == 'rel_simple':
        if args.grounding == 'original':
            raise Exception(
                'The simple relatedness graph is only available with the new grounding'
            )
        parser = Parser(args.domain, args.problem)
        domain = parser.parse_domain(args.domain)
        if args.diameter == 'true':
            build_graph_rel_simple(domain, task, draw=True)
        elif args.diameter == 'false':
            build_graph_rel_simple(domain, task, draw=True, diameter=False)
    elif args.graphtype == 'causal':
        build_graph_causal(task)
_problem_input = """(define (problem BLOCKS-5-0)
(:domain BLOCKS)
(:story_objs B E A C D - block)
(:INIT (CLEAR D) (CLEAR C) (ONTABLE D) (ONTABLE A) (ON C E) (ON E B) (ON B A)
 (HANDEMPTY))
(:goal (AND (ON A E) (ON E B) (ON B D) (ON D C)))
)
"""

_parser = Parser('')

_parser.domInput = _domain_input
_parser.probInput = _problem_input

_domain = _parser.parse_domain(False)
_problem = _parser.parse_problem(_domain, False)


def test_default_pddl_visitor_domain():
    defaultVisitor = pddl_tree_visitor.PDDLVisitor()
    input = _domain_input.split('\n')
    iter = parse_lisp_iterator(input)
    domAST = parse_domain_def(iter)
    # and traverse the AST
    domAST.accept(defaultVisitor)


def test_default_pddl_visitor_problem():
    defaultVisitor = pddl_tree_visitor.PDDLVisitor()
    input = _problem_input.split('\n')
Beispiel #17
0
def problemInfo(domFile):
    parser = Parser(domFile)
    domain = parser.parse_domain()
    return len(domain.predicates), len(domain.actions)
Beispiel #18
0
_problem_input = """(define (problem BLOCKS-5-0)
(:domain BLOCKS)
(:objects B E A C D - block)
(:INIT (CLEAR D) (CLEAR C) (ONTABLE D) (ONTABLE A) (ON C E) (ON E B) (ON B A)
 (HANDEMPTY))
(:goal (AND (ON A E) (ON E B) (ON B D) (ON D C)))
)
"""

_parser = Parser("")

_parser.domInput = _domain_input
_parser.probInput = _problem_input

_domain = _parser.parse_domain(False)
_problem = _parser.parse_problem(_domain, False)


def test_default_pddl_visitor_domain():
    defaultVisitor = pddl_tree_visitor.PDDLVisitor()
    input = _domain_input.split("\n")
    iter = parse_lisp_iterator(input)
    domAST = parse_domain_def(iter)
    # and traverse the AST
    domAST.accept(defaultVisitor)


def test_default_pddl_visitor_problem():
    defaultVisitor = pddl_tree_visitor.PDDLVisitor()
    input = _problem_input.split("\n")
def test_writer_complex():
    test = [
        """
        (define (domain BLOCKS)
      (:requirements :strips :typing)
      (:types block)
      (:predicates (on ?x - block ?y - block)
                   (ontable ?x - block)
                   (clear ?x - block)
                   (handempty)
                   (holding ?x - block)
                   )

      (:action pick-up
                 :parameters (?x - block)
                 :precondition (and (clear ?x) (ontable ?x) (handempty))
                 :effect
                 (and (not (ontable ?x))
                       (not (clear ?x))
                       (not (handempty))
                       (holding ?x)))

      (:action put-down
                 :parameters (?x - block)
                 :precondition (holding ?x)
                 :effect
                 (and (not (holding ?x))
                       (clear ?x)
                       (handempty)
                       (ontable ?x)))
      (:action stack
                 :parameters (?x - block ?y - block)
                 :precondition (and (holding ?x) (clear ?y))
                 :effect
                 (and (not (holding ?x))
                       (not (clear ?y))
                       (clear ?x)
                       (handempty)
                       (on ?x ?y)))
      (:action unstack
                 :parameters (?x - block ?y - block)
                 :precondition (and (on ?x ?y) (clear ?x) (handempty))
                 :effect
                 (and (holding ?x)
                       (clear ?y)
                       (not (clear ?x))
                       (not (handempty))
                       (not (on ?x ?y)))))
        """, """
                (define (problem BLOCKS-4-0)
                (:domain BLOCKS)
                (:objects D B A C - block)
                (:INIT (CLEAR C) (CLEAR A) (CLEAR B) (CLEAR D) (ONTABLE C) (ONTABLE A)
                (ONTABLE B) (ONTABLE D) (HANDEMPTY))
                (:goal (AND (ON D C) (ON C B) (ON B A)))
                )
                """
    ]
    parser = Parser(None)
    parser.domInput = test[0]
    parser.probInput = test[1]
    domain = parser.parse_domain(False)
    problem = parser.parse_problem(domain, False)
    writer = PDDLWriter()
    domain_string = writer.write_domain(domain)
    # print(domain_string)
    parser.domInput = domain_string
    domain = parser.parse_domain(False)  # Checking if our output is valid pddl
    assert domain is not None
    # print(writer.write_domain(domain))
    # assert domain_string == writer.write_domain(domain)
    print(writer.write_problem(problem))