use of kodkod.util.ints.IntIterator in project org.alloytools.alloy by AlloyTools.
the class SCEStrategy 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 corresponding to the clauses that
// form the translations of formulas identified by relevant vars
final IntSet relevantClauses = StrategyUtils.clausesFor(trace, relevantVars);
assert !relevantClauses.isEmpty() && !relevantClauses.contains(trace.size() - 1);
return relevantClauses;
}
}
varsToTry.clear();
return Ints.EMPTY_SET;
}
use of kodkod.util.ints.IntIterator in project org.alloytools.alloy by AlloyTools.
the class MiniSatProver method formatTrivial.
/**
* Returns a subset of the given trivial trace that consists only of axioms. A
* trace is trivial iff its last clause is an empty clause. This means that the
* empty axiom was added to the solver via {@linkplain #addClause(int[])}, so no
* resolution was necessary to reach the conflict. The empty axiom is always the
* last clause in the trace.
*
* @requires trace[trace.length].length = 0
* @return a subset of the given trivial trace that consists only of axioms
*/
private int[][] formatTrivial(int[][] trace) {
final int length = trace.length;
final int empty = length - 1;
final IntSet axioms = new IntBitSet(length);
axioms.add(empty);
final int numVars = numberOfVariables();
for (int i = 0; i < empty; i++) {
int[] clause = trace[i];
if (clause[0] <= numVars) {
axioms.add(i);
}
}
if (axioms.size() == length) {
return trace;
}
final int[][] axiomClauses = new int[axioms.size()][];
final IntIterator itr = axioms.iterator();
for (int i = 0; itr.hasNext(); i++) {
axiomClauses[i] = trace[itr.next()];
}
return axiomClauses;
}
use of kodkod.util.ints.IntIterator in project org.alloytools.alloy by AlloyTools.
the class SymmetryDetector method refinePartitions.
/**
* Refines the atomic partitions in this.parts based on the contents of the
* given tupleset, decomposed into its constituent IntSet and arity. The
* range2domain map is used for intermediate computations for efficiency (to
* avoid allocating it in each recursive call).
*
* @requires all disj s, q: this.parts[int] | some s.ints && some q.ints && (no
* s.ints & q.ints) && this.parts[int].ints =
* [0..this.bounds.universe.size())
* @ensures let usize = this.bounds.universe.size(), firstColFactor =
* usize^(arit-1) | all disj s, q: this.parts'[int] | some s.ints &&
* some q.ints && (no s.ints & q.ints) && this.parts'[int].ints =
* [0..usize) && all s: this.parts'[int] | all a1, a2:
* this.bounds.universe.atoms[s.ints] | all t1, t2: set.ints | t1 /
* firstColFactor = a1 && t2 / firstColFactor = a2 => t1 %
* firstColFactor = t2 % firstColFactor || t1 = a1*((1 -
* firstColFactor) / (1 - usize)) && t2 = a2*((1 - firstColFactor) / (1
* - usize)))
*/
private void refinePartitions(IntSet set, int arity, Map<IntSet, IntSet> range2domain) {
if (arity == 1) {
refinePartitions(set);
return;
}
final List<IntSet> otherColumns = new LinkedList<IntSet>();
int firstColFactor = (int) StrictMath.pow(usize, arity - 1);
IntSet firstCol = Ints.bestSet(usize);
for (IntIterator rbIter = set.iterator(); rbIter.hasNext(); ) {
firstCol.add(rbIter.next() / firstColFactor);
}
refinePartitions(firstCol);
int idenFactor = (1 - firstColFactor) / (1 - usize);
for (ListIterator<IntSet> partsIter = parts.listIterator(); partsIter.hasNext(); ) {
IntSet part = partsIter.next();
if (firstCol.contains(part.min())) {
// contains one, contains them
// all
range2domain.clear();
for (IntIterator atoms = part.iterator(); atoms.hasNext(); ) {
int atom = atoms.next();
IntSet atomRange = Ints.bestSet(firstColFactor);
for (IntIterator rbIter = set.iterator(atom * firstColFactor, (atom + 1) * firstColFactor - 1); rbIter.hasNext(); ) {
atomRange.add(rbIter.next() % firstColFactor);
}
IntSet atomDomain = range2domain.get(atomRange);
if (atomDomain != null)
atomDomain.add(atom);
else
range2domain.put(atomRange, oneOf(usize, atom));
}
partsIter.remove();
IntSet idenPartition = Ints.bestSet(usize);
for (Map.Entry<IntSet, IntSet> entry : range2domain.entrySet()) {
if (entry.getValue().size() == 1 && entry.getKey().size() == 1 && entry.getKey().min() == entry.getValue().min() * idenFactor) {
idenPartition.add(entry.getValue().min());
} else {
partsIter.add(entry.getValue());
otherColumns.add(entry.getKey());
}
}
if (!idenPartition.isEmpty())
partsIter.add(idenPartition);
}
}
// refine based on the remaining columns
for (IntSet otherCol : otherColumns) {
refinePartitions(otherCol, arity - 1, range2domain);
}
}
use of kodkod.util.ints.IntIterator in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method visit.
/**
* Calls lookup(castExpr) and returns the cached value, if any. If a translation
* has not been cached, translates the expression, calls cache(...) on it and
* returns it.
*
* @return let t = lookup(castExpr) | some t => t, cache(castExpr,
* translate(castExpr))
*/
@Override
public BooleanMatrix visit(IntToExprCast castExpr) {
BooleanMatrix ret = lookup(castExpr);
if (ret != null)
return ret;
final Int child = castExpr.intExpr().accept(this);
final BooleanFactory factory = interpreter.factory();
final IntSet ints = interpreter.ints();
ret = factory.matrix(Dimensions.square(interpreter.universe().size(), 1));
switch(castExpr.op()) {
case INTCAST:
for (IntIterator iter = ints.iterator(); iter.hasNext(); ) {
int i = iter.next();
int atomIndex = interpreter.interpret(i);
ret.set(atomIndex, factory.or(ret.get(atomIndex), child.eq(factory.integer(i))));
}
ret.setDefCond(child.defCond());
break;
case BITSETCAST:
final List<BooleanValue> twosComplement = child.twosComplementBits();
final int msb = twosComplement.size() - 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(interpreter.interpret(pow2), twosComplement.get(i));
}
}
// handle the sign bit
if (ints.contains(-1 << msb)) {
ret.set(interpreter.interpret(-1 << msb), twosComplement.get(msb));
}
break;
default:
throw new IllegalArgumentException("Unknown cast operator: " + castExpr.op());
}
return cache(castExpr, ret);
}
use of kodkod.util.ints.IntIterator in project org.alloytools.alloy by AlloyTools.
the class LeafInterpreter method interpret.
/**
* Returns a {@link kodkod.engine.bool.BooleanMatrix matrix} m of
* {@link kodkod.engine.bool.BooleanValue boolean formulas} representing the
* specified relation.
*
* @requires r in this.relations
* @return { m: BooleanMatrix | let lset =
* (this.rBounds[r].TupleSet).tuples.index, hset =
* (this.rBounds[r][TupleSet]).tuples.index, dset =
* [0..this.universe.size()^r.arity) | m.dimensions.dimensions =
* [0..r.arity) ->one this.universe.size() && m.elements[lset] = TRUE &&
* m.elements[dset-hset] = FALSE && all disj i, j: hset-lset |
* m.elements[i]+m.elements[j] in this.vars[r] && m.elements[i].label <
* m.elements[j].label <=> i < j }
* @throws UnboundLeafException r !in this.relations
*/
public final BooleanMatrix interpret(Relation r) {
if (!lowers.containsKey(r))
throw new UnboundLeafException("Unbound relation: ", r);
final IntSet lowerBound = lowers.get(r).indexView();
final IntSet upperBound = uppers.get(r).indexView();
final BooleanMatrix m = factory.matrix(Dimensions.square(universe().size(), r.arity()), upperBound, lowerBound);
if (upperBound.size() > lowerBound.size()) {
int varId = vars.get(r).min();
for (IntIterator indeces = upperBound.iterator(); indeces.hasNext(); ) {
int tupleIndex = indeces.next();
if (!lowerBound.contains(tupleIndex))
m.set(tupleIndex, factory.variable(varId++));
}
}
return m;
}
Aggregations