Пример #1
0
def create_problem_instance_from_file(dir_path,
                                      domain_path_name,
                                      problem_index=0):
    domain_file = os.path.join(dir_path, 'pddl', f'{domain_path_name}.pddl')
    problem_dir = os.path.join(dir_path, 'pddl', f'{domain_path_name}')

    env = PDDLEnv(domain_file,
                  problem_dir,
                  raise_error_on_invalid_action=True,
                  dynamic_action_space=False)

    env.fix_problem_index(problem_index)
    return env, env.problems[problem_index]
Пример #2
0
def test_dynamic_action_space(verbose=False):
    """
    """
    dir_path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "pddl")

    for name in [
            # "Manymiconic",
            # "Manygripper",
            "Blocks_operator_actions",
            "Hanoi_operator_actions",
    ]:
        domain_file = os.path.join(dir_path, "{}.pddl".format(name.lower()))
        problem_dir = os.path.join(dir_path, name.lower())
        # problem_dir = os.path.join(dir_path, name.lower()+"_test")

        env1 = PDDLEnv(
            domain_file,
            problem_dir,
            operators_as_actions=True,
            dynamic_action_space=False,
        )

        env2 = PDDLEnv(
            domain_file,
            problem_dir,
            operators_as_actions=True,
            dynamic_action_space=True,
        )

        env1.action_space.seed(0)
        env2.action_space.seed(0)
        state1, _ = env1.reset()
        state2, _ = env2.reset()
        assert state1 == state2

        for _ in range(25):
            start_time = time.time()
            valid_actions1 = env1.action_space.all_ground_literals(state1)
            if verbose:
                print(
                    "Computing valid action spaces without instantiator took {} seconds"
                    .format(time.time() - start_time))
            start_time = time.time()
            valid_actions2 = env2.action_space.all_ground_literals(state2)
            if verbose:
                print(
                    "Computing valid action spaces *with* instantiator took {} seconds"
                    .format(time.time() - start_time))
            assert valid_actions2.issubset(valid_actions1)
            action = env2.action_space.sample(state2)
            state1, _, _, _ = env1.step(action)
            state2, _, _, _ = env2.step(action)

        if verbose:
            print("Test passed for environment {}.".format(name))

    print("Test passed.")
Пример #3
0
def test_determinize():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    domain_file = os.path.join(dir_path, 'pddl',
                               'test_probabilistic_domain.pddl')
    problem_dir = os.path.join(dir_path, 'pddl', 'test_probabilistic_domain')

    env = PDDLEnv(domain_file,
                  problem_dir,
                  raise_error_on_invalid_action=True,
                  dynamic_action_space=True)
    env.domain.determinize()

    obs, _ = env.reset()
    action = env.action_space.all_ground_literals(obs).pop()
    transitions = env.get_all_possible_transitions(action, return_probs=True)

    transition_list = list(transitions)
    assert len(transition_list) == 1
    assert transition_list[0][1] == 1.0
    newstate = transition_list[0][0][0]

    type1 = Type('type1')
    type2 = Type('type2')
    pred1 = Predicate('pred1', 1, [type1])
    pred2 = Predicate('pred2', 1, [type2])
    pred3 = Predicate('pred3', 3, [type1, type2, type2])

    assert newstate.literals == frozenset({
        pred1('b2'),
        pred2('c1'),
        pred3('a1', 'c1', 'd1'),
        pred3('a2', 'c2', 'd2'),
        pred3('b2', 'd1', 'c1')
    })

    print("Test passed.")
