Beispiel #1
0
def _writePBCosntraints():

    if not _pbm.hasConstraints():
        return

    t = time.clock()
    pbmgr = PBManager()
    pbmgr.flush()
    d = time.clock()
    _pbm.elapsed_time += d - t
Beispiel #2
0
def _writePBCosntraints():
    #write any pseudoboolean constraints
    if not PBManager().hasConstraints():
        return
    

    t = time.clock()
    pbmgr = PBManager()
    pbmgr.flush();
    d = time.clock()
    PBManager().elapsed_time += d-t
Beispiel #3
0
def clear():
    clearAIG()
    GraphManager().clear()
    GeometryManager().clear()
    PBManager().clear()
    FSMManager().clear()
    BVManager().clear()
Beispiel #4
0
def _writePBCosntraints():
    # write any pseudoboolean constraints
    if not PBManager().hasConstraints():
        return

    t = time.clock()
    pbmgr = PBManager()
    pbmgr.flush()
    d = time.clock()
    PBManager().elapsed_time += d - t
Beispiel #5
0
from monosat.gnf import writeGNF
from monosat.graphcircuit import Graph, GraphManager
from monosat.logic import *
from monosat.pbtheory import PBManager, MinisatPlus

parser = argparse.ArgumentParser(description='Convert GNF format to plain CNF format')
parser.add_argument('input', type=str,
                   help='Input file, in GNF format')

parser.add_argument('output', type=str,nargs='?',default=None,
                   help='Output file, in CNF format')


args = parser.parse_args()

pbm = PBManager()
pbm.setPB(MinisatPlus())
mgr = GraphManager()

infile = open(args.input,'r')
output = open(args.output,'w') if args.input is not None else sys.stdout

graphs = []

#Read in the input file; echo the cnf right back to stdout - note that this will result in bad headers! Fix that later...

tmpcnf = tempfile.NamedTemporaryFile(mode='w',delete=False)
tmpcnfname = tmpcnf.name
#tmpcnf.close()

nclauses = 0
Beispiel #6
0
def writeGNF(circuit, managers, filestr, comments=None):
    if not circuit or isTrue(circuit):
        print(
            "Warning: no constraints enforced or written to file (trivially satisfiable)\n"
        )
        return

    if managers is None:
        managers = []
    if isinstance(managers, tuple):
        managers = list(managers)
    if not isinstance(managers, list):
        managers = [managers]

    for m in [GraphManager(), GeometryManager(), FSMManager(), BVManager()]:
        if m not in managers:
            managers.append(m)

    print("Writing to " + filestr + "...\n")
    fileout = open(filestr, 'w')
    print("c Created with monosat_py", file=fileout)
    if comments is not None:
        print("c " + str(comments), file=fileout)

    #aigfile = mktemp(suffix=".aig");
    #print("Writing to " +  aigfile + "...\n")
    if (VAR(circuit).isConstFalse()):
        print("Warning, constraints are trivially false")
    elif (VAR(circuit).isConstTrue()):
        print("Warning, constraints are trivially true")
    #print ("Writing aig to file " + aigfile)

    cnffile = mktemp(suffix=".cnf")
    varmap = WriteCNF(circuit, cnffile)
    setLiteralMap(varmap)

    #append the cnf to the file we are building
    cnfin = open(cnffile, "r")
    for line in cnfin:
        fileout.write(line)
    #fileout.write(cnfin.read())
    cnfin.close()
    os.remove(cnffile)
    fileout.flush()
    pbmgr = PBManager()
    pbmgr.write(filestr)

    print("Writing graph...")
    graphfile = open(filestr, 'a+')
    for manager in managers:
        if (manager is not None):
            manager.write(graphfile)
    all_symbols = list(getSymbols().keys())
    all_symbols.sort()
    for symbol in all_symbols:
        assert (symbol in getSymbols())
        var = getSymbols()[symbol]
        graphfile.write("c var " + str(var.getInputLiteral()) + " " + symbol +
                        "\n")

    graphfile.close()
    print("Done writing to " + filestr + "\n")
