Search in sources :

Example 1 with IntTreeSet

use of kodkod.util.ints.IntTreeSet in project org.alloytools.alloy by AlloyTools.

the class StrategyUtils method rootVars.

/**
 * Returns the variables that correspond to the roots of log.formula.
 *
 * @return
 *
 *         <pre>
 *
 * { v: int | some r: log.records |
 *   r.node in log.roots() and
 *   r.env.isEmpty() and
 *   abs(r.literal) != Integer.MAX_VALUE and
 *   v = abs(r.literal) and
 *   no r': log.records | r'.node = r.node && log.replay.r' > log.replay.r }
 *         </pre>
 */
public static IntSet rootVars(TranslationLog log) {
    final IntSet rootVars = new IntTreeSet();
    final Set<Formula> roots = log.roots();
    final Map<Formula, int[]> maxRootVar = new LinkedHashMap<Formula, int[]>(roots.size());
    final RecordFilter filter = new RecordFilter() {

        @Override
        public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
            return roots.contains(translated) && env.isEmpty();
        }
    };
    for (Iterator<TranslationRecord> itr = log.replay(filter); itr.hasNext(); ) {
        TranslationRecord record = itr.next();
        int[] var = maxRootVar.get(record.translated());
        if (var == null) {
            var = new int[1];
            maxRootVar.put(record.translated(), var);
        }
        var[0] = StrictMath.abs(record.literal());
    }
    for (int[] var : maxRootVar.values()) {
        int topVar = var[0];
        if (// formula simplified to TRUE
        topVar != Integer.MAX_VALUE)
            rootVars.add(var[0]);
    }
    return rootVars;
}
Also used : IntSet(kodkod.util.ints.IntSet) IntTreeSet(kodkod.util.ints.IntTreeSet) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) LinkedHashMap(java.util.LinkedHashMap) Formula(kodkod.ast.Formula) IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

Example 2 with IntTreeSet

use of kodkod.util.ints.IntTreeSet in project org.alloytools.alloy by AlloyTools.

the class StrategyUtils method coreVars.

/**
 * Returns the variables that correspond to the roots of log.formula, in the
 * order in which they were specified in log.formula.
 *
 * @return variables that correspond to the roots of log.formula, in the order
 *         in which they were specified in log.formula.
 */
// static IntVector orderedRootVars(TranslationLog log) {
// final Set<Formula> roots = log.roots();
// final Map<Formula,int[]> maxRootVar = new
// LinkedHashMap<Formula,int[]>(roots.size());
// final RecordFilter filter = new RecordFilter() {
// public boolean accept(Node node, int literal, Map<Variable, TupleSet>
// env) {
// return roots.contains(node) && env.isEmpty();
// }
// };
// for(Iterator<TranslationRecord> itr = log.replay(filter); itr.hasNext();)
// {
// TranslationRecord record = itr.next();
// int[] var = maxRootVar.get(record.node());
// if (var==null) {
// var = new int[1];
// maxRootVar.put((Formula)record.node(), var);
// }
// var[0] = StrictMath.abs(record.literal());
// }
// final IntSet uniqueRoots = new IntTreeSet();
// final IntVector orderedRoots = new ArrayIntVector(roots.size());
// for(int[] var : maxRootVar.values()) {
// int topVar = var[0];
// if (topVar != Integer.MAX_VALUE) // formula simplified to TRUE
// if (uniqueRoots.add(var[0])) {
// orderedRoots.add(var[0]);
// };
// }
// return orderedRoots;
// }
/**
 * Returns relevant core variables; that is, all variables that occur both in
 * the positive and negative phase in trace.core.
 *
 * @return { v: [1..) | (some p, n: trace.core | v in trace.elts[p].literals and
 *         -v in trace.elts[n].literals) }
 */
