Search in sources :

Example 96 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class PBEncoderTest method testCC1.

@Test
public void testCC1() {
    for (final PBEncoder encoder : this.encoders) {
        final int numLits = 100;
        final int rhs = 1;
        final List<Literal> lits = new ArrayList<>();
        final List<Integer> coeffs = new ArrayList<>();
        final Variable[] problemLits = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            final Variable var = this.f.variable("v" + i);
            lits.add(var);
            problemLits[i] = var;
            coeffs.add(1);
        }
        final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, rhs, lits, coeffs));
        final SATSolver solver = MiniSat.miniSat(this.f);
        solver.add(clauses);
        assertSolverSat(solver);
        assertThat(solver.enumerateAllModels(problemLits)).hasSize(numLits + 1).allMatch(m -> m.positiveVariables().size() <= rhs);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 97 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class ModelTest method testCNFWithAuxiliaryVarsRestrictedToOriginal.

@ParameterizedTest
@MethodSource("solvers")
public void testCNFWithAuxiliaryVarsRestrictedToOriginal(final SATSolver solver) throws ParserException {
    solver.reset();
    final Formula formula = f.parse("(A => B & C) & (~A => C & ~D) & (C => (D & E | ~E & B)) & ~F");
    final Formula cnf = formula.transform(new TseitinTransformation(0));
    solver.add(cnf);
    solver.sat();
    final Assignment model = solver.model(formula.variables());
    assertThat(formula.evaluate(model)).isTrue();
    final List<Assignment> allModels = solver.enumerateAllModels(formula.variables());
    assertThat(allModels).hasSize(4);
    assertThat(model.formula(f).variables()).isEqualTo(formula.variables());
    for (final Assignment assignment : allModels) {
        assertThat(formula.evaluate(assignment)).isTrue();
        assertThat(assignment.formula(f).variables()).isEqualTo(formula.variables());
    }
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) TseitinTransformation(org.logicng.transformations.cnf.TseitinTransformation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 98 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class ModelTest method testNonCNFOnlyFormulaVars.

@ParameterizedTest
@MethodSource("solvers")
public void testNonCNFOnlyFormulaVars(final SATSolver solver) throws ParserException {
    solver.reset();
    final Formula formula = f.parse("(A => B & C) & (~A => C & ~D) & (C => (D & E | ~E & B)) & ~F");
    solver.add(formula);
    solver.sat();
    final Assignment model = solver.model(formula.variables());
    assertThat(formula.evaluate(model)).isTrue();
    assertThat(model.formula(f).variables()).isEqualTo(formula.variables());
    final List<Assignment> allModels = solver.enumerateAllModels(formula.variables());
    assertThat(allModels).hasSize(4);
    for (final Assignment assignment : allModels) {
        assertThat(formula.evaluate(assignment)).isTrue();
        assertThat(assignment.formula(f).variables()).isEqualTo(formula.variables());
    }
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 99 with Formula

use of org.logicng.formulas.Formula 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 100 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class BackboneFunctionTest method testRealFormulaIncremental1.

@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncremental1(final MiniSat solver) throws IOException, ParserException {
    solver.reset();
    final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
    solver.add(formula);
    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.add(f.variable("v385"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v275"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v188"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v103"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("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) 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

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16