Esempio n. 1
0
class Model(object):

    def __init__(self, config,
                 debug_information=False,
                 is_train=True):
        self.debug = debug_information
        self.XO={}
        self.pen=None
        self.config = config
        self.batch_size = self.config.batch_size
        self.img_size = self.config.data_info[0]
        self.c_dim = self.config.data_info[2]
        self.q_dim = self.config.data_info[3]
        self.a_dim = self.config.data_info[4]
        self.conv_info = self.config.conv_info
        self.acc=0
        self.feat_count = 64
        self.ilp_params = None

        # create placeholders for the input
        self.img = tf.placeholder(
            name='img', dtype=tf.float32,
            shape=[self.batch_size, self.img_size, self.img_size, self.c_dim],
        )
        self.q = tf.placeholder(
            name='q', dtype=tf.float32, shape=[self.batch_size, self.q_dim],
        )
        self.a = tf.placeholder(
            name='a', dtype=tf.float32, shape=[self.batch_size, self.a_dim],
        )

        self.is_training = tf.placeholder_with_default(bool(is_train), [], name='is_training')

        self.build(is_train=is_train)

    def load_ilp_config(self):

        parser = argparse.ArgumentParser()
        batch_size = self.batch_size
        # extra_feat_count=16
 
        # parser.add_argument('--CHECK_CONVERGENCE',default=1,help='Check for convergence',type=int)
        # parser.add_argument('--SHOW_PRED_DETAILS',default=0,help='Print predicates definition details',type=int)

        parser.add_argument('--BS',default=16,help='Batch Size',type=int)
        parser.add_argument('--T',default=1 ,help='Number of forward chain',type=int)
        # parser.add_argument('--LR_SC', default={ (-1000,2):.005 ,  (2,1e5):.01} , help='Learning rate schedule',type=dict)

        # parser.add_argument('--BINARAIZE', default=1 , help='Enable binrizing at fast convergence',type=int)
        # parser.add_argument('--MAX_DISP_ITEMS', default=10 , help='Max number  of facts to display',type=int)
        # parser.add_argument('--DISP_BATCH_VALUES',default=[],help='Batch Size',type=list)
        # parser.add_argument('--W_DISP_TH', default=.2 , help='Display Threshold for weights',type=int)
        # parser.add_argument('--ITER', default=400000, help='Maximum number of iteration',type=int)
        # parser.add_argument('--ITER2', default=200, help='Epoch',type=int)
        # parser.add_argument('--PRINTPRED',default=1,help='Print predicates',type=int)
        # parser.add_argument('--PRINT_WEIGHTS',default=0,help='Print raw weights',type=int)
        parser.add_argument('--MAXTERMS',default=6 ,help='Maximum number of terms in each clause',type=int)
        parser.add_argument('--L1',default=0 ,help='Penalty for maxterm',type=float)
        parser.add_argument('--L2',default=0 ,help='Penalty for distance from binary for weights',type=float)
        parser.add_argument('--L3',default=0 ,help='Penalty for distance from binary for each term',type=float)
        parser.add_argument('--L2LOSS',default=0,help='Use L2 instead of cross entropy',type=int)
        parser.add_argument('--SYNC',default=0,help='Synchronized Update',type=int)
        # parser.add_argument('--ALLTIMESTAMP',default=0 ,help='Add loss for each timestamp',type=int)
        # parser.add_argument('--FILT_TH_MEAN', default=.5 , help='Fast convergence total loss threshold MEAN',type=float)
        # parser.add_argument('--FILT_TH_MAX', default=.5 , help='Fast convergence total loss threshold MAX',type=float)
        # parser.add_argument('--OPT_TH', default=.05 , help='Per value accuracy threshold',type=float)
        # parser.add_argument('--PLOGENT', default=.50 , help='Crossentropy coefficient',type=float)
        # parser.add_argument('--BETA1', default=.90 , help='ADAM Beta1',type=float)
        # parser.add_argument('--BETA2', default=.999 , help='ADAM Beta2',type=float)
        # parser.add_argument('--EPS', default=1e-6, help='ADAM Epsillon',type=float)
        parser.add_argument('--GPU', default=1, help='Use GPU',type=int)
        # parser.add_argument('--LOGDIR', default='./logs/Logic', help='Log Dir',type=str)
        parser.add_argument('--TB', default=0, help='Use Tensorboard',type=int)
        parser.add_argument('--SEED',default=0,help='Random seed',type=int)
        # parser.add_argument('--ADDGRAPH', default=1, help='Add graph to Tensorboard',type=int)
        # parser.add_argument('--CLIP_NORM', default=0, help='Clip gradient',type=float)
        self.args_ilp = parser.parse_args()

    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 )





    def get_feed_dict(self, batch_chunk, step=None, is_training=None):
        fd = {
            self.img: batch_chunk['img'],  # [B, h, w, c]
            self.q: batch_chunk['q'],  # [B, n]
            self.a: batch_chunk['a'],  # [B, m]
        }
        if is_training is not None:
            fd[self.is_training] = is_training

        return fd

    def build(self, is_train=True):

        n = self.a_dim
        conv_info = self.conv_info

        # build loss and accuracy {{{
        def build_loss(logits, labels):
            # Cross-entropy loss
            loss = tf.nn.softmax_cross_entropy_with_logits(logits=logits*10, labels=labels)
            # loss = tf.reduce_sum( neg_ent_loss (labels,logits) , -1 )

            # Classification accuracy
            correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
            self.acc = tf.cast(correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
            return tf.reduce_mean(loss), accuracy
        # }}}

        # def concat_coor(o, i, d):
        #     coor = tf.tile(tf.expand_dims(
        #         [float(int(i / d)) / d, (i % d) / d], axis=0), [self.batch_size, 1])
        #     o = tf.concat([o, tf.to_float(coor)], axis=1)
        #     return o

        # def g_theta(o_i, o_j, q, scope='g_theta', reuse=True):
        #     with tf.variable_scope(scope, reuse=reuse) as scope:
        #         if not reuse: log.warn(scope.name)
        #         g_1 = fc(tf.concat([o_i, o_j, q], axis=1), 256, name='g_1')
        #         g_2 = fc(g_1, 256, name='g_2')
        #         g_3 = fc(g_2, 256, name='g_3')
        #         g_4 = fc(g_3, 256, name='g_4')
        #         return g_4

        # Classifier: takes images as input and outputs class label [B, m]
        def CONV(img, q, scope='CONV'):
            nD = 16 
            nC=6
             

            with tf.variable_scope(scope) as scope:
                log.warn(scope.name)
                
                
                self.X0['question'] = q[:,:nC]
                
                x1 = tf.layers.conv2d( img, 32*2, 3, strides=(2,2) , activation='tanh', padding='valid')
                x2 = tf.layers.conv2d( x1, 32*2, 3, strides=(2,2), activation='tanh' , padding='valid' )
                x2 = tf.layers.conv2d( x2, 32*2, 3, strides=(2,2), activation='tanh' , padding='valid' )
                sz=x2.shape[1].value
                feat = tf.reshape(x2, [-1,sz*sz,32*2])

                 
                def feat2scores2(feat,nX,nF):
                    # pos = tf.constant(  np.eye(nF) , tf.float32 )
                    w = weight_variable( [ feat.shape[-1].value,nF]  )
                    b = bias_variable([nF,])
                    m = tf.matmul( feat,w+b)
                    # mm = tf.matmul( m , pos)
                    mm = tf.nn.softmax( m,1)
                    mmm = tf.transpose( mm, [0,2,1] )
                    return tf.layers.dense( mmm, nX, tf.nn.softmax )

                    

                
                
                with tf.variable_scope('conv_pos') as scope:
                    # Fs = feat2scores2(feat,nC)    
                    # rect = tf.layers.dense( Fs, 3, tf.nn.softmax )
                    rect = feat2scores2(feat,nC,3)    
                    rect = tf.transpose( rect, [0,2,1] )

                # with tf.variable_scope('obj'):
                
                #     # obj = tf.layers.dense( obj, 2, tf.nn.softmax )
                with tf.variable_scope('cl'):
                    pos = feat2scores2(feat,nC,nD)
                    # Fs = feat2scores2(feat,nC)
                    # pos = tf.layers.dense( feat, nD, tf.nn.softmax )
                    pos = tf.transpose( pos, [0,2,1] )


                # cl1,cl2 = get_conv(1,[nL1,nL2],24)
                
                # self.X0['obj'] = obj[:,0,:]
                
                self.X0['rectangle'] = rect[:,:,1]
                self.X0['exist'] = 1.0-rect[:,:,0]
                self.X0['pos'] = tf.reshape( pos, [-1, nC*nD])

                for i in range(self.q_dim):
                    self.X0['is_q_%d'%i] = q[:,i:(i+1)]

                  
                
                with tf.variable_scope('myscope'):
                    self.XO,L3 = self.mdl.getTSteps(self.X0)

                

                os = tf.concat( [self.XO['CL_%d'%i] for i in range(self.a_dim)],-1)
                return os
                
                
                # return fc(os, self.a_dim, activation_fn=None, name='fc_3')
                # # os=os+ fc(os, 1, activation_fn=tf.sigmoid, name='fc_2')*0
                # # return os
                # all_g = tf.concat( [self.XO[i.oname] for i in self.predColl.outpreds],-1)
                
                # all_g = fc( 2*all_g-1, 256, activation_fn=tf.nn.relu, name='fc_1')
                # # all_g = slim.dropout(all_g, keep_prob=0.5, is_training=is_train, scope='fc_3/')
                # all_g = fc(all_g, 256, activation_fn=tf.nn.relu, name='fc_2')
                # # all_g = slim.dropout(all_g, keep_prob=0.8, is_training=is_train, scope='fc_2')
                # return fc(all_g, self.a_dim, activation_fn=None, name='fc_3')
                # # all_g = tf.concat( [XO[i.oname] for i in self.predColl.outpreds if 'aux' in i.dname],-1)
                # os = tf.concat( [self.XO['CL_%d'%i] for i in range(self.a_dim)],-1)
                # # os=os+ fc(os, 1, activation_fn=tf.sigmoid, name='fc_2')*0
                # return os*5 
                                # return all_g
                # return all_g

        def f_phi(g, scope='f_phi'):
            with tf.variable_scope(scope) as scope:
                log.warn(scope.name)
                fc_1 = fc(g, 256, name='fc_1')
                fc_2 = fc(fc_1, 256, name='fc_2')
                # fc_2 = slim.dropout(fc_2, keep_prob=0.5, is_training=is_train, scope='fc_3/')
                fc_3 = fc(fc_2, n, activation_fn=None, name='fc_3')
                return fc_3

        self.load_ilp_config()
        self.define_preds()
        
        logits = CONV(self.img, self.q, scope='CONV')
        # logits = f_phi(g, scope='f_phi')
        self.all_preds = tf.nn.softmax(logits)
        self.loss, self.accuracy = build_loss(logits, self.a)

        # Add summaries
        def draw_iqa(img, q, target_a, pred_a):
            fig, ax = tfplot.subplots(figsize=(6, 6))
            ax.imshow(img)
            ax.set_title(question2str(q))
            ax.set_xlabel(answer2str(target_a)+answer2str(pred_a, 'Predicted'))
            return fig

        try:
            tfplot.summary.plot_many('IQA/',
                                     draw_iqa, [self.img, self.q, self.a, self.all_preds],
                                     max_outputs=4,
                                     collections=["plot_summaries"])
        except:
            pass

        tf.summary.scalar("loss/accuracy", self.accuracy)
        tf.summary.scalar("loss/cross_entropy", self.loss)
        log.warn('Successfully loaded the model.')
Esempio n. 2
0
class ILP_MODEL(object):
    def __init__(self, DIM1, DIM2, F_COUNT=3):
        self.DIM1 = DIM1
        self.DIM2 = DIM2
        self.F_COUNT = F_COUNT

        self.args = self.load_ilp_config()
        self.define_preds()
        self.Xo = None
        self.X0 = None

    def load_ilp_config(self):

        param = dotdict({})
        param.BS = 1
        param.T = 1
        param.W_DISP_TH = .2
        param.GPU = 1

        return param

    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)

    def run(self, states):
        bs = tf.shape(states[0])[0]
        self.X0 = OrderedDict()

        for p in self.predColl.outpreds:
            tmp = tf.expand_dims(
                tf.constant(self.bg.get_X0(p.oname), tf.float32), 0)
            self.X0[p.oname] = tf.tile(tmp, [bs, 1])

        self.X0['agent'] = states[0]
        self.X0['predLR'] = states[1]
        self.X0['predRL'] = states[2]
        self.X0['food'] = states[3]

        self.Xo, L3 = self.mdl.getTSteps(self.X0)

        xo_r = {}
        for p in self.Xo:
            xo_r[p] = tf.reshape(self.Xo[p], [-1] + [
                len(self.predColl.constants[i])
                for i in self.predColl[p].arguments
            ])
        move = [
            self.Xo[i] for i in [
                'action_noop', 'action_up', 'action_right', 'action_left',
                'action_down'
            ]
        ]
        # move = [ 1.0-self.Xo[i] for i in ['en_noop','en_up','en_right','en_left', 'en_down']]
        # move[0] = tf.zeros_like( move[0] )-5
        return tf.concat(move, -1), self.X0, self.Xo, xo_r
