Exemplo n.º 1
0
def test_basic_representation_queries():
    lang = generate_small_fstrips_bw_language(nblocks=5)
    clear, loc, b1, b2, b3 = lang.get('clear', 'loc', 'b1', 'b2', 'b3')
    x = lang.variable('x', lang.ns.block)

    assert rep.is_function_free(b1)
    assert rep.is_function_free(clear(b1))
    assert rep.is_function_free((clear(b1) & clear(b2)) | clear(b3))
    assert rep.is_function_free(exists(x, clear(x)))
    assert not rep.is_function_free(loc(b1) == b2)

    assert rep.is_conjunction_of_literals(clear(b1))
    assert rep.is_conjunction_of_literals(~clear(b1))
    assert rep.is_conjunction_of_literals(~~clear(b1))

    assert rep.is_conjunction_of_literals(clear(b1) & clear(b2))
    assert rep.is_conjunction_of_literals(clear(b1) & ~clear(b2))
    assert rep.is_conjunction_of_literals(clear(b1) & ~~clear(b2))

    assert rep.is_conjunction_of_literals(loc(b1) == b2)
    assert rep.is_conjunction_of_literals(loc(b1) != b2)
    assert rep.is_conjunction_of_literals(~(loc(b1) == b2))

    assert not rep.is_conjunction_of_literals(~(clear(b1) & clear(b2)))
    assert not rep.is_conjunction_of_literals((clear(b1) & clear(b2))
                                              | clear(b3))
    assert not rep.is_conjunction_of_literals(exists(x, clear(x)))
Exemplo n.º 2
0
def create_small_bw_task():
    lang = generate_small_fstrips_bw_language()
    init = tarski.model.create(lang)

    b1, b2, b3, b4, clear, loc, table = lang.get('b1', 'b2', 'b3', 'b4',
                                                 'clear', 'loc', 'table')
    block, place = lang.get('block', 'place')

    init.set(loc, b1, b2)  # loc(b1) := b2
    init.set(loc, b2, b3)  # loc(b2) := b3
    init.set(loc, b3, table)  # loc(b3) := table
    init.set(loc, b4, table)  # loc(b4) := table

    init.add(clear, b1)  # clear(b1)
    init.add(clear, b4)  # clear(b4)
    init.add(clear, table)  # clear(table)

    src = lang.variable('src', block)
    dest = lang.variable('dest', place)

    x = lang.variable('x', block)
    y = lang.variable('y', block)
    clear_constraint = forall(
        x, equiv(neg(clear(x)), land(x != table, exists(y,
                                                        loc(y) == x))))
    G = land(loc(b1) == b2, loc(b2) == b3, loc(b3) == b4, loc(b4) == table)

    problem = fs.Problem("tower4", "blocksworld")
    problem.language = lang
    problem.init = init
    problem.goal = G
    problem.constraints += [clear_constraint]
    problem.action('move', [src, dest], land(clear(src), clear(dest)),
                   [fs.FunctionalEffect(loc(src), dest)])
    return problem
Exemplo n.º 3
0
def test_lp_compilation():
    problem = create_sample_problem()
    lang = problem.language

    x, y, z = [lang.variable(x, lang.Object) for x in ["x", "y", "z"]]
    room, ball, at_robby, free, at, gripper, carry = lang.get(
        "room", "ball", "at-robby", "free", "at", "gripper", "carry")

    # The conjunction results in a rule body "room(X), room(Y)", with both variable names capitalized, and room prefixed
    # with "atom" to prevent name clashes
    a1, a2 = create_compiler(problem).process_formula(room(x) & room(y))
    assert isinstance(
        a1, LPAtom) and a1.symbol == a2.symbol == "atom_room" and a1.args == [
            "X"
        ] and a2.args == ["Y"]

    # A simple built-in equality atom results in an LPAtom
    a1, = create_compiler(problem).process_formula(x == y)
    assert isinstance(a1,
                      LPAtom) and a1.symbol == "=" and a1.args == ["X", "Y"]

    # A disjunction results in an auxiliary atom plus two rules
    c = create_compiler(problem)
    a1, = c.process_formula(room(x) | (room(y) & room(z)))
    assert isinstance(a1, LPAtom) and len(a1.args) == 3 and c.lp.nrules() == 2

    # A nested conjunction with two subatoms
    a1, a2 = create_compiler(problem).process_formula(
        room(x) & (room(y) | room(z)))
    assert str(a1) == "atom_room(X)" and "__f" in str(a2)

    # An existentially quantified formula
    c = create_compiler(problem)
    a1, = c.process_formula(exists(x, y, room(x) & room(y)))
    assert str(a1) == "__f1()" and c.lp.nrules() == 1
