Example #1
0
def parse_problem(domain_file, problem_file, path=None):
    if path is not None:
        domain_file = os.path.join(path, domain_file)
        problem_file = os.path.join(path, problem_file)
    parser = Parser(domain_file, problem_file)
    domain = parser.parse_domain()
    problem = parser.parse_problem(
        domain)  # domain can be found as an attribute of problem
    problem.objects_by_type = get_objects_by_type(problem)
    return problem
Example #2
0
def _parse(domain_file: str, problem_file: str) -> Tuple[Domain, Problem]:
    """ Parse the domain and problem """
    # Parsing
    parser = Parser(domain_file, problem_file)
    _log.debug("Parsing Domain {0}".format(domain_file))
    domain = parser.parse_domain()
    _log.debug("Parsing Problem {0}".format(problem_file))
    problem = parser.parse_problem(domain)
    _log.debug("{0} Predicates parsed".format(len(domain.predicates)))
    _log.debug("{0} Actions parsed".format(len(domain.actions)))
    _log.debug("{0} Objects parsed".format(len(problem.objects)))
    _log.debug("{0} Constants parsed".format(len(domain.constants)))
    return domain, problem
Example #3
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
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
Example #5
0
def test_hAdd_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 = hAddHeuristic(task)
    h_val = heuristic(make_root_node(task.initial_state))
    assert h_val, False == 6.0
Example #6
0
                   (clear ?y)
                   (not (clear ?x))
                   (not (handempty))
                   (not (on ?x ?y)))))
