Search in sources :

Example 31 with IntSet

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

the class FOL2BoolTranslator method approximate.

/**
 * Translates the given annotated expression into a boolean matrix that is a
 * least sound upper bound on the expression's value, given the leaf and
 * variable bindings in the the provided interpreter and environment.
 *
 * @requires interpreter.relations = AnnotatedNode.relations(annotated)
 * @return a boolean matrix that is a least sound upper bound on the
 *         expression's value
 * @throws HigherOrderDeclException annotated.node contains a higher order
 *             declaration
 * @throws UnboundLeafException annotated.node refers to a variable that neither
 *             declared nor bound in env
 */
static final BooleanMatrix approximate(AnnotatedNode<Expression> annotated, LeafInterpreter interpreter, Environment<BooleanMatrix, Expression> env) {
    final FOL2BoolTranslator approximator = new FOL2BoolTranslator(new FOL2BoolCache(annotated), interpreter, env) {

        @Override
        public final BooleanMatrix visit(BinaryExpression binExpr) {
            final BooleanMatrix ret = lookup(binExpr);
            if (ret != null)
                return ret;
            switch(binExpr.op()) {
                case DIFFERENCE:
                    return cache(binExpr, binExpr.left().accept(this));
                case OVERRIDE:
                    return cache(binExpr, binExpr.left().accept(this).or(binExpr.right().accept(this)));
                default:
                    return super.visit(binExpr);
            }
        }

        @Override
        public final BooleanMatrix visit(Comprehension cexpr) {
            final BooleanMatrix ret = lookup(cexpr);
            return ret != null ? ret : cache(cexpr, super.visit((Comprehension) Formula.TRUE.comprehension(cexpr.decls())));
        }

        @Override
        public BooleanMatrix visit(IfExpression ifExpr) {
            final BooleanMatrix ret = lookup(ifExpr);
            return ret != null ? ret : cache(ifExpr, ifExpr.thenExpr().accept(this).or(ifExpr.elseExpr().accept(this)));
        }

        @Override
        public BooleanMatrix visit(IntToExprCast castExpr) {
            BooleanMatrix ret = lookup(castExpr);
            if (ret != null)
                return ret;
            switch(castExpr.op()) {
                case INTCAST:
                    return cache(castExpr, Expression.INTS.accept(this));
                case BITSETCAST:
                    final BooleanFactory factory = super.interpreter.factory();
                    ret = factory.matrix(Dimensions.square(super.interpreter.universe().size(), 1));
                    final IntSet ints = super.interpreter.ints();
                    final int msb = factory.bitwidth() - 1;
                    // handle all bits but the sign bit
                    for (int i = 0; i < msb; i++) {
                        int pow2 = 1 << i;
                        if (ints.contains(pow2)) {
                            ret.set(super.interpreter.interpret(pow2), BooleanConstant.TRUE);
                        }
                    }
                    // handle the sign bit
                    if (ints.contains(-1 << msb)) {
                        ret.set(super.interpreter.interpret(-1 << msb), BooleanConstant.TRUE);
                    }
                    return cache(castExpr, ret);
                default:
                    throw new IllegalArgumentException("Unknown operator: " + castExpr.op());
            }
        }
    };
    return annotated.node().accept(approximator);
}
Also used : IfExpression(kodkod.ast.IfExpression) BinaryExpression(kodkod.ast.BinaryExpression) IntToExprCast(kodkod.ast.IntToExprCast) IntSet(kodkod.util.ints.IntSet) BooleanMatrix(kodkod.engine.bool.BooleanMatrix) Comprehension(kodkod.ast.Comprehension) BooleanFactory(kodkod.engine.bool.BooleanFactory)

Example 32 with IntSet

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

the class AdaptiveRCEStrategy method next.

/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.satlab.ReductionStrategy#next(kodkod.engine.satlab.ResolutionTrace)
 */
