Esempio n. 1
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)
Esempio n. 2
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
Esempio n. 3
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()
Esempio n. 4
0
def optimized_rule_test():
    rule_template_1 = Rule_Template(0, False)
    rule_template_2 = Rule_Template(1, True)
    target = Atom([Term(True, 'X_0'), Term(True, 'X_1')], 'q')
    p_e = [Atom([Term(True, 'X_0'), Term(True, 'X_1')], 'p')]
    rule_manager = Optimized_Combinatorial_Generator(
        [target], (rule_template_1, rule_template_2), target, p_e)
    rule_matrix = rule_manager.generate_clauses()
    assert len(rule_matrix[0]) == 8
    assert len(rule_matrix[1]) == 58
Esempio n. 5
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
Esempio 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)
Esempio 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)
Esempio n. 8
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
Esempio n. 9
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)
Esempio n. 10
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)
Esempio n. 11
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()
Esempio n. 12
0
'''File for testing inference functions (Inference.py)
'''
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()
Esempio n. 13
0
def rule_template_test():
    rule_template = Rule_Template(1, True)
    assert rule_template.v == 1
    assert rule_template.allow_intensional == True