Search in sources :

Example 6 with IntIterator

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

the class RCEStrategy 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 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);
            return relevantClauses;
        }
    }
    varsToTry.clear();
    return Ints.EMPTY_SET;
}
Also used : IntIterator(kodkod.util.ints.IntIterator) IntSet(kodkod.util.ints.IntSet)

Example 7 with IntIterator

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

the class StrategyUtils method clausesFor.

/**
 * Returns the indices of all axioms 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) } | C.*T
 */
static IntSet clausesFor(ResolutionTrace trace, IntSet relevantVars) {
    // System.out.println("relevant: " + relevantVars);
    final IntSet axioms = trace.axioms();
    final IntSet reachableVars = new IntBitSet(relevantVars.max() + 1);
    reachableVars.addAll(relevantVars);
    final IntSet relevantAxioms = new IntBitSet(axioms.size());
    final Iterator<Clause> itr = trace.reverseIterator(axioms);
    for (int i = axioms.max(); i >= 0; i--) {
        Clause clause = itr.next();
        int maxVar = clause.maxVariable();
        if (reachableVars.contains(maxVar)) {
            for (IntIterator lits = clause.literals(); lits.hasNext(); ) {
                reachableVars.add(StrictMath.abs(lits.next()));
            }
            relevantAxioms.add(i);
        }
    }
    return relevantAxioms;
}
Also used : IntBitSet(kodkod.util.ints.IntBitSet) IntIterator(kodkod.util.ints.IntIterator) IntSet(kodkod.util.ints.IntSet) Clause(kodkod.engine.satlab.Clause)

Example 8 with IntIterator

use of kodkod.util.ints.IntIterator 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 9 with IntIterator

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

the class Clause method toString.

/**
 * Returns a string representation of this clause.
 *
 * @return a string representation of this clause.
 */
@Override
public String toString() {
    final StringBuilder ret = new StringBuilder();
    if (numberOfAntecedents() == 0) {
        ret.append("AXIOM");
    } else {
        ret.append("RESOLVENT");
    }
    ret.append(". Literals: {");
    for (IntIterator iter = literals(); iter.hasNext(); ) {
        ret.append(" ");
        ret.append(iter.next());
    }
    ret.append(" }");
    return ret.toString();
}
Also used : IntIterator(kodkod.util.ints.IntIterator)

Example 10 with IntIterator

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

the class Clause method equals.

/**
 * // * Returns the antecedent at the given index. // * @requires 0 <= index <
 * this.numberOfAntecedents() // * @return this.antecedents[index] // * @throws
 * IndexOutOfBoundsException index < 0 || index >= this.numberOfAntecedents() //
 */
// public abstract Clause antecedent(int index);
/**
 * Returns true if o is a Clause whose literals and antecedents are
 * <tt>equal</tt> to those of this clause.
 *
 * @return o in Clause && o.literals.equals(this.literals) &&
 *         o.antecedents.equals(this.antecedents)
 */
@Override
public boolean equals(Object o) {
    if (o == this)
        return true;
    if (o instanceof Clause) {
        final Clause c = (Clause) o;
        if (size() == c.size()) {
            final IntIterator itr1 = literals(), itr2 = literals();
            while (itr1.hasNext()) {
                if (itr1.next() != itr2.next())
                    return false;
            }
        }
        final int ante = numberOfAntecedents();
        if (ante > 0 && ante == c.numberOfAntecedents()) {
            final Iterator<Clause> itr1 = antecedents(), itr2 = c.antecedents();
            while (itr1.hasNext()) {
                if (!itr1.next().equals(itr2.next()))
                    return false;
            }
        }
        return ante == 0;
    }
    return false;
}
Also used : IntIterator(kodkod.util.ints.IntIterator)

Aggregations

IntIterator (kodkod.util.ints.IntIterator)27 IntSet (kodkod.util.ints.IntSet)21 IntBitSet (kodkod.util.ints.IntBitSet)7 Relation (kodkod.ast.Relation)5 Clause (kodkod.engine.satlab.Clause)5 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BooleanFactory (kodkod.engine.bool.BooleanFactory)2 BooleanValue (kodkod.engine.bool.BooleanValue)2 TupleFactory (kodkod.instance.TupleFactory)2 TupleSet (kodkod.instance.TupleSet)2 IntTreeSet (kodkod.util.ints.IntTreeSet)2 Comparator (java.util.Comparator)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 BooleanAccumulator (kodkod.engine.bool.BooleanAccumulator)1 Int (kodkod.engine.bool.Int)1 SATSolver (kodkod.engine.satlab.SATSolver)1 Instance (kodkod.instance.Instance)1