@Override
public IntSet next(ResolutionTrace trace) {
    if (varsToTry.isEmpty())
        // tried everything
        return Ints.EMPTY_SET;
    final IntSet relevantVars = StrategyUtils.coreTailUnits(trace);
    for (IntIterator varItr = varsToTry.iterator(); varItr.hasNext(); ) {
        final int var = varItr.next();
        varItr.remove();
        if (relevantVars.remove(var)) {
            // relevant variables
            if (relevantVars.isEmpty())
                // there was only one root formula left
                break;
            // get all axioms and resolvents corresponding to the clauses
            // that
            // form the translations of formulas identified by relevant vars
            final IntSet relevantClauses = clausesFor(trace, relevantVars);
            assert !relevantClauses.isEmpty() && !relevantClauses.contains(trace.size() - 1);
            if (DBG)
                System.out.println("relevant clauses: " + relevantClauses.size() + ", removed " + var);
            return relevantClauses;
        }
    }
    varsToTry.clear();
    return Ints.EMPTY_SET;
}
Also used : IntIterator(kodkod.util.ints.IntIterator) IntSet(kodkod.util.ints.IntSet)

Example 33 with IntSet

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

the class AdaptiveRCEStrategy method clausesFor.

/**
 * Returns the indices of all axioms and resolvents in the given trace that form
 * the translations of the formulas identified by the given variables. This
 * method assumes that the axioms in the given trace were generated by the
 * Kodkod {@linkplain Translator}.
 *
 * @return let C = { c: trace.prover.clauses | c.maxVariable() in relevantVars
 *         }, T = { c1, c2: C | c2.maxVariable() in abs(c1.literals) }, A = C.*T
 *         | trace.backwardReachable(A) - trace.backwardReachable(trace.axioms()
 *         - A)
 */
private IntSet clausesFor(ResolutionTrace trace, IntSet relevantVars) {
    final double hardness = (double) trace.size() / (double) trace.axioms().size();
    final double coreRatio = ((double) trace.core().size() / (double) trace.axioms().size());
    if (DBG)
        System.out.println("\ntrace size: " + trace.size() + ", axioms: " + trace.axioms().size() + ", core: " + trace.core().size() + ", resolvents: " + trace.resolvents().size());
    if (DBG)
        System.out.println("hardness: " + hardness + ", coreRatio: " + coreRatio);
    final IntSet relevantAxioms = StrategyUtils.clausesFor(trace, relevantVars);
    if (DBG)
        System.out.println("relevant axioms:  " + relevantAxioms.size());
    if (coreRatio < noRecycleRatio) {
        return relevantAxioms;
    } else if (hardness < hardnessCutOff) {
        return trace.learnable(relevantAxioms);
    } else {
        IntSet current = relevantAxioms, last;
        final int maxRelevant = (int) Math.rint(relevantAxioms.size() * recycleLimit);
        do {
            last = current;
            current = trace.directlyLearnable(current);
        } while (last.size() < current.size() && current.size() < maxRelevant);
        if (DBG)
            System.out.println("last: " + last.size() + ", current: " + current.size() + ", maxRelevant: " + maxRelevant);
        return current.size() < maxRelevant ? current : last;
    }
}
Also used : IntSet(kodkod.util.ints.IntSet)

Example 34 with IntSet

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

the class DynamicRCEStrategy method clausesFor.

/**
 * Returns the indices of all axioms and resolvents in the given trace that form
 * the translations of the formulas identified by the given variables. This
 * method assumes that the axioms in the given trace were generated by the
 * Kodkod {@linkplain Translator}.
 *
 * @return let C = { c: trace.prover.clauses | c.maxVariable() in relevantVars
 *         }, T = { c1, c2: C | c2.maxVariable() in abs(c1.literals) }, A = C.*T
 *         | trace.backwardReachable(A) - trace.backwardReachable(trace.axioms()
 *         - A)
 */