Beispiel #7
0
def writeCNF(circuit, graphmgr, filestr,comments=None, use_unary_encoding=True):
    print("Writing to " +  filestr + "...\n")
    fileout = open(filestr,'w')
    print("c Created with monosat_py", file=fileout)
    if comments is not None:
        print("c " + str(comments),file=fileout);
    fileout.close()
    print("Writing to " +  filestr + "...\n")
    
    if(VAR(circuit).isConstFalse()):
        print("Warning, constraints are trivially false");
    elif(VAR(circuit).isConstTrue()):
        print("Warning, constraints are trivially true");

    print("Building CNF graph constraints:")
    old_assertions = list(assertions)    
    clearAssertions()
    Assert(circuit)
    for gid in range(len(graphmgr.graphs)):
        
        g = CNFGraph(gid,use_unary_encoding)
        for _ in range(graphmgr.graphs[gid].numNodes()):
            g.addNode()
        
        for (v,w,var,weight) in graphmgr.graphs[gid].alledges:
            g.addEdge(v,w,weight,var)
                
        for (start,steps,reach,v) in graphmgr.graphs[gid].queries:
            Assert(v== g.reaches(start,reach,steps))
                
                
        for (start,steps,reach,v) in graphmgr.graphs[gid].undirected_queries:
            Assert(v== g.connects(start,reach,steps))
        
        for (minweight,v) in graphmgr.graphs[gid].mstQueries:
            if v is truenode:
                g.AssertMinimumSpanningTreeLessEq(minweight)
            else:
                Assert(v== g.minimumSpanningTreeLessEq(minweight))
                   
        for (edgeVar,v) in graphmgr.graphs[gid].mstEdgeQueries:     
           
            Assert(v== g.edgeInMinimumSpanningTree(edgeVar))  
            
        
        for (s,t, flow,v) in graphmgr.graphs[gid].flowQueries:
            #catch constants here 
            if v is truenode:
                g.AssertMaxFlowGreaterOrEqualToOneSided(s,t,flow)
            elif v is falsenode:
                g.AssertMinCutLessOrEqualToOneSided(s,t,flow-1)
            else:
                Assert(v== g.maxFlowGreaterOrEqualTo(s,t,flow))
            
        for (min_components,v) in graphmgr.graphs[gid].componentsQueries :
            if v is truenode:
                g.AssertConnectedComponentsGreaterThan(min_components)
            elif v is falsenode:
                g.AssertConnectedComponentsLessEq(min_components)
            else:
                Assert(v== g.connectedComponentsGreaterThan(min_components))                   
      
        for steiner in graphmgr.graphs[gid].steiners:
            #Not implemented.
            assert(False)
            
     
    
    
    varmap = WriteCNF(And(assertions),filestr)
    setLiteralMap(varmap)
    
    pbmgr = PBManager()
    pbmgr.write(filestr)    
    fileout = open(filestr,'a')
    all_symbols = list(getSymbols().keys())
    all_symbols.sort()
    for symbol in all_symbols:
        assert(symbol in getSymbols())
        var = getSymbols()[symbol]
        fileout.write("c var " + str(var.getInputLiteral()) + " " + symbol + "\n")
    fileout.close()
    print("Done writing to " +  filestr + "\n")   
    
    clearAssertions()
    setAssertions(old_assertions)
    clearLiteralMap()
     
Beispiel #8
0
from monosat.monosat_c import Monosat
from monosat.logic import *
from monosat.bvtheory import BitVector
from monosat.graphtheory import Graph
from monosat.pbtheory import PBManager
import time
_monosat = Monosat()
_pbm = PBManager()
_pbm.elapsed_time = 0
_monosat.elapsed_time = 0


def Solve(assumptions=None, preprocessing=True):
    #first, write any pseudoboolean constraints
    _writePBCosntraints()

    #if preprocessing:
    #    _monosat.preprocess();
    print("Solving in Monosat...")
    t = time.clock()
    if assumptions is not None:
        r = _monosat.solveAssumptions([x.getLit() for x in assumptions])
    else:
        r = _monosat.solve()
    _monosat.elapsed_time += time.clock() - t
    return r


def _writePBCosntraints():

    if not _pbm.hasConstraints():
