コード例 #1
0
ファイル: test_grounding.py プロジェクト: aibasel/pyperplan
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
コード例 #2
0
 def change_goal(self, g):
     if type(g) is not list:
         g = [g]
     assert type(g[0]) is Predicate
     self.task.goals = _get_partial_state(g)
コード例 #3
0
ファイル: test_grounding.py プロジェクト: aibasel/pyperplan
def test_operators():

    # action with signature with 2 types
    action_drive_vehicle = get_action(
        "DRIVE-VEHICLE",
        [
            ("vehicle", [types["car"], types["truck"]]),
            ("orig", [types["city"]]),
            ("dest", [types["city"]]),
        ],
        [predicate_veh_orig],
        [predicate_veh_dest],
        [predicate_veh_orig],
    )

    # action with predicate in add & delete list
    action_add_delete = get_action(
        "STAY",
        [("car", [types["car"]]), ("in", [types["city"]])],
        [predicate_in],
        [predicate_in],
        [predicate_in],
    )

    # action with constant input
    action_constant = get_action(
        "CONSTANT-ACTION",
        [("my_car", [types["my_car"]]), ("city", [types["city"]])],
        [],
        [
            Predicate(
                "in", [("basel", [types["city"]]), ("switzerland", [types["country"]])]
            )
        ],
        [],
    )

    # action with only delete effects
    action_only_delete = get_action(
        "LEAVE",
        [("car", [types["car"]]), ("in", [types["city"]])],
        [predicate_in],
        [],
        [predicate_in],
    )

    # action with delete effect which does not occur in precondition
    action_delete = get_action(
        "DELETE",
        [("car", [types["car"]]), ("orig", [types["city"]]), ("dest", [types["city"]])],
        [],
        [predicate_car_orig],
        [predicate_car_dest],
    )

    type_map = grounding._create_type_map(objects)

    grounded_initial_state = grounding._get_partial_state(initial_state)

    grounded_drive_car = list(
        grounding._ground_action(action_drive_car, type_map, [], grounded_initial_state)
    )
    grounded_drive_vehicle = list(
        grounding._ground_action(
            action_drive_vehicle, type_map, [], grounded_initial_state
        )
    )
    grounded_add_delete = list(
        grounding._ground_action(
            action_add_delete, type_map, [], grounded_initial_state
        )
    )
    grounded_only_delete = list(
        grounding._ground_action(
            action_only_delete, type_map, [], grounded_initial_state
        )
    )
    grounded_delete = list(
        grounding._ground_action(action_delete, type_map, [], grounded_initial_state)
    )

    domain = Domain(
        "test_domain",
        types,
        {
            "in": Predicate(
                "in", [("city", types["city"]), ("country", types["country"])]
            )
        },
        {"action-constant": action_constant},
        {"my_car": types["car"]},
    )

    problem = Problem("test_problem", domain, objects, initial_state, goal_state)
    task = grounding.ground(problem)

    expected = [
        ("(DRIVE-CAR red_car freiburg basel)", grounded_drive_car),
        ("(DRIVE-VEHICLE blue_truck freiburg basel)", grounded_drive_vehicle),
        ("(STAY red_car freiburg)", grounded_add_delete),
        ("(LEAVE red_car freiburg)", grounded_only_delete),
        ("(DELETE red_car freiburg basel)", grounded_delete),
    ]

    for operator, grounded_operators in expected:
        assert any(op.name == operator for op in grounded_operators)