"""

_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)
Example #7
0
def test_add_del_effects():
    parser = Parser("")

    def parse_problem(domain, problem):
        parser.domInput = domain
        parser.probInput = problem
        domain = parser.parse_domain(False)
        return parser.parse_problem(domain, False)

    dom_pddl = """
    (define (domain dom)
      (:requirements :typing)
      (:predicates (ok ?v - object))

      (:action theaction
       :parameters (?x - object)
       :precondition {0}
       :effect {1}
      )
    )
    """

    prob_pddl = """
    ;; See domain file for description of this test.

    (define (problem prob)
      (:domain dom)
      (:objects y - object)
      (:init)
      (:goal (ok y)))
    """

    tests = [
        # Only add effect
        ("(and)", "(ok ?x)", set(), {"(ok y)"}, set()),
        # Only delete effect
        ("(and)", "(and (not (ok ?x)))", set(), set(), {"(ok y)"}),
        # Both add and delete effect
        ("(and)", "(and (ok ?x) (not (ok ?x)))", set(), {"(ok y)"}, set()),
        # Precondition and add effect
        ("(and (ok ?x))", "(ok ?x)", {"(ok y)"}, set(), set()),
        # Precondition and delete effect
        ("(and (ok ?x))", "(and (not (ok ?x)))", {"(ok y)"}, set(), {"(ok y)"}),
        # Precondition and both add and delete effect
        ("(and (ok ?x))", "(and (ok ?x) (not (ok ?x)))", {"(ok y)"}, set(), set()),
    ]

    for pre_in, eff_in, pre_exp, add_exp, del_exp in tests:
        dom = dom_pddl.format(pre_in, eff_in)
        problem = parse_problem(dom, prob_pddl)

        domain = problem.domain
        actions = domain.actions.values()
        predicates = domain.predicates.values()

        # Objects
        objects = problem.objects
        objects.update(domain.constants)

        # Get the names of the static predicates
        statics = grounding._get_statics(predicates, actions)

        # Create a map from types to objects
        type_map = grounding._create_type_map(objects)

        # Transform initial state into a specific
        init = grounding._get_partial_state(problem.initial_state)

        # Ground actions
        operators = grounding._ground_actions(actions, type_map, statics, init)

        assert len(operators) == 1
        op = operators[0]
        assert op.preconditions == pre_exp
        assert op.add_effects == add_exp
        assert op.del_effects == del_exp
Example #8
0
def test_regression():
    parser = Parser("")

    def parse_problem(domain, problem):
        parser.domInput = domain
        parser.probInput = problem
        domain = parser.parse_domain(False)
        return parser.parse_problem(domain, False)

    prob_05 = """
    ;; See domain file for description of this test.

    (define (problem regression-test-05)
      (:domain regression-test)
      (:objects y - object)
      (:init)
      (:goal (the-predicate x y)))
    """

    dom_05 = """
    ;; Expected behaviour: plan of length one found
    ;; Observed behaviour (r265): plan of length zero found

    (define (domain regression-test)
      (:requirements :typing) ;; work around problem in regression test #4.
      (:predicates (the-predicate ?v1 ?v2 - object))
      (:constants x - object)

      (:action theaction
       :parameters (?x - object)
       :precondition (and)
       :effect (the-predicate x ?x)
      )
    )
    """

    prob_06 = """
    ;; See domain file for description of this test.

    (define (problem regression-test-06)
      (:domain regression-test)
      (:objects y - object)
      (:init)
      (:goal (the-predicate y y)))

    """
    dom_06 = """
    ;; Expected behaviour: planner proves that no plan exists
    ;; Observed behaviour (r265): plan of length one found

    (define (domain regression-test)
      (:requirements :typing) ;; work around problem in regression test #4.
      (:predicates (the-predicate ?v1 ?v2 - object))
      (:constants x - object)

      (:action theaction
       :parameters (?x - object)
       :precondition (and)
       :effect (the-predicate x ?x)
      )
    )
    """

    # problem / domain 07 contains a different action compared
    # to the actions of domain 5 & 6
    prob_07 = prob_06

    dom_07 = """
    (define (domain regression-test)
      (:requirements :typing) ;; work around problem in regression test #4.
      (:predicates (the-predicate ?v1 ?v2 - object))
      (:constants y - object)

      (:action theaction
       :parameters (?x - object)
       :precondition (and)
       :effect (the-predicate y ?x)
      )
    )
    """

    # action of problem / domain 8 differs only in the variable name compared
    # to the actions of problem 5 and 6: After grounding there should be no
    # difference between the grounded actions
    prob_08 = prob_05

    dom_08 = """
    (define (domain regression-test)
      (:requirements :typing) ;; work around problem in regression test #4.
      (:predicates (the-predicate ?v1 ?v2 - object))
      (:constants x - object)

      (:action theaction
       :parameters (?z - object)
       :precondition (and)
       :effect (the-predicate x ?z)
      )
    )
    """

    parsed_problem5 = parse_problem(dom_05, prob_05)
    parsed_problem6 = parse_problem(dom_06, prob_06)
    parsed_problem7 = parse_problem(dom_07, prob_07)
    parsed_problem8 = parse_problem(dom_08, prob_08)

    # coded input:
    type_object = Type("object", None)
    types = {"object": type_object}
    predicates = {
        "the_predicate": Predicate(
            "the-predicate", [("v1", type_object), ("v2", type_object)]
        )
    }
    constants = {"x": type_object}
    actions = {
        "theaction": get_action(
            "theaction",
            [("?x", [type_object])],
            [],
            [Predicate("the-predicate", [("x", type_object), ("?x", type_object)])],
            [],
        )
    }
    domain = Domain("regression-test", types, predicates, actions, constants)
    problem5 = Problem(
        "regression-test-05",
        domain,
        {"y": type_object},
        [],
        [Predicate("the-predicate", [("x", type_object), ("y", type_object)])],
    )
    problem6 = Problem(
        "regression-test-06",
        domain,
        {"y": type_object},
        [],
        [Predicate("the-predicate", [("y", type_object), ("y", type_object)])],
    )

    parsed_task5 = grounding.ground(parsed_problem5)
    coded_task5 = grounding.ground(problem5)
    parsed_task6 = grounding.ground(parsed_problem6)
    coded_task6 = grounding.ground(problem6)
    parsed_task7 = grounding.ground(parsed_problem7)
    parsed_task8 = grounding.ground(parsed_problem8)

    expected = [
        (parsed_task5.operators, coded_task5.operators, True),
        (parsed_task6.operators, coded_task6.operators, True),
        (parsed_task5.operators, coded_task6.operators, False),
        (parsed_task5.operators, parsed_task7.operators, False),
        (parsed_task5.operators, parsed_task8.operators, True),
    ]

    for operator1, operator2, expected_result in expected:
        assert compare_operators(operator1, operator2) == expected_result
Example #9
0
def parse_domain(domain_file, path=None):
    if path is not None:
        domain_file = os.path.join(path, domain_file)
    parser = Parser(domain_file, probFile=None)
    return parser.parse_domain()