Search in sources :

Example 16 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class BackboneFunctionTest method testConstants.

@ParameterizedTest
@MethodSource("solvers")
public void testConstants(final MiniSat solver) {
    solver.reset();
    SolverState state = null;
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        state = solver.saveState();
    }
    solver.add(f.falsum());
    Backbone backbone = solver.backbone(v("a b c"));
    assertThat(backbone.isSat()).isFalse();
    assertThat(backbone.getCompleteBackbone()).isEmpty();
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        solver.loadState(state);
    } else {
        solver.reset();
    }
    solver.add(f.verum());
    backbone = solver.backbone(v("a b c"));
    assertThat(backbone.isSat()).isTrue();
    assertThat(backbone.getCompleteBackbone()).isEmpty();
}
Also used : Backbone(org.logicng.backbones.Backbone) MiniSat2Solver(org.logicng.solvers.sat.MiniSat2Solver) SolverState(org.logicng.solvers.SolverState) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 17 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class BackboneFunctionTest method testRealFormulaIncrementalDecremental2.

@ParameterizedTest
@MethodSource("solvers")
@LongRunningTag
public void testRealFormulaIncrementalDecremental2(final MiniSat solver) throws IOException, ParserException {
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        solver.reset();
        final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/small_formulas.txt", f);
        solver.add(formula);
        final SolverState state = solver.saveState();
        final List<String> expectedBackbones = new ArrayList<>();
        final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_small_formulas.txt"));
        while (reader.ready()) {
            expectedBackbones.add(reader.readLine());
        }
        reader.close();
        Backbone backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(0)));
        solver.add(f.variable("v2609"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048 & v39"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048 & v39 & v1663"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048 & v39 & v1663 & v2238"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEmpty();
        assertThat(backbone.isSat()).isFalse();
    }
}
Also used : Backbone(org.logicng.backbones.Backbone) Formula(org.logicng.formulas.Formula) MiniSat2Solver(org.logicng.solvers.sat.MiniSat2Solver) SolverState(org.logicng.solvers.SolverState) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) LongRunningTag(org.logicng.LongRunningTag) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 18 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class PlaistedGreenbaumTransformationSolverTest method random.

@Test
@RandomTag
public void random() {
    for (int i = 0; i < 1000; i++) {
        final FormulaFactory f = new FormulaFactory();
        final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().cnfMethod(MiniSatConfig.CNFMethod.FULL_PG_ON_SOLVER).auxiliaryVariablesInModels(false).build());
        final FormulaRandomizer randomizer = new FormulaRandomizer(f, FormulaRandomizerConfig.builder().numVars(10).weightPbc(1).seed(i * 42).build());
        final Formula randomFormula01 = randomSATFormula(randomizer, 4, f);
        final Formula randomFormula02 = randomSATFormula(randomizer, 4, f);
        solver.reset();
        solver.add(randomFormula01);
        if (solver.sat() == Tristate.TRUE) {
            final List<Assignment> models = solver.enumerateAllModels();
            final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
            assertThat(f.equivalence(randomFormula01, dnf).holds(new TautologyPredicate(f))).isTrue();
        }
        final SolverState state = solver.saveState();
        solver.add(randomFormula02);
        if (solver.sat() == Tristate.TRUE) {
            final List<Assignment> models = solver.enumerateAllModels();
            final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
            assertThat(f.equivalence(f.and(randomFormula01, randomFormula02), dnf).holds(new TautologyPredicate(f))).isTrue();
        }
        solver.loadState(state);
        if (solver.sat() == Tristate.TRUE) {
            final List<Assignment> models = solver.enumerateAllModels();
            final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
            assertThat(f.equivalence(randomFormula01, dnf).holds(new TautologyPredicate(f))).isTrue();
        }
        solver.add(randomFormula02);
        if (solver.sat() == Tristate.TRUE) {
            final List<Assignment> models = solver.enumerateAllModels();
            final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
            assertThat(f.equivalence(f.and(randomFormula01, randomFormula02), dnf).holds(new TautologyPredicate(f))).isTrue();
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) TautologyPredicate(org.logicng.predicates.satisfiability.TautologyPredicate) SolverState(org.logicng.solvers.SolverState) FormulaRandomizer(org.logicng.util.FormulaRandomizer) Test(org.junit.jupiter.api.Test) RandomTag(org.logicng.RandomTag)

Example 19 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class IncDecTest method testIncDec.

@Test
public void testIncDec() {
    for (final MiniSat s : this.solvers) {
        s.add(this.f.variable("a"));
        final SolverState state1 = s.saveState();
        if (s.underlyingSolver() instanceof MiniCard) {
            assertThat(state1.toString()).isEqualTo("SolverState{id=0, state=[1, 1, 0, 0, 1]}");
        } else {
            assertThat(state1.toString()).isEqualTo("SolverState{id=0, state=[1, 1, 0, 0, 1, 0, 0]}");
        }
        assertSolverSat(s);
        s.add(this.pg.generate(5));
        assertSolverUnsat(s);
        s.loadState(state1);
        assertSolverSat(s);
        s.add(this.f.literal("a", false));
        assertSolverUnsat(s);
        s.loadState(state1);
        assertSolverSat(s);
        s.add(this.pg.generate(5));
        final SolverState state2 = s.saveState();
        if (s.underlyingSolver() instanceof MiniCard) {
            assertThat(state2.toString()).isEqualTo("SolverState{id=1, state=[1, 31, 81, 0, 1]}");
        } else {
            assertThat(state2.toString()).isEqualTo("SolverState{id=1, state=[1, 31, 81, 0, 1, 0, 0]}");
        }
        s.add(this.pg.generate(4));
        assertSolverUnsat(s);
        s.loadState(state2);
        assertSolverUnsat(s);
        s.loadState(state1);
        assertSolverSat(s);
    }
}
Also used : SolverState(org.logicng.solvers.SolverState) MiniSat(org.logicng.solvers.MiniSat) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 20 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class SATTest method testKnownVariables.

@Test
public void testKnownVariables() throws ParserException {
    final PropositionalParser parser = new PropositionalParser(this.f);
    final Formula phi = parser.parse("x1 & x2 & x3 & (x4 | ~x5)");
    final SATSolver minisat = MiniSat.miniSat(this.f);
    final SATSolver minicard = MiniSat.miniCard(this.f);
    minisat.add(phi);
    minicard.add(phi);
    final SortedSet<Variable> expected = new TreeSet<>(Arrays.asList(this.f.variable("x1"), this.f.variable("x2"), this.f.variable("x3"), this.f.variable("x4"), this.f.variable("x5")));
    assertThat(minisat.knownVariables()).isEqualTo(expected);
    assertThat(minicard.knownVariables()).isEqualTo(expected);
    final SolverState state = minisat.saveState();
    final SolverState stateCard = minicard.saveState();
    minisat.add(this.f.variable("x6"));
    minicard.add(this.f.variable("x6"));
    final SortedSet<Variable> expected2 = new TreeSet<>(Arrays.asList(this.f.variable("x1"), this.f.variable("x2"), this.f.variable("x3"), this.f.variable("x4"), this.f.variable("x5"), this.f.variable("x6")));
    assertThat(minisat.knownVariables()).isEqualTo(expected2);
    assertThat(minicard.knownVariables()).isEqualTo(expected2);
    // load state for minisat
    minisat.loadState(state);
    minicard.loadState(stateCard);
    assertThat(minisat.knownVariables()).isEqualTo(expected);
    assertThat(minicard.knownVariables()).isEqualTo(expected);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) TreeSet(java.util.TreeSet) PropositionalParser(org.logicng.io.parsers.PropositionalParser) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

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