use of kodkod.engine.fol2sat.TranslationRecord 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.engine.fol2sat.TranslationRecord in project org.alloytools.alloy by AlloyTools.
the class StrategyUtils method rootVars.
/**
* Returns the variables that correspond to the roots of log.formula.
*
* @return
*
* <pre>
*
* { v: int | some r: log.records |
* r.node in log.roots() 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>
*/
public static IntSet rootVars(TranslationLog log) {
final IntSet rootVars = new IntTreeSet();
final Set<Formula> roots = log.roots();
final Map<Formula, int[]> maxRootVar = new LinkedHashMap<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 (int[] var : maxRootVar.values()) {
int topVar = var[0];
if (// formula simplified to TRUE
topVar != Integer.MAX_VALUE)
rootVars.add(var[0]);
}
return rootVars;
}
use of kodkod.engine.fol2sat.TranslationRecord in project org.alloytools.alloy by AlloyTools.
the class A4Solution method solve.
// ===================================================================================================//
/**
* Solve for the solution if not solved already; if cmd==null, we will simply
* use the lowerbound of each relation as its value.
*/
A4Solution solve(final A4Reporter rep, Command cmd, Simplifier simp, boolean tryBookExamples) throws Err, IOException {
// If already solved, then return this object as is
if (solved)
return this;
// the lower bound of each relation
if (cmd == null) {
Instance inst = new Instance(bounds.universe());
for (int max = max(), i = min(); i <= max; i++) {
Tuple it = factory.tuple("" + i);
inst.add(i, factory.range(it, it));
}
for (Relation r : bounds.relations()) inst.add(r, bounds.lowerBound(r));
eval = new Evaluator(inst, solver.options());
rename(this, null, null, new UniqueNameGenerator());
solved();
return this;
}
// Otherwise, prepare to do the solve...
final A4Options opt = originalOptions;
long time = System.currentTimeMillis();
rep.debug("Simplifying the bounds...\n");
if (opt.inferPartialInstance && simp != null && formulas.size() > 0 && !simp.simplify(rep, this, formulas))
addFormula(Formula.FALSE, Pos.UNKNOWN);
rep.translate(opt.solver.id(), bitwidth, maxseq, solver.options().skolemDepth(), solver.options().symmetryBreaking());
Formula fgoal = Formula.and(formulas);
rep.debug("Generating the solution...\n");
kEnumerator = null;
Solution sol = null;
final Reporter oldReporter = solver.options().reporter();
final boolean[] solved = new boolean[] { true };
solver.options().setReporter(new // Set up a
AbstractReporter() {
// reporter to
// catch the
// type+pos of
// skolems
@Override
public void skolemizing(Decl decl, Relation skolem, List<Decl> predecl) {
try {
Type t = kv2typepos(decl.variable()).a;
if (t == Type.EMPTY)
return;
for (int i = (predecl == null ? -1 : predecl.size() - 1); i >= 0; i--) {
Type pp = kv2typepos(predecl.get(i).variable()).a;
if (pp == Type.EMPTY)
return;
t = pp.product(t);
}
kr2type(skolem, t);
}// Exception here is not fatal
catch (Throwable ex) {
}
}
@Override
public void solvingCNF(int primaryVars, int vars, int clauses) {
if (solved[0])
return;
else
// initially solved[0] is true, so we
solved[0] = true;
// won't report the # of vars/clauses
if (rep != null)
rep.solve(primaryVars, vars, clauses);
}
});
if (!opt.solver.equals(SatSolver.CNF) && !opt.solver.equals(SatSolver.KK) && tryBookExamples) {
// try
// book
// examples
A4Reporter r = AlloyCore.isDebug() ? rep : null;
try {
sol = BookExamples.trial(r, this, fgoal, solver, cmd.check);
} catch (Throwable ex) {
sol = null;
}
}
// this allows the reporter to report the # of
solved[0] = false;
// vars/clauses
for (Relation r : bounds.relations()) {
formulas.add(r.eq(r));
}
// Without this, kodkod refuses to grow unmentioned relations
fgoal = Formula.and(formulas);
// Now pick the solver and solve it!
if (opt.solver.equals(SatSolver.KK)) {
File tmpCNF = File.createTempFile("tmp", ".java", new File(opt.tempDirectory));
String out = tmpCNF.getAbsolutePath();
Util.writeAll(out, debugExtractKInput());
rep.resultCNF(out);
return null;
}
if (opt.solver.equals(SatSolver.CNF)) {
File tmpCNF = File.createTempFile("tmp", ".cnf", new File(opt.tempDirectory));
String out = tmpCNF.getAbsolutePath();
solver.options().setSolver(WriteCNF.factory(out));
try {
sol = solver.solve(fgoal, bounds);
} catch (WriteCNF.WriteCNFCompleted ex) {
rep.resultCNF(out);
return null;
}
// The formula is trivial (otherwise, it would have thrown an
// exception)
// Since the user wants it in CNF format, we manually generate a
// trivially satisfiable (or unsatisfiable) CNF file.
Util.writeAll(out, sol.instance() != null ? "p cnf 1 1\n1 0\n" : "p cnf 1 2\n1 0\n-1 0\n");
rep.resultCNF(out);
return null;
}
if (!solver.options().solver().incremental()) /*
* || solver.options().solver()==SATFactory. ZChaffMincost
*/
{
if (sol == null)
sol = solver.solve(fgoal, bounds);
} else {
kEnumerator = new Peeker<Solution>(solver.solveAll(fgoal, bounds));
if (sol == null)
sol = kEnumerator.next();
}
if (!solved[0])
rep.solve(0, 0, 0);
final Instance inst = sol.instance();
// To ensure no more output during SolutionEnumeration
solver.options().setReporter(oldReporter);
// If unsatisfiable, then retreive the unsat core if desired
if (inst == null && solver.options().solver() == SATFactory.MiniSatProver) {
try {
lCore = new LinkedHashSet<Node>();
Proof p = sol.proof();
if (sol.outcome() == UNSATISFIABLE) {
// only perform the minimization if it was UNSATISFIABLE,
// rather than TRIVIALLY_UNSATISFIABLE
int i = p.highLevelCore().size();
rep.minimizing(cmd, i);
if (opt.coreMinimization == 0)
try {
p.minimize(new RCEStrategy(p.log()));
} catch (Throwable ex) {
}
if (opt.coreMinimization == 1)
try {
p.minimize(new HybridStrategy(p.log()));
} catch (Throwable ex) {
}
rep.minimized(cmd, i, p.highLevelCore().size());
}
for (Iterator<TranslationRecord> it = p.core(); it.hasNext(); ) {
Object n = it.next().node();
if (n instanceof Formula)
lCore.add((Formula) n);
}
Map<Formula, Node> map = p.highLevelCore();
hCore = new LinkedHashSet<Node>(map.keySet());
hCore.addAll(map.values());
} catch (Throwable ex) {
lCore = hCore = null;
}
}
// If satisfiable, then add/rename the atoms and skolems
if (inst != null) {
eval = new Evaluator(inst, solver.options());
rename(this, null, null, new UniqueNameGenerator());
}
// report the result
solved();
time = System.currentTimeMillis() - time;
if (inst != null)
rep.resultSAT(cmd, time, this);
else
rep.resultUNSAT(cmd, time, this);
return this;
}
use of kodkod.engine.fol2sat.TranslationRecord in project org.alloytools.alloy by AlloyTools.
the class ResolutionBasedProof method highLevelCore.
/**
* {@inheritDoc}
*
* @see kodkod.engine.Proof#highLevelCore()
*/
@Override
public final Map<Formula, Node> highLevelCore() {
if (coreRoots == null) {
final RecordFilter unitFilter = new RecordFilter() {
final IntSet coreUnits = StrategyUtils.coreUnits(solver.proof());
final Set<Formula> roots = log().roots();
@Override
public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
return roots.contains(translated) && coreUnits.contains(Math.abs(literal));
}
};
coreRoots = new LinkedHashMap<Formula, Node>();
final IntSet seenUnits = new IntTreeSet();
for (Iterator<TranslationRecord> itr = log().replay(unitFilter); itr.hasNext(); ) {
// it is possible that two top-level formulas have identical
// meaning,
// and are represented with the same core unit; in that case, we
// want only
// one of them in the core.
final TranslationRecord rec = itr.next();
if (seenUnits.add(rec.literal())) {
coreRoots.put(rec.translated(), rec.node());
}
}
coreRoots = Collections.unmodifiableMap(coreRoots);
}
return coreRoots;
}
use of kodkod.engine.fol2sat.TranslationRecord in project org.alloytools.alloy by AlloyTools.
the class ResolutionBasedProof method connectedCore.
/**
* Returns the connected core based on the given set of core variables.
*
* @requires coreVar = StrategyUtils.coreVars(solver.proof());
* @return let formulas = (this.log.records[int] & literal.{i: int | abs(i) in
* coreVars}).formula | connected = {f: formulas | some s: set coreNodes
* | f + this.log.formula in s and (s - this.log.formula).~components in
* s }
*/
private Set<Formula> connectedCore(final IntSet coreVars) {
final Set<Formula> coreNodes = new IdentityHashSet<Formula>();
final RecordFilter filter = new RecordFilter() {
@Override
public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
return coreVars.contains(StrictMath.abs(literal));
}
};
for (Iterator<TranslationRecord> itr = log().replay(filter); itr.hasNext(); ) {
coreNodes.add(itr.next().translated());
}
final Set<Formula> connected = new IdentityHashSet<Formula>();
final AbstractVoidVisitor traverser = new AbstractVoidVisitor() {
final Set<Node> visited = new IdentityHashSet<Node>();
/**
* Returns true if the given node has been visited before or if it is not
* contained in this.nodes set. Otherwise adds the node to the connected set and
* returns false.
*
* @ensures this.visited' = this.visited + n
* @ensures n !in this.visited && n in coreNodes => connected' = connected + n
* else connected' = connected
* @return n in visited || n !in coreNodes
*/
@Override
protected boolean visited(Node n) {
if (visited.add(n) && coreNodes.contains(n)) {
connected.add((Formula) n);
return false;
}
return true;
}
};
for (Formula root : log().roots()) {
root.accept(traverser);
}
return connected;
}
Aggregations