Esempio n. 3
0
class ILP_MODEL(object):
    def __init__(self, num_box, is_train=True):
        self.num_box = num_box
        self.args = self.load_ilp_config()
        self.define_preds()
        self.Xo = None
        self.X0 = None

    def load_ilp_config(self):

        param = dotdict({})
        param.BS = 1
        param.T = 1
        param.W_DISP_TH = .1
        param.GPU = 1

        return param

    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)

    def run(self, state_in_x, state_in_y):
        bs = tf.shape(state_in_x)[0]
        self.X0 = OrderedDict()

        for p in self.predColl.outpreds:
            tmp = tf.expand_dims(
                tf.constant(self.bg.get_X0(p.oname), tf.float32), 0)
            self.X0[p.oname] = tf.tile(tmp, [bs, 1])

        flx = np.zeros((self.num_box + 1, self.num_box + 1), dtype=np.float32)
        flx[0, :] = 1
        xx = tf.pad(state_in_x,
                    [[0, 0], [1, 0], [1, 0]]) + flx[np.newaxis, :, :]
        self.X0['posH'] = tf.reshape(xx, [-1, (self.num_box + 1)**2])

        fly = np.zeros((self.num_box + 1, self.num_box + 1), dtype=np.float32)
        fly[0, 0] = 1
        yy = tf.pad(state_in_y,
                    [[0, 0], [1, 0], [1, 0]]) + fly[np.newaxis, :, :]
        self.X0['posV'] = tf.reshape(yy, [-1, (self.num_box + 1)**2])
        self.Xo, L3 = self.mdl.getTSteps(self.X0)

        return self.Xo