Пример #4
0
def test_dynamic_action_space_same_obj():
    """
    """
    dir_path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "pddl")

    name = "dynamic_action_space_same_obj"
    domain_file = os.path.join(dir_path, "{}.pddl".format(name.lower()))
    problem_dir = os.path.join(dir_path, name.lower())

    env = PDDLEnv(
        domain_file,
        problem_dir,
        operators_as_actions=True,
        dynamic_action_space=True,
    )
    assert len(env.problems) == 2  # both problems have the same object set

    env.fix_problem_index(0)
    state1, _ = env.reset()
    # Only one action is possible: unstack(a, b)
    for _ in range(25):
        act1 = env.action_space.sample(state1)
        assert act1.predicate.name == "unstack"
        assert act1.variables[0].name == "a"
        assert act1.variables[1].name == "b"

    env.fix_problem_index(1)
    state2, _ = env.reset()
    assert state1.objects == state2.objects
    # Only one action is possible: unstack(d, c)
    for _ in range(25):
        act2 = env.action_space.sample(state2)
        assert act2.predicate.name == "unstack"
        assert act2.variables[0].name == "d"
        assert act2.variables[1].name == "c"

    print("Test passed.")
Пример #5
0
def test_pddlenv():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    domain_file = os.path.join(dir_path, 'pddl', 'test_domain.pddl')
    problem_dir = os.path.join(dir_path, 'pddl', 'test_domain')

    env = PDDLEnv(domain_file,
                  problem_dir,
                  raise_error_on_invalid_action=True,
                  dynamic_action_space=True)
    env2 = PDDLEnv(domain_file,
                   problem_dir,
                   raise_error_on_invalid_action=True,
                   dynamic_action_space=False)

    type1 = Type('type1')
    type2 = Type('type2')
    pred1 = Predicate('pred1', 1, [type1])
    pred2 = Predicate('pred2', 1, [type2])
    pred3 = Predicate('pred3', 1, [type1, type2, type2])
    operator_name = 'action1'
    action_pred = Predicate('actionpred', 1, [type1])

    obs, _ = env.reset()

    assert obs == {
        pred1('b2'),
        pred2('c1'),
        pred3('a1', 'c1', 'd1'),
        pred3('a2', 'c2', 'd2')
    }

    operator = env.domain.operators[operator_name]

    # Invalid action
    action = action_pred('b1')

    try:
        env.step(action)
        assert False, "Action was supposed to be invalid"
    except InvalidAction:
        pass

    assert action not in env.action_space.all_ground_literals(
    ), "Dynamic action space not working"
    env2.reset()
    assert action in env2.action_space.all_ground_literals(
    ), "Dynamic action space not working"

    # Valid args
    action = action_pred('b2')

    obs, _, _, _ = env.step(action)

    assert obs == {
        pred1('b2'),
        pred2('c1'),
        pred2('c2'),
        pred3('b2', 'd2', 'c2'),
        pred3('a1', 'c1', 'd1'),
        pred3('a2', 'c2', 'd2')
    }

    print("Test passed.")
Пример #6
0
def test_pddlenv_hierarchical_types():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    domain_file = os.path.join(dir_path, 'pddl',
                               'hierarchical_type_test_domain.pddl')
    problem_dir = os.path.join(dir_path, 'pddl',
                               'hierarchical_type_test_domain')

    env = PDDLEnv(domain_file, problem_dir)
    obs, _ = env.reset()

    ispresent = Predicate("ispresent", 1, [Type("entity")])
    islight = Predicate("islight", 1, [Type("object")])
    isfurry = Predicate("isfurry", 1, [Type("animal")])
    ishappy = Predicate("ishappy", 1, [Type("animal")])
    pet = Predicate("pet", 1, [Type("animal")])

    nomsy = Type("jindo")("nomsy")
    rover = Type("corgi")("rover")
    rene = Type("cat")("rene")
    block1 = Type("block")("block1")
    block2 = Type("block")("block2")
    cylinder1 = Type("cylinder")("cylinder1")

    assert obs.literals == frozenset({
        ispresent(nomsy),
        ispresent(rover),
        ispresent(rene),
        ispresent(block1),
        ispresent(block2),
        ispresent(cylinder1),
        islight(block1),
        islight(cylinder1),
        isfurry(nomsy),
    })

    obs, _, _, _ = env.step(pet('block1'))

    assert obs.literals == frozenset({
        ispresent(nomsy),
        ispresent(rover),
        ispresent(rene),
        ispresent(block1),
        ispresent(block2),
        ispresent(cylinder1),
        islight(block1),
        islight(cylinder1),
        isfurry(nomsy),
    })

    obs, _, _, _ = env.step(pet(nomsy))

    assert obs.literals == frozenset({
        ispresent(nomsy),
        ispresent(rover),
        ispresent(rene),
        ispresent(block1),
        ispresent(block2),
        ispresent(cylinder1),
        islight(block1),
        islight(cylinder1),
        isfurry(nomsy),
        ishappy(nomsy)
    })

    print("Test passed.")