private IntSet clausesFor(ResolutionTrace trace, IntSet relevantVars) {
    final double hardness = (double) trace.size() / (double) trace.axioms().size();
    final double coreRatio = ((double) trace.core().size() / (double) trace.axioms().size());
    if (DBG)
        System.out.println("trace size: " + trace.size() + ", axioms: " + trace.axioms().size() + ", core: " + trace.core().size() + ", resolvents: " + trace.resolvents().size());
    if (DBG)
        System.out.println("hardness: " + hardness + ", coreRatio: " + coreRatio);
    final IntSet relevantAxioms = StrategyUtils.clausesFor(trace, relevantVars);
    if (DBG)
        System.out.println("relevant axioms:  " + relevantAxioms.size());
    if (coreRatio < noRecycleRatio) {
        return relevantAxioms;
    } else if (hardness < hardnessCutOff) {
        return trace.learnable(relevantAxioms);
    } else {
        IntSet current = relevantAxioms, last;
        final int maxRelevant = (int) Math.rint(relevantAxioms.size() * recycleLimit);
        do {
            last = current;
            current = trace.directlyLearnable(current);
        } while (last.size() < current.size() && current.size() < maxRelevant);
        if (DBG)
            System.out.println("last: " + last.size() + ", current: " + current.size() + ", maxRelevant: " + maxRelevant);
        return current.size() < maxRelevant ? current : last;
    }
}
Also used : IntSet(kodkod.util.ints.IntSet)

Example 35 with IntSet

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

the class DynamicRCEStrategy method sortByRelevance.

/**
 * Returns an array R of longs such that for each i, j in [0..R.length) i < j
 * implies that the formula identified by (int)R[i] in this.hits contributes
 * fewer clauses to the core of the given trace than the formula identified by
 * (int)R[j].
 *
 * @return an array as described above
 */
private long[] sortByRelevance(ResolutionTrace trace, IntSet relevantVars) {
    hits.indices().retainAll(relevantVars);
    if (hits.get(hits.indices().min()) == null) {
        // the hits
        for (IntIterator varItr = relevantVars.iterator(); varItr.hasNext(); ) {
            final int var = varItr.next();
            final IntSet varReachable = new IntBitSet(var + 1);
            varReachable.add(var);
            hits.put(var, varReachable);
        }
        for (Iterator<Clause> clauseItr = trace.reverseIterator(trace.axioms()); clauseItr.hasNext(); ) {
            final Clause clause = clauseItr.next();
            final int maxVar = clause.maxVariable();
            for (IntSet reachableVars : hits.values()) {
                if (reachableVars.contains(maxVar)) {
                    for (IntIterator lits = clause.literals(); lits.hasNext(); ) {
                        reachableVars.add(StrictMath.abs(lits.next()));
                    }
                }
            }
        }
    }
    final long[] counts = new long[hits.size()];
    for (Iterator<Clause> clauseItr = trace.iterator(trace.core()); clauseItr.hasNext(); ) {
        final Clause clause = clauseItr.next();
        final int maxVar = clause.maxVariable();
        int i = 0;
        for (IntSet reachableVars : hits.values()) {
            if (reachableVars.contains(maxVar)) {
                counts[i]++;
            }
            i++;
        }
    }
    int i = 0;
    for (IntIterator varItr = hits.indices().iterator(); varItr.hasNext(); ) {
        final int var = varItr.next();
        counts[i] = (counts[i] << 32) | var;
        i++;
    }
    Arrays.sort(counts);
    return counts;
}
Also used : IntBitSet(kodkod.util.ints.IntBitSet) IntIterator(kodkod.util.ints.IntIterator) IntSet(kodkod.util.ints.IntSet) Clause(kodkod.engine.satlab.Clause)

Aggregations

IntSet (kodkod.util.ints.IntSet)45 IntIterator (kodkod.util.ints.IntIterator)21 IntBitSet (kodkod.util.ints.IntBitSet)9 IntTreeSet (kodkod.util.ints.IntTreeSet)8 Relation (kodkod.ast.Relation)7 Clause (kodkod.engine.satlab.Clause)6 TupleSet (kodkod.instance.TupleSet)6 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)5 Map (java.util.Map)4 Set (java.util.Set)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Formula (kodkod.ast.Formula)3 Node (kodkod.ast.Node)3 BooleanFactory (kodkod.engine.bool.BooleanFactory)3 RecordFilter (kodkod.engine.fol2sat.RecordFilter)3 TranslationRecord (kodkod.engine.fol2sat.TranslationRecord)3 Bounds (kodkod.instance.Bounds)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2