Example #1
0
def test_lm_cut_heuristic_value_simple_task_always_true():
    task1 = _get_simple_task_always_true()
    heuristic = LmCutHeuristic(task1)
    h_val = heuristic(make_root_node(task1.initial_state))
    # print('Printing goal plateau')
    # for f in heuristic.goal_plateau:
    #    print(repr(f))
    # print('Printing cut')
    # for op in cut:
    #    print(repr(op))
    assert h_val == 1.0
Example #2
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 #3
0
def compare_h_values(Heuristic, task, expected):
    rh = Heuristic(task)
    h_value = rh(make_root_node(task.initial_state))
    print(
        "Wrong value for",
        task.name,
        "with",
        str(Heuristic),
        ". Expected",
        expected,
        ", but got",
        h_value,
    )
    assert h_value == expected
Example #4
0
def test_heuristics():
    # simple task: two operators have to be applied
    task1 = Task(
        "task1",
        {"A", "B", "C"},
        frozenset({"A"}),
        frozenset({"C", "B"}),
        [
            Operator("op1", {"A"}, {"B"}, set()),
            Operator("op2", {"B"}, {"C"}, set()),
            Operator("op3", {"B"}, {"A"}, set()),
        ],
    )

    # initial state is part of the goal state: one operator has to be applied
    task2 = Task(
        "task2",
        {"A", "B", "C"},
        frozenset(["A", "B"]),
        frozenset(["B", "C"]),
        [
            Operator("op1", {"A"}, {"B"}, set()),
            Operator("op2", {"B"}, {"C"}, set())
        ],
    )

    # task with one operator with two preconditions
    task3 = Task(
        "task3",
        {"A", "B", "C"},
        frozenset(["A", "B"]),
        frozenset(["C"]),
        [Operator("op1", {"A", "B"}, {"C"}, set())],
    )

    # task with one operator with two effects
    task4 = Task(
        "task4",
        {"A", "B", "C"},
        frozenset(["A"]),
        frozenset(["C", "B"]),
        [Operator("op1", {"A"}, {"B", "C"}, set())],
    )

    # task with one operator with equal precondition and effect,
    task4b = Task(
        "task4b",
        {"A", "B", "C"},
        frozenset(["A"]),
        frozenset(["C", "B"]),
        [Operator("op1", {"A"}, {"A", "B", "C"}, set())],
    )

    # task with one operator with several effects,
    # 2 operators have to be applied
    task5 = Task(
        "task5",
        {"A", "B", "C", "D", "E", "F"},
        ["A"],
        ["E", "F"],
        [
            Operator("op1", {"A"}, {"B", "C", "D", "E"}, set()),
            Operator("op2", {"C"}, {"F"}, set()),
        ],
    )

    # task with one operator with several preconditions
    task6 = Task(
        "task6",
        {"A", "B", "C", "D", "E"},
        ["A"],
        ["E"],
        [
            Operator("op1", {"A"}, {"B"}, set()),
            Operator("op2", {"B"}, {"C"}, set()),
            Operator("op3", {"A"}, {"D"}, set()),
            Operator("op4", {"A", "C", "B", "D"}, {"E"}, set()),
        ],
    )

    # task with empty initial state: no operator can be applied
    task7 = Task(
        "task7",
        {"A", "B", "C"},
        [],
        ["C"],
        [
            Operator("op1", {"A"}, {"B"}, set()),
            Operator("op2", {"B"}, {"C"}, set())
        ],
    )

    # task with initial state = goal state: no operator has to be applied
    task8 = Task(
        "task8",
        {"A", "B", "C"},
        ["C"],
        ["C"],
        [
            Operator("op1", {"A"}, {"B"}, set()),
            Operator("op2", {"B"}, {"C"}, set())
        ],
    )

    # task with operator with empty precondition
    task9 = Task(
        "task9",
        {"A", "B", "C"},
        [],
        ["C"],
        [
            Operator("op1", {}, {"B"}, set()),
            Operator("op2", {"B"}, {"C"}, set())
        ],
    )

    # a more complex task
    task10 = Task(
        "task10",
        {"v1", "v2", "v3", "v4", "v5", "v6", "g"},
        ["v1"],
        ["g"],
        [
            Operator("op1", {"v1"}, {"v2"}, set()),
            Operator("op2", {"v2"}, {"v3"}, set()),
            Operator("op3", {"v3"}, {"v4", "v5"}, set()),
            Operator("op4", {"v4", "v5"}, {"g"}, set()),
            Operator("op5", {"v2"}, {"v6"}, set()),
            Operator("op6", {"v6"}, {"v5"}, set()),
        ],
    )

    # another complex task
    task12 = Task(
        "task12",
        {"A", "B", "C", "D", "E", "F", "G", "H", "I"},
        ["A", "B"],
        ["F", "G", "H"],
        [
            Operator("op1", {"A"}, {"C"}, set()),
            Operator("op2", {"C", "D"}, {"F"}, set()),
            Operator("op3", {"D", "E"}, {"G", "H"}, set()),
            Operator("op4", {"B"}, {"D", "E"}, set()),
            Operator("op5", {"I"}, {"H"}, set()),
        ],
    )

    # task with no goal:
    task13 = Task(
        "task13",
        {"A", "B", "C"},
        ["A", "B"],
        [],
        [Operator("op1", {"A", "B"}, {"C"}, set())],
    )
    # task with no reachable goal:
    task14 = Task(
        "task14",
        {"A", "B", "C"},
        ["A"],
        ["B", "C"],
        [Operator("op1", {"A"}, {"B"}, set())],
    )

    # columns:           landmarks      lm_costs                h
    expected = [
        (task1, {"B", "C"}, {
            "B": 1,
            "C": 1
        }, 2),
        (task2, {"B", "C"}, {
            "B": 1,
            "C": 1
        }, 1),
        (task3, {"C"}, {
            "C": 1
        }, 1),
        (task4, {"B", "C"}, {
            "B": 0.5,
            "C": 0.5
        }, 1),
    ]

    for task, expected_landmarks, expected_lmc, exptected_h in expected:
        assert landmarks.get_landmarks(task) == expected_landmarks
        assert (landmarks.compute_landmark_costs(
            task, expected_landmarks) == expected_lmc)
        assert (landmarks.LandmarkHeuristic(task)(make_root_node(
            task.initial_state)) == exptected_h)
Example #5
0
def test_lm_cut_heuristic_value_two_initial_facts():
    task1 = _get_intermediate_task_two_initial_facts()
    heuristic = LmCutHeuristic(task1)
    h_val = heuristic(make_root_node(task1.initial_state))
    assert h_val == 4.0
Example #6
0
def test_lm_cut_at_goal():
    task = _get_simple_task_at_goal()
    heuristic = LmCutHeuristic(task)
    h_val = heuristic(make_root_node(task.initial_state))
    heuristic.compute_goal_plateau(heuristic.explicit_goal)
    assert h_val == 0.0