forked from ai-systems/DILP-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
107 lines (84 loc) · 3.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'''For testing DILP
'''
from src.core import Term, Atom
from src.ilp import Language_Frame, Program_Template, Rule_Template
from src.dilp import DILP
import tensorflow as tf
tf.compat.v1.enable_eager_execution()
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)
dilp.train()
def prdecessor():
B = [Atom([Term(False, '0')], 'zero')] + \
[Atom([Term(False, str(i)), Term(False, str(i + 1))], 'succ')
for i in range(0, 9)]
P = [Atom([Term(False, str(i + 1)), Term(False, str(i))], 'target')
for i in range(0, 9)]
N = []
for i in range(0, 10):
for j in range(0, 10):
if j != i + 1:
N.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), None)
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()
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()
even_numbers_test()
# less_than()
# prdecessor()