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
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
def generate_ground_atoms(self): '''Generates the ground atoms from p_i,p_a,target and constants ''' p = list( set(self.language_frame.p_e + self.program_template.p_a + [self.language_frame.target])) constants = self.language_frame.constants # Build constant matrix constant_matrix = [] for const1 in constants: for const2 in constants: term1 = Term(False, const1) term2 = Term(False, const2) constant_matrix.append([term1, term2]) # Build ground atoms ground_atoms = [] ground_atoms.append(Atom([], '⊥')) added_atoms = {} for pred in p: for term in constant_matrix: atom = Atom([term[i] for i in range(0, pred.arity)], pred.predicate) if atom not in added_atoms: ground_atoms.append(atom) added_atoms[atom] = 1 return ground_atoms
def generate_ground_literals(self): '''Generates the ground atoms from p_i,p_a,target and constants ''' p = list(set(self.language_frame.p_e + self.program_template.p_a + [self.language_frame.target])) constants = self.language_frame.constants # Build constant matrix constant_matrix = [] for const1 in constants: for const2 in constants: term1 = Term(False, const1) term2 = Term(False, const2) constant_matrix.append([term1, term2]) # Build ground, non negated, atoms ground_positive_literals = [] ground_positive_literals.append(Literal(Atom([], '⊥'), False)) added_literals = {} for pred in p: for term in constant_matrix: literal = Literal(Atom([term[i] for i in range(0, pred.arity)], pred.predicate), False) if literal not in added_literals: ground_positive_literals.append(literal) added_literals[literal] = 1 ground_literals = [] ground_literals += ground_positive_literals if self.uses_negation: for literal in ground_positive_literals: #if atom == Atom([],'⊥'): # continue negative_literal = literal.__copy__() negative_literal.negate() ground_literals.append(negative_literal) return ground_literals
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)
def generate_clauses(self): '''Generate the clauses based on brute force approach with no optmization ''' rule_matrix = [] for rule in self.rules: clauses = [] if (rule.allow_intensional): p = self.p_e + self.p_a + [self.target] p_i = self.p_a + [self.target] intensional_predicates = [atom.predicate for atom in p_i] else: p = self.p_e variables = [ 'X_%d' % i for i in range(0, self.target.arity + rule.v) ] target_variables = [ 'X_%d' % i for i in range(0, self.target.arity) ] # NOTE: Only allows 2 predicates and 2 arity maximum # TODO: Need for optimizing for i1 in range(0, len(p)): for v1 in variables: for v2 in variables: for i2 in range(i1, len(p)): for v3 in variables: for v4 in variables: # unsafe if not set(target_variables).issubset( [v1, v2, v3, v4]): continue head = Atom([ Term(True, var) for var in target_variables ], self.target.predicate) body1 = Atom( [Term(True, v1), Term(True, v2)], p[i1].predicate) body2 = Atom( [Term(True, v3), Term(True, v4)], p[i2].predicate) clause = Clause(head, [body1, body2]) if head == body1 or head == body2: continue # NOTE: Based on appendix requires to have a intensional predicate if rule.allow_intensional and not ( body1.predicate in intensional_predicates or body2.predicate in intensional_predicates): continue # duplicate if clause not in clauses: clauses.append(clause) print(clauses) rule_matrix.append(clauses) return rule_matrix
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
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()
def equality_test(): term1 = Term(True, 'X_0') term2 = Term(True, 'X_1') atom1 = Atom([term1, term1], 'p') atom2 = Atom([term1, term2], 'p') head = Atom([term1, term2], 'q') clause1 = Clause(head, [atom1, atom2]) clause2 = Clause(head, [atom2, atom1]) pred_dict = {} pred_dict[clause1] = 1 pred_dict[clause2] = 1 assert clause1 == clause2 assert len(pred_dict) == 1
def process_file(filename): atoms = [] predicates = set() with open(filename) as f: data = f.read().replace('\n', '') result = prolog_sentences.parseString(data) for fact in result['facts']: predicate = fact['relationship'] terms = [Term(False, term) for term in fact['arguments']] constants = set([term for term in fact['arguments']]) term_var = [Term(True, f'X_{i}') for i in range(len(terms))] predicates.add(Atom(term_var, predicate)) atoms.append(Atom(terms, predicate)) return atoms, predicates, constants
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)
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)
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)
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)
def x_c(clause: Clause, dict_valuation: dict, constants: list): ''' Arguments: clause {Clause} -- clause to generate f_c valuations {list} -- valuation list constants {list} -- constants ''' if clause == None: return np.zeros((len(dict_valuation), 1, 2), dtype=int) constant_terms = [Term(False, constant) for constant in constants] head_variables = clause.head.variables variables = [Term(True, variable) for variable in clause.variables] existential_variable = len(variables) - len(head_variables) comb = [] for var in variables: temp = [] for c in constant_terms: temp.append((var, c)) comb.append(temp) w = np.power( len(constants), existential_variable) x_c = np.zeros((len(dict_valuation), w, 2), dtype=int) derived_valuation = defaultdict(list) for elm in product(*comb): subs = {a[0]: a[1] for a in elm} substituted_head = Atom( [subs[term] for term in clause.head.terms], clause.head.predicate) derived = [] for literal in clause.body: substituted_body_atom = Atom([subs[term] for term in literal.terms], literal.predicate) substituted_body = Literal(substituted_body_atom, literal.negated) derived.append(int(dict_valuation[substituted_body])) if derived not in derived_valuation[substituted_head]: # BIG MAYBE derived_valuation[substituted_head].append(derived) for literal in derived_valuation: for i in range(0, len(derived_valuation[literal])): x_c[dict_valuation[literal], i, 0] = 0 x_c[dict_valuation[literal], i, 0] = derived_valuation[literal][i][0] x_c[dict_valuation[literal], i, 1] = derived_valuation[literal][i][1] return x_c
def f_c(clause: Clause, valuations, dict_valuation: dict, constants: list): ''' Arguments: clause {Clause} -- clause to generate f_c valuations {list} -- valuation list constants {list} -- constants ''' constant_terms = [Term(False, constant) for constant in constants] variables = [Term(True, variable) for variable in clause.variables] comb = [] for var in variables: temp = [] for c in constant_terms: temp.append((var, c)) comb.append(temp) # dict_valuation = {valuations[i][0]: # valuations[i][1] for i in range(0, len(valuations))} derived_valuation = defaultdict(list) derived_valuation2 = defaultdict(list) for elm in product(*comb): subs = {a[0]: a[1] for a in elm} substituted_head = Atom( [subs[term] for term in clause.head.terms], clause.head.predicate) derived = [] for atom in clause.body: substituted_body = Atom([subs[term] for term in atom.terms], atom.predicate) derived.append(valuations[dict_valuation[substituted_body]]) derived_valuation[substituted_head].append( reduce(lambda a, b: a * b, derived, 1)) derived_valuation2[substituted_head].append(derived) z_c = np.zeros(valuations.shape[0], dtype=np.float32) for atom in dict_valuation: if(atom in derived_valuation): z_c[dict_valuation[atom]] = max(derived_valuation[atom]) else: z_c[dict_valuation[atom]] = 0 return z_c
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)
def x_c_test(): '''Testing f_c function ''' term_x = Term(True, 'X_0') term_y = Term(True, 'X_1') term_z = Term(True, 'X_2') r = Atom([term_x, term_y], '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)
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
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)
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)
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)
'''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()
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()
def generate_clauses(self): '''Generate all clauses with some level of optimization ''' rule_matrix = [] for rule in self.rules: # logger.info('Generating clauses') if rule is None: rule_matrix.append([None]) continue clauses = [] if rule.allow_intensional: p = list(set(self.p_e + self.p_i + [self.target])) p_i = list(set(self.p_i)) intensional_predicates = [atom.predicate for atom in p_i] else: p = list(set(self.p_e)) variables = ['X_%d' % i for i in range(0, self.target.arity + rule.v)] target_variables = ['X_%d' % i for i in range(0, self.target.arity)] # Generate the body list body_list = [] head = Atom( [Term(True, var) for var in target_variables], self.target.predicate) for var1 in variables: for var2 in variables: term1 = Term(True, var1) term2 = Term(True, var2) body_list.append([term1, term2]) # Generate the list added_pred = {} for ind1 in range(0, len(p)): pred1 = p[ind1] for b1 in body_list: for ind2 in range(ind1, len(p)): pred2 = p[ind2] for b2 in body_list: for negations in range(4): if not rule.neg and negations > 0: continue body1_atom = Atom([b1[index] for index in range(0, pred1.arity)], pred1.predicate) body2_atom = Atom([b2[index] for index in range(0, pred2.arity)], pred2.predicate) if negations == 0: # No negation body1 = Literal(body1_atom, False) body2 = Literal(body2_atom, False) if negations == 1: # First body is negated body1 = Literal(body1_atom, True) body2 = Literal(body2_atom, False) if negations == 2: # Second body is negated body1 = Literal(body1_atom, False) body2 = Literal(body2_atom, True) if negations == 3: # Both bodies are negated body1 = Literal(body1_atom, True) body2 = Literal(body2_atom, True) clause = Clause(head, [body1, body2]) # logger.info(clause) # All variables in head should be in the body if not set(target_variables).issubset([v.name for v in b1] + [v.name for v in b2]): continue elif head == body1 or head == body2: # No Circular continue # NOTE: Based on appendix requires to have a intensional predicate elif rule.allow_intensional and not (body1.predicate in intensional_predicates or body2.predicate in intensional_predicates): continue elif clause in added_pred: continue # Disallow a clause with the head negated in the body elif head.predicate == body1.predicate and body1.negated: continue elif head.predicate == body2.predicate and body2.negated: continue # Disallow positive and negative versions of the same literal in a clause elif body1_atom == body2_atom and body1.negated != body2.negated: # If the variables of the clause are different then allow this clause variables1 = body1_atom.terms variables2 = body2_atom.terms add_clause = True for i in range(len(variables1)): if variables1[i] != variables2[i]: add_clause = False break if not add_clause: continue else: added_pred[clause] = 1 clauses.append(clause) rule_matrix.append(clauses) # logger.info('Clauses Generated') return rule_matrix
def no_negative_cycle(): ''' 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', 'g', 'h'] edge_relations = [ Atom([Term(False, 'a'), Term(False, 'b')], 'edge'), Atom([Term(False, 'a'), Term(False, 'c')], 'edge'), Atom([Term(False, 'b'), Term(False, 'c')], 'edge'), Atom([Term(False, 'c'), Term(False, 'd')], 'edge'), Atom([Term(False, 'c'), Term(False, 'e')], 'edge'), Atom([Term(False, 'd'), Term(False, 'h')], 'edge'), Atom([Term(False, 'h'), Term(False, 'd')], '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'), Atom([Term(False, 'e'), Term(False, 'a')], 'edge') ] edge_type = [ Atom([Term(False, 'a'), Term(False, 'b')], 'positive'), Atom([Term(False, 'a'), Term(False, 'c')], 'negative'), Atom([Term(False, 'b'), Term(False, 'c')], 'positive'), Atom([Term(False, 'c'), Term(False, 'd')], 'positive'), Atom([Term(False, 'c'), Term(False, 'e')], 'negative'), Atom([Term(False, 'd'), Term(False, 'e')], 'positive'), Atom([Term(False, 'h'), Term(False, 'd')], 'positive'), Atom([Term(False, 'f'), Term(False, 'a')], 'positive'), Atom([Term(False, 'f'), Term(False, 'b')], 'positive'), Atom([Term(False, 'g'), Term(False, 'e')], 'positive'), Atom([Term(False, 'g'), Term(False, 'c')], 'positive'), Atom([Term(False, 'e'), Term(False, 'a')], 'positive') ] B_atom = edge_relations + edge_type B = [Literal(atom, False) for atom in B_atom] P_atom = [ Atom([Term(False, 'f')], 'target'), Atom([Term(False, 'g')], 'target'), Atom([Term(False, 'd')], 'target'), Atom([Term(False, 'h')], 'target') ] P = [Literal(atom, False) for atom in P_atom] N_atom = [ Atom([Term(False, 'a')], 'target'), Atom([Term(False, 'b')], 'target'), Atom([Term(False, 'c')], 'target'), Atom([Term(False, 'e')], '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], 'positive'), False), Literal(Atom([term_x_0, term_x_1], 'negative'), 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, True), 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} 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)
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)
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)
def generate_clauses(self): '''Generate all clauses with some level of optimization ''' rule_matrix = [] for rule in self.rules: # logger.info('Generating clauses') if rule == None: rule_matrix.append([None]) continue clauses = [] if (rule.allow_intensional): p = list(set(self.p_e + self.p_i + [self.target])) p_i = list(set(self.p_i)) intensional_predicates = [atom.predicate for atom in p_i] else: p = list(set(self.p_e)) variables = [ 'X_%d' % i for i in range(0, self.target.arity + rule.v) ] target_variables = [ 'X_%d' % i for i in range(0, self.target.arity) ] # Generate the body list body_list = [] head = Atom([Term(True, var) for var in target_variables], self.target.predicate) for var1 in variables: for var2 in variables: term1 = Term(True, var1) term2 = Term(True, var2) body_list.append([term1, term2]) # Generate the list added_pred = {} for ind1 in range(0, len(p)): pred1 = p[ind1] for b1 in body_list: for ind2 in range(ind1, len(p)): pred2 = p[ind2] for b2 in body_list: body1 = Atom( [b1[index] for index in range(0, pred1.arity)], pred1.predicate) body2 = Atom( [b2[index] for index in range(0, pred2.arity)], pred2.predicate) clause = Clause(head, [body1, body2]) # logger.info(clause) # All variables in head should be in the body if not set(target_variables).issubset( [v.name for v in b1] + [v.name for v in b2]): continue elif head == body1 or head == body2: # No Circular continue # NOTE: Based on appendix requires to have a intensional predicate elif rule.allow_intensional and not ( body1.predicate in intensional_predicates or body2.predicate in intensional_predicates): continue elif clause in added_pred: continue else: added_pred[clause] = 1 clauses.append(clause) rule_matrix.append(clauses) # logger.info('Clauses Generated') return rule_matrix
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)