Пример #7
0
def test_get_all_possible_transitions_multiple_effects():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    domain_file = os.path.join(dir_path, 'pddl',
                               'test_probabilistic_domain_alt_2.pddl')
    problem_dir = os.path.join(dir_path, 'pddl',
                               'test_probabilistic_domain_alt_2')

    env = PDDLEnv(domain_file,
                  problem_dir,
                  raise_error_on_invalid_action=True,
                  dynamic_action_space=True)

    obs, _ = env.reset()
    action = env.action_space.all_ground_literals(obs).pop()
    transitions = env.get_all_possible_transitions(action)

    transition_list = list(transitions)
    assert len(transition_list) == 3

    states = set({
        transition_list[0][0].literals, transition_list[1][0].literals,
        transition_list[2][0].literals
    })

    type1 = Type('type1')
    type2 = Type('type2')
    pred1 = Predicate('pred1', 1, [type1])
    pred2 = Predicate('pred2', 1, [type2])
    pred3 = Predicate('pred3', 3, [type1, type2, type2])

    expected_states = set({
        frozenset(
            {pred1('b2'),
             pred3('a1', 'c1', 'd1'),
             pred3('a2', 'c2', 'd2')}),
        frozenset(
            {pred2('c1'),
             pred3('a1', 'c1', 'd1'),
             pred3('a2', 'c2', 'd2')}),
        frozenset({
            pred1('b2'),
            pred2('c1'),
            pred3('a1', 'c1', 'd1'),
            pred3('a2', 'c2', 'd2'),
            pred3('b2', 'd1', 'c1')
        })
    })

    assert states == expected_states

    # Now test again with return_probs=True.
    transitions = env.get_all_possible_transitions(action, return_probs=True)

    transition_list = list(transitions)
    assert len(transition_list) == 3
    states_and_probs = {
        transition_list[0][0][0].literals: transition_list[0][1],
        transition_list[1][0][0].literals: transition_list[1][1],
        transition_list[2][0][0].literals: transition_list[2][1]
    }

    type1 = Type('type1')
    type2 = Type('type2')
    pred1 = Predicate('pred1', 1, [type1])
    pred2 = Predicate('pred2', 1, [type2])
    pred3 = Predicate('pred3', 3, [type1, type2, type2])

    expected_states = {
        frozenset({
            pred1('b2'),
            pred3('a1', 'c1', 'd1'),
            pred3('a2', 'c2', 'd2')
        }):
        0.5,
        frozenset({
            pred2('c1'),
            pred3('a1', 'c1', 'd1'),
            pred3('a2', 'c2', 'd2')
        }):
        0.4,
        frozenset({
            pred1('b2'),
            pred2('c1'),
            pred3('a1', 'c1', 'd1'),
            pred3('a2', 'c2', 'd2'),
            pred3('b2', 'd1', 'c1')
        }):
        0.1
    }

    for s, prob in states_and_probs.items():
        assert s in expected_states
        assert prob - expected_states[s] < 1e-5

    print("Test passed.")