public static IntSet coreVars(ResolutionTrace trace) {
    final IntSet posVars = new IntTreeSet(), negVars = new IntTreeSet();
    for (Iterator<Clause> iter = trace.iterator(trace.core()); iter.hasNext(); ) {
        Clause clause = iter.next();
        for (IntIterator lits = clause.literals(); lits.hasNext(); ) {
            int lit = lits.next();
            if (lit > 0)
                posVars.add(lit);
            else
                negVars.add(-lit);
        }
    }
    posVars.retainAll(negVars);
    assert !posVars.isEmpty();
    final IntSet ret = new IntBitSet(posVars.max() + 1);
    ret.addAll(posVars);
    return ret;
}
Also used : IntBitSet(kodkod.util.ints.IntBitSet) IntIterator(kodkod.util.ints.IntIterator) IntSet(kodkod.util.ints.IntSet) IntTreeSet(kodkod.util.ints.IntTreeSet) Clause(kodkod.engine.satlab.Clause)

Example 3 with IntTreeSet

use of kodkod.util.ints.IntTreeSet in project org.alloytools.alloy by AlloyTools.

the class ResolutionBasedProof method highLevelCore.

/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.Proof#highLevelCore()
 */
@Override
public final Map<Formula, Node> highLevelCore() {
    if (coreRoots == null) {
        final RecordFilter unitFilter = new RecordFilter() {

            final IntSet coreUnits = StrategyUtils.coreUnits(solver.proof());

            final Set<Formula> roots = log().roots();

            @Override
            public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
                return roots.contains(translated) && coreUnits.contains(Math.abs(literal));
            }
        };
        coreRoots = new LinkedHashMap<Formula, Node>();
        final IntSet seenUnits = new IntTreeSet();
        for (Iterator<TranslationRecord> itr = log().replay(unitFilter); itr.hasNext(); ) {
            // it is possible that two top-level formulas have identical
            // meaning,
            // and are represented with the same core unit; in that case, we
            // want only
            // one of them in the core.
            final TranslationRecord rec = itr.next();
            if (seenUnits.add(rec.literal())) {
                coreRoots.put(rec.translated(), rec.node());
            }
        }
        coreRoots = Collections.unmodifiableMap(coreRoots);
    }
    return coreRoots;
}
Also used : Formula(kodkod.ast.Formula) IntTreeSet(kodkod.util.ints.IntTreeSet) IdentityHashSet(kodkod.util.collections.IdentityHashSet) IntSet(kodkod.util.ints.IntSet) Set(java.util.Set) TupleSet(kodkod.instance.TupleSet) IntSet(kodkod.util.ints.IntSet) IntTreeSet(kodkod.util.ints.IntTreeSet) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

Example 4 with IntTreeSet

use of kodkod.util.ints.IntTreeSet in project org.alloytools.alloy by AlloyTools.

the class BenchmarkSymmStats2 method allSymms.

