Exemplo n.º 1
0
def program_template_test():
    rule_template = Rule_Template(1, True)
    try:
        program_template = Program_Template([atom1],
                                            (rule_template, rule_template),
                                            300)
        assert False
    except ValueError as err:
        assert True

    program_template = Program_Template([atom3],
                                        (rule_template, rule_template), 300)
Exemplo n.º 2
0
def even_numbers_negation_test():
    B_atom = [Atom([Term(False, '0')], 'zero')] + \
             [Atom([Term(False, str(i)), Term(False, str(i + 1))], 'succ')
              for i in range(0, 20)]
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [Atom([Term(False, str(i))], 'target') for i in range(0, 21, 2)]
    P = [Literal(atom, False) for atom in P_atom]
    N_atom = [Atom([Term(False, str(i))], 'target') for i in range(1, 21, 2)]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [
        Literal(Atom([term_x_0], 'zero'), False),
        Literal(Atom([term_x_0, term_x_1], 'succ'), False)
    ]
    p_a = [Literal(Atom([term_x_0, term_x_1], 'pred'), False)]
    target = Literal(Atom([term_x_0], 'target'), False)
    constants = [str(i) for i in range(0, 21)]

    # Define rules for intensional predicates
    p_a_rule = (Rule_Template_Negation(1, False, False), None)
    target_rule = (Rule_Template_Negation(0, False, True),
                   Rule_Template_Negation(1, True, False))
    rules = {p_a[0]: p_a_rule, target: target_rule}

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    #program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(langage_frame, B, P, N, program_template)

    return snafdilp.train(steps=300)
Exemplo n.º 3
0
def action_sw(bg_all):
    #num = 70
    #inx=random.sample(range(0,100),num)
    #neg_inx = list(set(range(100))-set(inx))
    B_0, P_up, P_sw, P_stay, N_up, N_sw, N_stay = get_B_N_P(bg_all)
    B = [Atom([Term(False, str(x[1]))], x[0]) for x in B_0]
    P = [Atom([Term(False, str(id))], 'sw') for id in P_sw]
    N = [Atom([Term(False, str(id))], 'sw') for id in N_sw]
    term_x_0 = Term(True, 'X')
    term_x_1 = Term(True, 'X1')
    p_e = [
        Atom([term_x_0], 'busy'),
        Atom([term_x_0], 'NObusy'),
        Atom([term_x_0], 'side'),
        Atom([term_x_0], 'front'),
        Atom([term_x_0], 'back')
    ]
    p_a = []
    target = Atom([term_x_0], 'sw')
    # target_rule = (Rule_Template(0, False), Rule_Template(1, True))
    target_rule = (Rule_Template(0, False), None)
    rules = {target: target_rule}
    constants = [str(i) for i in range(0, 400)]

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    dilp = DILP(langage_frame, B, P, N, program_template)
    loss, cl_set = dilp.train(steps=51)
    return loss, cl_set
Exemplo n.º 4
0
def even_numbers_test():
    B = [Atom([Term(False, '0')], 'zero')] + \
        [Atom([Term(False, str(i)), Term(False, str(i + 1))], 'succ')
         for i in range(0, 20)]

    P = [Atom([Term(False, str(i))], 'target') for i in range(0, 21, 2)]
    N = [Atom([Term(False, str(i))], 'target') for i in range(1, 21, 2)]
    print(P)
    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [Atom([term_x_0], 'zero'), Atom([term_x_0, term_x_1], 'succ')]
    p_a = [Atom([term_x_0, term_x_1], 'pred')]
    target = Atom([term_x_0], 'target')
    constants = [str(i) for i in range(0, 21)]

    # Define rules for intensional predicates
    p_a_rule = (Rule_Template(1, False), None)
    target_rule = (Rule_Template(0, False), Rule_Template(1, True))
    rules = {p_a[0]: p_a_rule, target: target_rule}

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    #program_template = Program_Template(p_a, rules, 300)

    dilp = DILP(langage_frame, B, P, N, program_template)
Exemplo n.º 5
0
def generate_ground_atoms_test():
    term1 = Term(True, 'X_0')
    term2 = Term(True, 'X_1')
    target = Atom([term1, term2], 'r')
    p_e = [Atom([term1, term2], 'p')]
    p_a = [Atom([term1, term2], 'q')]

    rule_template_1 = Rule_Template(0, False)
    rule_template_2 = Rule_Template(1, True)

    language_frame = Language_Frame(target, p_e, set(['a', 'b']))
    program_template = Program_Template(p_a,
                                        (rule_template_1, rule_template_2),
                                        300)

    term_a = Term(False, 'a')
    term_b = Term(False, 'b')
    # Background facts: p(a,a) p(a,b)
    background = [Atom([term_a, term_a], 'p'), Atom([term_a, term_b], 'p')]
    # Positive: p(b,a)
    positive = [Atom([term_b, term_a], 'p')]
    # Negative: p(b,b)
    negative = [Atom([term_b, term_b], 'p')]
    ilp = ILP(language_frame, background, positive, negative, program_template)
    initial_valuation = ilp.convert()
    for valuation in initial_valuation:
        if valuation[0] in background:
            assert valuation[1] == 1
        else:
            assert valuation[1] == 0
