Beispiel #1
0
def test_inference_taxonomies():
    p = '$PRACMLN_HOME/examples/taxonomies/taxonomies.pracmln'
    mln = MLN(mlnfile=('%s:wts.learned.taxonomy.mln' % p), grammar='PRACGrammar', logic='FuzzyLogic')
    db = Database(mln, dbfile='%s:evidence.db' % p)
    for method in ('EnumerationAsk', 'WCSPInference'):
        print '=== INFERENCE TEST:', method, '==='
        query(queries='has_sense, action_role', method=method, mln=mln, db=db, verbose=False, cw=True).run().write()
Beispiel #2
0
def main(arg='.'):
    #if (len(sys))
    pth = os.path.join(arg, 'wts.pybpll.smoking-train-smoking.mln')
    mln = MLN(mlnfile=pth, grammar='StandardGrammar')
    pth = os.path.join(arg, 'smoking-test-smaller.db')
    db = Database(mln, dbfile=pth)
    #with open(os.path.join(arg, 'man_performance.txt'), 'a') as fl:

    start = time.time()
    #t = 1000* timeit.timeit("test(False)", setup="from __main__ import test, set; set()", number=10)
    query(queries='Cancer,Smokes,Friends',
          method='MC-SAT',
          mln=mln,
          db=db,
          verbose=False,
          multicore=True).run()
    t1 = time.time() - start
    #print('single core exact inference test: {}'.format(t))
    #fl.write(str(t))
    #fl.write('\t(SingleCore)\n')
    start = time.time()
    #t = 1000* timeit.timeit("test(True)", setup="from __main__ import test, set; set()", number=10)
    query(queries='Cancer,Smokes,Friends',
          method='MC-SAT',
          mln=mln,
          db=db,
          verbose=False,
          multicore=False).run()
    t2 = time.time() - start
    #print('multi core exact inference test: {}'.format(t))
    print('Inference, MC-SAT, {}, {}'.format(t1, t2))
Beispiel #3
0
def main(arg='.'):  #(arg='..'):
    #if (len(sys))
    pth = os.path.join(arg, 'wts.pybpll.smoking-train-smoking.mln')
    #pth = os.path.join(arg, 'learnt.cll.scenes-new-object-detection.mln')
    mln = MLN(mlnfile=pth, grammar='StandardGrammar')
    #mln = MLN(mlnfile=pth, grammar='PRACGrammar')
    pth = os.path.join(arg, 'smoking-test.db')
    #pth = os.path.join(arg, 'test.db')
    db = Database(mln, dbfile=pth)
    #with open(os.path.join(arg, 'man_performance.txt'), 'a') as fl:

    #start = time.time()
    #t = 1000* timeit.timeit("test(False)", setup="from __main__ import test, set; set()", number=10)

    #MULTICORE:#
    #query(queries='Cancer,Smokes,Friends', method='EnumerationAsk', mln=mln, db=db, verbose=False, multicore=True).run()

    #t1 = time.time()-start
    #print('single core exact inference test: {}'.format(t))
    #fl.write(str(t))
    #fl.write('\t(SingleCore)\n')
    start = time.time()
    #t = 1000* timeit.timeit("test(True)", setup="from __main__ import test, set; set()", number=10)
    query(method='EnumerationAsk',
          mln=mln,
          db=db,
          verbose=False,
          multicore=False).run()
    t2 = time.time() - start
    print('exact inference test: {}'.format(t2))
Beispiel #4
0
def test_inference_smokers():
    p = '$PRACMLN_HOME/examples/smokers/smokers.pracmln'
    mln = MLN(mlnfile=('%s:wts.pybpll.smoking-train-smoking.mln' % p), grammar='StandardGrammar')
    db = Database(mln, dbfile='%s:smoking-test-smaller.db' % p)
    for method in ('EnumerationAsk', 'MC-SAT', 'WCSPInference', 'GibbsSampler'):
        for multicore in (False, True):
            print '=== INFERENCE TEST:', method, '==='
            query(queries='Cancer,Smokes,Friends', method=method, mln=mln, db=db, verbose=True, multicore=multicore).run()
Beispiel #5
0
def main(arg='.'):
    pth = os.path.join(arg, 'wts.pybpll.smoking-train-smoking.mln')
    mln = MLN(mlnfile=pth, grammar='StandardGrammar')
    pth = os.path.join(arg, 'smoking-test-tiny.db')
    db = Database(mln, dbfile=pth)
    start = time.time()
    query(method='EnumerationAsk',
          mln=mln,
          db=db,
          verbose=False,
          multicore=False).run()
    t2 = time.time() - start
    print('exact inference test: {}'.format(t2))
