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;
}
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;
}
Aggregations