Beispiel #1
0
def test_have_cake_and_eat_cake_too():
    p = have_cake_and_eat_cake_too()
    assert p.goal_test() is False
    solution = [expr("Eat(Cake)"), expr("Bake(Cake)")]

    for action in solution:
        p.act(action)

    assert p.goal_test()
def test_extend_example():
    assert list(test_network.extend_example({x: A, y: B}, expr('Conn(x, z)'))) == [
        {x: A, y: B, z: B}, {x: A, y: B, z: D}]
    assert list(test_network.extend_example({x: G}, expr('Conn(x, y)'))) == [{x: G, y: I}]
    assert list(test_network.extend_example({x: C}, expr('Conn(x, y)'))) == []
    assert len(list(test_network.extend_example({}, expr('Conn(x, y)')))) == 10
    assert len(list(small_family.extend_example({x: expr('Andrew')}, expr('Father(x, y)')))) == 2
    assert len(list(small_family.extend_example({x: expr('Andrew')}, expr('Mother(x, y)')))) == 0
    assert len(list(small_family.extend_example({x: expr('Andrew')}, expr('Female(y)')))) == 6
def test_new_clause():
    target = expr('Open(x, y)')
    examples_pos = [{x: B}, {x: A}, {x: G}]
    examples_neg = [{x: C}, {x: F}, {x: I}]
    clause = test_network.new_clause([examples_pos, examples_neg], target)[0][1]
    assert len(clause) == 1 and clause[0].op == 'Conn' and clause[0].args[0] == x
    target = expr('Flow(x, y)')
    examples_pos = [{x: B}, {x: D}, {x: E}, {x: G}]
    examples_neg = [{x: A}, {x: C}, {x: F}, {x: I}, {x: H}]
    clause = test_network.new_clause([examples_pos, examples_neg], target)[0][1]
    assert len(clause) == 2 and \
        ((clause[0].args[0] == x and clause[1].args[1] == x) or \
        (clause[0].args[1] == x and clause[1].args[0] == x))
Beispiel #4
0
def test_three_block_tower():
    p = three_block_tower()
    assert p.goal_test() is False
    solution = [
        expr("MoveToTable(C, A)"),
        expr("Move(B, Table, C)"),
        expr("Move(A, Table, B)")
    ]

    for action in solution:
        p.act(action)

    assert p.goal_test()
Beispiel #5
0
def test_spare_tire():
    p = spare_tire()
    assert p.goal_test() is False
    solution = [
        expr("Remove(Flat, Axle)"),
        expr("Remove(Spare, Trunk)"),
        expr("PutOn(Spare, Axle)")
    ]

    for action in solution:
        p.act(action)

    assert p.goal_test()
Beispiel #6
0
def test_graph_call():
    pddl = spare_tire()
    negkb = FolKB([expr('At(Flat, Trunk)')])
    graph = Graph(pddl, negkb)

    levels_size = len(graph.levels)
    graph()

    assert levels_size == len(graph.levels) - 1
Beispiel #7
0
def test_air_cargo_2():
    p = air_cargo()
    assert p.goal_test() is False
    solution_2 = [
        expr("Load(C2, P2, JFK)"),
        expr("Fly(P2, JFK, SFO)"),
        expr("Unload (C2, P2, SFO)"),
        expr("Load(C1 , P1, SFO)"),
        expr("Fly(P1, SFO, JFK)"),
        expr("Unload(C1, P1, JFK)")
    ]

    for action in solution_2:
        p.act(action)

    assert p.goal_test()
def test_choose_literal():
    literals = [expr('Conn(p, q)'), expr('Conn(x, z)'), expr('Conn(r, s)'), expr('Conn(t, y)')]
    examples_pos = [{x: A, y: B}, {x: A, y: D}]
    examples_neg = [{x: A, y: C}, {x: C, y: A}, {x: C, y: B}, {x: A, y: I}]
    assert test_network.choose_literal(literals, [examples_pos, examples_neg]) == expr('Conn(x, z)')
    literals = [expr('Conn(x, p)'), expr('Conn(p, x)'), expr('Conn(p, q)')]
    examples_pos = [{x: C}, {x: F}, {x: I}]
    examples_neg = [{x: D}, {x: A}, {x: B}, {x: G}]
    assert test_network.choose_literal(literals, [examples_pos, examples_neg]) == expr('Conn(p, x)')
    literals = [expr('Father(x, y)'), expr('Father(y, x)'), expr('Mother(x, y)'), expr('Mother(x, y)')]
    examples_pos = [{x: expr('Philip')}, {x: expr('Mark')}, {x: expr('Peter')}]
    examples_neg = [{x: expr('Elizabeth')}, {x: expr('Sarah')}]
    assert small_family.choose_literal(literals, [examples_pos, examples_neg]) == expr('Father(x, y)')
    literals = [expr('Father(x, y)'), expr('Father(y, x)'), expr('Male(x)')]
    examples_pos = [{x: expr('Philip')}, {x: expr('Mark')}, {x: expr('Andrew')}]
    examples_neg = [{x: expr('Elizabeth')}, {x: expr('Sarah')}]
    assert small_family.choose_literal(literals, [examples_pos, examples_neg]) == expr('Male(x)')
