Search in sources :

Example 1 with AbstractReporter

use of kodkod.engine.config.AbstractReporter in project org.alloytools.alloy by AlloyTools.

the class BenchmarkSymmStats2 method printGBP.

// <symm time (ms)> <# of symms> <state bits> <SAT|UNSAT> <SAT time (ms)>
private static void printGBP(Formula formula, Bounds bounds) {
    final class SymmReporter extends AbstractReporter {

        long gbpTime;

        BigInteger symms;

        @Override
        public void detectingSymmetries(Bounds bounds) {
            gbpTime = bean.getCurrentThreadUserTime();
        }

        @Override
        public void detectedSymmetries(Set<IntSet> parts) {
            final long end = bean.getCurrentThreadUserTime();
            gbpTime = (end - gbpTime) / 1000000;
            symms = new BigInteger("1");
            for (IntSet s : parts) {
                symms = symms.multiply(fact(s.size()));
            }
        // System.out.println(parts);
        }
    }
    ;
    final SymmReporter reporter = new SymmReporter();
    final Solver solver = new Solver();
    solver.options().setBitwidth(8);
    solver.options().setSolver(SATFactory.MiniSat);
    solver.options().setReporter(reporter);
    final Solution sol = solver.solve(formula, bounds);
    // <gbp (ms)> <gbp (symms)>
    System.out.print(reporter.gbpTime + "\t");
    System.out.print(reporter.symms + "\t");
    // <state bits> <SAT|UNSAT> <SAT time (ms)>
    System.out.print(sol.stats().primaryVariables() + "\t");
    System.out.print(sol.instance() == null ? "UNSAT\t" : "SAT\t");
    System.out.print(sol.stats().solvingTime() + "\t");
}
Also used : Solver(kodkod.engine.Solver) LinkedHashSet(java.util.LinkedHashSet) TupleSet(kodkod.instance.TupleSet) IntTreeSet(kodkod.util.ints.IntTreeSet) IntSet(kodkod.util.ints.IntSet) Set(java.util.Set) IntSet(kodkod.util.ints.IntSet) Bounds(kodkod.instance.Bounds) AbstractReporter(kodkod.engine.config.AbstractReporter) BigInteger(java.math.BigInteger) Solution(kodkod.engine.Solution)

Example 2 with AbstractReporter

use of kodkod.engine.config.AbstractReporter in project org.alloytools.alloy by AlloyTools.

the class BenchmarkSymmStats2 method printGAD.

// <symm time (ms)> <# of symms> <state bits> <SAT|UNSAT> <SAT time (ms)>
private static void printGAD(Formula formula, Bounds bounds) {
    final class SymmReporter extends AbstractReporter {

        String symms, time;

        Bounds bounds;

        @Override
        public void detectingSymmetries(Bounds bounds) {
            this.bounds = bounds.clone();
        }

        @Override
        public void detectedSymmetries(Set<IntSet> parts) {
            parts.clear();
            final SymmInfo allSymms = allSymms(bounds);
            parts.addAll(allSymms.parts);
            symms = allSymms.symms;
            time = allSymms.time;
        // System.out.println(parts);
        }
    }
    ;
    final SymmReporter reporter = new SymmReporter();
    final Solver solver = new Solver();
    solver.options().setBitwidth(8);
    solver.options().setSolver(SATFactory.MiniSat);
    solver.options().setReporter(reporter);
    final Solution sol = solver.solve(formula, bounds);
    // <gbp (ms)> <gbp (symms)>
    System.out.print(reporter.time + "\t");
    System.out.print(reporter.symms + "\t");
    // <state bits> <SAT|UNSAT> <SAT time (ms)>
    System.out.print(sol.stats().primaryVariables() + "\t");
    System.out.print(sol.instance() == null ? "UNSAT\t" : "SAT\t");
    System.out.print(sol.stats().solvingTime() + "\t");
}
Also used : Solver(kodkod.engine.Solver) LinkedHashSet(java.util.LinkedHashSet) TupleSet(kodkod.instance.TupleSet) IntTreeSet(kodkod.util.ints.IntTreeSet) IntSet(kodkod.util.ints.IntSet) Set(java.util.Set) Bounds(kodkod.instance.Bounds) AbstractReporter(kodkod.engine.config.AbstractReporter) Solution(kodkod.engine.Solution)

Example 3 with AbstractReporter

use of kodkod.engine.config.AbstractReporter in project org.alloytools.alloy by AlloyTools.

the class A4Solution method solve.

// ===================================================================================================//
/**
 * Solve for the solution if not solved already; if cmd==null, we will simply
 * use the lowerbound of each relation as its value.
 */