Beispiel #9
0
def writeCNF(circuit, graphmgr, filestr,comments=None, use_unary_encoding=True):    
    print("Writing to " +  filestr + "...\n")
    fileout = open(filestr,'w')
    print("c Created with monosat_py", file=fileout)
    if comments is not None:
        print("c " + str(comments),file=fileout);
    fileout.close()
    print("Writing to " +  filestr + "...\n")
    
    if(VAR(circuit).isConstFalse()):
        print("Warning, constraints are trivially false");
    elif(VAR(circuit).isConstTrue()):
        print("Warning, constraints are trivially true");

    print("Building CNF graph constraints:")
    old_assertions = list(assertions)    
    clearAssertions()
    Assert(circuit)
    for gid in range(len(graphmgr.graphs)):
        
        g = CNFGraph(gid,use_unary_encoding)
        for _ in range(graphmgr.graphs[gid].numNodes()):
            g.addNode()
        
        for (v,w,var,weight) in graphmgr.graphs[gid].alledges:
            g.addEdge(v,w,weight,var)
                
        for (start,steps,reach,v) in graphmgr.graphs[gid].queries:
            Assert(v== g.reaches(start,reach,steps))
                
                
        for (start,steps,reach,v) in graphmgr.graphs[gid].undirected_queries:
            Assert(v== g.connects(start,reach,steps))
        
        for (minweight,v) in graphmgr.graphs[gid].mstQueries:
            if v is truenode:
                g.AssertMinimumSpanningTreeLessEq(minweight)
            else:
                Assert(v== g.minimumSpanningTreeLessEq(minweight))
                   
        for (edgeVar,v) in graphmgr.graphs[gid].mstEdgeQueries:     
           
            Assert(v== g.edgeInMinimumSpanningTree(edgeVar))  
            
        
        for (s,t, flow,v) in graphmgr.graphs[gid].flowQueries:
            #catch constants here 
            if v is truenode:
                g.AssertMaxFlowGreaterOrEqualToOneSided(s,t,flow)
            elif v is falsenode:
                g.AssertMinCutLessOrEqualToOneSided(s,t,flow-1)
            else:
                Assert(v== g.maxFlowGreaterOrEqualTo(s,t,flow))
            
        for (min_components,v) in graphmgr.graphs[gid].componentsQueries :
            if v is truenode:
                g.AssertConnectedComponentsGreaterThan(min_components)
            elif v is falsenode:
                g.AssertConnectedComponentsLessEq(min_components)
            else:
                Assert(v== g.connectedComponentsGreaterThan(min_components))                   
      
        for steiner in graphmgr.graphs[gid].steiners:
            #Not implemented.
            assert(False)
            
     
    
    
    varmap = WriteCNF(And(assertions),filestr)
    setLiteralMap(varmap)
    
    pbmgr = PBManager()
    pbmgr.write(filestr)    
    fileout = open(filestr,'a')
    all_symbols = list(getSymbols().keys())
    all_symbols.sort()
    for symbol in all_symbols:
        assert(symbol in getSymbols())
        var = getSymbols()[symbol]
        fileout.write("c var " + str(var.getInputLiteral()) + " " + symbol + "\n")
    fileout.close()
    print("Done writing to " +  filestr + "\n")   
    
    clearAssertions()
    setAssertions(old_assertions)
    clearLiteralMap()
     
Beispiel #10
0
def writeGNF(circuit, managers, filestr,comments=None):
    if not circuit or isTrue(circuit):
        print("Warning: no constraints enforced or written to file (trivially satisfiable)\n")
        return 
    
    if managers is None:
        managers=[]
    if isinstance(managers,tuple):
        managers=list(managers)
    if not isinstance(managers,list):
        managers=[managers]
        
    for m in [GraphManager(),GeometryManager(),FSMManager(),BVManager()]:
        if m not in managers:
            managers.append(m)
    
    print("Writing to " +  filestr + "...\n")
    fileout = open(filestr,'w')
    print("c Created with monosat_py", file=fileout)
    if comments is not None:
        print("c " + str(comments),file=fileout);
    
    
    #aigfile = mktemp(suffix=".aig");
    #print("Writing to " +  aigfile + "...\n")
    if(VAR(circuit).isConstFalse()):
        print("Warning, constraints are trivially false");
    elif(VAR(circuit).isConstTrue()):
        print("Warning, constraints are trivially true");
    #print ("Writing aig to file " + aigfile)


    cnffile = mktemp(suffix=".cnf");
    varmap = WriteCNF(circuit,cnffile)
    setLiteralMap(varmap)    
    
    #append the cnf to the file we are building
    cnfin = open(cnffile,"r");    
    for line in cnfin:
        fileout.write(line);
    #fileout.write(cnfin.read())
    cnfin.close();
    os.remove(cnffile)
    fileout.flush();
    pbmgr = PBManager()
    pbmgr.write(filestr)
    
    print ("Writing graph...")
    graphfile = open(filestr,'a+');
    for manager in managers:
        if(manager is not None):
            manager.write(graphfile)
    all_symbols = list(getSymbols().keys())
    all_symbols.sort()
    for symbol in all_symbols:
        assert(symbol in getSymbols())
        var = getSymbols()[symbol]
        graphfile.write("c var " + str(var.getInputLiteral()) + " " + symbol + "\n")
    
    graphfile.close()
    print("Done writing to " +  filestr + "\n")