Esempio n. 4
0
class ILP_MODEL(object):
    def __init__(self, num_box, is_train=True):
        self.num_box = num_box
        self.args = self.load_ilp_config()
        self.define_preds()
        self.Xo = None
        self.X0 = None
        self.has_key = None

    def reset(self):
        self.has_key = None

    def load_ilp_config(self):

        param = dotdict({})
        param.BS = 1
        param.T = 1
        param.W_DISP_TH = .1
        param.GPU = 1

        return param

    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)

    def run(self, state):
        has_key, color = state

        bs = tf.shape(has_key)[0]
        self.X0 = OrderedDict()
        for p in self.predColl.outpreds:
            tmp = tf.expand_dims(
                tf.constant(self.bg.get_X0(p.oname), tf.float32), 0)
            self.X0[p.oname] = tf.tile(tmp, [bs, 1])

        self.X0['color'] = color
        self.X0['has_key'] = has_key
        # if self.has_key is not None:
        #     self.X0['has_key'] = has_key

        self.Xo, L3 = self.mdl.getTSteps(self.X0)
        # self.has_key = self.X0['has_key']
        return self.Xo["move"]

    def runtest(self, state):
        has_key, color = state
        bs = tf.shape(has_key)[0]

        self.X0 = OrderedDict()
        for p in self.predColl.outpreds:
            tmp = tf.expand_dims(
                tf.constant(self.bg.get_X0(p.oname), tf.float32), 0)
            self.X0[p.oname] = tf.tile(tmp, [bs, 1])
        # if self.has_key is not None:
        #     self.X0['has_key'] = has_key

        self.X0['color'] = color
        self.X0['has_key'] = has_key

        self.Xo, L3 = self.mdl.getTSteps(self.X0)
        # self.has_key = self.X0['has_key']

        L1 = 0
        L2 = 0

        def get_pen(x):
            return tf.nn.relu(2 * x -
                              2) - 2 * tf.nn.relu(2 * x - 1) + tf.nn.relu(
                                  2 * x)

        # for p in  self.predColl.preds:
        #     vs = tf.get_collection( p.dname)
        #     # for wi in vs:
        #     if '_AND' in wi.name:
        #         wi = p.pFunc.conv_weight(wi)

        #         # L2 += tf.reduce_mean( wi*(1.0-wi))
        #         L2 += tf.reduce_mean( get_pen(wi))

        #         s = tf.reduce_sum( wi,-1)
        #         # L1 += tf.reduce_mean(  tf.nn.relu( s-7)  )
        #         s = tf.reduce_max( wi,-1)
        #         L1 += tf.reduce_mean(  tf.nn.relu( 1-s)  )

        return self.Xo, L2 * 0
