Search in sources :

Example 21 with SolverState

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);
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) MiniSat(org.logicng.solvers.MiniSat) TreeSet(java.util.TreeSet) PropositionalParser(org.logicng.io.parsers.PropositionalParser) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 22 with SolverState

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);
}
Also used : SolverState(org.logicng.solvers.SolverState) MiniSat(org.logicng.solvers.MiniSat) ArrayList(java.util.ArrayList) UNSATCore(org.logicng.explanations.UNSATCore) Proposition(org.logicng.propositions.Proposition)

Aggregations

SolverState (org.logicng.solvers.SolverState)22 Test (org.junit.jupiter.api.Test)12 Variable (org.logicng.formulas.Variable)10 SATSolver (org.logicng.solvers.SATSolver)10 LogicNGTest (org.logicng.LogicNGTest)9 Formula (org.logicng.formulas.Formula)8 Backbone (org.logicng.backbones.Backbone)6 ArrayList (java.util.ArrayList)5 TreeSet (java.util.TreeSet)5 MiniSat (org.logicng.solvers.MiniSat)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 Assignment (org.logicng.datastructures.Assignment)4 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)4 FormulaFactory (org.logicng.formulas.FormulaFactory)4 MiniSat2Solver (org.logicng.solvers.sat.MiniSat2Solver)4 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 List (java.util.List)2 Literal (org.logicng.formulas.Literal)2