def test_new_literals():
    assert len(list(test_network.new_literals([expr('p | q'), [expr('p')]]))) == 8
    assert len(list(test_network.new_literals([expr('p'), [expr('q'), expr('p | r')]]))) == 15
    assert len(list(small_family.new_literals([expr('p'), []]))) == 8
    assert len(list(small_family.new_literals([expr('p & q'), []]))) == 20
def test_foil():
    target = expr('Reach(x, y)')
    examples_pos = [{x: A, y: B},
                    {x: A, y: C},
                    {x: A, y: D},
                    {x: A, y: E},
                    {x: A, y: F},
                    {x: A, y: G},
                    {x: A, y: I},
                    {x: B, y: C},
                    {x: D, y: C},
                    {x: D, y: E},
                    {x: D, y: F},
                    {x: D, y: G},
                    {x: D, y: I},
                    {x: E, y: F},
                    {x: E, y: G},
                    {x: E, y: I},
                    {x: G, y: I},
                    {x: H, y: G},
                    {x: H, y: I}]
    nodes = {A, B, C, D, E, F, G, H, I}
    examples_neg = [example for example in [{x: a, y: b} for a in nodes for b in nodes]
                    if example not in examples_pos]
    ## TODO: Modify FOIL to recursively check for satisfied positive examples
#    clauses = test_network.foil([examples_pos, examples_neg], target)
#    assert len(clauses) == 2
    target = expr('Parent(x, y)')
    examples_pos = [{x: expr('Elizabeth'), y: expr('Anne')},
                    {x: expr('Elizabeth'), y: expr('Andrew')},
                    {x: expr('Philip'), y: expr('Anne')},
                    {x: expr('Philip'), y: expr('Andrew')},
                    {x: expr('Anne'), y: expr('Peter')},
                    {x: expr('Anne'), y: expr('Zara')},
                    {x: expr('Mark'), y: expr('Peter')},
                    {x: expr('Mark'), y: expr('Zara')},
                    {x: expr('Andrew'), y: expr('Beatrice')},
                    {x: expr('Andrew'), y: expr('Eugenie')},
                    {x: expr('Sarah'), y: expr('Beatrice')},
                    {x: expr('Sarah'), y: expr('Eugenie')}]
    examples_neg = [{x: expr('Anne'), y: expr('Eugenie')},
                    {x: expr('Beatrice'), y: expr('Eugenie')},
                    {x: expr('Mark'), y: expr('Elizabeth')},
                    {x: expr('Beatrice'), y: expr('Philip')}]
    clauses = small_family.foil([examples_pos, examples_neg], target)
    assert len(clauses) == 2 and \
        ((clauses[0][1][0] == expr('Father(x, y)') and clauses[1][1][0] == expr('Mother(x, y)')) or \
        (clauses[1][1][0] == expr('Father(x, y)') and clauses[0][1][0] == expr('Mother(x, y)')))
    target = expr('Grandparent(x, y)')
    examples_pos = [{x: expr('Elizabeth'), y: expr('Peter')},
                    {x: expr('Elizabeth'), y: expr('Zara')},
                    {x: expr('Elizabeth'), y: expr('Beatrice')},
                    {x: expr('Elizabeth'), y: expr('Eugenie')},
                    {x: expr('Philip'), y: expr('Peter')},
                    {x: expr('Philip'), y: expr('Zara')},
                    {x: expr('Philip'), y: expr('Beatrice')},
                    {x: expr('Philip'), y: expr('Eugenie')}]
    examples_neg = [{x: expr('Anne'), y: expr('Eugenie')},
                    {x: expr('Beatrice'), y: expr('Eugenie')},
                    {x: expr('Elizabeth'), y: expr('Andrew')},
                    {x: expr('Philip'), y: expr('Anne')},
                    {x: expr('Philip'), y: expr('Andrew')},
                    {x: expr('Anne'), y: expr('Peter')},
                    {x: expr('Anne'), y: expr('Zara')},
                    {x: expr('Mark'), y: expr('Peter')},
                    {x: expr('Mark'), y: expr('Zara')},
                    {x: expr('Andrew'), y: expr('Beatrice')},
                    {x: expr('Andrew'), y: expr('Eugenie')},
                    {x: expr('Sarah'), y: expr('Beatrice')},
                    {x: expr('Mark'), y: expr('Elizabeth')},
                    {x: expr('Beatrice'), y: expr('Philip')}]
    r_example('No', 'No', 'No', 'No', 'None', '$', 'No', 'No', 'Thai', '0-10', False),
    r_example('Yes', 'Yes', 'Yes', 'Yes', 'Full', '$', 'No', 'No', 'Burger', '30-60', True)
]