Esempio n. 5
0
class ILP_MODEL(object):
    def __init__(self, num_box,is_train=True):
        self.num_box = num_box
        self.args = self.load_ilp_config()
        self.define_preds()
        self.Xo=None
        self.X0=None
        self.has_key=None
    def reset(self):        
        self.has_key=None

    def load_ilp_config(self):
        
        param = dotdict({})
        param.BS = 1
        param.T =  1
        param.W_DISP_TH = .1
        param.GPU = 1
         
        return param
        

    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 run( self, state):
        has_key,color = state
        
        bs= tf.shape(has_key)[0]
        self.X0=OrderedDict()
        for p in self.predColl.outpreds:
            tmp = tf.expand_dims( tf.constant( self.bg.get_X0(p.oname) ,tf.float32) , 0)
            self.X0[p.oname] = tf.tile( tmp , [bs,1]  )

        self.X0['color'] = color
        self.X0['has_key'] = has_key
        # if self.has_key is not None:
        #     self.X0['has_key'] = has_key

        self.Xo,L3 = self.mdl.getTSteps(self.X0)
        # self.has_key = self.X0['has_key'] 
        return  self.Xo["move"]


    def runtest( self, state):
        has_key,color = state
        bs= tf.shape(has_key)[0]
        
        self.X0=OrderedDict()
        for p in self.predColl.outpreds:
            tmp = tf.expand_dims( tf.constant( self.bg.get_X0(p.oname) ,tf.float32) , 0)
            self.X0[p.oname] = tf.tile( tmp , [bs,1]  )
        # if self.has_key is not None:
        #     self.X0['has_key'] = has_key

        self.X0['color'] = color
        self.X0['has_key'] = has_key
        
        self.Xo,L3 = self.mdl.getTSteps(self.X0)
        # self.has_key = self.X0['has_key'] 

        L1=0
        L2=0
        
        def get_pen(x):
            return tf.nn.relu(2*x-2)-2*tf.nn.relu(2*x-1)+tf.nn.relu(2*x)
        # for p in  self.predColl.preds:
        #     vs = tf.get_collection( p.dname)
        #     # for wi in vs:
            #     if '_AND' in wi.name:
            #         wi = p.pFunc.conv_weight(wi)
                    
            #         # L2 += tf.reduce_mean( wi*(1.0-wi))
            #         L2 += tf.reduce_mean( get_pen(wi))

            #         s = tf.reduce_sum( wi,-1)
            #         # L1 += tf.reduce_mean(  tf.nn.relu( s-7)  )
            #         s = tf.reduce_max( wi,-1)
            #         L1 += tf.reduce_mean(  tf.nn.relu( 1-s)  )


        return  self.Xo,L2*0
