use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class SATTest method testAddWithoutUnknown.
@Test
public void testAddWithoutUnknown() throws ParserException {
final PropositionalParser parser = new PropositionalParser(this.f);
final Formula phi = parser.parse("x1 & (~x2 | x3) & (x4 | ~x5)");
final SortedSet<Variable> phiVars = new TreeSet<>(Arrays.asList(this.f.variable("x1"), this.f.variable("x2"), this.f.variable("x3"), this.f.variable("x4"), this.f.variable("x5")));
final Formula add1 = parser.parse("x1 | x6 | x7");
final Formula add2 = parser.parse("~x1 | ~x6 | x8");
final Formula add3 = parser.parse("x2 & ~x3 | x7");
final Formula add4 = parser.parse("x8 | x9");
final SATSolver minisat = MiniSat.miniSat(this.f);
final SATSolver minicard = MiniSat.miniCard(this.f);
final SATSolver[] solvers = new SATSolver[] { minisat, minicard };
for (final SATSolver solver : solvers) {
solver.add(phi);
solver.addWithoutUnknown(add1);
assertThat(solver.sat()).isEqualTo(Tristate.TRUE);
assertThat(solver.model().formula(this.f).variables()).isEqualTo(phiVars);
solver.addWithoutUnknown(add2);
assertThat(solver.sat()).isEqualTo(Tristate.TRUE);
assertThat(solver.model().formula(this.f).variables()).isEqualTo(phiVars);
if (solver instanceof MiniSat) {
final SolverState state = solver.saveState();
solver.addWithoutUnknown(add3);
assertThat(solver.sat()).isEqualTo(Tristate.FALSE);
solver.loadState(state);
solver.add(add1);
assertThat(solver.sat()).isEqualTo(Tristate.TRUE);
assertThat(solver.model().formula(this.f).variables().containsAll(Arrays.asList(this.f.variable("x6"), this.f.variable("x7")))).isTrue();
solver.loadState(state);
solver.sat();
assertThat(solver.model().formula(this.f).variables()).isEqualTo(phiVars);
} else {
solver.add(add1);
assertThat(solver.sat()).isEqualTo(Tristate.TRUE);
assertThat(solver.model().formula(this.f).variables().containsAll(Arrays.asList(this.f.variable("x6"), this.f.variable("x7")))).isTrue();
solver.add(this.f.variable("x7"));
assertThat(solver.sat()).isEqualTo(Tristate.TRUE);
assertThat(solver.model().formula(this.f).variables().containsAll(Arrays.asList(this.f.variable("x6"), this.f.variable("x7")))).isTrue();
solver.addWithoutUnknown(add4);
assertThat(solver.sat()).isEqualTo(Tristate.FALSE);
}
}
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class DeletionBasedMUS method computeMUS.
@Override
public <T extends Proposition> UNSATCore<T> computeMUS(final List<T> propositions, final FormulaFactory f, final MUSConfig config) {
start(config.handler);
final List<T> mus = new ArrayList<>(propositions.size());
final List<SolverState> solverStates = new ArrayList<>(propositions.size());
final MiniSat solver = MiniSat.miniSat(f);
for (final Proposition proposition : propositions) {
solverStates.add(solver.saveState());
solver.add(proposition);
}
boolean sat = solver.sat() == Tristate.TRUE;
if (aborted(config.handler)) {
return null;
}
if (sat) {
throw new IllegalArgumentException("Cannot compute a MUS for a satisfiable formula set.");
}
for (int i = solverStates.size() - 1; i >= 0; i--) {
solver.loadState(solverStates.get(i));
for (final Proposition prop : mus) {
solver.add(prop);
}
sat = solver.sat(config.handler) == Tristate.TRUE;
if (aborted(config.handler)) {
return null;
}
if (sat) {
mus.add(propositions.get(i));
}
}
return new UNSATCore<>(mus, true);
}
Aggregations