Search in sources :

Example 11 with SolverState

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

the class CnfMethodComparisonTest method computeBackbonePerVariable.

private Map<Variable, Backbone> computeBackbonePerVariable(final String fileName, final CNFConfig cnfConfig, final MiniSatConfig.CNFMethod cnfMethod) throws IOException, ParserException {
    final long start = System.currentTimeMillis();
    final FormulaFactory f = new FormulaFactory();
    f.putConfiguration(cnfConfig);
    final Formula formula = FormulaReader.readPseudoBooleanFormula(fileName, f);
    final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().cnfMethod(cnfMethod).build());
    solver.add(formula);
    final SolverState solverState = solver.saveState();
    final Map<Variable, Backbone> result = new TreeMap<>();
    int counter = 1000;
    for (final Variable variable : formula.variables()) {
        if (counter-- > 0) {
            solver.add(variable);
            if (solver.sat() == Tristate.TRUE) {
                final Backbone backbone = solver.backbone(formula.variables());
                result.put(variable, backbone);
            }
            solver.loadState(solverState);
        }
    }
    final long stop = System.currentTimeMillis();
    System.out.println(fileName + " " + cnfConfig.algorithm + " " + cnfConfig.fallbackAlgorithmForAdvancedEncoding + " " + cnfMethod + ": " + (stop - start) + " ms.");
    return result;
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Backbone(org.logicng.backbones.Backbone) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) SolverState(org.logicng.solvers.SolverState) Variable(org.logicng.formulas.Variable) TreeMap(java.util.TreeMap)

Example 12 with SolverState

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

the class DRUPTest method testPropositionIncDec.

@Test
public void testPropositionIncDec() throws ParserException {
    final SATSolver solver = this.solvers[0];
    final StandardProposition p1 = new StandardProposition("P1", this.f.parse("((a & b) => c) &  ((a & b) => d)"));
    final StandardProposition p2 = new StandardProposition("P2", this.f.parse("(c & d) <=> ~e"));
    final StandardProposition p3 = new StandardProposition("P3", this.f.parse("~e => f | g"));
    final StandardProposition p4 = new StandardProposition("P4", this.f.parse("(f => ~a) & (g => ~b) & p & q"));
    final StandardProposition p5 = new StandardProposition("P5", this.f.parse("a => b"));
    final StandardProposition p6 = new StandardProposition("P6", this.f.parse("a"));
    final StandardProposition p7 = new StandardProposition("P7", this.f.parse("g | h"));
    final StandardProposition p8 = new StandardProposition("P8", this.f.parse("(x => ~y | z) & (z | w)"));
    final StandardProposition p9 = new StandardProposition("P9", this.f.parse("a & b"));
    final StandardProposition p10 = new StandardProposition("P10", this.f.parse("(p => q) & p"));
    final StandardProposition p11 = new StandardProposition("P11", this.f.parse("a & ~q"));
    solver.addPropositions(p1, p2, p3, p4);
    final SolverState state1 = solver.saveState();
    solver.addPropositions(p5, p6);
    final SolverState state2 = solver.saveState();
    solver.addPropositions(p7, p8);
    assertThat(solver.sat()).isEqualTo(FALSE);
    UNSATCore<Proposition> unsatCore = solver.unsatCore();
    assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p5, p6);
    solver.loadState(state2);
    assertThat(solver.sat()).isEqualTo(FALSE);
    unsatCore = solver.unsatCore();
    assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p5, p6);
    solver.loadState(state1);
    solver.add(p9);
    assertThat(solver.sat()).isEqualTo(FALSE);
    unsatCore = solver.unsatCore();
    assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p9);
    solver.loadState(state1);
    solver.add(p5);
    solver.add(p6);
    assertThat(solver.sat()).isEqualTo(FALSE);
    unsatCore = solver.unsatCore();
    assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p5, p6);
    solver.loadState(state1);
    solver.add(p10);
    solver.add(p11);
    assertThat(solver.sat()).isEqualTo(FALSE);
    unsatCore = solver.unsatCore();
    assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p4, p11);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) SolverState(org.logicng.solvers.SolverState) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 13 with SolverState

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

the class CCIncrementalFormulaTest method testSimpleIncrementalALK.

