use of kodkod.engine.satlab.SATSolver in project org.alloytools.alloy by AlloyTools.
the class Translation method interpret.
public Instance interpret(Bounds bounds) {
final SATSolver solver = cnf();
final Instance instance = new Instance(bounds.universe());
final TupleFactory f = bounds.universe().factory();
for (IndexedEntry<TupleSet> entry : bounds.intBounds()) {
instance.add(entry.index(), entry.value());
}
for (Relation r : bounds.relations()) {
// if (bnds != bounds && bnds.findRelByName(r.name()) == null)
// continue;
TupleSet lower = bounds.lowerBound(r);
IntSet indices = Ints.bestSet(lower.capacity());
indices.addAll(lower.indexView());
IntSet vars = primaryVariables(r);
if (!vars.isEmpty()) {
// System.out.println(r + ": [" + vars.min() + ", " + vars.max()
// + "]");
int lit = vars.min();
for (IntIterator iter = bounds.upperBound(r).indexView().iterator(); iter.hasNext(); ) {
final int index = iter.next();
if (!indices.contains(index) && solver.valueOf(lit++))
indices.add(index);
}
}
instance.add(r, f.setOf(r.arity(), indices));
}
return instance;
}
use of kodkod.engine.satlab.SATSolver in project org.alloytools.alloy by AlloyTools.
the class Solver method solveTranslation.
public static Solution solveTranslation(final long translTime, final Translation translation) {
final Solution solution;
if (translation.trivial()) {
final Statistics stats = new Statistics(translation, translTime, 0);
if (translation.cnf().solve()) {
solution = Solution.triviallySatisfiable(stats, translation.interpret());
} else {
solution = Solution.triviallyUnsatisfiable(stats, null);
}
} else {
final SATSolver cnf = translation.cnf();
translation.options().reporter().solvingCNF(translation.numPrimaryVariables(), cnf.numberOfVariables(), cnf.numberOfClauses());
final long startSolve = System.currentTimeMillis();
final boolean sat = cnf.solve();
final long endSolve = System.currentTimeMillis();
final Statistics stats = new Statistics(translation, translTime, endSolve - startSolve);
if (sat) {
solution = Solution.satisfiable(stats, translation.interpret());
} else {
solution = Solution.unsatisfiable(stats, null);
}
}
return solution;
}
use of kodkod.engine.satlab.SATSolver in project org.alloytools.alloy by AlloyTools.
the class IncrementalSolver method solve.
/**
* Adds the specified formula and bounds to the solver's state, modifies the
* current solution to reflect the updated state (if needed), and returns this
* solver. This solver should not be used again if a call to this method results
* in an exception.
*
* @requires this.{@link #usable() usable}()
* @requires f.*components & Relation in (this.bounds + b).relations
* @requires some this.bounds => this.bounds.universe = b.universe && no
* b.intBound && no (this.bounds.relations & b.relations)
* @requires some this.bounds => all s:
* {@link SymmetryDetector#partition(Bounds) partition}(this.bounds) |
* some p: {@link SymmetryDetector#partition(Bounds) partition}(b) |
* s.elements in p.elements
* @ensures this.formulas' = this.formulas + f
* @ensures some this.bounds => (this.bounds.relations' = this.bounds.relations
* + b.relations && this.bounds.upperBound' = this.bounds.upperBound +
* b.upperBound && this.bounds.lowerBound' = this.bounds.lowerBound +
* b.lowerBound) else (this.bounds' = bounds)
* @return some sol: Solution | sol.instance() = null => UNSAT(this.formulas',
* this.bounds', this.options) else sol.instance() in
* MODELS(Formula.and(this.formulas'), this.bounds', this.options)
* @throws IllegalStateException a prior call returned an UNSAT solution or
* resulted in an exception
* @throws NullPointerException any of the arguments are null
* @throws UnboundLeafException the formula refers to an undeclared variable or
* a relation not mapped by this.bounds + b
* @throws HigherOrderDeclException the formula contains a higher order
* declaration
* @throws IllegalArgumentException any of the remaining preconditions on
* {@code f} and {@code b} are violated
* @throws AbortedException this solving task has been aborted
*/
@Override
public Solution solve(Formula f, Bounds b) throws HigherOrderDeclException, UnboundLeafException, AbortedException {
if (outcome == Boolean.FALSE)
throw new IllegalStateException("Cannot use this solver since a prior call to solve(...) produced an UNSAT solution.");
if (outcome != null && translation == null)
throw new IllegalStateException("Cannot use this solver since a prior call to solve(...) resulted in an exception.");
final Solution solution;
try {
final long startTransl = System.currentTimeMillis();
translation = translation == null ? Translator.translateIncremental(f, b, options) : Translator.translateIncremental(f, b, translation);
final long endTransl = System.currentTimeMillis();
if (translation.trivial()) {
final Statistics stats = new Statistics(translation, endTransl - startTransl, 0);
if (translation.cnf().solve()) {
solution = Solution.triviallySatisfiable(stats, translation.interpret());
} else {
solution = Solution.triviallyUnsatisfiable(stats, null);
}
} else {
final SATSolver cnf = translation.cnf();
translation.options().reporter().solvingCNF(translation.numPrimaryVariables(), cnf.numberOfVariables(), cnf.numberOfClauses());
final long startSolve = System.currentTimeMillis();
final boolean sat = cnf.solve();
final long endSolve = System.currentTimeMillis();
final Statistics stats = new Statistics(translation, endTransl - startTransl, endSolve - startSolve);
if (sat) {
solution = Solution.satisfiable(stats, translation.interpret());
} else {
solution = Solution.unsatisfiable(stats, null);
}
}
} catch (SATAbortedException sae) {
free();
throw new AbortedException(sae);
} catch (RuntimeException e) {
free();
throw e;
}
if (solution.sat()) {
outcome = Boolean.TRUE;
} else {
outcome = Boolean.FALSE;
free();
}
return solution;
}
use of kodkod.engine.satlab.SATSolver in project org.alloytools.alloy by AlloyTools.
the class Solver method unsat.
/**
* Returns the result of solving an unsat formula.
*
* @param translation the translation
* @param stats translation / solving stats
* @return the result of solving an unsat formula.
*/
static Solution unsat(Translation.Whole translation, Statistics stats) {
final SATSolver cnf = translation.cnf();
final TranslationLog log = translation.log();
if (cnf instanceof SATProver && log != null) {
return Solution.unsatisfiable(stats, new ResolutionBasedProof((SATProver) cnf, log));
} else {
// can free memory
final Solution sol = Solution.unsatisfiable(stats, null);
cnf.free();
return sol;
}
}
use of kodkod.engine.satlab.SATSolver in project org.alloytools.alloy by AlloyTools.
the class Solver method solve.
/**
* Attempts to satisfy the given {@code formula} and {@code bounds} with respect
* to {@code this.options} or, optionally, prove the problem's unsatisfiability.
* If the method completes normally, the result is a {@linkplain Solution
* solution} containing either an {@linkplain Instance instance} of the given
* problem or, optionally, a {@linkplain Proof proof} of its unsatisfiability.
* An unsatisfiability proof will be constructed iff {@code this.options.solver}
* specifies a {@linkplain SATProver} and
* {@code this.options.logTranslation > 0}.
*
* @return some sol: {@link Solution} | some sol.instance() => sol.instance() in
* MODELS(formula, bounds, this.options) else UNSAT(formula, bound,
* this.options)
* @throws NullPointerException formula = null || bounds = null
* @throws UnboundLeafException the formula contains an undeclared variable or a
* relation not mapped by the given bounds
* @throws HigherOrderDeclException the formula contains a higher order
* declaration that cannot be skolemized, or it can be skolemized
* but {@code this.options.skolemDepth} is insufficiently large
* @throws AbortedException this solving task was aborted
* @see Options
* @see Solution
* @see Instance
* @see Proof
*/
@Override
public Solution solve(Formula formula, Bounds bounds) throws HigherOrderDeclException, UnboundLeafException, AbortedException {
final long startTransl = System.currentTimeMillis();
try {
final Translation.Whole translation = Translator.translate(formula, bounds, options);
final long endTransl = System.currentTimeMillis();
if (translation.trivial())
return trivial(translation, endTransl - startTransl);
final SATSolver cnf = translation.cnf();
options.reporter().solvingCNF(translation.numPrimaryVariables(), cnf.numberOfVariables(), cnf.numberOfClauses());
final long startSolve = System.currentTimeMillis();
final boolean isSat = cnf.solve();
final long endSolve = System.currentTimeMillis();
final Statistics stats = new Statistics(translation, endTransl - startTransl, endSolve - startSolve);
return isSat ? sat(translation, stats) : unsat(translation, stats);
} catch (SATAbortedException sae) {
throw new AbortedException(sae);
}
}
Aggregations