predColl.add_pred(dname='son', arguments=['C', 'C']) predColl.add_pred(dname='sister', arguments=['C', 'C']) predColl.add_pred(dname='brother', arguments=['C', 'C']) predColl.add_pred(dname='aunt', arguments=['C', 'C']) predColl.add_pred(dname='uncle', arguments=['C', 'C']) predColl.add_pred(dname='niece', arguments=['C', 'C']) predColl.add_pred(dname='nephew', arguments=['C', 'C']) # predColl.add_pred(dname='aux' ,arguments=['C','C'], variables=[] , pFunc = DNF('aux',terms=4,init=[-1,.1,-1,-1],sig=2 ,predColl=predColl) ,Fam='or') # predColl.add_pred(dname='grandparent' ,arguments=['C','C'], variables=['C'] , pFunc = DNF('grandparent',terms=1,init=[-1,.1,-1,-1],sig=2),Fam='or',inc_preds=['aux'] ) predColl.add_pred(dname='grandparent', arguments=['C', 'C'], variables=['C'], pFunc=DNF('grandparent', terms=4, init=[-1, .1, -1, -1], sig=2)) predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_backgroud('father', ('Christopher', 'Arthur')) bg.add_backgroud('father', ('Christopher', 'Victoria')) bg.add_backgroud('father', ('Andrew', 'James')) bg.add_backgroud('father', ('Andrew', 'Jennifer')) bg.add_backgroud('father', ('James', 'Colin')) bg.add_backgroud('father', ('James', 'Charlotte')) bg.add_backgroud('father', ('Roberto', 'Emilio')) bg.add_backgroud('father', ('Roberto', 'Lucia'))
predColl.add_pred(dname='husband' ,arguments=['C','C'] ) predColl.add_pred(dname='father' ,arguments=['C','C'] ) predColl.add_pred(dname='daughter' ,arguments=['C','C'] ) predColl.add_pred(dname='mother' ,arguments=['C','C'] ) predColl.add_pred(dname='son' ,arguments=['C','C'] ) predColl.add_pred(dname='sister' ,arguments=['C','C'] ) predColl.add_pred(dname='brother' ,arguments=['C','C'] ) predColl.add_pred(dname='aunt' ,arguments=['C','C'] ) predColl.add_pred(dname='uncle' ,arguments=['C','C'] ) predColl.add_pred(dname='niece' ,arguments=['C','C'] ) predColl.add_pred(dname='nephew' ,arguments=['C','C'] ) # predColl.add_pred(dname='aux' ,arguments=['C','C'], variables=[] , pFunc = DNF('aux',terms=4,init=[-1,.1,-1,-1],sig=2 ,predColl=predColl) ,Fam='or') # predColl.add_pred(dname='grandparent' ,arguments=['C','C'], variables=['C'] , pFunc = DNF('grandparent',terms=1,init=[-1,.1,-1,-1],sig=2),Fam='or',inc_preds=['aux'] ) predColl.add_pred(dname='grandparent' ,arguments=['C','C'], variables=['C'] , pFunc = DNF('grandparent',terms=4,init=[-1,.1,-1,-1],sig=2) ) predColl.initialize_predicates() #add background bg = Background( predColl ) bg.add_backgroud ('father', ('Christopher','Arthur')) bg.add_backgroud ('father', ('Christopher', 'Victoria')) bg.add_backgroud ('father', ('Andrew', 'James')) bg.add_backgroud ('father', ('Andrew', 'Jennifer')) bg.add_backgroud ('father', ('James', 'Colin')) bg.add_backgroud ('father', ('James', 'Charlotte')) bg.add_backgroud ('father', ('Roberto', 'Emilio')) bg.add_backgroud ('father', ('Roberto', 'Lucia'))
def pred_func(self, xi, xcs, t): ind = self.predColl.preds_by_name['even'].inp_list.index('odd(A)') return 1.0 - xi[:, ind] def get_func(self, session, names, threshold=.2, print_th=True): return "" #define predicates predColl = PredCollection(Constants) predColl.add_pred(dname='zero', arguments=['C']) predColl.add_pred(dname='succ', arguments=['C', 'C']) predColl.add_pred(dname='odd', arguments=['C'], variables=['C'], pFunc=DNF('odd', terms=4, init=[-1, 1, -1, 1], sig=2)) #defining even predicate in terms of odd predColl.add_pred(dname='even', arguments=['C'], variables=['C'], pFunc=MyFuncEven(predColl=predColl)) predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_backgroud('zero', ('0', )) for i in range(maxN + 1): if (i + 1 <= maxN): bg.add_backgroud('succ', ('%d' % i, '%d' % (i + 1)))
#predColl.add_pred(name='lt',arguments=[] ) intervals_list = new_pred.spacing(data_x_pos, data_x_neg, 10) for index, val in enumerate(names): predColl.add_continous(name=val, no_lt=10, no_gt=10, lt_init=intervals_list[index], gt_init=intervals_list[index]) for i in range(1): predColl.add_pred(name='class_%d' % (i + 1), arguments=[], variables=[], pFunc=DNF('class_%d' % (i + 1), terms=1, init=[-1, .1, -1, .1], sig=2), use_cnt_vars=True, inc_preds=[]) predColl.initialize_predicates() #add backgrounds bg_train = [] bg_test = [] count = 0 for j in range(5): for i in range(L): bg = Background(predColl)
def define_preds(self): X = ['%d' % i for i in range(self.DIM1)] Y = ['%d' % i for i in range(self.DIM2)] self.Constants = dict({'N': X, 'Y': Y}) self.predColl = PredCollection(self.Constants) self.predColl.add_pred(dname='sameX', arguments=['N', 'N']) self.predColl.add_pred(dname='sameY', arguments=['Y', 'Y']) self.predColl.add_pred(dname='X_U', arguments=['N']) self.predColl.add_pred(dname='X_D', arguments=['N']) self.predColl.add_pred(dname='Y_L', arguments=['Y']) self.predColl.add_pred(dname='Y_R', arguments=['Y']) self.predColl.add_pred(dname='ltY', arguments=['Y', 'Y']) self.predColl.add_pred(dname='close', arguments=['Y', 'Y']) self.predColl.add_pred(dname='agent', arguments=['N', 'Y']) self.predColl.add_pred(dname='predLR', arguments=['N', 'Y']) self.predColl.add_pred(dname='predRL', arguments=['N', 'Y']) self.predColl.add_pred(dname='food', arguments=['N', 'Y']) count_type = 'max' self.predColl.add_pred(dname='pred', arguments=['N', 'Y'], variables=[], pFunc=DNF( 'pred', init_terms=['predLR(A,B)', 'predRL(A,B)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type='or', arg_funcs=[]) self.predColl.add_pred(dname='agentX', arguments=['N'], variables=['Y'], pFunc=DNF('agentX', init_terms=['agent(A,B)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type='or', arg_funcs=[]) self.predColl.add_pred(dname='agentY', arguments=['Y'], variables=['N'], pFunc=DNF('agentY', init_terms=['agent(B,A)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type='or', arg_funcs=[]) self.predColl.add_pred( dname='C1', arguments=[], variables=['N', 'N'], pFunc=DNF('C1', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=['agentX(A), agentX(B), not sameX(A,B)'], predColl=self.predColl, fast=True), use_neg=True, Fam='eq', count_type='max', arg_funcs=[]) self.predColl.add_pred( dname='C2', arguments=[], variables=['Y', 'Y'], pFunc=DNF('C2', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=['agentY(A), agentY(B), not close(A,B)'], predColl=self.predColl, fast=True), use_neg=True, Fam='eq', count_type='max', arg_funcs=[]) self.predColl.add_pred(dname='C3', arguments=[], variables=['N', 'Y'], pFunc=DNF('C3', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=[ 'predLR(A,B), predRL(A,B)', 'agent(A,B), predRL(A,B)', 'agent(A,B), predLR(A,B)', ], predColl=self.predColl, fast=True), use_neg=True, Fam='eq', count_type='max', arg_funcs=[]) self.predColl.add_pred(dname='C4', arguments=[], variables=['N', 'Y'], pFunc=DNF('C4', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=['agent(A,B)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type='max', arg_funcs=[]) excs = [ 'action_noop', 'action_up', 'action_down', 'action_left', 'action_right', 'Q' ] w = [-1, .1, -3, .1] self.predColl.add_pred(dname='en_up', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF( 'en_up', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=[ 'agent(A,B), X_U(A)', 'agent(A,B), pred(M_A,C), close(B,C)' ], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type=count_type, count_th=100, arg_funcs=['M']) self.predColl.add_pred(dname='en_down', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF( 'en_down', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=[ 'agent(A,B), X_D(A)', 'agent(A,B), pred(P_A,C), close(B,C)' ], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type=count_type, count_th=100, arg_funcs=['P']) self.predColl.add_pred( dname='en_right', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF('en_right', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=[ 'agent(A,B), Y_R(B)', 'agent(A,B), pred(A,C), close(B,C), ltY(B,C)' ], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type=count_type, count_th=100, arg_funcs=[]) self.predColl.add_pred( dname='en_left', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF('en_left', terms=1, init=[-1, .1, -1, .1], sig=2, init_terms=[ 'agent(A,B), Y_L(B)', 'agent(A,B), pred(A,C), close(B,C), ltY(C,B)' ], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type=count_type, count_th=100, arg_funcs=[]) self.predColl.add_pred( dname='en_noop', arguments=[], variables=[], pFunc=DNF( 'en_noop', terms=1, init=[-1, .1, -1, .1], sig=2, #init_terms=['agent(A,B), predRL(A,C), ltY(C,B)','agent(A,B), predLR(A,C), ltY(B,C)' ], init_terms=['en_right()', 'en_left()'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq', count_type=count_type, count_th=100) pt = [('and', 'not en_noop()')] self.predColl.add_pred(dname='action_noop', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF('action_noop', terms=8, init=w, sig=2, predColl=self.predColl, fast=False, post_terms=pt), use_neg=True, Fam='eq', exc_preds=excs, count_type=count_type, arg_funcs=['M']) pt = [('and', 'not en_up()')] self.predColl.add_pred(dname='action_up', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF('action_up', terms=8, init=w, sig=2, predColl=self.predColl, fast=False, post_terms=pt), use_neg=True, Fam='eq', exc_preds=excs, count_type=count_type, count_th=100, arg_funcs=['M']) pt = [('and', 'not en_right()')] self.predColl.add_pred(dname='action_right', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF('action_right', terms=8, init=w, sig=2, predColl=self.predColl, fast=False, post_terms=pt), use_neg=True, Fam='eq', exc_preds=excs, count_type=count_type, arg_funcs=['M']) pt = [('and', 'not en_left()')] self.predColl.add_pred(dname='action_left', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF('action_left', terms=8, init=w, sig=2, predColl=self.predColl, fast=False, post_terms=pt), use_neg=True, Fam='eq', exc_preds=excs, count_type=count_type, arg_funcs=['M']) pt = [('and', 'not en_down()')] self.predColl.add_pred(dname='action_down', arguments=[], variables=['N', 'Y', 'Y'], pFunc=DNF('action_down', terms=8, init=w, sig=2, predColl=self.predColl, fast=False, post_terms=pt), use_neg=True, Fam='eq', exc_preds=excs, count_type=count_type, arg_funcs=['M']) self.predColl.initialize_predicates() self.bg = Background(self.predColl) self.bg.add_backgroud('X_U', ('%d' % 0, )) self.bg.add_backgroud('X_D', ('%d' % (self.DIM1 - 1), )) self.bg.add_backgroud('Y_L', ('%d' % 0, )) self.bg.add_backgroud('Y_R', ('%d' % (self.DIM2 - 1), )) for i in range(self.DIM1): self.bg.add_backgroud('sameX', ( '%d' % i, '%d' % i, )) for i in range(self.DIM2): self.bg.add_backgroud('sameY', ( '%d' % i, '%d' % i, )) for i in range(self.DIM2): for j in range(self.DIM2): if i < j: self.bg.add_backgroud('ltY', ( '%d' % i, '%d' % j, )) if abs(i - j) < 2: self.bg.add_backgroud('close', ( '%d' % i, '%d' % j, )) print('displaying config setting...') self.mdl = ILPRLEngine(args=self.args, predColl=self.predColl, bgs=None)
from Lib.ILPRLEngine import * from Lib.DNF import DNF import argparse #define constants C=['a','b','c'] Ls = [ 'a','b','c','acbc' , 'cbca' , 'cbc','acb' , 'bca' , 'ac' , 'ca' , 'ab' , 'ba','cb','bc'] Constants = dict( {'C':C,'L':(Ls )}) #define predicates predColl = PredCollection (Constants) predColl.add_list_preds( ops=['eqC', 'singleL']) pred_1 = predColl.add_pred(dname='reverse' ,arguments=['L','L'] , pFunc = DNF('reverse',terms=4,init=[-1,.1,-1,-1],sig=2), arg_funcs=['tH','Th'] ) predColl.initialize_predicates() #add background bg = Background( predColl ) bg.add_list_bg(C,Ls ,ops=['eqC', 'singleL']) for a in Ls: for b in Ls: if a==b[::-1]: bg.add_example('reverse', (a,b)) bg.add_all_neg_example('reverse')
predColl.add_pred(dname='wife', arguments=['C', 'C']) predColl.add_pred(dname='husband', arguments=['C', 'C']) predColl.add_pred(dname='father', arguments=['C', 'C']) predColl.add_pred(dname='daughter', arguments=['C', 'C']) predColl.add_pred(dname='mother', arguments=['C', 'C']) predColl.add_pred(dname='son', arguments=['C', 'C']) predColl.add_pred(dname='sister', arguments=['C', 'C']) predColl.add_pred(dname='brother', arguments=['C', 'C']) predColl.add_pred(dname='aunt', arguments=['C', 'C']) predColl.add_pred(dname='uncle', arguments=['C', 'C']) predColl.add_pred(dname='niece', arguments=['C', 'C']) predColl.add_pred(dname='grandparent', arguments=['C', 'C']) predColl.add_pred(dname='nephew', arguments=['C', 'C'], variables=['C'], pFunc=DNF('nephew', terms=4, init=[1, -1, -2, .1], sig=2)) predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_backgroud('father', ('Christopher', 'Arthur')) bg.add_backgroud('father', ('Christopher', 'Victoria')) bg.add_backgroud('father', ('Andrew', 'James')) bg.add_backgroud('father', ('Andrew', 'Jennifer')) bg.add_backgroud('father', ('James', 'Colin')) bg.add_backgroud('father', ('James', 'Charlotte')) bg.add_backgroud('father', ('Roberto', 'Emilio')) bg.add_backgroud('father', ('Roberto', 'Lucia')) bg.add_backgroud('father', ('Pierro', 'Marco'))
predColl.add_pred(dname='hasPosition_%s' % pos, arguments=['P']) # predColl.add_pred(dname='hasanyPosition',arguments=['P'] ) predColl.add_pred(dname='projectMember', arguments=['P', 'X']) for phase in C_phase: predColl.add_pred(dname='inPhase_%s' % phase, arguments=['P']) predColl.add_pred(dname='inanyPhase', arguments=['P']) predColl.add_pred(dname='student', arguments=['P'], variables=['P'], pFunc=DNF( 'student', terms=2, predColl=predColl, init_terms=['advisedBy(A,B)', 'tempAdvisedBy(A,B)'], fast=True), use_neg=False, Fam='or', chunk_count=0, inc_preds=['advisedBy', 'tempAdvisedBy']) predColl.add_pred(dname='professor', arguments=['P'], variables=['P'], pFunc=DNF( 'professor', terms=2, predColl=predColl, init_terms=['advisedBy(B,A)', 'tempAdvisedBy(B,A)'], fast=True),
################################################ Constants = {'P': Persons, 'G': Genres, 'M': Movies} predColl = PredCollection(Constants) predColl.add_pred(dname='director', arguments=['P']) predColl.add_pred(dname='actor', arguments=['P']) predColl.add_pred(dname='genre', arguments=['P', 'G'], variables=[]) predColl.add_pred(dname='isFemale', arguments=['P']) predColl.add_pred(dname='movie', arguments=['M', 'P'], variables=['P']) predColl.add_pred( dname='workedUnder', arguments=['P', 'P'], variables=['P', 'M'], pFunc=DNF('workedUnder', terms=1, init=[1, .1, 0, .5], sig=1), use_neg=True, exc_conds=[('*', 'rep1')], exc_terms=[], Fam='or', ) # this is the version without the recursion #predColl.add_pred(dname='workedUnder',arguments=['P','P'] , variables=['P','M'] ,pFunc = DNF('workedUnder',terms=1,init=[1,.1,0,.5],sig=1) , use_neg=True, exc_conds=[('*','rep1') ] , exc_terms=[],exc_pred=['workedUnder'] Fam='or', ) predColl.initialize_predicates() def PI(item, i, j): return Persons[C_person[i].index(item[j])]
def define_preds(self): nCOLOR = 10 Colors = [i for i in range(nCOLOR)] Pos = [i for i in range(12)] # Ds = ['%d'%i for i in range(self.num_box)] self.Constants = dict({ 'C': Colors, 'P': Pos, 'Q': Pos }) #, 'N':['%d'%i for i in range(6)] }) self.predColl = PredCollection(self.Constants) self.predColl.add_pred(dname='color', arguments=['P', 'Q', 'C']) for i in range(nCOLOR): self.predColl.add_pred(dname='is%d' % i, arguments=['C']) # self.predColl.add_pred(dname='Pos0' ,arguments=['P']) # self.predColl.add_pred(dname='Pos11' ,arguments=['P']) self.predColl.add_pred(dname='has_key', arguments=['C']) self.predColl.add_pred(dname='neq', arguments=['C', 'C']) # self.predColl.add_pred(dname='lt' ,arguments=['P','P']) # self.predColl.add_pred(dname='incp' ,arguments=['P','P']) self.predColl.add_pred(dname='incq', arguments=['Q', 'Q']) self.predColl.add_pred(dname='isBK', arguments=['P', 'Q'], variables=['C'], pFunc=DNF('isBK', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['color(A,B,C), is0(C)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred(dname='isAgent', arguments=['P', 'Q'], variables=['C'], pFunc=DNF('isAgent', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['color(A,B,C), is1(C)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred(dname='isGem', arguments=['P', 'Q'], variables=['C'], pFunc=DNF('isGem', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['color(A,B,C), is2(C)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred( dname='isItem', arguments=['P', 'Q'], variables=[], pFunc=DNF('isItem', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['not isBK(A,B), not isAgent(A,B)'], predColl=self.predColl, fast=True), use_neg=True, Fam='eq') # self.predColl.add_pred(dname='sameColor' ,arguments=['P','P','P','P'], variables=['C'] ,pFunc = # DNF('sameColor',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['color(A,B,E), color(C,D,E)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='or') self.predColl.add_pred( dname='locked', arguments=['P', 'Q'], variables=['Q'], pFunc=DNF('locked', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['isItem(A,B), isItem(A,C), incq(B,C)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred( dname='isLock', arguments=['P', 'Q'], variables=['Q'], pFunc=DNF('isLock', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['isItem(A,B), isItem(A,C), incq(C,B)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') # self.predColl.add_pred(dname='locked1' ,arguments=['P','P'], variables=['P'] ,pFunc = # DNF('locked1',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['isItem(A,B), isItem(C,B), inc(C,B)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='or') #self.predColl.add_pred(dname='LockColor' ,arguments=['C','C'], variables=['P','Q','Q'] ,pFunc = # DNF('LockColor',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['isItem(C,D), isItem(C,E), color(C,D,A), color(C,E,B), incq(D,E)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='eq') #self.predColl.add_pred(dname='loosekey' ,arguments=['P','Q'], variables=[] ,pFunc = # DNF('loosekey',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['isItem(A,B), not isLock(A,B), not locked(A,B)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='eq') #self.predColl.add_pred(dname='key_color' ,arguments=['P','Q','C'], variables=['Q'] ,pFunc = ## DNF('key_color',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['isItem(A,B), isLock(A,B), color(A,B,C)', # 'isItem(A,B), locked(A,D), incq(D,B), color(A,D,C)' ],predColl=self.predColl,fast=True) , use_neg=True, Fam='eq') #self.predColl.add_pred(dname='inGoal' ,arguments=['C'], variables=['C'] ,pFunc = # DNF('inGoal',terms=2,init=[1,.1,-1,.1],sig=2,init_terms=['is2(A)' , 'inGoal(B), LockColor(B,A)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='or') # self.predColl.add_pred(dname='valid',oname='valid',arguments=['P','P'] , variables=['C'] ,pFunc = # DNF('valid',terms=2,init_terms=['loosekey(A,B)', 'isItem(A,B), isLock(A,B), color(A,B,C), has_key(C)'],sig=2,predColl=self.predColl,fast=True ) , use_neg=True, Fam='eq') #,post_terms=[('and','isItem(A,B)')] ) # self.predColl.add_pred(dname='move',oname='move',arguments=['P','P'] , variables=['C'] ,pFunc = # DNF('move',terms=4,init=[-1,.1,-1,.1],sig=2,predColl=self.predColl,fast=False,post_terms=[('and','not isBK(A,B)')]) , use_neg=True, Fam='eq') # self.predColl.add_pred(dname='move',oname='move',arguments=['P','P'] , variables=['C','C'] ,pFunc = # DNF('move',terms=8,init=[1,-1,-2,.3],sig=1,predColl=self.predColl,fast=False,post_terms=[('and','isItem(A,B)')]) , use_neg=True, Fam='eq', exc_preds=['move']+[ 'is%d'%i for i in range(nCOLOR)],exc_conds=[('*','rep1') ]) # DNF('move',terms=5,init=[1,-3,-1,.1],sig=2,predColl=self.predColl,fast=False,post_terms=[('and','isItem(A,B)')]) , use_neg=False, Fam='eq', exc_preds=['move']+[ 'is%d'%i for i in range(nCOLOR)],exc_conds=[('*','rep1') ]) # DNF('move',terms=10,init=[1,-1,-1,.1],sig=2,predColl=self.predColl,fast=False,post_terms=[('and','isItem(A,B)'),('or','loosekey(A,B)')]) , use_neg=True, Fam='eq', exc_preds=['move', 'color']) # DNF('move',terms=5,init_terms=['color(A,B,C), inGoal(C), loosekey(A,B)','key_color(A,B,D), color(A,B,C), inGoal(D), isLock(A,B), inGoal(D), LockColor(D,C), has_key(C)'],sig=2,predColl=self.predColl,fast=True ) , use_neg=True, Fam='eq') # DNF('move',terms=2,init_terms=['loosekey(A,B)', 'isLock(A,B), color(A,B,C), has_key(C)'],sig=2,predColl=self.predColl,fast=True ) , use_neg=True, Fam='eq') # for i in range(10): # self.predColl.add_pred(dname='aux%d'%i,oname='aux%d'%i,arguments=['P','Q'] , variables=['C','C'] ,pFunc = # CONJ('aux%d'%i,init=[-1,1],init_terms=[],sig=1,predColl=self.predColl,post_terms=[]) , use_neg=False, Fam='eq', exc_preds=['move']+[ '%d'%i for i in range(nCOLOR)]+['aux%d'%i for i in range(10)],exc_conds=[('*','rep1') ]) # it=[] #self.predColl.add_pred(dname='Q',oname='Q',arguments=['P','Q'] , variables=['P', 'C' ] ,pFunc = # DNF('Q',terms=6,init=[-1,.1,-1,1],init_terms=[],sig=1,predColl=self.predColl ) , use_neg=True, Fam='eq', exc_preds=['move','Q']+[ 'is%d'%i for i in range(nCOLOR)],exc_conds=[('*','rep1') ]) #self.predColl.add_pred(dname='move',oname='move',arguments=['P','Q'] , variables=['C' ] ,pFunc = # DNF('move',terms=6,init=[1,-1,-2,1],init_terms=[],sig=2,predColl=self.predColl,post_terms=[('and','isItem(A,B)'),('and','inGoal(C)') ]) , use_neg=True, Fam='eq', exc_preds=['move','Q']+[ 'is%d'%i for i in range(nCOLOR)],exc_conds=[('*','rep1') ]) pt = [('and', 'isItem(A,B)')] pt = [] self.predColl.add_pred(dname='move', oname='move', arguments=['P', 'Q'], variables=['C'], pFunc=DNF('move', terms=6, init=[-1, .1, -1, .1], init_terms=[], sig=1, predColl=self.predColl, post_terms=pt), use_neg=True, Fam='eq', exc_preds=['move', 'Q'] + ['is%d' % i for i in range(nCOLOR)], exc_conds=[('*', 'rep1')]) # for i in range(5): # if i==0: # tp="eq" # # it=['color(A,B,C), inGoal(C), loosekey(A,B)'] # else: # tp="or" # it=[] # vvv=1 # vv=-1 # Vars = ['C','C' ] # if i==5: # Vars=['C','C'] # vvv=2 # vv=-1 # # if i==1: # # it=['key_color(A,B,D), color(A,B,C), isLock(A,B), inGoal(D), LockColor(D,C), has_key(C)'] # self.predColl.add_pred(dname='move%d'%i,oname='move',arguments=['P','Q'] , variables=Vars ,pFunc = # CONJ('move%d'%i,init=[vv,vvv],init_terms=it,sig=1,predColl=self.predColl,post_terms=[('and','isItem(A,B)')]) , use_neg=False, Fam=tp, exc_preds=['move']+[ '%d'%i for i in range(nCOLOR)],exc_conds=[('*','rep1') ]) # self.predColl.add_pred(dname='move2',oname='move',arguments=['P','P'] , variables=['C'] ,pFunc = # CONJ('move2',init=[-1,.1],sig=2,predColl=self.predColl,fast=False) , use_neg=True, Fam='eq', exc_preds=['move'],exc_conds=[('*','rep1') ]) # MLP('move',dims=[64,64,1], acts=[relu1,relu1,tf.nn.sigmoid] ) , use_neg=False, Fam='eq') # DNF('move',terms=10,init=[1,-1,-1,.1],sig=2,predColl=self.predColl,fast=False,post_terms=[('and','isItem(A,B)'),('or','loosekey(A,B)')]) , use_neg=True, Fam='eq', exc_preds=['move', 'color']) # self.predColl.add_pred(dname='move',oname='move',arguments=['P','P'] , variables=['C'] ,pFunc = # DNF('move',terms=2,init_terms=['loosekey(A,B)', 'isLock(A,B), color(A,B,C), has_key(C)'],sig=2,predColl=self.predColl,fast=True ) , use_neg=True, Fam='eq') # self.predColl.add_pred(dname='has_key' ,arguments=['C'], variables=['P','P'] ,pFunc = # DNF('has_key',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['loosekey(B,C), move(B,C), color(B,C,A)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='or') # self.predColl.add_pred(dname='enableLeft' ,arguments=['P','P'], variables=['P'] ,pFunc = # DNF('enableLeft',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['agent(A,B), inc(C,A), not pos0(C)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='or') # self.predColl.add_pred(dname='enableRight' ,arguments=['P','P'], variables=['P'] ,pFunc = # DNF('enableRight',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['agent(A,B), not locked(A,B), not Pos0(A)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='or') # self.predColl.add_pred(dname='enableLeft' ,arguments=['P','P'], variables=['P'] ,pFunc = # DNF('enableLeft',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['agent(A,B), not locked(A,B), not Pos0(A)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='or') # self.predColl.add_pred(dname='enableLeft' ,arguments=['P','P'], variables=['P'] ,pFunc = # DNF('enableLeft',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['agent(A,B), not locked(A,B), not Pos0(A)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='or') # self.predColl.add_pred(dname='moveLeft',oname='moveLeft',arguments=[] , variables=['P','P','C'] ,pFunc = # DNF('moveLeft',terms=6,init=[1,.1,-1,.1],sig=2,predColl=self.predColl,fast=False) , use_neg=True, Fam='eq') # self.predColl.add_pred(dname='moveRight',oname='moveRight',arguments=[] , variables=['P','P','C'] ,pFunc = # DNF('moveRight',terms=6,init=[1,.1,-1,.1],sig=2,predColl=self.predColl,fast=False) , use_neg=True, Fam='eq') # self.predColl.add_pred(dname='moveLeft',oname='moveLeft',arguments=[] , variables=['P','P','C'] ,pFunc = # DNF('moveLeft',terms=6,init=[1,.1,-1,.1],sig=2,predColl=self.predColl,fast=False) , use_neg=True, Fam='eq') # self.predColl.add_pred(dname='moveLeft',oname='moveLeft',arguments=[] , variables=['P','P','C'] ,pFunc = # DNF('moveLeft',terms=6,init=[1,.1,-1,.1],sig=2,predColl=self.predColl,fast=False) , use_neg=True, Fam='eq') self.predColl.initialize_predicates() self.bg = Background(self.predColl) #define backgrounds self.bg.add_backgroud('Pos0', (0, )) self.bg.add_backgroud('Pos11', (11, )) for i in range(nCOLOR): self.bg.add_backgroud('is%d' % i, (i, )) for j in range(nCOLOR): if i != j: self.bg.add_backgroud('neq', (i, i)) for i in range(12): self.bg.add_backgroud('eq', (i, i)) for j in range(12): if i < j: self.bg.add_backgroud('lt', (i, j)) if j == i + 1: # self.bg.add_backgroud('incp',(i,j) ) self.bg.add_backgroud('incq', (i, j)) print('displaying config setting...') self.mdl = ILPRLEngine(args=self.args, predColl=self.predColl, bgs=None)
C = [] for i in range(maxN + 1): C.append('%d' % i) Constants = dict({'P': C}) #define predicates predColl = PredCollection(Constants) predColl.add_pred(dname='eq', arguments=['P', 'P']) predColl.add_pred(dname='zero', arguments=['P']) predColl.add_pred(dname='inc', arguments=['P', 'P']) predColl.add_pred(dname='add', arguments=['P', 'P', 'P'], variables=['P', 'P'], pFunc=DNF('add', terms=6, init=[-1, .1, -.1, .1], sig=2), arg_funcs=[], Fam='or', exc_conds=[('*', 'rep1')], exc_terms=['add(A,B,C)']) predColl.initialize_predicates() #add background bg = Background(predColl) for i in range(maxN + 1): if i == 0: bg.add_backgroud('zero', ('%d' % i, )) if i > 0: bg.add_backgroud('inc', ('%d' % (i - 1), '%d' % (i)))
#define constants maxN =5 C = [] for i in range(maxN+1): C.append( '%d'%i) Constants = dict( {'P':C }) #define predicates predColl = PredCollection (Constants) predColl.add_pred(dname='eq' ,arguments=['P','P'] ) predColl.add_pred(dname='zero',arguments=['P'] ) predColl.add_pred(dname='inc' ,arguments=['P','P'] ) predColl.add_pred(dname='add' ,arguments=['P','P','P'],variables=[ 'P','P'] , pFunc = DNF('add',terms=6,init=[-1,.1,-.1,.1],sig=2 ),arg_funcs=[ ] , Fam='or',exc_conds=[('*','rep1') ] , exc_terms=['add(A,B,C)']) predColl.initialize_predicates() #add background bg = Background( predColl) for i in range(maxN+1): if i==0: bg.add_backgroud ('zero', ('%d'%i,)) if i>0: bg.add_backgroud ('inc', ('%d'%(i-1),'%d'%(i)) ) for i in range(maxN+1): for j in range(maxN+1):
def define_preds(self): nCOLOR = 10 Colors=[i for i in range(nCOLOR)] Pos = [i for i in range(12)] # Ds = ['%d'%i for i in range(self.num_box)] self.Constants = dict( {'C':Colors,'P':Pos,'Q':Pos}) #, 'N':['%d'%i for i in range(6)] }) self.predColl = PredCollection (self.Constants) self.predColl.add_pred(dname='color' ,arguments=['P','Q','C']) for i in range(nCOLOR): self.predColl.add_pred(dname='is%d'%i ,arguments=['C']) self.predColl.add_pred(dname='has_key' ,arguments=['C'] ) self.predColl.add_pred(dname='neq' ,arguments=['C','C']) self.predColl.add_pred(dname='incq' ,arguments=['Q','Q']) self.predColl.add_pred(dname='isBK' ,arguments=['P','Q'], variables=['C'] ,pFunc = DNF('isBK',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['color(A,B,C), is0(C)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='eq') self.predColl.add_pred(dname='isAgent' ,arguments=['P','Q'], variables=['C'] ,pFunc = DNF('isAgent',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['color(A,B,C), is1(C)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='eq') self.predColl.add_pred(dname='isGem' ,arguments=['P','Q'], variables=['C'] ,pFunc = DNF('isGem',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['color(A,B,C), is2(C)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='eq') self.predColl.add_pred(dname='isItem' ,arguments=['P','Q'], variables=[] ,pFunc = DNF('isItem',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['not isBK(A,B), not isAgent(A,B)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='eq') pt=[] self.predColl.add_pred(dname='move',oname='move',arguments=['P','Q'] , variables=['C' ] ,pFunc = DNF('move',terms=6,init=[-1,.1,-1,.1],init_terms=[],sig=1,predColl=self.predColl,post_terms=pt) , use_neg=True, Fam='eq', exc_preds=['move','Q']+[ 'is%d'%i for i in range(nCOLOR)],exc_conds=[('*','rep1') ]) self.predColl.initialize_predicates() self.bg = Background( self.predColl ) #define backgrounds self.bg.add_backgroud('Pos0',(0,) ) self.bg.add_backgroud('Pos11',(11,) ) for i in range(nCOLOR): self.bg.add_backgroud('is%d'%i , (i,)) for j in range(nCOLOR): if i!=j: self.bg.add_backgroud('neq',(i,i) ) for i in range(12): self.bg.add_backgroud('eq',(i,i) ) for j in range(12): if i<j: self.bg.add_backgroud('lt',(i,j) ) if j==i+1: # self.bg.add_backgroud('incp',(i,j) ) self.bg.add_backgroud('incq',(i,j) ) print('displaying config setting...') self.mdl = ILPRLEngine( args=self.args ,predColl=self.predColl ,bgs=None )
def define_preds(self): X = ['%d'%i for i in range(self.DIM1)] Y = ['%d'%i for i in range(self.DIM2)] self.Constants = dict( {'N':X,'Y':Y}) self.predColl = PredCollection (self.Constants) # self.predColl.add_pred(dname='agentX' ,arguments=['N']) # self.predColl.add_pred(dname='agentY' ,arguments=['Y']) for i in range(self.DIM1): if i==0 or i==self.DIM1-1: self.predColl.add_pred(dname='X_%d'%i,arguments=['N']) for i in range(self.DIM2): if i==0 or i==self.DIM2-1: self.predColl.add_pred(dname='Y_%d'%i,arguments=['Y']) # self.predColl.add_pred(dname='f_1',arguments=['N','Y'], variables=[ ],pFunc = # DNF('f_1',init_terms=['agentX(A), agentY(B)' ],predColl=self.predColl,fast=True) , use_neg=False, Fam='eq') self.predColl.add_pred(dname='f_1',arguments=['N','Y']) self.predColl.add_pred(dname='f_2',arguments=['N','Y']) self.predColl.add_pred(dname='f_3',arguments=['N','Y']) self.predColl.add_pred(dname='f_4',arguments=['N','Y']) self.predColl.add_pred(dname='ltY',arguments=['Y','Y']) self.predColl.add_pred(dname='close',arguments=['Y','Y']) count_type = 'max' excs = ['action_noop', 'action_up', 'action_down','action_left','action_right', 'Q'] w = [-1,.1,-2,.1] Alicount_type = None self.predColl.add_pred(dname='en_up' ,arguments=[], variables=[ 'N','Y', 'Y'],pFunc = DNF('en_up',terms=1,init=[-1,.1,-1,.1],sig=2, init_terms=['f_1(A,B), X_0(A)','f_1(A,B), f_3(M_A,C), close(B,C), ltY(B,C)', 'f_1(A,B), f_2(M_A,C), close(B,C), ltY(C,B)' ], predColl=self.predColl,fast=True) , use_neg=False, Fam='eq',count_type=count_type, count_th=100,arg_funcs=['M']) self.predColl.add_pred(dname='en_down' ,arguments=[ ], variables=['N','Y', 'Y'],pFunc = DNF('en_down',terms=1,init=[-1,.1,-1,.1],sig=2, init_terms=['f_1(A,B), X_%d(A)'%(self.DIM1-1) , 'f_1(A,B), f_3(P_A,C), close(B,C), ltY(B,C)', 'f_1(A,B), f_2(P_A,C), close(B,C), ltY(C,B)' ] ,predColl=self.predColl,fast=True) , use_neg=False, Fam='eq',count_type=count_type, count_th=100,arg_funcs=['P']) self.predColl.add_pred(dname='en_right' ,arguments=[ ], variables=['N','Y','Y'],pFunc = DNF('en_right',terms=1,init=[-1,.1,-1,.1],sig=2, init_terms=['f_1(A,B), Y_%d(B)'%(self.DIM2-1) ,'f_1(A,B), f_3(A,C), close(B,C), ltY(B,C)' ,'f_1(A,B), f_2(A,C), close(B,C), ltY(B,C)' ], predColl=self.predColl,fast=True) , use_neg=False, Fam='eq',count_type=count_type, count_th=100,arg_funcs=[ ]) self.predColl.add_pred(dname='en_left' ,arguments=[ ], variables=['N','Y','Y'],pFunc = DNF('en_left',terms=1,init=[-1,.1,-1,.1],sig=2, init_terms=['f_1(A,B), Y_%d(B)'%0, 'f_1(A,B), f_2(A,C), close(B,C), ltY(C,B)','f_1(A,B), f_3(A,C), close(B,C), ltY(C,B)' ], predColl=self.predColl,fast=True) , use_neg=False, Fam='eq',count_type=count_type, count_th=100,arg_funcs=[ ]) self.predColl.add_pred(dname='en_noop' ,arguments=[ ], variables=[],pFunc = DNF('en_noop',terms=1,init=[-1,.1,-1,.1],sig=2, #init_terms=['f_1(A,B), f_3(A,C), ltY(C,B)','f_1(A,B), f_2(A,C), ltY(B,C)' ], init_terms=['en_right()','en_left()' ], predColl=self.predColl,fast=True) , use_neg=False, Fam='eq',count_type=count_type, count_th=100) pt = [('and', 'not en_noop()') ] self.predColl.add_pred(dname='action_noop',arguments=[] , variables=['N','Y','Y' ] ,pFunc = DNF('action_noop',terms=8,init=w,sig=2,predColl=self.predColl,fast=False,post_terms=pt) , use_neg=True, Fam='eq' , exc_preds=excs,count_type=count_type,arg_funcs=['M']) pt = [('and', 'not en_up()') ] self.predColl.add_pred(dname='action_up',arguments=[] , variables=['N','Y','Y' ] ,pFunc = DNF('action_up',terms=8,init=w,sig=2,predColl=self.predColl,fast=False,post_terms=pt) , use_neg=True, Fam='eq' , exc_preds=excs,count_type=count_type, count_th=100,arg_funcs=[ 'M']) pt = [('and', 'not en_right()') ] self.predColl.add_pred(dname='action_right',arguments=[] , variables=['N','Y','Y' ] ,pFunc = DNF('action_right',terms=8,init=w,sig=2,predColl=self.predColl,fast=False,post_terms=pt) , use_neg=True, Fam='eq' , exc_preds=excs,count_type=count_type,arg_funcs=['M']) pt = [('and', 'not en_left()') ] self.predColl.add_pred(dname='action_left',arguments=[] , variables=['N','Y', 'Y' ] ,pFunc = DNF('action_left',terms=8,init=w,sig=2,predColl=self.predColl,fast=False,post_terms=pt) , use_neg=True, Fam='eq' , exc_preds=excs,count_type=count_type, arg_funcs=['M']) pt = [('and', 'not en_down()') ] self.predColl.add_pred(dname='action_down',arguments=[] , variables=['N','Y','Y' ] ,pFunc = DNF('action_down',terms=8,init=w,sig=2,predColl=self.predColl,fast=False,post_terms=pt) , use_neg=True, Fam='eq' , exc_preds=excs,count_type=count_type, arg_funcs=['M']) self.predColl.initialize_predicates() self.bg = Background( self.predColl ) #define backgrounds self.bg.add_backgroud('X_%d'%0 ,('%d'%0,) ) self.bg.add_backgroud('X_%d'%(self.DIM1-1) ,('%d'%(self.DIM1-1),) ) self.bg.add_backgroud('Y_%d'%0 ,('%d'%0,) ) self.bg.add_backgroud('Y_%d'%(self.DIM2-1) ,('%d'%(self.DIM2-1),) ) self.bg.add_backgroud('Y_%d'%0 ,('%d'%1,) ) self.bg.add_backgroud('Y_%d'%(self.DIM2-1) ,('%d'%(self.DIM2-2),) ) for i in range(self.DIM2): for j in range(self.DIM2): if i<=j+1: self.bg.add_backgroud('ltY' ,('%d'%i,'%d'%j,) ) if abs(i-j)<3: self.bg.add_backgroud('close' ,('%d'%i,'%d'%j,) ) print('displaying config setting...') self.mdl = ILPRLEngine( args=self.args ,predColl=self.predColl ,bgs=None )
def define_preds(self): Box = ['%d' % i for i in range(self.num_box + 1)] Ds = ['%d' % i for i in range(self.num_box + 1)] self.Constants = dict({'C': Box, 'D': Ds}) self.predColl = PredCollection(self.Constants) self.predColl.add_pred(dname='posH', arguments=['C', 'D']) self.predColl.add_pred(dname='posV', arguments=['C', 'D']) self.predColl.add_pred(dname='is_one', arguments=['D']) self.predColl.add_pred(dname='lt', arguments=['D', 'D']) self.predColl.add_pred(dname='inc', arguments=['D', 'D']) self.predColl.add_pred(dname='same', arguments=['C', 'C']) self.predColl.add_pred(dname='is_floor', arguments=['C']) self.predColl.add_pred(dname='is_blue', arguments=['C']) self.predColl.add_pred(dname='same_col', arguments=['C', 'C'], variables=['D'], pFunc=DNF('same_col', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['posH(A,C), posH(B,C)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred( dname='above', arguments=['C', 'C'], variables=['D', 'D'], pFunc=DNF( 'above', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['same_col(A,B), posV(A,C), posV(B,D), lt(D,C)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred( dname='below', arguments=['C', 'C'], variables=['D', 'D'], pFunc=DNF( 'below', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['same_col(A,B), posV(A,C), posV(B,D), lt(C,D)'], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred( dname='on', arguments=['C', 'C'], variables=['D', 'D'], pFunc=DNF('on', terms=2, init=[1, .1, -1, .1], sig=2, init_terms=[ 'is_floor(B), posV(A,C), is_one(C)', 'same_col(A,B), posV(A,C), posV(B,D), inc(D,C)' ], predColl=self.predColl, fast=True), use_neg=False, Fam='eq') self.predColl.add_pred(dname='isCovered', arguments=['C'], variables=['C'], pFunc=DNF( 'isCovered', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['on(B,A), not is_floor(A)'], predColl=self.predColl, fast=True), use_neg=True, Fam='eq') self.predColl.add_pred( dname='lower', oname='lower', arguments=['C', 'C'], variables=['D', 'D'], pFunc=DNF('lower', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=['posV(A,C), posV(B,D), lt(C,D)'], predColl=self.predColl, fast=True), use_neg=True, Fam='eq') self.predColl.add_pred( dname='moveable', oname='moveable', arguments=['C', 'C'], variables=[], pFunc=DNF( 'moveable', terms=1, init=[1, .1, -1, .1], sig=2, init_terms=[ 'not isCovered(A), not isCovered(B), not same(A,B), not is_floor(A), not on(A,B), not is_blue(A), not is_floor(B), not lower(B,A)' ], predColl=self.predColl, fast=True), use_neg=True, Fam='eq') pt = [ ('and', 'moveable(A,B)'), ] self.predColl.add_pred(dname='move', oname='move', arguments=['C', 'C'], variables=[], pFunc=DNF('move', terms=4, init=[1, -1, -1, .1], sig=2, predColl=self.predColl, fast=False, post_terms=pt), use_neg=True, Fam='eq', exc_preds=[], exc_conds=[('*', 'rep1')]) self.predColl.initialize_predicates() self.bg = Background(self.predColl) #define backgrounds self.bg.add_backgroud('is_floor', ('0', )) self.bg.add_backgroud('is_one', ('1', )) self.bg.add_backgroud('is_blue', ('1', )) for i in range(self.num_box + 1): self.bg.add_backgroud('same', ('%d' % i, '%d' % i)) if '%d' % (i + 1) in Ds: self.bg.add_backgroud('inc', ('%d' % i, '%d' % (i + 1))) for i in range(self.num_box + 1): for j in range(self.num_box + 1): if i < j: self.bg.add_backgroud('lt', ('%d' % i, '%d' % (j))) print('displaying config setting...') self.mdl = ILPRLEngine(args=self.args, predColl=self.predColl, bgs=None)
maxN = 5 #define constants C = [] for i in range(maxN + 1): C.append('%d' % i) Constants = dict({'C': C}) #define predicates predColl = PredCollection(Constants) predColl.add_pred(dname='zero', arguments=['C']) predColl.add_pred(dname='succ', arguments=['C', 'C']) predColl.add_pred(dname='even', arguments=['C'], variables=['C', 'C'], pFunc=DNF('even', terms=4, init=[-1, 1, -1, 1], sig=2), Fam='eq') predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_backgroud('zero', ('0', )) for i in range(maxN + 1): if (i + 1 <= maxN): bg.add_backgroud('succ', ('%d' % i, '%d' % (i + 1))) bg.add_example('even', ('%d' % i, ), float(i % 2 == 0)) def bgs(it, is_training): return [
def define_preds(self): nC=6 nD=16 C = ['%d'%i for i in range(nC)] D = ['%d'%i for i in range(nD)] self.Constants = dict( { 'C':C, 'D':D}) #, 'N':['%d'%i for i in range(6)] }) self.predColl = PredCollection (self.Constants) self.predColl.add_pred(dname='pos' ,arguments=['C','D' ]) self.predColl.add_pred(dname='question' ,arguments=['C']) self.predColl.add_pred(dname='eq' ,arguments=['D','D' ]) self.predColl.add_pred(dname='ltD' ,arguments=['D','D','D']) self.predColl.add_pred(dname='gtD' ,arguments=['D','D','D']) self.predColl.add_pred(dname='left' ,arguments=['D']) self.predColl.add_pred(dname='button' ,arguments=['D']) for i in range(self.q_dim): self.predColl.add_pred(dname='is_q_%d'%i ,arguments=[]) for i in range(nC): self.predColl.add_pred(dname='is_color_%d'%i ,arguments=['C']) # ,pFunc = # DNF('obj',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['is_l_0(A)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='or') self.predColl.add_pred(dname='rectangle' ,arguments=['C']) self.predColl.add_pred(dname='exist' ,arguments=['C']) self.predColl.add_pred(dname='eqC' ,arguments=['C','C'],variables=['D','D'] ,pFunc = DNF('eqC',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['pos(A,C), pos(B,D), eq(C,D)'],predColl=self.predColl,fast=True) , use_neg=True, Fam='or') self.predColl.add_pred(dname='closer',arguments=['C','C','C'], variables=['D','D','D'] ,pFunc = DNF('closer',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['pos(A,D), pos(B,E), pos(C,F), exist(A), exist(B), exist(C), ltD(D,E,F)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='or') self.predColl.add_pred(dname='farther',arguments=['C','C','C'], variables=['D','D','D'] ,pFunc = DNF('farther',terms=1,init=[1,.1,-1,.1],sig=2,init_terms=['pos(A,D), pos(B,E), pos(C,F), exist(A), exist(B), exist(C), gtD(D,E,F)'],predColl=self.predColl,fast=True) , use_neg=False, Fam='or') self.predColl.add_pred(dname='closest',arguments=['C','C'], variables=['C'] ,pFunc = DNF('closest',terms=4,init=[1,.1,-1,.1],sig=2,init_terms=['closer(A,C,B)','not exist(A)','not exist(B)','eqC(A,B)'],predColl=self.predColl,fast=True,neg=True) , use_neg=True, Fam='eq') self.predColl.add_pred(dname='farthest',arguments=['C','C'], variables=['C'] ,pFunc = DNF('farthest',terms=4,init=[1,.1,-1,.1],sig=2,init_terms=['farther(A,C,B)','not exist(A)','not exist(B)','eqC(A,B)'],predColl=self.predColl,fast=True,neg=True) , use_neg=True, Fam='eq') exc = ['CL_%d'%i for i in range(self.a_dim)] exc=[] for k in range(0,self.a_dim): # # # # exc = ['CL_%d'%i for i in range(k,self.a_dim)] # # # # self.predColl.add_pred(dname='CL0_%d'%k,oname='CL_%d'%k,arguments=[] , variables=[] , pFunc = DNF('CL0_%d'%k,predColl=self.predColl,terms=10 ,init=[-1,.1,-1,.1],sig=2) ,use_neg=True, Fam='eq', exc_preds=exc ) # # self.predColl.add_pred(dname='CL1_%d'%k,oname='CL_%d'%k,arguments=[] , variables=['D'] , pFunc = DNF('CL1_%d'%k,predColl=self.predColl,terms=6 ,init=[-1,.1,-1,.1],sig=2) ,use_neg=True, Fam='eq',exc_conds=[('*','rep1') ] ) if k==0: post_terms=[] else: post_terms=[ ('and', 'not CL_%d()'%j ) for j in range(k)] post_terms=[] self.predColl.add_pred(dname='CL_%d'%k,oname='CL_%d'%k,arguments=[] , variables=['C','C','D'] , pFunc = DNF('CL_%d'%k,predColl=self.predColl,terms=14,init=[-1,-1,-1,.1],sig=2, post_terms=post_terms) ,use_neg=True, Fam='eq',exc_conds=[('*','rep1') ] ,exc_preds=exc ) # # # # # self.predColl.add_pred(dname='CL_%d'%k,oname='CL_%d'%k,arguments=[] , variables=['D','D'] , pFunc = MLP('CL_%d'%k,dims=[200,1], acts=[tf.nn.relu,tf.sigmoid] ) ,use_neg=False, Fam='eq', exc_preds=exc ) self.predColl.initialize_predicates() self.bg = Background( self.predColl ) # self.bg.add_backgroud('notExist', ('%d'%(nD-1),)) for i in range(nC): self.bg.add_backgroud('is_color_%d'%i, ('%d'%i,)) for i in range(nD): ri,ci=int(i//4),int(i%4) if ri>=2: self.bg.add_backgroud('button', ('%d'%i,)) if ci<2: self.bg.add_backgroud('left', ('%d'%i,)) # self.bg.add_backgroud('-%d'%i , ('%d'%i,) ) self.bg.add_backgroud('eq', ('%d'%i,'%d'%i)) for j in range(nD): rj,cj=int(j//4),int(j%4) for k in range(nD): rk,ck=int(k//4),int(k%4) d1=(ri-rj)**2+(ci-cj)**2 d2=(ri-rk)**2+(ci-ck)**2 if(d1<d2 and i!=j and i!=k and j!=k): self.bg.add_backgroud('ltD', ('%d'%i,'%d'%j,'%d'%k)) if(d1>d2 and i!=j and i!=k and j!=k): self.bg.add_backgroud('gtD', ('%d'%i,'%d'%j,'%d'%k)) a = '%d'%i # self.bg.add_backgroud('is_c_%d'%i , (a,) ) # self.bg.add_backgroud('is_r_%d'%i , (a,) ) # self.bg.add_backgroud('eqC' , (a,a) ) # self.bg.add_backgroud('eqR' , (a,a) ) # for j in range(nC): # if i<j: # self.bg.add_backgroud('ltC', ('%d'%i, '%d'%j)) # self.bg.add_backgroud('ltR', ('%d'%i, '%d'%j)) bg_set=[] self.X0=OrderedDict() for p in self.predColl.outpreds: if p.oname not in bg_set: tmp = tf.expand_dims( tf.constant( self.bg.get_X0(p.oname) ,tf.float32) , 0) self.X0[p.oname] = tf.tile( tmp , [self.batch_size,1] ) print('displaying config setting...') # for arg in vars(args): # print( '{}-{}'.format ( arg, getattr(args, arg) ) ) self.mdl = ILPRLEngine( args=self.args_ilp ,predColl=self.predColl ,bgs=None )
from Lib.ILPRLEngine import * import argparse from Lib.DNF import DNF #define constants Constants = dict({'C': ['a', 'b', 'c', 'd', 'e']}) predColl = PredCollection(Constants) #define predicates predColl.add_pred(dname='edge', arguments=['C', 'C']) predColl.add_pred(dname='connected', arguments=['C', 'C'], variables=['C'], pFunc=DNF('connected', terms=4, init=[-1, .1, -1, -1])) predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_backgroud('edge', ('a', 'b')) bg.add_backgroud('edge', ('b', 'c')) bg.add_backgroud('edge', ('c', 'd')) bg.add_backgroud('edge', ('e', 'd')) bg.add_example('connected', ('a', 'b')) bg.add_example('connected', ('a', 'c')) bg.add_example('connected', ('a', 'd')) bg.add_example('connected', ('b', 'c')) bg.add_example('connected', ('b', 'd')) bg.add_example('connected', ('c', 'd')) bg.add_example('connected', ('e', 'd'))
predColl.add_pred(dname='hasanyPosition', arguments=['P']) predColl.add_pred(dname='projectMember', arguments=['P', 'X']) for phase in C_phase: predColl.add_pred(dname='inPhase_%s' % phase, arguments=['P']) predColl.add_pred(dname='inanyPhase', arguments=['P']) predColl.add_pred(dname='sameProj', arguments=['P', 'P'], variables=['X'], pFunc=DNF( 'sameProj', terms=1, init=[1, .1, -1, .1], sig=1, init_terms=['projectMember(A,C), projectMember(B,C)'], predColl=predColl, fast=True), use_neg=True, inc_preds=['projectMember', 'samePerson'], exc_conds=[], Fam='or', chunk_count=0) predColl.add_pred(dname='colaborate', arguments=['P', 'P'], variables=['T'], pFunc=DNF('colaborate', terms=1, init=[1, .1, -1, .1],
from Lib.ILPRLEngine import * from Lib.DNF import DNF import argparse #define constants C = ['a', 'b', 'c', 'd'] Ls = [',', 'abcd', 'abc', 'ab', 'a'] Constants = dict({'C': C, 'L': (Ls)}) #define predicates predColl = PredCollection(Constants) predColl.add_list_preds(ops=['eqC']) pred_1 = predColl.add_pred(dname='member', arguments=['L', 'C'], pFunc=DNF('member', terms=4, init=[-1, .1, -1, -1], sig=2), arg_funcs='tH') predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_list_bg(C, Ls, ops=['eqC']) for a in Ls: for b in C: if b in a: bg.add_example('member', (a, b)) bg.add_all_neg_example('member')
DataSets.append( (data_x[inds[ i*L:(i+1)*L],:] , data_y[inds[ i*L:(i+1)*L]])) #define predicates Constants = {} predColl = PredCollection (Constants) #predColl.add_pred(name='mt',arguments=[] ) #predColl.add_pred(name='lt',arguments=[] ) intervals_list =new_pred.spacing(data_x_pos,data_x_neg,10) for index,val in enumerate (names): predColl.add_continous(name=val,no_lt=10,no_gt=10) for i in range(1): predColl.add_pred(name='class_%d'%(i+1),arguments=[] , variables=[] , pFunc = DNF('class_%d'%(i+1),terms=1,init=[-1,.1,-1,.1],sig=2), use_cnt_vars=True,inc_preds=[ ]) predColl.initialize_predicates() #add backgrounds bg_train=[] bg_test=[] count = 0 for j in range(5): for i in range(L): bg = Background( predColl ) bg.add_example(pred_name='class_1',pair=( ), value= float(DataSets[j][1][i]==1) )
#define constants C = ['a', 'b', 'c'] Ls = [ 'a', 'b', 'c', 'acbc', 'cbca', 'cbc', 'acb', 'bca', 'ac', 'ca', 'ab', 'ba', 'cb', 'bc' ] Constants = dict({'C': C, 'L': (Ls)}) #define predicates predColl = PredCollection(Constants) predColl.add_list_preds(ops=['eqC', 'singleL']) pred_1 = predColl.add_pred(dname='reverse', arguments=['L', 'L'], pFunc=DNF('reverse', terms=4, init=[-1, .1, -1, -1], sig=2), arg_funcs=['tH', 'Th']) predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_list_bg(C, Ls, ops=['eqC', 'singleL']) for a in Ls: for b in Ls: if a == b[::-1]: bg.add_example('reverse', (a, b)) bg.add_all_neg_example('reverse')
from Lib.DNF import DNF #define constants E = ['a', 'b', 'c', 'd', 'e', 'f'] C = ['green', 'red'] Constants = dict({'E': E, 'C': C}) predColl = PredCollection(Constants) #define predicates predColl.add_pred(dname='edge', arguments=['E', 'E']) predColl.add_pred(dname='color', arguments=['E', 'C']) predColl.add_pred(dname='target', arguments=['E'], variables=['E', 'C'], pFunc=DNF('target', terms=4, init=[-1, .1, -1, .1], sig=2)) predColl.initialize_predicates() all_bg = [] #add first background bg = Background(predColl) bg.add_backgroud('edge', ('a', 'b')) bg.add_backgroud('edge', ('b', 'c')) bg.add_backgroud('edge', ('b', 'd')) bg.add_backgroud('edge', ('c', 'e')) bg.add_backgroud('edge', ('e', 'f')) bg.add_backgroud('color', ('a', 'green')) bg.add_backgroud('color', ('b', 'red')) bg.add_backgroud('color', ('c', 'green')) bg.add_backgroud('color', ('d', 'green')) bg.add_backgroud('color', ('e', 'red'))
from Lib.ILPRLEngine import * import argparse from Lib.DNF import DNF #define constants Constants = dict({'C': [ 'a','b','c','d' ,'e'] }) predColl = PredCollection (Constants) #define predicates predColl.add_pred(dname='edge' ,arguments=['C','C']) predColl.add_pred(dname='connected',arguments=['C','C'] , variables=['C' ] , pFunc = DNF('connected',terms=4,init=[-1,.1,-1,-1]) ) predColl.initialize_predicates() #add background bg = Background( predColl ) bg.add_backgroud ('edge', ('a','b') ) bg.add_backgroud ('edge', ('b','c') ) bg.add_backgroud ('edge', ('c','d') ) bg.add_backgroud ('edge', ('e','d') ) bg.add_example('connected',('a','b')) bg.add_example('connected',('a','c')) bg.add_example('connected',('a','d')) bg.add_example('connected',('b','c')) bg.add_example('connected',('b','d')) bg.add_example('connected',('c','d')) bg.add_example('connected',('e','d'))
inc_preds=['HasWordTitle', 'HasWordAuthor'], exc_conds=[], Fam='or', chunk_count=0) # predColl.add_pred(dname='sameWTV',arguments=['T','V'], variables=['W'] ,pFunc = # CONJ('sameWTV',init=[-1,.1],sig=2,init_terms=['HasWordTitle(A,C), HasWordVenue(B,C)'],predColl=predColl,fast=True) , use_neg=False, inc_preds=['HasWordTitle','HasWordVenue'], exc_conds=[ ], Fam='or',chunk_count=0) # predColl.add_pred(dname='sameWAV',arguments=['A','V'], variables=['W'] ,pFunc = # CONJ('sameWAV',init=[-1,.1],sig=2,init_terms=['HasWordAuthor(A,C), HasWordVenue(B,C)'],predColl=predColl,fast=True) , use_neg=False, inc_preds=['HasWordAuthor','HasWordVenue'], exc_conds=[ ], Fam='or',chunk_count=0) predColl.add_pred(dname='SameBib1', oname='SameBib', arguments=['C', 'C'], variables=['A'], pFunc=DNF('SameBib1', terms=1, init=[-1, .1, -1, .1], sig=2), use_neg=True, Fam='or', chunk_count=0, pairs=pair_SameBib) predColl.add_pred(dname='SameBib2', oname='SameBib', arguments=['C', 'C'], variables=['T'], pFunc=DNF('SameBib2', terms=1, init=[-1, .1, -1, .1], sig=2), use_neg=True, Fam='or', chunk_count=0, pairs=pair_SameBib) predColl.initialize_predicates()
L1 = C #+ ['',] L2 = list(''.join(a) for a in itertools.permutations('abcd', 2)) L3 = list(''.join(a) for a in itertools.permutations('abcd', 3)) Ls = list(set(L1 + L2 + L3)) Constants = dict({'C': C, 'L': (Ls), 'L2': L2}) list_ops = ops = ['eqC', 'eqL'] predColl = PredCollection(Constants) predColl.add_list_preds(ops=list_ops) predColl.add_pred(dname='gt', arguments=['C', 'C'], variables=[]) predColl.add_pred(dname='lte', arguments=['C', 'C'], variables=[]) p = predColl.add_pred(dname='sort', arguments=['L', 'L'], variables=['L2', 'L2'], pFunc=DNF('sort', terms=4, init=[-1, .2, -2, .1], sig=2), arg_funcs='tH') pairs_bg = [] pairs_ex = dict() for a in Ls: sa = ''.join(sorted(a)) if sa in Ls and len(a) > 2: pairs_ex[(a, sa)] = 1 ct = 0 for b in L3[:]: if b != sa and ct < 10: ind = np.random.randint(len(L3)) v = b
from Lib.ILPRLEngine import * import argparse from Lib.DNF import DNF #define constants C = [ 'a','b','c','d','e', 'f','g','h','i', 'u','v'] Constants = dict( {'C':C }) predColl = PredCollection (Constants) #define predicates predColl.add_pred(dname='edge' ,arguments=['C','C']) predColl.add_pred(dname='aux',arguments=['C','C'] , variables=['C' ] , pFunc = DNF('aux',terms=4,init=[-1,1,-1,1],sig=2) ,inc_preds= ['edge','aux'] ) predColl.add_pred(dname='circular',arguments=['C'] , variables=['C' ] , pFunc = DNF('circular',terms=2,init=[-1,1,-1,1] ,sig=2) ,exc_preds=['circular'] ) predColl.initialize_predicates() #add background bg = Background( predColl ) bg.add_backgroud ('edge', ('a','b') ) bg.add_backgroud ('edge', ('b','c') ) bg.add_backgroud ('edge', ('c','d') ) bg.add_backgroud ('edge', ('d','e') ) bg.add_backgroud ('edge', ('e','b') ) bg.add_backgroud ('edge', ('e','f') ) bg.add_backgroud ('edge', ('f','g') ) bg.add_backgroud ('edge', ('g','h') ) bg.add_backgroud ('edge', ('h','i') ) bg.add_backgroud ('edge', ('u','v') )
Fam='eq', max_T=1, inc_preds=['charge_%d' % n]) # for n in range(LEVELS_CHARGE_DIFF): # predColl.add_pred(name='charge_diff_%d'%n,arguments=['A','A'] ,pFunc = CGT(name='charge_diff_%d'%n, init_w=np.random.random_sample(),c=4.0) , Fam='eq', max_T=1,inc_preds=['charge_diff_%d'%n], exc_terms=['charge_diff_%d(A,A)'%n,'charge_diff_%d(B,A)'%n,'charge_diff_%d(B,B)'%n]) # predColl.add_pred(name='charge_gt',arguments=['A','A'] ) incs = [] aux_cnt = 0 predColl.add_pred(dname='aux_%d' % aux_cnt, arguments=['A'], variables=['A', 'B'], pFunc=DNF('aux_%d' % aux_cnt, terms=2, init=[-1, .1, -1, .1], sig=1), use_neg=False, exc_conds=[], exc_terms=[], Fam='or', chunk_count=0) aux_cnt += 1 predColl.add_pred(dname='aux_%d' % aux_cnt, arguments=['A'], variables=['A', 'E'], pFunc=DNF('aux_%d' % aux_cnt, terms=2, init=[-1, .1, -1, .1], sig=1),
from Lib.ILPRLEngine import * from Lib.DNF import DNF import argparse #define constants C = ['a', 'b', 'c', 'd'] Ls = ['cdabca', 'cdabcd', 'cdabc', 'cdab', 'cda', 'cd', 'c'] Constants = dict({'C': C, 'L': (Ls)}) #define predicates predColl = PredCollection(Constants) predColl.add_list_preds(ops=['eqC', 'eqL', 'singleL']) pred_1 = predColl.add_pred(dname='append', arguments=['L', 'L', 'L'], pFunc=DNF('append', terms=4, init=[-1, .1, -1, -1], sig=2), arg_funcs='tH') predColl.initialize_predicates() #add background bg = Background(predColl) bg.add_list_bg(C, Ls, ops=['eqC', 'eqL', 'singleL']) for a in Ls: for b in Ls: if a + b in Ls: bg.add_example('append', (a, b, a + b), 1.) bg.add_all_neg_example('append')
for i in list( u2basej['d_quality'].unique()) : predColl.add_pred(dname='d_quality_'+str(i),arguments=['M'] ) for i in list( u2basej['avg_revenue'].unique()) : predColl.add_pred(dname='avg_revenue_'+str(i),arguments=['M'] ) for i in list( u2basej['isEnglish'].unique()) : predColl.add_pred(dname='isEnglish_'+str(i),arguments=['M'] ) for i in [ '%d'%(i) for i in range(5)]: predColl.add_pred(dname='movie_year_'+str(i),arguments=['M'] ) for i in range(10): predColl.add_pred(dname='female%d'%i,oname='female',arguments=[], variables=['M' ] ,pFunc = DNF('female%d'%i,terms=4,init=[-1,.1,-1,.1],sig=2 ,predColl=predColl) , use_neg=True, Fam='or',chunk_count=0 ) predColl.initialize_predicates() # extracting bacground knowledge corresponding to each user bgs_pos=[] bgs_neg=[] for i,u in users.iterrows(): bg = Background( predColl )