Search in sources :

Example 1 with TreeSequence

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

the class StrategyUtils method roots.

/**
 * Returns a map from variables to the corresponding roots of log.formula.
 *
 * @return
 *
 *         <pre>
 *
 * { v: int, f: Formula | some r: log.records |
 *   r.translated in log.roots() and
 *   r.translated = f 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>
 */
static SparseSequence<Formula> roots(TranslationLog log) {
    final SparseSequence<Formula> rootVars = new TreeSequence<Formula>();
    final Set<Formula> roots = log.roots();
    final Map<Formula, int[]> maxRootVar = new IdentityHashMap<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 (Map.Entry<Formula, int[]> entry : maxRootVar.entrySet()) {
        final int topVar = entry.getValue()[0];
        if (// formula simplified to TRUE
        topVar != Integer.MAX_VALUE)
            rootVars.put(topVar, entry.getKey());
    }
    return rootVars;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) Formula(kodkod.ast.Formula) TreeSequence(kodkod.util.ints.TreeSequence) IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

Example 2 with TreeSequence

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

the class TrivialProof method minimize.

/**
 * Minimizes the current core using the trivial strategy that does one of the
 * following: (1) if there is a root that simplified to FALSE, sets the minimal
 * core to that root; or (2) if not, there must be two roots that translated to
 * x and -x, where x is a boolean literal, so we pick those two as the minimal
 * core. The strategy argument is ignored (it can be null).
 *
 * @see Proof#minimize(ReductionStrategy)
 */
@Override
public void minimize(ReductionStrategy strategy) {
    final Map<Formula, int[]> rootLits = new LinkedHashMap<Formula, int[]>();
    final Map<Formula, Node> rootNodes = new LinkedHashMap<Formula, Node>();
    final Set<Formula> roots = log().roots();
    for (Iterator<TranslationRecord> itr = core(); itr.hasNext(); ) {
        final TranslationRecord rec = itr.next();
        if (roots.contains(rec.translated())) {
            // simply record the most recent output value for each formula:
            // this is guaranteed to be the final output value for that
            // formula because of the translation log guarantee that the
            // log is replayed in the order of translation: i.e. a child's
            // output value is always recorded before the parent's
            int[] val = rootLits.get(rec.translated());
            if (val == null) {
                val = new int[1];
                rootLits.put(rec.translated(), val);
            }
            val[0] = rec.literal();
            rootNodes.put(rec.translated(), rec.node());
        }
    }
    final SparseSequence<Formula> lits = new TreeSequence<Formula>();
    for (Map.Entry<Formula, int[]> entry : rootLits.entrySet()) {
        final int lit = entry.getValue()[0];
        if (lit == -Integer.MAX_VALUE) {
            coreRoots = Collections.singletonMap(entry.getKey(), rootNodes.get(entry.getKey()));
            break;
        } else if (lits.containsIndex(-lit)) {
            final Formula f0 = lits.get(-lit);
            final Formula f1 = entry.getKey();
            coreRoots = new LinkedHashMap<Formula, Node>(3);
            coreRoots.put(f0, rootNodes.get(f0));
            coreRoots.put(f1, rootNodes.get(f1));
            coreRoots = Collections.unmodifiableMap(coreRoots);
            break;
        } else {
            lits.put(lit, entry.getKey());
        }
    }
    coreFilter = null;
    assert coreRoots.size() == 1 && rootLits.get(coreRoots.keySet().iterator().next())[0] == -Integer.MAX_VALUE || coreRoots.size() == 2;
}
Also used : Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) LinkedHashMap(java.util.LinkedHashMap) BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) ConstantFormula(kodkod.ast.ConstantFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) TreeSequence(kodkod.util.ints.TreeSequence) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Formula (kodkod.ast.Formula)2 Node (kodkod.ast.Node)2 TranslationRecord (kodkod.engine.fol2sat.TranslationRecord)2 TreeSequence (kodkod.util.ints.TreeSequence)2 IdentityHashMap (java.util.IdentityHashMap)1 BinaryFormula (kodkod.ast.BinaryFormula)1 ComparisonFormula (kodkod.ast.ComparisonFormula)1 ConstantFormula (kodkod.ast.ConstantFormula)1 IntComparisonFormula (kodkod.ast.IntComparisonFormula)1 MultiplicityFormula (kodkod.ast.MultiplicityFormula)1 NaryFormula (kodkod.ast.NaryFormula)1 NotFormula (kodkod.ast.NotFormula)1 QuantifiedFormula (kodkod.ast.QuantifiedFormula)1 RecordFilter (kodkod.engine.fol2sat.RecordFilter)1