A4Solution solve(final A4Reporter rep, Command cmd, Simplifier simp, boolean tryBookExamples) throws Err, IOException {
    // If already solved, then return this object as is
    if (solved)
        return this;
    // the lower bound of each relation
    if (cmd == null) {
        Instance inst = new Instance(bounds.universe());
        for (int max = max(), i = min(); i <= max; i++) {
            Tuple it = factory.tuple("" + i);
            inst.add(i, factory.range(it, it));
        }
        for (Relation r : bounds.relations()) inst.add(r, bounds.lowerBound(r));
        eval = new Evaluator(inst, solver.options());
        rename(this, null, null, new UniqueNameGenerator());
        solved();
        return this;
    }
    // Otherwise, prepare to do the solve...
    final A4Options opt = originalOptions;
    long time = System.currentTimeMillis();
    rep.debug("Simplifying the bounds...\n");
    if (opt.inferPartialInstance && simp != null && formulas.size() > 0 && !simp.simplify(rep, this, formulas))
        addFormula(Formula.FALSE, Pos.UNKNOWN);
    rep.translate(opt.solver.id(), bitwidth, maxseq, solver.options().skolemDepth(), solver.options().symmetryBreaking());
    Formula fgoal = Formula.and(formulas);
    rep.debug("Generating the solution...\n");
    kEnumerator = null;
    Solution sol = null;
    final Reporter oldReporter = solver.options().reporter();
    final boolean[] solved = new boolean[] { true };
    solver.options().setReporter(new // Set up a
    AbstractReporter() {

        // reporter to
        // catch the
        // type+pos of
        // skolems
        @Override
        public void skolemizing(Decl decl, Relation skolem, List<Decl> predecl) {
            try {
                Type t = kv2typepos(decl.variable()).a;
                if (t == Type.EMPTY)
                    return;
                for (int i = (predecl == null ? -1 : predecl.size() - 1); i >= 0; i--) {
                    Type pp = kv2typepos(predecl.get(i).variable()).a;
                    if (pp == Type.EMPTY)
                        return;
                    t = pp.product(t);
                }
                kr2type(skolem, t);
            }// Exception here is not fatal
             catch (Throwable ex) {
            }
        }

        @Override
        public void solvingCNF(int primaryVars, int vars, int clauses) {
            if (solved[0])
                return;
            else
                // initially solved[0] is true, so we
                solved[0] = true;
            // won't report the # of vars/clauses
            if (rep != null)
                rep.solve(primaryVars, vars, clauses);
        }
    });
    if (!opt.solver.equals(SatSolver.CNF) && !opt.solver.equals(SatSolver.KK) && tryBookExamples) {
        // try
        // book
        // examples
        A4Reporter r = AlloyCore.isDebug() ? rep : null;
        try {
            sol = BookExamples.trial(r, this, fgoal, solver, cmd.check);
        } catch (Throwable ex) {
            sol = null;
        }
    }
    // this allows the reporter to report the # of
    solved[0] = false;
    // vars/clauses
    for (Relation r : bounds.relations()) {
        formulas.add(r.eq(r));
    }
    // Without this, kodkod refuses to grow unmentioned relations
    fgoal = Formula.and(formulas);
    // Now pick the solver and solve it!
    if (opt.solver.equals(SatSolver.KK)) {
        File tmpCNF = File.createTempFile("tmp", ".java", new File(opt.tempDirectory));
        String out = tmpCNF.getAbsolutePath();
        Util.writeAll(out, debugExtractKInput());
        rep.resultCNF(out);
        return null;
    }
    if (opt.solver.equals(SatSolver.CNF)) {
        File tmpCNF = File.createTempFile("tmp", ".cnf", new File(opt.tempDirectory));
        String out = tmpCNF.getAbsolutePath();
        solver.options().setSolver(WriteCNF.factory(out));
        try {
            sol = solver.solve(fgoal, bounds);
        } catch (WriteCNF.WriteCNFCompleted ex) {
            rep.resultCNF(out);
            return null;
        }
        // The formula is trivial (otherwise, it would have thrown an
        // exception)
        // Since the user wants it in CNF format, we manually generate a
        // trivially satisfiable (or unsatisfiable) CNF file.
        Util.writeAll(out, sol.instance() != null ? "p cnf 1 1\n1 0\n" : "p cnf 1 2\n1 0\n-1 0\n");
        rep.resultCNF(out);
        return null;
    }
    if (!solver.options().solver().incremental()) /*
                                                      * || solver.options().solver()==SATFactory. ZChaffMincost
                                                      */
    {
        if (sol == null)
            sol = solver.solve(fgoal, bounds);
    } else {
        kEnumerator = new Peeker<Solution>(solver.solveAll(fgoal, bounds));
        if (sol == null)
            sol = kEnumerator.next();
    }
    if (!solved[0])
        rep.solve(0, 0, 0);
    final Instance inst = sol.instance();
    // To ensure no more output during SolutionEnumeration
    solver.options().setReporter(oldReporter);
    // If unsatisfiable, then retreive the unsat core if desired
    if (inst == null && solver.options().solver() == SATFactory.MiniSatProver) {
        try {
            lCore = new LinkedHashSet<Node>();
            Proof p = sol.proof();
            if (sol.outcome() == UNSATISFIABLE) {
                // only perform the minimization if it was UNSATISFIABLE,
                // rather than TRIVIALLY_UNSATISFIABLE
                int i = p.highLevelCore().size();
                rep.minimizing(cmd, i);
                if (opt.coreMinimization == 0)
                    try {
                        p.minimize(new RCEStrategy(p.log()));
                    } catch (Throwable ex) {
                    }
                if (opt.coreMinimization == 1)
                    try {
                        p.minimize(new HybridStrategy(p.log()));
                    } catch (Throwable ex) {
                    }
                rep.minimized(cmd, i, p.highLevelCore().size());
            }
            for (Iterator<TranslationRecord> it = p.core(); it.hasNext(); ) {
                Object n = it.next().node();
                if (n instanceof Formula)
                    lCore.add((Formula) n);
            }
            Map<Formula, Node> map = p.highLevelCore();
            hCore = new LinkedHashSet<Node>(map.keySet());
            hCore.addAll(map.values());
        } catch (Throwable ex) {
            lCore = hCore = null;
        }
    }
    // If satisfiable, then add/rename the atoms and skolems
    if (inst != null) {
        eval = new Evaluator(inst, solver.options());
        rename(this, null, null, new UniqueNameGenerator());
    }
    // report the result
    solved();
    time = System.currentTimeMillis() - time;
    if (inst != null)
        rep.resultSAT(cmd, time, this);
    else
        rep.resultUNSAT(cmd, time, this);
    return this;
}
Also used : HybridStrategy(kodkod.engine.ucore.HybridStrategy) Instance(kodkod.instance.Instance) Node(kodkod.ast.Node) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) RCEStrategy(kodkod.engine.ucore.RCEStrategy) Solution(kodkod.engine.Solution) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) AbstractReporter(kodkod.engine.config.AbstractReporter) Reporter(kodkod.engine.config.Reporter) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) Decl(kodkod.ast.Decl) Evaluator(kodkod.engine.Evaluator) UniqueNameGenerator(edu.mit.csail.sdg.alloy4.UniqueNameGenerator) Type(edu.mit.csail.sdg.ast.Type) Proof(kodkod.engine.Proof) File(java.io.File) Tuple(kodkod.instance.Tuple)

