コード例 #1
0
ファイル: ltlmpsat_old.py プロジェクト: kimney/ltlmp_sat
def get_weighted_graph(graph, mp_prop_pair, opts):
    # MPList_i = mp_prop_pair['mp']
    # MPPropsList_i = mp_prop_pair['prop']
    
    # input: graph(automaton) mdg
    if opts.tgba:
        mdg = nx.MultiDiGraph(acccond=graph.graph['acccond'])
    else:
        mdg = nx.MultiDiGraph()
        
    for node in graph.nodes():
        mdg.add_node(node)
        
    for edge in graph.edges(data=True):
        
        DPsList = edge[2]['condList']
        if opts.pdebug:
            print DPsList
        # get Undecidable Prop List by removing DP from mpproplist
        UDPs = set([]) # UDPList According to 'Cond-or List'
        if DPsList == []:
            for mp_prop in mp_prop_pair:
                mpprops_ = set(mp_prop['prop'])
                UDPs = UDPs | mpprops_
        else:
            for DPs in DPsList:
                for mp_prop in mp_prop_pair:
                    mpprops_ = set(mp_prop['prop'])
                    mpprops_ = mpprops_ - set(DPs[0])
                    mpprops_ = mpprops_ - set(DPs[1])
                    UDPs = UDPs | mpprops_
        UDPs = list(UDPs)
        
        '    ---- decide weight for Dicidable Props ----'
        # Decide 0-1 for each MPtree
        mpsList_per_DPs = []
        # mplist * number of (DPs | DPs | DPs)
        # DPsList = (DPs | DPs | DPs)
        for DPs in DPsList:
            mps_ = []
            for mp_prop in mp_prop_pair:
                mp_ = copy.deepcopy(mp_prop['mp'])
                for dp in DPs[0]:
                    tltl.decide_cond_pos(mp_, dp)
                for dp in DPs[1]:
                    tltl.decide_cond_neg(mp_, dp)
                mps_.append(mp_)
            mpsList_per_DPs.append(mps_)
        # preudp [mp num][cond-or num]
        '    ---- decision Complete ----'
        
        mpsList_Preweight = []
        for mps in mpsList_per_DPs:
            if UDPs == []:
                m = [mps]
            else:
                m = tltl.get_weighted_uhlan(mps, UDPs)
            mpsList_Preweight.extend(m)
            
        # weights : weight per MP on 1 transition
        w8sList = []
        for mps in mpsList_Preweight:
            w8s = []
            for mp in mps:
                exec "w = "+ (tmtc.mp_out(mp.child[0]))
                # MP(t)~c ---> MP(t-c)~0
                w = w - int(mp.const)
                w8s.append(w)
            w8sList.append(w8s)
                
        # remove weight overlap in each edge
        w8list = []
        for w8 in w8sList:
            if not w8 in w8list:
                w8list.append(w8)
        mdg.add_node(edge[0])
        mdg.add_node(edge[1])
        if opts.tgba:
            for w8 in w8list:
                mdg.add_edge(edge[0],edge[1],weight=w8,acc=edge[2]['acc'])
        else:
            for w8 in w8list:
                mdg.add_edge(edge[0],edge[1],weight=w8)
        if opts.pdebug:
            for w8 in w8list:
                print '    '+str(edge[0])+' --> '+str(edge[1])+'  w8: '+str(w8)
    return mdg