Exemplo n.º 6
0
def action_up_2():
    B_0, P_up, P_sw, P_stay, N_up, N_sw, N_stay = get_B_N_P()
    B = [Atom([Term(False, str(x[1]))], x[0]) for x in B_0]

    N = [Atom([Term(False, str(id))], 'up') for id in P_up]
    P = [Atom([Term(False, str(id))], 'up') for id in N_up]
    term_x_0 = Term(True, 'X')
    term_x_1 = Term(True, 'X1')
    p_e = [
        Atom([term_x_0], 'busy'),
        Atom([term_x_0], 'NObusy'),
        Atom([term_x_0], 'side'),
        Atom([term_x_0], 'front'),
        Atom([term_x_0], 'back')
    ]
    p_a = []

    target = Atom([term_x_0], 'up')
    # target_rule = (Rule_Template(0, False), Rule_Template(1, True))
    target_rule = (Rule_Template(0, False), None)
    rules = {target: target_rule}
    constants = [str(i) for i in range(0, 400)]

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    dilp = DILP(langage_frame, B, P, N, program_template)
    dilp.train(steps=250)
Exemplo n.º 7
0
def action_switch_2():
    num = 30
    inx = random.sample(range(0, 100), num)
    neg_inx = list(set(range(100)) - set(inx))
    B = [Atom([Term(False, str(id))], 'busy') for id in inx
         ] + [Atom([Term(False, str(id))], 'NObusy') for id in neg_inx] + [
             Atom([Term(False, str(id))], 'front')
             for id in neg_inx if id % 2 == 0
         ] + [
             Atom([Term(False, str(id))], 'back')
             for id in neg_inx if id % 2 != 0
         ]
    P = [Atom([Term(False, str(id))], 'switch') for id in neg_inx]
    N = [Atom([Term(False, str(id))], 'switch') for id in inx]
    term_x_0 = Term(True, 'X')
    term_x_1 = Term(True, 'X1')
    p_e = [
        Atom([term_x_0], 'busy'),
        Atom([term_x_0], 'NObusy'),
        Atom([term_x_0], 'side'),
        Atom([term_x_0], 'front'),
        Atom([term_x_0], 'back')
    ]
    p_a = []
    target = Atom([term_x_0], 'switch')
    # target_rule = (Rule_Template(0, False), Rule_Template(1, True))
    target_rule = (Rule_Template(0, False), None)
    rules = {target: target_rule}
    constants = [str(i) for i in range(0, 100)]

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    dilp = DILP(langage_frame, B, P, N, program_template)
    dilp.train(steps=252)
Exemplo n.º 8
0
def less_than():
    B = [Atom([Term(False, '0')], 'zero')] + \
        [Atom([Term(False, str(i)), Term(False, str(i + 1))], 'succ')
         for i in range(0, 9)]

    P = []
    N = []
    for i in range(0, 10):
        for j in range(0, 10):
            if j >= i:
                N.append(
                    Atom([Term(False, str(j)), Term(False, str(i))], 'target'))
            else:
                P.append(
                    Atom([Term(False, str(j)), Term(False, str(i))], 'target'))

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [Atom([term_x_0], 'zero'), Atom([term_x_0, term_x_1], 'succ')]
    p_a = []

    target = Atom([term_x_0, term_x_1], 'target')
    # target_rule = (Rule_Template(0, False), Rule_Template(1, True))
    target_rule = (Rule_Template(0, False), Rule_Template(1, True))
    rules = {target: target_rule}
    constants = [str(i) for i in range(0, 10)]

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    dilp = DILP(langage_frame, B, P, N, program_template)
    dilp.train()