Esempio n. 6
0
class ILP_MODEL(object):
    def __init__(self, DIM1,DIM2,F_COUNT=3):
        self.DIM1=DIM1
        self.DIM2=DIM2
        self.F_COUNT=F_COUNT

        
        self.args = self.load_ilp_config()
        self.define_preds()
        self.Xo=None
        self.X0=None

    def load_ilp_config(self):
        
        param = dotdict({})
        param.BS = 1
        param.T = 1
        param.W_DISP_TH = .2
        param.GPU = 1
         
        return param
        

    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 run( self, states):
        bs= tf.shape(states[0])[0]
        self.X0=OrderedDict()
        
        for p in self.predColl.outpreds:
            tmp = tf.expand_dims( tf.constant( self.bg.get_X0(p.oname) ,tf.float32) , 0)
            self.X0[p.oname] = tf.tile( tmp , [bs,1]  )

        
        self.X0['f_1'] = states[0]
        self.X0['f_2'] = states[1]
        self.X0['f_3'] = states[2]
        self.X0['f_4'] = states[3]

        self.Xo,L3 = self.mdl.getTSteps(self.X0)

        move = [ self.Xo[i] for i in ['action_noop','action_up','action_right','action_left', 'action_down']]
        # move[0] = tf.zeros_like( move[0] )-5
        return  tf.concat(move,-1),self.X0,self.Xo