Beispiel #6
0
def test_inference_taxonomies():
    p = os.path.join(locs.examples, 'taxonomies', 'taxonomies.pracmln')
    mln = MLN(mlnfile=('%s:wts.learned.taxonomy.mln' % p),
              grammar='PRACGrammar',
              logic='FuzzyLogic')
    db = Database(mln, dbfile='%s:evidence.db' % p)
    for method in ('EnumerationAsk', 'WCSPInference'):
        print('=== INFERENCE TEST:', method, '===')
        query(queries='has_sense, action_role',
              method=method,
              mln=mln,
              db=db,
              verbose=False,
              cw=True).run().write()
Beispiel #7
0
def test_inference_smokers():
    p = os.path.join(locs.examples, 'smokers', 'smokers.pracmln')
    mln = MLN(mlnfile=('%s:wts.pybpll.smoking-train-smoking.mln' % p),
              grammar='StandardGrammar')
    db = Database(mln, dbfile='%s:smoking-test-smaller.db' % p)
    for method in ('EnumerationAsk', 'MC-SAT', 'WCSPInference',
                   'GibbsSampler'):
        for multicore in (False, True):
            print('=== INFERENCE TEST:', method, '===')
            query(queries='Cancer,Smokes,Friends',
                  method=method,
                  mln=mln,
                  db=db,
                  verbose=True,
                  multicore=multicore).run()
Beispiel #8
0
    def choose_action(self, state, valid_action):
        state_list = []
        for item in state:
            if item.predicate.name == "empty":
                state_list.append("Empty({0},{1})".format(item.terms[0], item.terms[1]))
                state_list.append("!Mine({0},{1})".format(item.terms[0], item.terms[1]))
                state_list.append("!Opponent({0},{1})".format(item.terms[0], item.terms[1]))
            elif item.predicate.name == "mine":
                state_list.append("Mine({0},{1})".format(item.terms[0], item.terms[1]))
                state_list.append("!Empty({0},{1})".format(item.terms[0], item.terms[1]))
                state_list.append("!Opponent({0},{1})".format(item.terms[0], item.terms[1]))
            elif item.predicate.name == "opponent":
                state_list.append("Opponent({0},{1})".format(item.terms[0], item.terms[1]))
                state_list.append("!Mine({0},{1})".format(item.terms[0], item.terms[1]))
                state_list.append("!Empty({0},{1})".format(item.terms[0], item.terms[1]))

        self.state_list = state_list
        if random.random() < self.EPSILON:
            # act = "Place{0}".format(valid_action[random.randint(0, len(valid_action) - 1)])
            act = self.action_list[random.randint(0,  2)][random.randint(0,  2)]
            print("random choice")
        else:
            # state_list = [state_list[:20], state_list[20:]]
            data, self.mln = self.model_config(self.predicate, self.formula, state_list, 'TicTacToe.mln', 'TicTacToe.db')
            reults = query(queries='Place(x,y)', method='MC-SAT', mln=self.mln, db=data, verbose=False, multicore=True).run().results
            probs = np.array(list(reults.values()))
            acts = np.array(list(reults.keys()))
            act = acts[probs.argmax(-1)]
            original_act = act
        act = act.replace(" ", "")
        # if (int(act[-4]), int(act[-2])) not in valid_action:
        #     act = "Place{0}".format(valid_action[random.randint(0, len(valid_action) - 1)])
        print("valid action:", valid_action)
        print("act:", act)
        return (int(act[-4]), int(act[-2])), str(act)
Beispiel #9
0
def test_inference_smokers():
    p = os.path.join(locs.examples, 'smokers', 'smokers')
    # mln = MLN(mlnfile=('%s:wts.pybpll.smoking-train-smoking.mln' % p),
    #           grammar='StandardGrammar')
    # db = Database(mln, dbfile='%s:smoking-test-smaller.db' % p)
    print(p)
    mln = MLN(mlnfile=('%s.mln' % p), grammar='StandardGrammar')
    db = Database(mln, dbfile='%s.db' % p)
    for method in ('GibbsSampler', ):
        print('=== INFERENCE TEST:', method, '===')
        query(queries='Cancer,Smokes,Friends',
              method=method,
              mln=mln,
              db=db,
              verbose=True,
              multicore=False).run()