Example 4 with AbstractReporter

use of kodkod.engine.config.AbstractReporter in project org.alloytools.alloy by AlloyTools.

the class BugTests method testFelix_11262007.

public final void testFelix_11262007() {
    Relation x6 = Relation.unary("R2");
    List<String> atomlist = Arrays.asList("X", "Y", "Z");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    bounds.bound(x6, factory.allOf(1));
    final Variable x32 = Variable.unary("a");
    final Decls x31 = x32.oneOf(x6);
    final Variable x36 = Variable.unary("b");
    final Decls x35 = x36.oneOf(x32.join(x6.product(x6)));
    final Formula x29 = x36.some().forSome(x35).forSome(x31);
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    solver.options().setSkolemDepth(0);
    final Set<Decl> decls = new LinkedHashSet<Decl>();
    solver.options().setReporter(new AbstractReporter() {

        @Override
        public void skolemizing(Decl decl, Relation skolem, List<Decl> predecl) {
            decls.add(decl);
        }
    });
    Solution sol = solver.solve(x29, bounds);
    assertEquals(2, decls.size());
    assertTrue(decls.contains(x31));
    assertTrue(decls.contains(x35));
    assertNotNull(sol.instance());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Solver(kodkod.engine.Solver) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Decl(kodkod.ast.Decl) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) AbstractReporter(kodkod.engine.config.AbstractReporter) Solution(kodkod.engine.Solution)

Aggregations

Solution (kodkod.engine.Solution)4 AbstractReporter (kodkod.engine.config.AbstractReporter)4 LinkedHashSet (java.util.LinkedHashSet)3 Solver (kodkod.engine.Solver)3 Bounds (kodkod.instance.Bounds)3 Set (java.util.Set)2 Decl (kodkod.ast.Decl)2 Formula (kodkod.ast.Formula)2 Relation (kodkod.ast.Relation)2 TupleSet (kodkod.instance.TupleSet)2 IntSet (kodkod.util.ints.IntSet)2 IntTreeSet (kodkod.util.ints.IntTreeSet)2 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1 UniqueNameGenerator (edu.mit.csail.sdg.alloy4.UniqueNameGenerator)1 Type (edu.mit.csail.sdg.ast.Type)1 File (java.io.File)1 BigInteger (java.math.BigInteger)1 BinaryFormula (kodkod.ast.BinaryFormula)1 Decls (kodkod.ast.Decls)1 Node (kodkod.ast.Node)1