Search in sources :

Example 6 with SolverState

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

the class CCIncrementalSolverTest method testSimpleIncrementalAMK.

@Test
public void testSimpleIncrementalAMK() {
    for (final CCConfig config : this.configs) {
        this.f.putConfiguration(this.configs[2]);
        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 SATSolver solver = MiniSat.miniSat(this.f);
        // >= 4
        solver.add(this.f.cc(CType.GE, 4, vars));
        // <= 7
        solver.add(this.f.cc(CType.LE, 7, vars));
        this.f.putConfiguration(config);
        final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.LE, 9, vars));
        // <= 9
        assertSolverSat(solver);
        // <= 8
        incData.newUpperBoundForSolver(8);
        assertSolverSat(solver);
        // <= 7
        incData.newUpperBoundForSolver(7);
        assertSolverSat(solver);
        // <= 6
        incData.newUpperBoundForSolver(6);
        assertSolverSat(solver);
        // <= 5
        incData.newUpperBoundForSolver(5);
        assertSolverSat(solver);
        // <= 4
        incData.newUpperBoundForSolver(4);
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // <= 3
        incData.newUpperBoundForSolver(3);
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 2
        incData.newUpperBoundForSolver(2);
        assertSolverUnsat(solver);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 7 with SolverState

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

the class CCIncrementalSolverTest method testSimpleIncrementalALK.

@Test
public void testSimpleIncrementalALK() {
    for (final CCConfig config : this.configs) {
        this.f.putConfiguration(this.configs[2]);
        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 SATSolver solver = this.solvers[2];
        solver.reset();
        // >= 4
        solver.add(this.f.cc(CType.GE, 4, vars));
        // <= 7
        solver.add(this.f.cc(CType.LE, 7, vars));
        this.f.putConfiguration(config);
        final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.GE, 2, vars));
        // >=2
        assertSolverSat(solver);
        // >= 3
        incData.newLowerBoundForSolver(3);
        assertSolverSat(solver);
        // >= 4
        incData.newLowerBoundForSolver(4);
        assertSolverSat(solver);
        // >= 5
        incData.newLowerBoundForSolver(5);
        assertSolverSat(solver);
        // >= 6
        incData.newLowerBoundForSolver(6);
        assertSolverSat(solver);
        // >= 7
        incData.newLowerBoundForSolver(7);
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // >= 8
        incData.newLowerBoundForSolver(8);
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 9
        incData.newLowerBoundForSolver(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) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 8 with SolverState

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

the class BackboneGenerationTest method testSimpleBackbones.

@Test
public void testSimpleBackbones() {
    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);
    final Literal u = f.literal("u", true);
    final Literal v = f.literal("v", true);
    final Collection<Variable> variables = new ArrayList<>(Arrays.asList(f.variable("x"), f.variable("y"), f.variable("z"), f.variable("u"), f.variable("v")));
    Formula formula = f.verum();
    SolverState before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(Collections.emptyList()).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = x;
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
    solver.loadState(before);
    formula = f.and(x, y);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
    solver.loadState(before);
    formula = f.or(x, y);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = x.negate();
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate());
    solver.loadState(before);
    formula = f.or(f.and(x, y, z), f.and(x, y, u), f.and(x, u, z));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
    solver.loadState(before);
    formula = f.and(f.or(x, y, z), f.or(x, y, u), f.or(x, u, z));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = f.and(f.or(x.negate(), y), x);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
    solver.loadState(before);
    formula = f.and(f.or(x, y), f.or(x.negate(), y));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(y);
    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);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate(), y, z);
    solver.loadState(before);
    formula = f.and(f.or(x, y), f.or(u, v), z);
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(z);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) MiniSat(org.logicng.solvers.MiniSat) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 9 with SolverState

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

the class IncDecTest method testIncDecDeep.

@Test
public void testIncDecDeep() {
    for (final SATSolver s : this.solvers) {
        s.add(this.f.variable("a"));
        final SolverState state1 = s.saveState();
        s.add(this.f.variable("b"));
        assertSolverSat(s);
        final SolverState state2 = s.saveState();
        s.add(this.f.literal("a", false));
        assertSolverUnsat(s);
        s.loadState(state1);
        try {
            s.loadState(state2);
            assert false;
        } catch (final IllegalArgumentException e) {
        // fine
        }
        s.add(this.f.literal("b", false));
        assertSolverSat(s);
        final SolverState state3 = s.saveState();
        s.add(this.f.literal("a", false));
        assertSolverUnsat(s);
        s.loadState(state3);
        s.add(this.f.variable("c"));
        final SolverState state4 = s.saveState();
        final SolverState state5 = s.saveState();
        s.loadState(state4);
        try {
            s.loadState(state5);
            assert false;
        } catch (final IllegalArgumentException e) {
        // fine
        }
        assertSolverSat(s);
        s.loadState(state1);
        assertSolverSat(s);
        try {
            s.loadState(state3);
            assert false;
        } catch (final IllegalArgumentException e) {
        // fine
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) SolverState(org.logicng.solvers.SolverState) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 10 with SolverState

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

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