Beispiel #10
0
def main(arg='.'):  #(arg='..'):
    #if (len(sys))
    pth = os.path.join(arg, 'wts.pybpll.smoking-train-smoking.mln')
    #pth = os.path.join(arg, 'learnt.cll.scenes-new-object-detection.mln')
    mln = MLN(mlnfile=pth, grammar='StandardGrammar')
    #mln = MLN(mlnfile=pth, grammar='PRACGrammar')
    pth = os.path.join(arg, 'smoking-test-small.db')
    #pth = os.path.join(arg, 'test.db')
    db = Database(mln, dbfile=pth)
    start = time.time()
    query(method='EnumerationAsk',
          mln=mln,
          db=db,
          verbose=False,
          multicore=False).run()
    t2 = time.time() - start
    print('exact inference test: {}'.format(t2))
Beispiel #11
0
 def inference_str(string, result, data, mln):
     print(
         query(queries=string,
               method='EnumerationAsk',
               mln=mln,
               db=data,
               verbose=True,
               multicore=False,
               save=True,
               output_filename=
               r'learnt.dbpll_cg.student-new-train-student-new-2.mln').run(
               ).results)
Beispiel #12
0
def test_learning_smokers(arg='.', disp=False):
    pth = os.path.join(arg, 'smoking.mln')
    mln = MLN(mlnfile=pth, grammar='StandardGrammar')
    pth = os.path.join(arg, 'smoking-train.db')
    db = Database(mln, dbfile=pth)
    for method in ('BPLL', 'BPLL_CG', 'CLL'):
        for multicore in (True, False):
            print('=== LEARNING TEST:', method, '===')
            if disp:
                query(queries='Cancer,Smokes,Friends',
                      method=method,
                      mln=mln,
                      db=db,
                      verbose=disp,
                      multicore=multicore).run().write()
            else:
                learn(method=method,
                      mln=mln,
                      db=db,
                      verbose=disp,
                      multicore=multicore).run()
Beispiel #13
0
def test_inference_smokers(arg='.', disp=False):
    pth = os.path.join(arg, 'wts.pybpll.smoking-train-smoking.mln')
    mln = MLN(mlnfile=pth, grammar='StandardGrammar')
    pth = os.path.join(arg, 'smoking-test-smaller.db')
    db = Database(mln, dbfile=pth)
    for method in ('EnumerationAsk', 'MC-SAT', 'WCSPInference',
                   'GibbsSampler'):
        for multicore in (False, True):
            print('=== INFERENCE TEST:', method, '===')
            if disp:
                query(queries='Cancer,Smokes,Friends',
                      method=method,
                      mln=mln,
                      db=db,
                      verbose=disp,
                      multicore=multicore).run().write()
            else:
                query(queries='Cancer,Smokes,Friends',
                      method=method,
                      mln=mln,
                      db=db,
                      verbose=disp,
                      multicore=multicore).run()
Beispiel #14
0
 def inference(path, result, data, mln):  #推理查询未知的命题
     query_list = []
     base_path = os.getcwd()
     file = open(base_path + '\\' + path, 'r', encoding='utf8')
     query_list = file.read()
     query_list = query_list.split('\n')
     query_list = [x for x in query_list if x != '']
     for i in query_list:
         print(
             query(queries=i,
                   method='MC-SAT',
                   mln=mln,
                   db=data,
                   verbose=False,
                   multicore=True).run().results)
    def inference(self, path,data, mln):
        """
        Returns the queries and probabilities
        --Inputs--
        path: .txt file containing the Queries
        result: the output object from the learned mln
        data: the inference data object
        mln: the mln object
        """

        query_list = []
        base_path = os.getcwd()
        file = open(base_path + '/' + path,'r',encoding = 'ascii')
        query_list = file.read()
        query_list = query_list.split('\n')
        query_list = [x for x in query_list if x !='']
        for i in query_list:
            print(query(queries=i.encode("ascii"), method='GibbsSampler', mln=mln, db=data, verbose=False, multicore=True).run().results) # check to change the method
Beispiel #16
0
 def inference(self, inference_query):
     mln = MLN(mlnfile=self.mln_path, grammar='StandardGrammar')
     db = Database(mln, dbfile=self.db_path)
     for method in [
             'EnumerationAsk'
             # 'MC-SAT',
             # 'WCSPInference',
             # 'GibbsSampler'
     ]:
         print('=== INFERENCE TEST:', method, '===')
         result = query(queries=inference_query,
                        method=method,
                        mln=mln,
                        db=db,
                        verbose=True,
                        multicore=False).run()
         print(result)
     if result:
         return result
     else:
         return []