Exemplo n.º 4
0
def test_simplification_of_ex_quantification():
    problem = generate_fstrips_counters_problem(ncounters=3)
    lang = problem.language
    value, max_int, counter, val_t, c1 = lang.get('value', 'max_int',
                                                  'counter', 'val', 'c1')
    x = lang.variable('x', counter)
    z = lang.variable('z', counter)
    two, three, six = [lang.constant(c, val_t) for c in (2, 3, 6)]

    phi = exists(z, land(x == z, top, value(z) < six))
    assert simplify_existential_quantification(phi, inplace=False) == land(top, value(x) < six), \
        "z has been replaced by x and removed from the quantification list, thus removing the quantifier"

    phi = exists(x, z, land(x == z, z == x, value(z) < six, flat=True))
    assert simplify_existential_quantification(phi, inplace=False) == exists(x, value(x) < six), \
        "The circular substitution dependency has been treated appropriately and only one substitution performed"
Exemplo n.º 5
0
def test_literal_collection():
    lang = generate_small_fstrips_bw_language(nblocks=5)
    clear, loc, b1, b2, b3 = lang.get('clear', 'loc', 'b1', 'b2', 'b3')
    x = lang.variable('x', lang.ns.block)

    assert rep.collect_literals_from_conjunction(clear(b1)) == {(clear(b1),
                                                                 True)}
    assert rep.collect_literals_from_conjunction(~clear(b1)) == {(clear(b1),
                                                                  False)}
    assert len(rep.collect_literals_from_conjunction(clear(b1)
                                                     & ~clear(b2))) == 2
    assert len(
        rep.collect_literals_from_conjunction(
            land(clear(b1), clear(b2), clear(b3)))) == 3

    assert len(
        rep.collect_literals_from_conjunction(clear(x) & clear(b1)
                                              & clear(x))) == 2

    # These ones are not conjunctions of literals, so should return None
    assert rep.collect_literals_from_conjunction(~(clear(b1)
                                                   & clear(b2))) is None
    assert rep.collect_literals_from_conjunction((clear(b1) & clear(b2))
                                                 | clear(b3)) is None
    assert rep.collect_literals_from_conjunction(exists(x, clear(x))) is None

    # ATM we don't want to deal with the complexity of nested negation, so we expect the method to return None for
    # "not not clear(b2)"
    assert rep.collect_literals_from_conjunction(clear(b1)
                                                 & ~~clear(b2)) is None
Exemplo n.º 6
0
def test_variables_classification():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    s = neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y)))))
    free = free_variables(s)
    assert len(free) == 1 and symref(free[0]) == symref(x)
    assert len(all_variables(s)) == 2
Exemplo n.º 7
0
def create_single_action_version(problem):
    lang = problem.language
    cell_t, at, reward, unblocked, picked, adjacent = lang.get("cell", "at", "reward", "unblocked", "picked", "adjacent")
    from_ = lang.variable("from", cell_t)
    to = lang.variable("to", cell_t)
    c = lang.variable("c", cell_t)
    problem.action(name='move', parameters=[from_, to],
                   precondition=land(adjacent(from_, to), at(from_), unblocked(to), exists(c, reward(c)), flat=True),
                   effects=[DelEffect(at(from_)),
                            AddEffect(at(to)),
                            # AddEffect(visited(to)),
                            DelEffect(reward(to))])
Exemplo n.º 8
0
def test_formula_writing1():
    problem, loc, clear, b1, table = get_bw_elements()
    lang = problem.language

    assert print_formula(clear(b1)) == "(clear b1)"
    assert print_formula(loc(b1) == table) == "(= (loc b1) table)"
    assert print_formula(loc(b1) != table) == "(not (= (loc b1) table))"

    assert print_formula(clear(b1) | clear(table)) == "(or (clear b1) (clear table))"

    b = lang.variable('B', lang.ns.block)
    assert print_formula(forall(b, clear(b))) == "(forall (?B - block) (clear ?B))"
    assert print_formula(exists(b, clear(b))) == "(exists (?B - block) (clear ?B))"
Exemplo n.º 9
0
    def compute_gamma(self, ml, symbol, idx):
        tvar = _get_timestep_var(ml)

        disjuncts = []
        for act, eff in idx[symbol.name]:
            action_binding = generate_action_arguments(ml, act)
            action_happens_at_t = ml.get_predicate(act.name)(*action_binding,
                                                             tvar)
            effcond = self.to_metalang(eff.condition, tvar)
            gamma_binding = self.compute_gamma_binding(ml, eff, symbol)

            gamma_act = land(action_happens_at_t,
                             effcond,
                             *gamma_binding,
                             flat=True)
            if action_binding:  # exist-quantify the action parameters other than the timestep t
                gamma_act = exists(*action_binding, gamma_act)

            # We chain a couple of simplifications of the original gamma expression
            gamma_act = Simplify().simplify_expression(
                simplify_existential_quantification(gamma_act))

            disjuncts.append(gamma_act)
        return lor(*disjuncts, flat=True)
Exemplo n.º 10
0
def test_detect_free_variables():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    s = neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y)))))
    assert len(free_variables(s)) == 1