Search in sources :

Example 1 with MiniSat2Solver

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

the class BackboneFunctionTest method testRealFormulaIncrementalDecremental1.

@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncrementalDecremental1(final MiniSat solver) throws IOException, ParserException {
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        solver.reset();
        final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.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_large_formula.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("v411"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275 & v188"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275 & v188 & v103"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275 & v188 & v103 & v404"));
        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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with MiniSat2Solver

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

the class EncodingsTest method testSequentialWeightCounterExceptionalBehavior.

@Test
public void testSequentialWeightCounterExceptionalBehavior() {
    assertThatThrownBy(() -> {
        final SequentialWeightCounter swc = new SequentialWeightCounter();
        swc.encode(new MiniSat2Solver(), new LNGIntVector(), new LNGIntVector(), Integer.MAX_VALUE);
    }).isInstanceOf(IllegalArgumentException.class).hasMessage("Overflow in the encoding.");
    assertThatThrownBy(() -> {
        final SequentialWeightCounter swc = new SequentialWeightCounter();
        swc.encode(new MiniSat2Solver(), new LNGIntVector(), new LNGIntVector(), Integer.MAX_VALUE, new LNGIntVector(), 1);
    }).isInstanceOf(IllegalArgumentException.class).hasMessage("Overflow in the encoding.");
}
Also used : MiniSat2Solver(org.logicng.solvers.sat.MiniSat2Solver) LNGIntVector(org.logicng.collections.LNGIntVector) Test(org.junit.jupiter.api.Test)

Example 3 with MiniSat2Solver

use of org.logicng.solvers.sat.MiniSat2Solver 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)

Example 4 with MiniSat2Solver

use of org.logicng.solvers.sat.MiniSat2Solver 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 5 with MiniSat2Solver

use of org.logicng.solvers.sat.MiniSat2Solver 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)

Aggregations

MiniSat2Solver (org.logicng.solvers.sat.MiniSat2Solver)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 Backbone (org.logicng.backbones.Backbone)4 SolverState (org.logicng.solvers.SolverState)4 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 ArrayList (java.util.ArrayList)2 Formula (org.logicng.formulas.Formula)2 Test (org.junit.jupiter.api.Test)1 LongRunningTag (org.logicng.LongRunningTag)1 LNGIntVector (org.logicng.collections.LNGIntVector)1