"""
A              H
|\            /|
| \          / |
v  v        v  v
B  D-->E-->G-->I
|  /   |
| /    |
vv     v
C      F
"""
test_network = FOIL_container([expr("Conn(A, B)"),
                               expr("Conn(A ,D)"),
                               expr("Conn(B, C)"),
                               expr("Conn(D, C)"),
                               expr("Conn(D, E)"),
                               expr("Conn(E ,F)"),
                               expr("Conn(E, G)"),
                               expr("Conn(G, I)"),
                               expr("Conn(H, G)"),
                               expr("Conn(H, I)")])

small_family = FOIL_container([expr("Mother(Anne, Peter)"),
                               expr("Mother(Anne, Zara)"),
                               expr("Mother(Sarah, Beatrice)"),
                               expr("Mother(Sarah, Eugenie)"),
                               expr("Father(Mark, Peter)"),
Beispiel #12
0
def test_action():
    precond = [[expr("P(x)"), expr("Q(y, z)")], [expr("Q(x)")]]
    effect = [[expr("Q(x)")], [expr("P(x)")]]
    a = Action(expr("A(x,y,z)"), precond, effect)
    args = [expr("A"), expr("B"), expr("C")]
    assert a.substitute(expr("P(x, z, y)"), args) == expr("P(A, C, B)")
    test_kb = FolKB([expr("P(A)"), expr("Q(B, C)"), expr("R(D)")])
    assert a.check_precond(test_kb, args)
    a.act(test_kb, args)
    assert test_kb.ask(expr("P(A)")) is False
    assert test_kb.ask(expr("Q(A)")) is not False
    assert test_kb.ask(expr("Q(B, C)")) is not False
    assert not a.check_precond(test_kb, args)
Beispiel #13
0
 def goal_test(kb):
     return kb.ask(expr('At(SFO)'))
Beispiel #14
0
def test_refinements():
    init = [expr('At(Home)')]

    def goal_test(kb):
        return kb.ask(expr('At(SFO)'))

    library = {
        "HLA": ["Go(Home,SFO)", "Taxi(Home, SFO)"],
        "steps": [["Taxi(Home, SFO)"], []],
        "precond_pos": [["At(Home)"], ["At(Home)"]],
        "precond_neg": [[], []],
        "effect_pos": [["At(SFO)"], ["At(SFO)"]],
        "effect_neg": [
            ["At(Home)"],
            ["At(Home)"],
        ]
    }
    # Go SFO
    precond_pos = [expr("At(Home)")]
    precond_neg = []
    effect_add = [expr("At(SFO)")]
    effect_rem = [expr("At(Home)")]
    go_SFO = HLA(expr("Go(Home,SFO)"), [precond_pos, precond_neg],
                 [effect_add, effect_rem])
    # Taxi SFO
    precond_pos = [expr("At(Home)")]
    precond_neg = []
    effect_add = [expr("At(SFO)")]
    effect_rem = [expr("At(Home)")]
    taxi_SFO = HLA(expr("Go(Home,SFO)"), [precond_pos, precond_neg],
                   [effect_add, effect_rem])
    prob = Problem(init, [go_SFO, taxi_SFO], goal_test)
    result = [i for i in Problem.refinements(go_SFO, prob, library)]
    assert (len(result) == 1)
    assert (result[0].name == "Taxi")
    assert (result[0].args == (expr("Home"), expr("SFO")))