@Test
public void testSimpleIncrementalALK() {
    for (final CCEncoder encoder : this.encoders) {
        final CCEncoder initialEncoder = new CCEncoder(this.f);
        final int numLits = 10;
        final Variable[] vars = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            vars[i] = this.f.variable("v" + i);
        }
        final Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GE, 2, vars));
        final CCIncrementalData incData = cc.second();
        final SATSolver solver = MiniSat.miniSat(this.f);
        // >= 4
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.GE, 4, vars)));
        // <= 7
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.LE, 7, vars)));
        solver.add(cc.first());
        // >=2
        assertSolverSat(solver);
        // >= 3
        solver.add(incData.newLowerBound(3));
        assertSolverSat(solver);
        // >= 4
        solver.add(incData.newLowerBound(4));
        assertSolverSat(solver);
        // >= 5
        solver.add(incData.newLowerBound(5));
        assertSolverSat(solver);
        // >= 6
        solver.add(incData.newLowerBound(6));
        assertSolverSat(solver);
        // >= 7
        solver.add(incData.newLowerBound(7));
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // >= 8
        solver.add(incData.newLowerBound(8));
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 9
        solver.add(incData.newLowerBound(9));
        assertSolverUnsat(solver);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 14 with SolverState

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

the class BackboneGenerationTest method testBackboneType.

@Test
public void testBackboneType() {
    final FormulaFactory f = new FormulaFactory();
    final MiniSat solver = MiniSat.miniSat(f);
    final Literal x = f.literal("x", true);
    final Literal y = f.literal("y", true);
    final Literal z = f.literal("z", true);
    Formula formula = f.not(x);
    SolverState before = solver.saveState();
    solver.add(formula);
    Backbone backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
    Backbone backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
    Backbone backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
    assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate());
    assertThat(backbonePositive.getCompleteBackbone()).isEmpty();
    assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate());
    SortedSet<Literal> combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
    combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
    solver.loadState(before);
    formula = f.and(f.or(x, y.negate()), x.negate());
    before = solver.saveState();
    solver.add(formula);
    backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
    backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
    backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
    assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate(), y.negate());
    assertThat(backbonePositive.getCompleteBackbone()).isEmpty();
    assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate(), y.negate());
    combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
    combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
    solver.loadState(before);
    formula = f.and(f.or(x, y), f.or(x.negate(), y));
    before = solver.saveState();
    solver.add(formula);
    backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
    backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
    backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
    assertThat(backbone.getCompleteBackbone()).containsExactly(y);
    assertThat(backbonePositive.getCompleteBackbone()).containsExactly(y);
    assertThat(backboneNegative.getCompleteBackbone()).isEmpty();
    combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
    combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
    solver.loadState(before);
    formula = f.and(f.and(f.or(x.negate(), y), x.negate()), f.and(z, f.or(x, y)));
    before = solver.saveState();
    solver.add(formula);
    backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
    backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
    backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
    assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate(), y, z);
    assertThat(backbone.getOptionalVariables()).containsExactly();
    assertThat(backbonePositive.getCompleteBackbone()).containsExactly(y, z);
    assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate());
    combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
    combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
    solver.loadState(before);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) SolverState(org.logicng.solvers.SolverState) MiniSat(org.logicng.solvers.MiniSat) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) Test(org.junit.jupiter.api.Test)

Example 15 with SolverState

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

the class BackboneFunctionTest method testSimpleBackbones.

@ParameterizedTest
@MethodSource("solvers")
public void testSimpleBackbones(final MiniSat solver) throws ParserException {
    solver.reset();
    SolverState state = null;
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        state = solver.saveState();
    }
    solver.add(f.parse("a & b & ~c"));
    Backbone backbone = solver.backbone(v("a b c"));
    assertThat(backbone.isSat()).isTrue();
    assertThat(backbone.getCompleteBackbone()).containsExactly(f.variable("a"), f.variable("b"), f.literal("c", false));
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        solver.loadState(state);
    } else {
        solver.reset();
    }
    solver.add(f.parse("~a & ~b & c"));
    backbone = solver.backbone(v("a c"));
    assertThat(backbone.isSat()).isTrue();
    assertThat(backbone.getCompleteBackbone()).containsExactly(f.literal("a", false), f.variable("c"));
}
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)

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