Search in sources :

Example 1 with WeightedMaxSatDecorator

use of org.sat4j.maxsat.WeightedMaxSatDecorator in project abstools by abstools.

the class SatGenerator method generate.

public Map<LocationTypeVariable, LocationType> generate() {
    long startNanos = System.nanoTime();
    Map<LocationTypeVariable, LocationType> tvl = new HashMap<>();
    for (Constraint c : constraints) {
        if (enableDebug)
            System.out.println(c);
        List<List<Integer>> gen = c.generateSat(e);
        output.addAll(gen);
        c.variables(vars);
    }
    long genNanos = System.nanoTime();
    if (enableStats) {
        System.out.println("Constraint generation time: " + (genNanos - startNanos) / 1000000);
    }
    StringBuilder weights = new StringBuilder();
    int countNiceConstraints = 0;
    for (LocationTypeVariable tv : vars) {
        if (tv.getNode() != null) {
            countNiceConstraints++;
            weights.append(Constraint.NICE_TO_HAVE);
            weights.append(" ");
            weights.append(e.get(tv, LocationType.NEAR));
            weights.append(" ");
            weights.append(e.get(tv, LocationType.FAR));
            weights.append(" ");
            for (LocationType lt : tv.parametricFarTypes()) {
                weights.append(e.get(tv, lt));
                weights.append(" ");
            }
            weights.append("0\n");
        }
    }
    StringBuilder sb = new StringBuilder();
    int nbclauses = output.size() + countNiceConstraints;
    int nbvars = e.current;
    addInitLine(sb, nbclauses, nbvars);
    // update should_have
    int newShouldHave = countNiceConstraints * Constraint.NICE_TO_HAVE + 1;
    for (List<Integer> line : output) {
        if (line.get(0).equals(Constraint.SHOULD_HAVE)) {
            line.set(0, newShouldHave);
        }
    }
    // System.out.println("SHOULD_HAVE value: "+ newShouldHave);
    try {
        for (List<Integer> line : output) {
            for (Integer i : line) {
                sb.append(i);
                sb.append(" ");
            }
            sb.append("0\n");
        }
    } catch (Exception e) {
        System.out.println(sb.length());
        throw e;
    }
    sb.append(weights);
    if (enableStats) {
        System.out.println("Number of variables: " + nbvars);
        System.out.println("Number of clauses: " + nbclauses);
    }
    // System.out.println(sb);
    // instance().defaultSolver();
    IPBSolver solver = org.sat4j.maxsat.SolverFactory.newDefault();
    // IPBSolver solver = org.sat4j.pb.SolverFactory.newBoth();
    // System.exit(0);
    WeightedMaxSatDecorator wmsd = new WeightedMaxSatDecorator(solver);
    WDimacsReader reader = new WDimacsReader(wmsd);
    try {
        InputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
        IProblem problem = reader.parseInstance(is);
        long parseNanos = System.nanoTime();
        if (enableStats) {
            System.out.println("Parsing time: " + (parseNanos - genNanos) / 1000000);
        }
        if (enableStats) {
            System.gc();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.gc();
        }
        OptToPBSATAdapter opt = new OptToPBSATAdapter(wmsd);
        opt.setVerbose(false);
        // opt.setTimeoutMs(10000);
        // parseNanos = System.nanoTime();
        // opt.setTimeout(arg0)
        parseNanos = System.nanoTime();
        if (opt.isSatisfiable()) {
            int[] model = opt.model();
            long solveNanos = System.nanoTime();
            if (enableStats) {
                System.out.println("Solving time: " + (solveNanos - parseNanos) / 1000000);
                System.out.println("Total time: " + (solveNanos - startNanos) / 1000000);
            }
            // System.out.println(Arrays.toString(model));
            for (int i : model) {
                if (i > 0) /*&& i <= nbvars*/
                {
                    TypedVar tv = e.vars().get(i - 1);
                    // System.out.println(tv.v + " : " + tv.t);
                    tvl.put(tv.v, tv.t);
                }
            }
            if (enableDebug)
                System.out.println("Solution: " + tvl);
            if (enableStats) {
                int fars = 0;
                int sws = 0;
                int nears = 0;
                int paramfars = 0;
                for (Entry<LocationTypeVariable, LocationType> e : tvl.entrySet()) {
                    if (e.getKey().getNode() != null) {
                        LocationType t = e.getValue();
                        if (t.isFar())
                            fars++;
                        if (t.isParametricFar())
                            paramfars++;
                        if (t.isNear())
                            nears++;
                        if (t.isSomewhere())
                            sws++;
                    }
                }
                System.out.println("Fars: " + fars);
                System.out.println("Somewheres: " + sws);
                System.out.println("Nears: " + nears);
                System.out.println("Parametric Fars: " + paramfars);
                System.out.println("Total: " + (fars + sws + nears + paramfars));
            }
        } else {
            return null;
        }
        return tvl;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParseFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ContradictionException e) {
    // TODO Auto-generated catch block
    // e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : IPBSolver(org.sat4j.pb.IPBSolver) WDimacsReader(org.sat4j.maxsat.reader.WDimacsReader) HashMap(java.util.HashMap) ContradictionException(org.sat4j.specs.ContradictionException) FileNotFoundException(java.io.FileNotFoundException) ParseFormatException(org.sat4j.reader.ParseFormatException) SemanticConditionList(abs.frontend.analyser.SemanticConditionList) ArrayList(java.util.ArrayList) List(java.util.List) TimeoutException(org.sat4j.specs.TimeoutException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) WeightedMaxSatDecorator(org.sat4j.maxsat.WeightedMaxSatDecorator) IProblem(org.sat4j.specs.IProblem) ContradictionException(org.sat4j.specs.ContradictionException) IOException(java.io.IOException) ParseFormatException(org.sat4j.reader.ParseFormatException) FileNotFoundException(java.io.FileNotFoundException) TimeoutException(org.sat4j.specs.TimeoutException) ByteArrayInputStream(java.io.ByteArrayInputStream) OptToPBSATAdapter(org.sat4j.pb.OptToPBSATAdapter) LocationType(abs.frontend.typechecker.locationtypes.LocationType)

Aggregations

SemanticConditionList (abs.frontend.analyser.SemanticConditionList)1 LocationType (abs.frontend.typechecker.locationtypes.LocationType)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 WeightedMaxSatDecorator (org.sat4j.maxsat.WeightedMaxSatDecorator)1 WDimacsReader (org.sat4j.maxsat.reader.WDimacsReader)1 IPBSolver (org.sat4j.pb.IPBSolver)1 OptToPBSATAdapter (org.sat4j.pb.OptToPBSATAdapter)1 ParseFormatException (org.sat4j.reader.ParseFormatException)1 ContradictionException (org.sat4j.specs.ContradictionException)1 IProblem (org.sat4j.specs.IProblem)1 TimeoutException (org.sat4j.specs.TimeoutException)1