private static SymmInfo allSymms(Bounds bounds) {
    try {
        final long startGen = bean.getCurrentThreadUserTime();
        final File tmp = File.createTempFile("symmgraph", ".txt");
        final PrintStream stream = new PrintStream(new FileOutputStream(tmp));
        toNauty(bounds, stream);
        stream.close();
        final long endGen = bean.getCurrentThreadUserTime();
        final String cmd = "/Users/emina/Desktop/tools/nauty22/run_dreadnaut " + tmp.getAbsoluteFile();
        final ProcessRunner runner = new ProcessRunner(cmd.split("\\s"));
        runner.start();
        try {
            runner.join(BenchmarkDriver.FIVE_MIN);
            if (runner.getState() != Thread.State.TERMINATED) {
                System.out.print("t\\o\t");
                System.out.print("t\\o\t");
                runner.destroyProcess();
                destroy("dreadnaut");
                tmp.delete();
                return new SymmInfo(bounds.universe().size());
            }
            final BufferedReader out = new BufferedReader(new InputStreamReader(runner.processOutput(), "ISO-8859-1"));
            String line;
            String allSymms = null;
            long time = -1;
            final Set<IntSet> parts = new LinkedHashSet<IntSet>();
            final Matcher smatcher = Pattern.compile(".+grpsize=(.+?);.*").matcher("");
            final Matcher tmatcher = Pattern.compile(".+cpu time = (.+?)\\s.*").matcher("");
            final Matcher omatcher = Pattern.compile("invarproc \"adjacencies\"").matcher("");
            while ((line = out.readLine()) != null) {
                smatcher.reset(line);
                if (smatcher.matches()) {
                    allSymms = smatcher.group(1);
                } else {
                    tmatcher.reset(line);
                    if (tmatcher.matches()) {
                        time = (long) (Double.parseDouble(tmatcher.group(1)) * 1000);
                        if (time == 0)
                            time++;
                    } else {
                        omatcher.reset(line);
                        if (omatcher.find()) {
                            break;
                        }
                    }
                }
            }
            if (line != null) {
                final StringBuilder builder = new StringBuilder();
                while ((line = out.readLine()) != null) {
                    builder.append(line);
                }
                line = builder.toString();
                // System.out.println(line);
                final String[] orbits = line.split(";");
                final Matcher dmatcher = Pattern.compile("\\s*(\\d+)\\s*").matcher("");
                for (String n : orbits) {
                    String[] range = n.split(":");
                    if (range.length == 2) {
                        dmatcher.reset(range[0]);
                        dmatcher.matches();
                        final int min = Integer.parseInt(dmatcher.group(1));
                        if (min >= bounds.universe().size())
                            break;
                        dmatcher.reset(range[1]);
                        dmatcher.matches();
                        parts.add(Ints.rangeSet(Ints.range(min, Integer.parseInt(dmatcher.group(1)))));
                    } else {
                        range = n.split("\\s+");
                        final IntSet part = new IntTreeSet();
                        for (int i = 0; i < range.length; i++) {
                            dmatcher.reset(range[i]);
                            if (dmatcher.matches()) {
                                final int match = Integer.parseInt(dmatcher.group(1));
                                if (match < bounds.universe().size())
                                    part.add(match);
                                else
                                    break;
                            }
                        }
                        if (part.isEmpty())
                            break;
                        else
                            parts.add(part);
                    }
                }
            }
            out.close();
            tmp.delete();
            runner.destroyProcess();
            if (time < 0 || allSymms == null || parts.isEmpty())
                throw new IllegalStateException();
            return new SymmInfo(parts, String.valueOf(time + (endGen - startGen) / 1000000), allSymms);
        } catch (IOException e) {
            tmp.delete();
            runner.destroyProcess();
            destroy("dreadnaut");
            throw new IllegalStateException(e);
        } catch (InterruptedException e) {
            tmp.delete();
            runner.destroyProcess();
            destroy("dreadnaut");
            e.printStackTrace();
            throw new IllegalStateException(e);
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) IntSet(kodkod.util.ints.IntSet) IntTreeSet(kodkod.util.ints.IntTreeSet) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) ProcessRunner(tests.util.ProcessRunner) File(java.io.File)

Example 5 with IntTreeSet

use of kodkod.util.ints.IntTreeSet in project org.alloytools.alloy by AlloyTools.

the class SparseSequenceTest method testIntTreeSet.

public void testIntTreeSet() {
    IntTreeSet s = new IntTreeSet();
    // s.add(1);
    // System.out.println(s);
    // s.add(3);
    // System.out.println(s);
    // s.add(2);
    // System.out.println(s);
    s.add(331);
    s.add(221);
    for (int i = 42; i <= 45; i++) s.add(i);
    for (int i = 47; i <= 51; i++) s.add(i);
    s.add(167);
    s.add(168);
    s.add(220);
    System.out.println(s);
    s.add(46);
    System.out.println(s);
}
Also used : IntTreeSet(kodkod.util.ints.IntTreeSet)

Aggregations

IntTreeSet (kodkod.util.ints.IntTreeSet)7 IntSet (kodkod.util.ints.IntSet)5 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Formula (kodkod.ast.Formula)2 Node (kodkod.ast.Node)2 RecordFilter (kodkod.engine.fol2sat.RecordFilter)2 TranslationRecord (kodkod.engine.fol2sat.TranslationRecord)2 IntIterator (kodkod.util.ints.IntIterator)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Clause (kodkod.engine.satlab.Clause)1