Пример #8
0
def test_get_all_possible_transitions():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    domain_file = os.path.join(dir_path, 'pddl',
                               'test_probabilistic_domain.pddl')
    problem_dir = os.path.join(dir_path, 'pddl', 'test_probabilistic_domain')

    env = PDDLEnv(domain_file,
                  problem_dir,
                  raise_error_on_invalid_action=True,
                  dynamic_action_space=True)

    obs, _ = env.reset()
    action = env.action_space.all_ground_literals(obs).pop()
    transitions = env.get_all_possible_transitions(action)

    transition_list = list(transitions)
    assert len(transition_list) == 2
    state1, state2 = transition_list[0][0], transition_list[1][0]

    type1 = Type('type1')
    type2 = Type('type2')
    pred1 = Predicate('pred1', 1, [type1])
    pred2 = Predicate('pred2', 1, [type2])
    pred3 = Predicate('pred3', 3, [type1, type2, type2])

    state1, state2 = (state1,
                      state2) if pred2('c1') in state2.literals else (state2,
                                                                      state1)

    assert state1.literals == frozenset({
        pred1('b2'),
        pred3('a1', 'c1', 'd1'),
        pred3('a2', 'c2', 'd2'),
        pred3('b2', 'd1', 'c1')
    })
    assert state2.literals == frozenset({
        pred1('b2'),
        pred2('c1'),
        pred3('a1', 'c1', 'd1'),
        pred3('a2', 'c2', 'd2'),
        pred3('b2', 'd1', 'c1')
    })

    # Now test again with return_probs=True.
    transitions = env.get_all_possible_transitions(action, return_probs=True)

    transition_list = list(transitions)
    assert len(transition_list) == 2
    assert abs(transition_list[0][1] -
               0.3) < 1e-5 or abs(transition_list[0][1] - 0.7) < 1e-5
    assert abs(transition_list[1][1] -
               0.3) < 1e-5 or abs(transition_list[1][1] - 0.7) < 1e-5
    assert abs(transition_list[0][1] - transition_list[1][1]) > 0.3
    state1, state2 = transition_list[0][0][0], transition_list[1][0][0]

    type1 = Type('type1')
    type2 = Type('type2')
    pred1 = Predicate('pred1', 1, [type1])
    pred2 = Predicate('pred2', 1, [type2])
    pred3 = Predicate('pred3', 3, [type1, type2, type2])

    state1, state2 = (state1,
                      state2) if pred2('c1') in state2.literals else (state2,
                                                                      state1)

    assert state1.literals == frozenset({
        pred1('b2'),
        pred3('a1', 'c1', 'd1'),
        pred3('a2', 'c2', 'd2'),
        pred3('b2', 'd1', 'c1')
    })
    assert state2.literals == frozenset({
        pred1('b2'),
        pred2('c1'),
        pred3('a1', 'c1', 'd1'),
        pred3('a2', 'c2', 'd2'),
        pred3('b2', 'd1', 'c1')
    })

    print("Test passed.")
Пример #9
0
def test_heuristic():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    domain_file = os.path.join(dir_path, 'pddl', 'easyblocks.pddl')
    problem_dir = os.path.join(dir_path, 'pddl', 'easyblocks')

    block = Type("block")
    pickup = Predicate("pickup", 1, [block])
    stack = Predicate("stack", 2, [block, block])

    shaped_env = PDDLEnv(
        domain_file,
        problem_dir,
        shape_reward_mode="lmcut",
        raise_error_on_invalid_action=True,
    )
    env = PDDLEnv(
        domain_file,
        problem_dir,
        shape_reward_mode=None,
        raise_error_on_invalid_action=True,
    )
    shaped_env.reset()
    env.reset()

    _, rew, _, _ = env.step(pickup("b"))
    _, shaped_rew, _, _ = shaped_env.step(pickup("b"))

    assert rew == 0.
    assert shaped_rew == 1.
    intermediate_state = env.get_state()

    def assert_last_step():
        _, rew, done, _ = env.step(stack("b", "a"))
        _, shaped_rew, shaped_done, _ = shaped_env.step(stack("b", "a"))
        assert done
        assert shaped_done
        assert rew == 1.
        assert shaped_rew == 2.

    # check if the step to the goal is terminal and with correct rewards
    assert_last_step()

    # check if shaped reward is consistent after setting the state
    shaped_env.set_state(intermediate_state)
    env.set_state(intermediate_state)
    assert_last_step()

    print("Test passed.")