Exemplo n.º 9
0
def innocent():
    '''
    Learn the target predicate of innocent(X)
    which is true if X is not guilty
    '''
    constants = [
        'Paul', 'Randy', 'Rachel', 'Bob', 'Alice', 'Stan', 'Kyle', 'Peter',
        'Tony', 'Monica'
    ]
    B_atom = [
        Atom([Term(False, 'Bob')], 'guilty'),
        Atom([Term(False, 'Randy')], 'guilty'),
        Atom([Term(False, 'Peter')], 'guilty'),
        Atom([Term(False, 'Alice')], 'guilty'),
        Atom([Term(False, 'Monica')], 'guilty')
    ]
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [
        Atom([Term(False, 'Paul')], 'target'),
        Atom([Term(False, 'Rachel')], 'target'),
        Atom([Term(False, 'Kyle')], 'target'),
        Atom([Term(False, 'Tony')], 'target'),
        Atom([Term(False, 'Stan')], 'target')
    ]
    P = [Literal(atom, False) for atom in P_atom]

    N_atom = [
        Atom([Term(False, 'Bob')], 'target'),
        Atom([Term(False, 'Randy')], 'target'),
        Atom([Term(False, 'Peter')], 'target'),
        Atom([Term(False, 'Alice')], 'target'),
        Atom([Term(False, 'Monica')], 'target')
    ]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')

    p_e = [Literal(Atom([term_x_0], 'guilty'), False)]
    p_a = []
    target = Literal(Atom([term_x_0], 'target'), False)

    # Define rules for intensional predicates
    p_a_rule = (None, None)
    target_rule = (Rule_Template_Negation(0, False, True), None)
    rules = {target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(language_frame, B, P, N, program_template)
    return snafdilp.train(steps=200)
Exemplo n.º 10
0
def is_cyclic():
    '''
    Learn the target predicate no_negative_cycle(X). The predicate checks if
    a given node in a graph is part of a negative cycle (cycle with at least one
    negative edge).
    '''
    constants = ['a', 'b', 'c', 'd', 'e', 'f']
    ### (B, P, N) Number 1
    B_atom = [
        Atom([Term(False, 'a'), Term(False, 'b')], 'edge'),
        Atom([Term(False, 'b'), Term(False, 'c')], 'edge'),
        Atom([Term(False, 'c'), Term(False, 'a')], 'edge'),
        Atom([Term(False, 'b'), Term(False, 'd')], 'edge'),
        Atom([Term(False, 'd'), Term(False, 'e')], 'edge'),
        Atom([Term(False, 'd'), Term(False, 'f')], 'edge'),
        Atom([Term(False, 'f'), Term(False, 'e')], 'edge'),
        Atom([Term(False, 'e'), Term(False, 'f')], 'edge')
    ]
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [
        Atom([Term(False, 'a')], 'target'),
        Atom([Term(False, 'b')], 'target'),
        Atom([Term(False, 'c')], 'target'),
        Atom([Term(False, 'e')], 'target'),
        Atom([Term(False, 'f')], 'target')
    ]
    P = [Literal(atom, False) for atom in P_atom]

    N_atom = [Atom([Term(False, 'd')], 'target')]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')
    term_x_2 = Term(True, 'X_2')

    p_e = [Literal(Atom([term_x_0, term_x_1], 'edge'), False)]
    p_a = [Literal(Atom([term_x_0, term_x_2], 'pred'), False)]
    target = Literal(Atom([term_x_0], 'target'), False)

    # Define rules for intensional predicates
    p_a_rule = [(Rule_Template_Negation(0, False, False),
                 Rule_Template_Negation(1, True, False))]
    target_rule = (Rule_Template_Negation(0, True, False), None)
    rules = {p_a[0]: p_a_rule[0], target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    dilp = DILP(language_frame, B, P, N, program_template)
    return dilp.train(steps=300)
Exemplo n.º 11
0
def f_c_test2():
    '''Testing f_c function
    '''
    term1 = Term(True, 'X_0')
    term2 = Term(True, 'X_1')
    target = Atom([term1], 'r')
    p_e = [Atom([term1, term2], 'p')]
    p_a = [Atom([term1, term2], 'q')]

    rule_template_1 = Rule_Template(0, False)
    rule_template_2 = Rule_Template(1, True)

    language_frame = Language_Frame(target, p_e, set(['a', 'b']))
    program_template = Program_Template(p_a,
                                        (rule_template_1, rule_template_2),
                                        300)

    term_a = Term(False, 'a')
    term_b = Term(False, 'b')
    # Background facts: p(a,a) p(a,b)
    background = [Atom([term_a, term_a], 'p'), Atom([term_a, term_b], 'p')]
    # Positive: p(b,a)
    positive = [Atom([term_b, term_a], 'p')]
    # Negative: p(b,b)
    negative = [Atom([term_b, term_b], 'p')]
    ilp = ILP(language_frame, background, positive, negative, program_template)
    initial_valuation, valuation_mapping = ilp.convert()

    term_x = Term(True, 'X_0')
    term_y = Term(True, 'X_1')
    term_z = Term(True, 'X_2')
    r = Atom([term_x], 'r')
    p = Atom([term_x, term_z], 'p')
    q = Atom([term_z, term_y], 'q')
    clause = Clause(r, [p, q])

    # Example from the book
    example_val = {
        Atom([term_a, term_a], 'p'): 1.0,
        Atom([term_a, term_b], 'p'): 0.9,
        Atom([term_a, term_a], 'q'): 0.1,
        Atom([term_b, term_a], 'q'): 0.2,
        Atom([term_b, term_b], 'q'): 0.8,
    }
    for key in example_val:
        initial_valuation[valuation_mapping[key]] = example_val[key]
    x_c = Inference.x_c(clause, valuation_mapping, ['a', 'b'])
    print(valuation_mapping)
    print(x_c)
    assert False
Exemplo n.º 12
0
def greater_than():
    '''
    Learn the target predicate greater_than(X,Y) which is true
    if X is greater than Y. The aim is to mirror the less_than
    predicate use of negation as failure.
    '''
    constants = [str(i) for i in range(0, 10)]
    B_atom = [Atom([Term(False, '0')], 'zero')] + \
        [Atom([Term(False, str(i)), Term(False, str(i + 1))], 'succ')
         for i in range(0, 9)]
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = []
    N_atom = []
    for i in range(0, 10):
        for j in range(0, 10):
            if j > i:
                P_atom.append(
                    Atom([Term(False, str(j)),
                          Term(False, str(i))], 'target'))
            else:
                N_atom.append(
                    Atom([Term(False, str(j)),
                          Term(False, str(i))], 'target'))
    P = [Literal(atom, False) for atom in P_atom]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [
        Literal(Atom([term_x_0], 'zero'), False),
        Literal(Atom([term_x_0, term_x_1], 'succ'), False)
    ]
    p_a = [Literal(Atom([term_x_0, term_x_1], 'pred'), False)]
    target = Atom([term_x_0, term_x_1], 'target')

    # Define rules for intensional predicates
    p_a_rule = [(Rule_Template_Negation(0, False, False),
                 Rule_Template_Negation(1, True, False))]
    target_rule = (Rule_Template_Negation(0, True, True), None)
    rules = {p_a[0]: p_a_rule[0], target: target_rule}

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)

    snafdilp = SNAFDILP(langage_frame, B, P, N, program_template)
    return snafdilp.train(steps=500)
Exemplo n.º 13
0
def odd():
    '''
   Learn the target predicate of odd(X)
   which is true if X is an odd number
   '''
    B_atom = [Atom([Term(False, '0')], 'zero')] + \
             [Atom([Term(False, str(i)), Term(False, str(i + 1))], 'succ')
              for i in range(0, 20)]
    B = [Literal(atom, False) for atom in B_atom]
    # Odd numbers
    P_atom = [Atom([Term(False, str(i))], 'target') for i in range(1, 21, 2)]
    P = [Literal(atom, False) for atom in P_atom]
    # Even numbers
    N_atom = [Atom([Term(False, str(i))], 'target') for i in range(0, 21, 2)]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [
        Literal(Atom([term_x_0], 'zero'), False),
        Literal(Atom([term_x_0, term_x_1], 'succ'), False)
    ]
    p_a = [
        Literal(Atom([term_x_0, term_x_1], 'succ2'), False),
        Literal(Atom([term_x_0], 'even'), False)
    ]
    target = Literal(Atom([term_x_0], 'target'), False)
    constants = [str(i) for i in range(0, 21)]

    # Define rules for intensional predicates
    succ2_rule = (Rule_Template_Negation(1, False, False), None)
    even_rule = (Rule_Template_Negation(0, False, False),
                 Rule_Template_Negation(1, True, False))
    target_rule = (Rule_Template_Negation(0, True, True), None)
    rules = {p_a[0]: succ2_rule, p_a[1]: even_rule, target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(language_frame, B, P, N, program_template)
    return snafdilp.train(steps=400)
Exemplo n.º 14
0
def action_switch():
    #num = 70
    #inx=random.sample(range(0,100),num)
    #neg_inx = list(set(range(100))-set(inx))
    B_0, P_up, P_sw, P_stay, N_up, N_sw, N_stay = get_B_N_P()
    B = [Atom([Term(False, str(x[1]))], x[0]) for x in B_0]
    # B = [Atom([Term(False, str(id))], 'busy')
    #      for id in inx]+ [Atom([Term(False, str(id))], 'NObusy')
    #      for id in neg_inx]
    # print("B..........")
    # print(B)
    P = [Atom([Term(False, str(id))], 'sw') for id in P_sw]
    N = []  #[Atom([Term(False, str(id))], 'sw')
    #for id in N_sw]
    # print("P..........")
    # print(P)
    # print("N..........")
    # print(N)
    term_x_0 = Term(True, 'X')
    term_x_1 = Term(True, 'X1')
    p_e = [
        Atom([term_x_0], 'busy'),
        Atom([term_x_0], 'NObusy'),
        Atom([term_x_0], 'side'),
        Atom([term_x_0], 'front'),
        Atom([term_x_0], 'back')
    ]
    #p_a = []
    p_a = [Atom([term_x_0], 'pred')]
    target = Atom([term_x_0], 'sw')
    # target_rule = (Rule_Template(0, False), Rule_Template(1, True))
    target_rule = (Rule_Template(0, True), Rule_Template(0, False))
    p_a_rule = (Rule_Template(0, False), None)
    rules = {p_a[0]: p_a_rule, target: target_rule}
    constants = [str(i) for i in range(0, 400)]

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    dilp = DILP(langage_frame, B, P, N, program_template)
    dilp.train(steps=252)
Exemplo n.º 15
0
def two_children():
    '''
    Learn the target predicate of has_at_least_two_children(X).
    In a directed graph we want to check if a node has at least two children
    (that are not equal).
    '''
    constants = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    edge_relation = [
        Atom([Term(False, 'a'), Term(False, 'b')], 'edge'),
        Atom([Term(False, 'a'), Term(False, 'c')], 'edge'),
        Atom([Term(False, 'b'), Term(False, 'd')], 'edge'),
        Atom([Term(False, 'c'), Term(False, 'd')], 'edge'),
        Atom([Term(False, 'c'), Term(False, 'e')], 'edge'),
        Atom([Term(False, 'd'), Term(False, 'e')], 'edge'),
        Atom([Term(False, 'f'), Term(False, 'a')], 'edge'),
        Atom([Term(False, 'f'), Term(False, 'b')], 'edge'),
        Atom([Term(False, 'g'), Term(False, 'e')], 'edge'),
        Atom([Term(False, 'g'), Term(False, 'c')], 'edge')
    ]
    equals_relation = [
        Atom([Term(False, x), Term(False, y)], 'equals')
        for x, y in zip(constants, constants) if x == y
    ]
    B_atom = edge_relation + equals_relation
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [
        Atom([Term(False, 'a')], 'target'),
        Atom([Term(False, 'c')], 'target'),
        Atom([Term(False, 'f')], 'target'),
        Atom([Term(False, 'g')], 'target')
    ]
    P = [Literal(atom, False) for atom in P_atom]

    N_atom = [
        Atom([Term(False, 'b')], 'target'),
        Atom([Term(False, 'd')], 'target'),
        Atom([Term(False, 'e')], 'target'),
        Atom([Term(False, 'h')], 'target')
    ]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')
    term_x_2 = Term(True, 'X_2')

    p_e = [
        Literal(Atom([term_x_0, term_x_1], 'edge'), False),
        Literal(Atom([term_x_0, term_x_1], 'equals'), False)
    ]
    p_a = [Literal(Atom([term_x_0, term_x_2], 'pred'), False)]
    target = Literal(Atom([term_x_0], 'target'), False)

    # Define rules for intensional predicates
    p_a_rule = (Rule_Template_Negation(1, False, True), None)
    target_rule = (Rule_Template_Negation(1, True, False), None)
    rules = {p_a[0]: p_a_rule, target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(language_frame, B, P, N, program_template)
    return snafdilp.train(steps=300)
Exemplo n.º 16
0
def no_ancestor():
    '''
    Learn the target predicate of ancestor(X)
    which is true if X has no ancestor
    '''
    constants = [
        'Paul', 'Randy', 'Rachel', 'Bob', 'Alice', 'Stan', 'Kyle', 'Peter',
        'Tony', 'Monica', 'Jessica', 'Duncan'
    ]
    B_atom = [
        Atom([Term(False, 'Bob'), Term(False, 'Paul')], 'parent'),
        Atom([Term(False, 'Bob'), Term(False, 'Alice')], 'parent'),
        Atom([Term(False, 'Randy'), Term(False, 'Tony')], 'parent'),
        Atom([Term(False, 'Randy'), Term(False, 'Stan')], 'parent'),
        Atom([Term(False, 'Peter'), Term(False, 'Kyle')], 'parent'),
        Atom([Term(False, 'Alice'), Term(False, 'Kyle')], 'parent'),
        Atom([Term(False, 'Jessica'),
              Term(False, 'Bob')], 'parent'),
        Atom([Term(False, 'Jessica'),
              Term(False, 'Randy')], 'parent'),
        Atom([Term(False, 'Matthew'),
              Term(False, 'Jessica')], 'parent')
    ]

    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [
        Atom([Term(False, 'Peter')], 'target'),
        Atom([Term(False, 'Monica')], 'target'),
        Atom([Term(False, 'Rachel')], 'target'),
        Atom([Term(False, 'Jessica')], 'target'),
        Atom([Term(False, 'Duncan')], 'target'),
        Atom([Term(False, 'Matthew')], 'target')
    ]
    positive_literal_P = [Literal(atom, False) for atom in P_atom]

    N_atom = [
        Atom([Term(False, 'Randy')], 'target'),
        Atom([Term(False, 'Bob')], 'target'),
        Atom([Term(False, 'Alice')], 'target'),
        Atom([Term(False, 'Kyle')], 'target'),
        Atom([Term(False, 'Tony')], 'target'),
        Atom([Term(False, 'Stan')], 'target'),
        Atom([Term(False, 'Paul')], 'target')
    ]
    positive_literal_N = [Literal(atom, False) for atom in N_atom]

    negative_literal_P = [Literal(atom, True) for atom in N_atom]
    negative_literal_N = [Literal(atom, True) for atom in P_atom]

    P = positive_literal_P + negative_literal_P
    N = positive_literal_N + negative_literal_N

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [Atom([term_x_0, term_x_1], 'parent')]
    p_a = []
    target = Atom([term_x_0], 'target')

    # Define rules for intensional predicates
    p_a_rule = (None, None)
    target_rule = (Rule_Template_Negation(1, False, True), None)
    rules = {target: target_rule}

    langage_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    #program_template = Program_Template(p_a, rules, 300)

    dilp = DILP(langage_frame, B, P, N, program_template)
    dilp.train(steps=200)
Exemplo n.º 17
0
from src.core import Term, Atom, Clause
from src.ilp import Language_Frame, Program_Template, Rule_Template, Inference, ILP

epsilon = 0.001

term1 = Term(True, 'X_0')
term2 = Term(True, 'X_1')
target = Atom([term1, term2], 'r')
p_e = [Atom([term1, term2], 'p')]
p_a = [Atom([term1, term2], 'q')]

rule_template_1 = Rule_Template(0, False)
rule_template_2 = Rule_Template(1, True)

language_frame = Language_Frame(target, p_e, set(['a', 'b']))
program_template = Program_Template(p_a, (rule_template_1, rule_template_2),
                                    300)

term_a = Term(False, 'a')
term_b = Term(False, 'b')
# Background facts: p(a,a) p(a,b)
background = [Atom([term_a, term_a], 'p'), Atom([term_a, term_b], 'p')]
# Positive: p(b,a)
positive = [Atom([term_b, term_a], 'p')]
# Negative: p(b,b)
negative = [Atom([term_b, term_b], 'p')]
ilp = ILP(language_frame, background, positive, negative, program_template)
(initial_valuation, valuation_mapping) = ilp.convert()


def x_c_test():
    '''Testing f_c function
Exemplo n.º 18
0
def not_grandparent():
    '''
    Learn the target predicate not_grandparent(X,Y) which is true
    if X is not the grapndparent of Y.
    '''
    constants = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
    B_atom = [
        Atom([Term(False, 'a'), Term(False, 'b')], 'mother'),
        Atom([Term(False, 'i'), Term(False, 'a')], 'father'),
        Atom([Term(False, 'a'), Term(False, 'b')], 'father'),
        Atom([Term(False, 'a'), Term(False, 'c')], 'father'),
        Atom([Term(False, 'b'), Term(False, 'd')], 'father'),
        Atom([Term(False, 'b'), Term(False, 'e')], 'mother'),
        Atom([Term(False, 'c'), Term(False, 'f')], 'mother'),
        Atom([Term(False, 'c'), Term(False, 'g')], 'mother'),
        Atom([Term(False, 'f'), Term(False, 'h')], 'mother')
    ]
    B = [Literal(atom, False) for atom in B_atom]

    N_atom = [
        Atom([Term(False, 'i'), Term(False, 'b')], 'target'),
        Atom([Term(False, 'i'), Term(False, 'c')], 'target'),
        Atom([Term(False, 'a'), Term(False, 'd')], 'target'),
        Atom([Term(False, 'a'), Term(False, 'e')], 'target'),
        Atom([Term(False, 'a'), Term(False, 'f')], 'target'),
        Atom([Term(False, 'a'), Term(False, 'g')], 'target'),
        Atom([Term(False, 'c'), Term(False, 'h')], 'target')
    ]

    possible_target_atoms = [
        Atom([Term(False, x), Term(False, y)], 'target')
        for x, y in itertools.product(constants, constants)
    ]
    P_atom = []
    for atom in possible_target_atoms:
        if atom not in N_atom:
            c1 = atom.terms[0].name
            c2 = atom.terms[1].name
            P_atom.append(Atom([Term(False, c1), Term(False, c2)], 'target'))
    P_atom = random.sample(P_atom, len(N_atom))

    P = [Literal(atom, False) for atom in P_atom]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')
    term_x_2 = Term(True, 'X_2')

    p_e = [
        Literal(Atom([term_x_0, term_x_1], 'mother'), False),
        Literal(Atom([term_x_0, term_x_1], 'father'), False)
    ]
    p_a = [
        Literal(Atom([term_x_0, term_x_1], 'pred1'), False),
        Literal(Atom([term_x_0, term_x_1], 'pred2'), False)
    ]
    target = Literal(Atom([term_x_0, term_x_2], 'target'), False)

    # Define rules for intensional predicates
    p_a_rule = [(Rule_Template_Negation(1, True, False), None),
                (Rule_Template_Negation(0, False, False),
                 Rule_Template_Negation(0, False, False))]
    target_rule = (Rule_Template_Negation(0, True, True), None)
    rules = {p_a[0]: p_a_rule[0], p_a[1]: p_a_rule[1], target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(language_frame, B, P, N, program_template)
    return snafdilp.train(steps=300)
Exemplo n.º 19
0
def orphan():
    '''
    Learn the target predicate of orphan(X)
    which is true if X has no parent
    '''
    constants = [
        'Paul', 'Randy', 'Rachel', 'Bob', 'Alice', 'Steve', 'Frank', 'Julia',
        'Stan', 'Kyle', 'Peter', 'Tony', 'Monica', 'Carl', 'Dolores', 'Tommy',
        'Pedro', 'Will', 'Sophie', 'Eric', 'Jon', 'Robert', 'Sansa', 'Arya',
        'Tormund', 'Mance'
    ]
    # parent(X, Y) is true if X is the father of Y
    B_atom = [
        Atom([Term(False, 'Bob'), Term(False, 'Paul')], 'parent'),
        Atom([Term(False, 'Bob'), Term(False, 'Alice')], 'parent'),
        Atom([Term(False, 'Randy'), Term(False, 'Tony')], 'parent'),
        Atom([Term(False, 'Randy'), Term(False, 'Stan')], 'parent'),
        Atom([Term(False, 'Peter'), Term(False, 'Kyle')], 'parent'),
        Atom([Term(False, 'Alice'), Term(False, 'Kyle')], 'parent'),
        Atom([Term(False, 'Tony'), Term(False, 'Carl')], 'parent'),
        Atom([Term(False, 'Dolores'),
              Term(False, 'Tony')], 'parent'),
        Atom([Term(False, 'Stan'), Term(False, 'Julia')], 'parent'),
        Atom([Term(False, 'Julia'), Term(False, 'Tommy')], 'parent'),
        Atom([Term(False, 'Kyle'), Term(False, 'Tommy')], 'parent'),
        Atom([Term(False, 'Carl'), Term(False, 'Sophie')], 'parent'),
        Atom([Term(False, 'Carl'), Term(False, 'Eric')], 'parent'),
        Atom([Term(False, 'Tommy'), Term(False, 'Arya')], 'parent'),
        Atom([Term(False, 'Tommy'),
              Term(False, 'Tormund')], 'parent'),
        Atom([Term(False, 'Tommy'), Term(False, 'Mance')], 'parent')
    ]
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [
        Atom([Term(False, 'Randy')], 'target'),
        Atom([Term(False, 'Bob')], 'target'),
        Atom([Term(False, 'Peter')], 'target'),
        Atom([Term(False, 'Rachel')], 'target'),
        Atom([Term(False, 'Monica')], 'target'),
        Atom([Term(False, 'Dolores')], 'target'),
        Atom([Term(False, 'Steve')], 'target'),
        Atom([Term(False, 'Frank')], 'target'),
        Atom([Term(False, 'Pedro')], 'target'),
        Atom([Term(False, 'Will')], 'target'),
        Atom([Term(False, 'Jon')], 'target'),
        Atom([Term(False, 'Robert')], 'target'),
        Atom([Term(False, 'Sansa')], 'target'),
    ]
    P = [Literal(atom, False) for atom in P_atom]

    N_atom = [
        Atom([Term(False, 'Paul')], 'target'),
        Atom([Term(False, 'Alice')], 'target'),
        Atom([Term(False, 'Kyle')], 'target'),
        Atom([Term(False, 'Tony')], 'target'),
        Atom([Term(False, 'Stan')], 'target'),
        Atom([Term(False, 'Carl')], 'target'),
        Atom([Term(False, 'Julia')], 'target'),
        Atom([Term(False, 'Tommy')], 'target'),
        Atom([Term(False, 'Sophie')], 'target'),
        Atom([Term(False, 'Eric')], 'target'),
        Atom([Term(False, 'Arya')], 'target'),
        Atom([Term(False, 'Tormund')], 'target'),
        Atom([Term(False, 'Mance')], 'target')
    ]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [Literal(Atom([term_x_0, term_x_1], 'parent'), False)]
    p_a = []
    target = Literal(Atom([term_x_0], 'target'), False)

    # Define rules for intensional predicates
    p_a_rule = (None, None)
    target_rule = (Rule_Template_Negation(1, False, True), None)
    rules = {target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(language_frame, B, P, N, program_template)
    return snafdilp.train(steps=200)
Exemplo n.º 20
0
def can_fly():
    '''
    Learn the target predicate of can_fly(X)
    which is true if X is a bird, but not an abnormal bird
    '''
    constants = [
        'bluebird', 'penguin', 'ostrich', 'blackbird', 'robin', 'sparrow',
        'starling', 'chicken', 'kiwi', 'steamerduck', 'kakapo', 'takahe',
        'weka', 'pigeon', 'swan', 'duck', 'goldfinch', 'woodpecker', 'bluetit',
        'greattit', 'puddle', 'forrestcat', 'pitbull', 'goldenretriever',
        'perser', 'bengal', 'siamese', 'sphynx', 'ragdoll', 'savannah',
        'sibiriancat', 'greyhound', 'malteser', 'dobermann', 'rottweiler',
        'bostonterrier', 'scottishfold', 'exotic', 'russianblue'
    ]
    B_atom = [  # birds
        Atom([Term(False, 'bluebird')], 'is_bird'),
        Atom([Term(False, 'woodpecker')], 'is_bird'),
        Atom([Term(False, 'penguin')], 'is_bird'),
        Atom([Term(False, 'bluetit')], 'is_bird'),
        Atom([Term(False, 'greattit')], 'is_bird'),
        Atom([Term(False, 'ostrich')], 'is_bird'),
        Atom([Term(False, 'blackbird')], 'is_bird'),
        Atom([Term(False, 'robin')], 'is_bird'),
        Atom([Term(False, 'sparrow')], 'is_bird'),
        Atom([Term(False, 'starling')], 'is_bird'),
        Atom([Term(False, 'chicken')], 'is_bird'),
        Atom([Term(False, 'kiwi')], 'is_bird'),
        Atom([Term(False, 'steamerduck')], 'is_bird'),
        Atom([Term(False, 'kakapo')], 'is_bird'),
        Atom([Term(False, 'takahe')], 'is_bird'),
        Atom([Term(False, 'weka')], 'is_bird'),
        Atom([Term(False, 'pigeon')], 'is_bird'),
        Atom([Term(False, 'swan')], 'is_bird'),
        Atom([Term(False, 'duck')], 'is_bird'),
        Atom([Term(False, 'goldfinch')], 'is_bird'),
        # abnormal birds
        Atom([Term(False, 'penguin')], 'abnormal_bird'),
        Atom([Term(False, 'ostrich')], 'abnormal_bird'),
        Atom([Term(False, 'chicken')], 'abnormal_bird'),
        Atom([Term(False, 'kiwi')], 'abnormal_bird'),
        Atom([Term(False, 'steamerduck')], 'abnormal_bird'),
        Atom([Term(False, 'kakapo')], 'abnormal_bird'),
        Atom([Term(False, 'takahe')], 'abnormal_bird'),
        Atom([Term(False, 'weka')], 'abnormal_bird')
    ]
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [
        Atom([Term(False, 'bluebird')], 'target'),
        Atom([Term(False, 'blackbird')], 'target'),
        Atom([Term(False, 'robin')], 'target'),
        Atom([Term(False, 'sparrow')], 'target'),
        Atom([Term(False, 'starling')], 'target'),
        Atom([Term(False, 'pigeon')], 'target'),
        Atom([Term(False, 'swan')], 'target'),
        Atom([Term(False, 'duck')], 'target'),
        Atom([Term(False, 'goldfinch')], 'target'),
        Atom([Term(False, 'woodpecker')], 'target'),
        Atom([Term(False, 'bluetit')], 'target'),
        Atom([Term(False, 'greattit')], 'target')
    ]
    P = [Literal(atom, False) for atom in P_atom]

    N_atom = [
        Atom([Term(False, 'penguin')], 'target'),
        Atom([Term(False, 'chicken')], 'target'),
        Atom([Term(False, 'kiwi')], 'target'),
        Atom([Term(False, 'steamerduck')], 'target'),
        Atom([Term(False, 'takahe')], 'target'),
        Atom([Term(False, 'puddle')], 'target'),
        Atom([Term(False, 'siamese')], 'target'),
        Atom([Term(False, 'forrestcat')], 'target'),
        Atom([Term(False, 'pitbull')], 'target'),
        Atom([Term(False, 'sphynx')], 'target'),
        Atom([Term(False, 'sibiriancat')], 'target'),
        Atom([Term(False, 'greyhound')], 'target'),
        Atom([Term(False, 'russianblue')], 'target')
    ]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')

    p_e = [
        Literal(Atom([term_x_0], 'is_bird'), False),
        Literal(Atom([term_x_0], 'abnormal_bird'), False)
    ]
    p_a = []
    target = Literal(Atom([term_x_0], 'target'), False)

    # Define rules for intensional predicates
    p_a_rule = (None, None)
    target_rule = (Rule_Template_Negation(0, False, True), None)
    rules = {target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(language_frame, B, P, N, program_template)
    return snafdilp.train(steps=200)
Exemplo n.º 21
0
B, pred_f, constants_f = process_file('%s/facts.dilp' % sys.argv[1])
P, target_p, constants_p = process_file('%s/positive.dilp' % sys.argv[1])
N, target_n, constants_n = process_file('%s/negative.dilp' % sys.argv[1])

if not (target_p == target_n):
    raise Exception('Positive and Negative files have different targets')
elif not len(target_p) == 1:
    raise Exception('Can learn only one predicate at a time')
elif not constants_n.issubset(constants_f) or not constants_p.issubset(
        constants_f):
    raise Exception(
        'Constants not in fact file exists in positive/negative file')

term_x_0 = Term(True, 'X_0')
term_x_1 = Term(True, 'X_1')

p_e = list(pred_f)
p_a = [Atom([term_x_0, term_x_1], 'pred')]
target = list(target_p)[0]
constants = constants_f

p_a_rule = (Rule_Template(1, False), None)
target_rule = (Rule_Template(0, False), Rule_Template(1, True))
rules = {p_a[0]: p_a_rule, target: target_rule}

langage_frame = Language_Frame(target, p_e, constants)
program_template = Program_Template(p_a, rules, 10)
dilp = DILP(langage_frame, B, P, N, program_template)
dilp.train()
Exemplo n.º 22
0
def has_roommate():
    '''
    Learn the target predicate of has_roommate(X, Y)
    which is true if there exists a person Y that X is married to
    and Y is not a researcher.
    '''
    constants = [
        'Paul', 'Randy', 'Rachel', 'Bob', 'Alice', 'Steve', 'Frank', 'Julia',
        'Stan', 'Kyle', 'Peter', 'Tony', 'Monica', 'Carl', 'Dolores', 'Tommy',
        'Pedro', 'Will', 'Sophie', 'Eric', 'Jon', 'Robert', 'Sansa', 'Arya',
        'Tormund', 'Mance'
    ]
    # married(X,Y) with symmetric closure
    B_atom = [
        Atom([Term(False, 'Bob'), Term(False, 'Rachel')], 'married'),
        Atom([Term(False, 'Rachel'), Term(False, 'Bob')], 'married'),
        Atom([Term(False, 'Randy'), Term(False, 'Alice')], 'married'),
        Atom([Term(False, 'Alice'), Term(False, 'Randy')], 'married'),
        Atom([Term(False, 'Steve'), Term(False, 'Julia')], 'married'),
        Atom([Term(False, 'Julia'), Term(False, 'Steve')], 'married'),
        Atom(
            [Term(False, 'Frank'), Term(False, 'Monica')], 'married'),
        Atom([Term(False, 'Monica'),
              Term(False, 'Frank')], 'married'),
        Atom([Term(False, 'Stan'), Term(False, 'Dolores')], 'married'),
        Atom([Term(False, 'Dolores'),
              Term(False, 'Frank')], 'married'),
        Atom([Term(False, 'Will'), Term(False, 'Sophie')], 'married'),
        Atom(
            [Term(False, 'Sophie'), Term(False, 'Will')], 'married'),
        Atom([Term(False, 'Eric'), Term(False, 'Arya')], 'married'),
        Atom([Term(False, 'Arya'), Term(False, 'Eric')], 'married'),
        # researcher(X)
        Atom([Term(False, 'Robert')], 'researcher'),
        Atom([Term(False, 'Eric')], 'researcher'),
        Atom([Term(False, 'Bob')], 'researcher'),
        Atom([Term(False, 'Frank')], 'researcher'),
        Atom([Term(False, 'Monica')], 'researcher'),
        Atom([Term(False, 'Mance')], 'researcher'),
        Atom([Term(False, 'Jon')], 'researcher'),
        Atom([Term(False, 'Tormund')], 'researcher'),
        Atom([Term(False, 'Sansa')], 'researcher')
    ]
    B = [Literal(atom, False) for atom in B_atom]

    P_atom = [
        Atom([Term(False, 'Randy')], 'target'),
        Atom([Term(False, 'Alice')], 'target'),
        Atom([Term(False, 'Steve')], 'target'),
        Atom([Term(False, 'Julia')], 'target'),
        Atom([Term(False, 'Dolores')], 'target'),
        Atom([Term(False, 'Stan')], 'target'),
        Atom([Term(False, 'Will')], 'target'),
        Atom([Term(False, 'Sophie')], 'target')
    ]
    P = [Literal(atom, False) for atom in P_atom]

    N_atom = [
        Atom([Term(False, 'Bob')], 'target'),
        Atom([Term(False, 'Rachel')], 'target'),
        Atom([Term(False, 'Frank')], 'target'),
        Atom([Term(False, 'Monica')], 'target'),
        Atom([Term(False, 'Kyle')], 'target'),
        Atom([Term(False, 'Peter')], 'target'),
        Atom([Term(False, 'Tony')], 'target'),
        Atom([Term(False, 'Carl')], 'target'),
        Atom([Term(False, 'Tommy')], 'target'),
        Atom([Term(False, 'Pedro')], 'target'),
        Atom([Term(False, 'Tommy')], 'target'),
        Atom([Term(False, 'Eric')], 'target'),
        Atom([Term(False, 'Arya')], 'target'),
        Atom([Term(False, 'Jon')], 'target'),
        Atom([Term(False, 'Robert')], 'target'),
        Atom([Term(False, 'Mance')], 'target'),
        Atom([Term(False, 'Tormund')], 'target')
    ]
    N = [Literal(atom, False) for atom in N_atom]

    term_x_0 = Term(True, 'X_0')
    term_x_1 = Term(True, 'X_1')

    p_e = [
        Literal(Atom([term_x_0, term_x_1], 'married'), False),
        Literal(Atom([term_x_0], 'researcher'), False)
    ]
    p_a = [Literal(Atom([term_x_0, term_x_1], 'pred'), False)]
    target = Literal(Atom([term_x_0], 'target'), False)

    # Define rules for intensional predicates
    p_a_rule = (Rule_Template_Negation(0, False, True), None)
    target_rule = (Rule_Template_Negation(1, True, False), None)
    rules = {p_a[0]: p_a_rule, target: target_rule}

    language_frame = Language_Frame(target, p_e, constants)
    program_template = Program_Template(p_a, rules, 10)
    # program_template = Program_Template(p_a, rules, 300)

    snafdilp = SNAFDILP(language_frame, B, P, N, program_template)
    return snafdilp.train(steps=200)