コード例 #2
0
ファイル: ltlmpsat_old.py プロジェクト: kimney/ltlmp_sat
def satmain(ltlmp_formula, opts):
    total_result = {'sat':False, 'time':[], 'graphs':[], 'total_wg_time':0.0 }
    print "----- Parsing ", repr(ltlmp_formula), '-----'
    char_stream = antlr3.ANTLRStringStream(ltlmp_formula)
    lexer = LTLMPLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = LTLMPParser(tokens)
    r = parser.start()
    ast_root = r.tree # root of the AST(Type: CommonTree(antlr))
    if opts.showprogress:
        print "----- Parsing Done -----"

    nodes = antlr3.tree.CommonTreeNodeStream(ast_root)
    nodes.setTokenStream(tokens)
    evaltree = LTLMPTree(nodes)
    if opts.debug:
        print "----- Tree Parsing -----"
    ltlmp_root = evaltree.expr()
    if opts.showprogress:
        print "----- Tree Parsing Done -----"
        print '----- AST Parse Result -----'
        print tmtc.ltl_out(ltlmp_root)
        print '----- End AST Parse Reslt-----'
        print '----- LTLMP to Nomal Form List -----'
    # from tree_ltl
    # the pairs of (ltlmp-NF, )
    pairList = tltl.get_MPNF_list(ltlmp_root)
    
    if opts.debug:
        print '----- Printing MP-NF Result -----'
        for pair in pairList:
            print '  --',tmtc.ltl_out(pair[0]),'--'
            for mp in pair[1]:
                print '   ',tmtc.mp_out(mp)
            
    # improve LTL tree
    # TODO: this is future work

    # LTL2BA: LTL to never-claim(nvc)
    # LTL2TGBA: LTL to Transition-based Generalized BA
    # LTLTrans: 
    MPNF_List = []   # MP Normal Form  Sentence? list
    GraphList = []   # 
    MPList = []      # MP-Constraints List according to Graph
    MPPropsList = [] # Props(var) used in MP-Constraints according to Graph
    if opts.tgba:
        for pair in pairList:
            g = graph_tgba(pair, opts)
            if g:
                MPNF_List.append(g)
    elif opts.ltltrans:
        for pair in pairList:
            g = graph_ltltrans(pair, opts)
            if g:
                MPNF_List.append(g)
    else:
        for pair in pairList:
            g = graph_ba(pair, opts)
            if g:
                MPNF_List.append(g)
    if opts.showprogress:
        print '---- Weighted Graph generating ----'

    wg_st = time.time()

     
    print '  -------- temporal result ---------'
    for mpnf in MPNF_List:
        res_graph = {'nodes':0, 'edges':0, 'scclist':{'nodes':[], 'edges':[]} }
        res_graph['edges'] = nx.number_of_edges(mpnf['graph'])
        res_graph['nodes'] = nx.number_of_nodes(mpnf['graph'])
        print str(res_graph['nodes'])+'  --   Nodes'
        print str(res_graph['edges'])+'  --   Edges'
        st_gtime = time.time()
        mpnf['mdg'] = get_weighted_graph(mpnf['graph'], mpnf['mp-prop-pair'], opts)
        # print mpnf['mdg']
        en_gtime = time.time()
        res_graph['mnodes'] = nx.number_of_nodes(mpnf['mdg'])
        res_graph['medges'] = nx.number_of_edges(mpnf['mdg'])
        res_graph['time'] = str(en_gtime-st_gtime)+ ' -- Weighted Graph Generating Time: '
        mpnf['graph_result'] = res_graph
        print str(en_gtime-st_gtime)+ ' -- Weighted Graph Generating Time: '
        
    if opts.showprogress:
        print '----- end weighted Graph ----'
        
    wg_en = time.time()
    total_result['total_wg_time'] = wg_en - wg_st
    '  -- Total Weighted Graph Generating Time:  '
    print str(wg_en - wg_st) + '  -- Total Weighted Graph Generating Time:  '
    print '  -------- temporal result ---------'
    for mpnf in MPNF_List:
        print str(mpnf['graph_result']['mnodes'])+'  --   Nodes'
        print str(mpnf['graph_result']['medges'])+'  --   Edges'

    """
    if opts.pdebug:
        print ' pdebug: ---- Generated MultiDiGraph ----'
        if opts.tgba:
            print ' TGBA Acc Cond: ',MDGraphList[i].graph['acccond']
        for i in range(len(MDGraphList)):
            edges = MDGraphList[i].edges(data=True)
            for edge in edges:
                print '    [',
                print edge[0]+'  --->  '+edge[1] + '\tWeight: ', edge[2]['weight'],
                if opts.tgba:
                    print '\t',edge[2]['acc'],
                print ' ]'
                """
                
    if opts.showprogress:
        print '---- SMT input generating ----'

    # for SCC which includes 'accept' state'
    #     do SAT 
    # convert Weighted Graph to SMT-Solver Sentence
    SMTCodesExtList = []
    for mpnf in MPNF_List:
        mpnf['Code/SCC'] = get_SMTCode(mpnf['mdg'], mpnf['mp-prop-pair'], opts)
        
    if opts.showprogress:
        print '---- solving SAT ----'

    # temp
    print '  -------- temporal result ---------'
    for mpnf in MPNF_List:
        # mpnf- 'Code/SCC' - 'AccSCCs' - 'sat-time'
        for accscc in mpnf['Code/SCC']['AccSCCs']:
            print str(accscc['nodes'])+'  --  AccSCC Nodes'
            print str(accscc['edges'])+'  --  AccSCC Edges'
        print str(mpnf['graph_result']['nodes'])+'  --   Nodes'
        print str(mpnf['graph_result']['edges'])+'  --   Edges'
        
    unsat = 0
    for mpnf in MPNF_List:
        # SAT for Each MPNF
        for accscc in mpnf['Code/SCC']['AccSCCs']:
            # SAT for each SCC
            st = time.time()
            smt_result = do_sat(accscc['SMTCode'], opts)
            en = time.time()
            accscc['sat_time'] = en - st
            if smt_result['sat']:
                print "   SCC-SAT found"
                total_result['sat']=True
                return process_result(MPNF_List, total_result)
            else:
                print '      SCC-partial UNSAT'
                unsat = 1
        print '    SCC-total-UNSAT'
    if unsat ==1:
        print '  Totally UNSAT'
        total_result['sat'] = False
        return process_result(MPNF_List, total_result)
        # do SAT-checking for Each SCC
    else:
        print '  all through UNSAT, no BA/TGBA-acceptable SCC, noSMTcode'
        total_result['sat'] = False
        return process_result(MPNF_List, total_result)
コード例 #3
0
ファイル: ltlmpsat.py プロジェクト: kimney/ltlmp_sat
def debug1(pairList):
    print '----- Printing MP-NF Result -----'
    for pair in pairList:
        print '  --', tmtc.ltl_out(pair[0]), '--'
        for mp in pair[1]:
            print '   ', tmtc.mp_out(mp)