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 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 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 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 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 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)
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 generate_stratified_dilps(self): ''' Construct n DILP objects. All objects have clauses seperated into strata to ensure stratification. :return: ''' rules = self.program_template.rules n_auxiliary_predicates = len( rules) - 1 # Subtract 1 for target predicate # If only target predicate then no risk for stratification if n_auxiliary_predicates <= 0: return [ DILP(self.language_frame, self.background, self.positive, self.negative, self.program_template) ] target = self.language_frame.target rules = rules.copy() rules.pop(target) keys = list(rules.keys()) if n_auxiliary_predicates == 1: strata1 = {keys[0]: 1, target: 2} dilp1 = self.create_DILP(strata1) # Removed all predicates in same strata. No need return [dilp1] if n_auxiliary_predicates == 2: strata1 = {keys[0]: 2, keys[1]: 1, target: 2} strata2 = {keys[0]: 1, keys[1]: 2, target: 2} strata3 = {keys[0]: 1, keys[1]: 1, target: 2} strata4 = {keys[0]: 1, keys[1]: 2, target: 3} strata5 = {keys[0]: 2, keys[1]: 1, target: 3} dilp1 = self.create_DILP(strata1) dilp2 = self.create_DILP(strata2) dilp3 = self.create_DILP(strata3) dilp4 = self.create_DILP(strata4) dilp5 = self.create_DILP(strata5) return [dilp1, dilp2, dilp3, dilp4, dilp5]
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) dilp = DILP(langage_frame, B, P, N, program_template) loss = dilp.train(steps=300) dilp.show_definition() return loss
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 create_DILP(self, strata): clause_parameters = self.create_clause_parameters(strata) return DILP(self.language_frame, self.background, self.positive, self.negative, self.program_template, clause_parameters)
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)