Esempio n. 7
0
class Model(object):
    def __init__(self, config, debug_information=False, is_train=True):
        self.debug = debug_information
        self.XO = {}
        self.pen = None
        self.config = config
        self.batch_size = self.config.batch_size
        self.img_size = self.config.data_info[0]
        self.c_dim = self.config.data_info[2]
        self.q_dim = self.config.data_info[3]
        self.a_dim = self.config.data_info[4]
        self.conv_info = self.config.conv_info
        self.acc = 0
        self.feat_count = 64
        self.ilp_params = None

        # create placeholders for the input
        self.img = tf.placeholder(
            name='img',
            dtype=tf.float32,
            shape=[self.batch_size, self.img_size, self.img_size, self.c_dim],
        )
        self.q = tf.placeholder(
            name='q',
            dtype=tf.float32,
            shape=[self.batch_size, self.q_dim],
        )
        self.a = tf.placeholder(
            name='a',
            dtype=tf.float32,
            shape=[self.batch_size, self.a_dim],
        )

        self.is_training = tf.placeholder_with_default(bool(is_train), [],
                                                       name='is_training')

        self.build(is_train=is_train)

    def load_ilp_config(self):

        parser = argparse.ArgumentParser()
        batch_size = self.batch_size

        parser.add_argument('--BS',
                            default=16 * 2,
                            help='Batch Size',
                            type=int)
        parser.add_argument('--T',
                            default=1,
                            help='Number of forward chain',
                            type=int)

        parser.add_argument('--MAXTERMS',
                            default=6,
                            help='Maximum number of terms in each clause',
                            type=int)
        parser.add_argument('--L1',
                            default=0,
                            help='Penalty for maxterm',
                            type=float)
        parser.add_argument(
            '--L2',
            default=0,
            help='Penalty for distance from binary for weights',
            type=float)
        parser.add_argument(
            '--L3',
            default=0,
            help='Penalty for distance from binary for each term',
            type=float)
        parser.add_argument('--L2LOSS',
                            default=0,
                            help='Use L2 instead of cross entropy',
                            type=int)
        parser.add_argument('--SYNC',
                            default=0,
                            help='Synchronized Update',
                            type=int)
        parser.add_argument('--GPU', default=1, help='Use GPU', type=int)
        parser.add_argument('--TB',
                            default=0,
                            help='Use Tensorboard',
                            type=int)
        parser.add_argument('--SEED', default=0, help='Random seed', type=int)

        self.args_ilp = parser.parse_args()

    def define_preds(self):
        nL = 6
        nD = 16

        D = ['%d' % i for i in range(nD)]
        self.Constants = dict({'D': D})

        self.predColl = PredCollection(self.Constants)

        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'])
        # self.predColl.add_pred(dname='top'  ,arguments=['D'])
        # self.predColl.add_pred(dname='right'  ,arguments=['D'])

        self.predColl.add_pred(dname='obj', arguments=['D'])
        self.predColl.add_pred(dname='rectangle', arguments=['D'])

        #instead of color(D,C) we define a set of is_
        for i in range(nL):
            self.predColl.add_pred(dname='is_color_%d' % i, arguments=['D'])

        # for i in range(nD):
        #     self.predColl.add_pred(dname='is_d_%d'%i  ,arguments=['D'])

        for i in range(self.q_dim):
            self.predColl.add_pred(dname='is_q_%d' % i, arguments=[])

        self.predColl.add_pred(
            dname='closer',
            arguments=['D', 'D', 'D'],
            variables=[],
            pFunc=DNF('closer',
                      terms=1,
                      init=[1, .1, -1, .1],
                      sig=2,
                      init_terms=['obj(A), obj(B), obj(C), ltD(A,B,C)'],
                      predColl=self.predColl,
                      fast=True),
            use_neg=False,
            Fam='or')
        self.predColl.add_pred(
            dname='farther',
            arguments=['D', 'D', 'D'],
            variables=[],
            pFunc=DNF('farther',
                      terms=1,
                      init=[1, .1, -1, .1],
                      sig=2,
                      init_terms=['obj(A), obj(B), obj(C), gtD(A,B,C)'],
                      predColl=self.predColl,
                      fast=True),
            use_neg=False,
            Fam='or')

        self.predColl.add_pred(dname='notClosest',
                               arguments=['D', 'D'],
                               variables=['D'],
                               pFunc=DNF('notClosest',
                                         terms=3,
                                         init=[1, .1, -1, .1],
                                         sig=2,
                                         init_terms=[
                                             'closer(A,C,B)', 'not obj(A)',
                                             'not obj(B)', 'eq(A,B)'
                                         ],
                                         predColl=self.predColl,
                                         fast=True,
                                         neg=True),
                               use_neg=True,
                               Fam='eq')

        self.predColl.add_pred(dname='notFarthest',
                               arguments=['D', 'D'],
                               variables=['D'],
                               pFunc=DNF('notFarthest',
                                         terms=3,
                                         init=[1, .1, -1, .1],
                                         sig=2,
                                         init_terms=[
                                             'farther(A,C,B)', 'not obj(A)',
                                             'not obj(B)', 'eq(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)]

        self.predColl.add_pred(
            dname='qa',
            oname='qa',
            arguments=['D'],
            variables=[],
            pFunc=DNF('qa',
                      predColl=self.predColl,
                      init_terms=[
                          'is_q_0(), is_color_0(A)', 'is_q_1(), is_color_1(A)',
                          'is_q_2(), is_color_2(A)', 'is_q_3(), is_color_3(A)',
                          'is_q_4(), is_color_4(A)', 'is_q_5(), is_color_5(A)'
                      ],
                      fast=True),
            use_neg=True,
            Fam='eq',
            exc_conds=[('*', 'rep1')],
            exc_preds=exc)

        for k in range(0, 10):

            post_term = [('and', 'qa(A)')]
            if k == 6:
                post_term.append(('and', 'not rectangle(A)'))
            if k == 7:
                post_term.append(('and', 'rectangle(A)'))

            post_terms = []
            self.predColl.add_pred(dname='CL_%d' % k,
                                   oname='CL_%d' % k,
                                   arguments=[],
                                   variables=['D', 'D'],
                                   pFunc=DNF('CL_%d' % k,
                                             terms=12,
                                             init=[-1, -1, -1, -1],
                                             sig=1,
                                             predColl=self.predColl,
                                             post_terms=post_term),
                                   use_neg=True,
                                   Fam='eq',
                                   exc_preds=exc + ['eq', 'ltD', 'gtD'])

        self.predColl.initialize_predicates()

        self.bg = Background(self.predColl)
        for i in range(nD):

            self.bg.add_backgroud('eq', ('%d' % i, '%d' % i))

            ri, ci = int(i // 4), int(i % 4)
            Y1 = (ri + 0.5) * 28 + ri**2
            X1 = (ci + 0.5) * 28 + ci**2
            if Y1 > 64:
                self.bg.add_backgroud('button', ('%d' % i, ))
            if X1 < 64:
                self.bg.add_backgroud('left', ('%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)
                    Y2 = (rj + 0.5) * 28 + rj**2
                    X2 = (cj + 0.5) * 28 + cj**2
                    Y3 = (rk + 0.5) * 28 + rk**2
                    X3 = (ck + 0.5) * 28 + ck**2

                    d1 = 1.1 * (X1 - X2)**2 + (Y1 - Y2)**2
                    d2 = 1.1 * (X1 - X3)**2 + (Y1 - Y3)**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))

        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...')

        self.mdl = ILPRLEngine(args=self.args_ilp,
                               predColl=self.predColl,
                               bgs=None)

    def get_feed_dict(self, batch_chunk, step=None, is_training=None):
        fd = {
            self.img: batch_chunk['img'],  # [B, h, w, c]
            self.q: batch_chunk['q'],  # [B, n]
            self.a: batch_chunk['a'],  # [B, m]
        }
        if is_training is not None:
            fd[self.is_training] = is_training

        return fd

    def build(self, is_train=True):

        n = self.a_dim
        conv_info = self.conv_info

        # build loss and accuracy {{{
        def build_loss(logits, labels):

            # Cross-entropy loss
            loss = tf.nn.softmax_cross_entropy_with_logits(
                logits=logits, labels=labels) + .02 * self.pen

            correct_prediction = tf.equal(tf.argmax(logits, 1),
                                          tf.argmax(labels, 1))
            self.acc = tf.cast(correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
            return tf.reduce_mean(loss), accuracy

        def concat_coor(o, i, d):
            coor = tf.tile(
                tf.expand_dims([float(int(i / d)) / d, (i % d) / d], axis=0),
                [self.batch_size, 1])
            o = tf.concat([o, tf.to_float(coor)], axis=1)
            return o

        def CONV(img, q, scope='CONV'):
            with tf.variable_scope(scope) as scope:
                log.warn(scope.name)

                conv_1 = conv2d(img,
                                conv_info[0],
                                is_train,
                                s_h=3,
                                s_w=3,
                                name='conv_1',
                                batch_norm=True,
                                activation_fn=tf.nn.relu)
                conv_2 = conv2d(conv_1,
                                conv_info[1],
                                is_train,
                                s_h=3,
                                s_w=3,
                                name='conv_2',
                                batch_norm=True,
                                activation_fn=tf.nn.relu)
                conv_3 = conv2d(conv_2,
                                conv_info[2],
                                is_train,
                                name='conv_3',
                                batch_norm=True,
                                activation_fn=tf.nn.relu)
                conv_4 = conv2d(conv_3,
                                conv_info[3] * 2,
                                is_train,
                                name='conv_4',
                                batch_norm=True,
                                activation_fn=tf.nn.relu)

                d = conv_4.get_shape().as_list()[1]
                all_g = []
                for i in range(d * d):
                    o_i = conv_4[:, int(i / d), int(i % d), :]
                    all_g.append(o_i)

                feat = tf.stack(all_g, axis=1)

                nD = 16
                nL = 6

                for i in range(self.q_dim):
                    self.X0['is_q_%d' % i] = q[:, i:(i + 1)]

                def makebin(x, t, fn):
                    #return fn(x)
                    fw = tf.cast(tf.greater_equal(x, t), tf.float32)
                    return custom_grad(fw, fn(x))

                f = tf.layers.dense(feat, 1)
                self.X0['rectangle'] = makebin(f[:, :, 0], 0.0, tf.sigmoid)

                #self.pen = tf.reduce_mean( self.X0['rectangle']*(1.0-self.X0['rectangle']))

                f = tf.layers.dense(feat, 7, tf.nn.softmax)
                #self.pen += tf.reduce_mean( f*(1.0-f))

                for i in range(6):
                    self.X0['is_color_%d' % i] = makebin(
                        f[:, :, i + 1], .5, tf.identity)

                self.X0['obj'] = (1.0 - makebin(f[:, :, 0], .5, tf.identity))
                #self.pen = tf.reduce_mean ( tf.square( tf.reduce_sum(self.X0['obj'],-1)-4.0  ))*.01

                with tf.variable_scope('myscope'):
                    self.XO, L3 = self.mdl.getTSteps(self.X0)

                loss_w = 0
                loss_sum = 0
                vs = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                       scope='CONV/myscope')

                for v in vs:
                    if v is None:
                        continue

                    print(v)
                    w = tf.sigmoid(2 * v)
                    loss_w += tf.reduce_mean(w * (1 - .0 - w))
                    loss_sum += tf.reduce_mean(
                        tf.nn.relu(tf.reduce_sum(w, -1) - 6))

                self.pen = loss_w + loss_sum

                os = tf.concat(
                    [self.XO['CL_%d' % i] for i in range(self.a_dim)], -1)
                #os = tf.concat( [   custom_grad( sharp_sigmoid(self.XO['CL_%d'%i]-.5,7),self.XO['CL_%d'%i] )   for i in range(self.a_dim)],-1)
                return 10 * os

        self.load_ilp_config()
        self.define_preds()

        logits = CONV(self.img, self.q, scope='CONV')
        # logits = f_phi(g, scope='f_phi')
        self.all_preds = tf.nn.softmax(logits)
        self.loss, self.accuracy = build_loss(logits, self.a)

        # Add summaries
        def draw_iqa(img, q, target_a, pred_a):
            fig, ax = tfplot.subplots(figsize=(6, 6))
            ax.imshow(img)
            ax.set_title(question2str(q))
            ax.set_xlabel(
                answer2str(target_a) + answer2str(pred_a, 'Predicted'))
            return fig

        try:
            tfplot.summary.plot_many(
                'IQA/',
                draw_iqa, [self.img, self.q, self.a, self.all_preds],
                max_outputs=4,
                collections=["plot_summaries"])
        except:
            pass

        tf.summary.scalar("loss/accuracy", self.accuracy)
        tf.summary.scalar("loss/cross_entropy", self.loss)
        log.warn('Successfully loaded the model.')