Search in sources :

Example 1 with TseitinTransformation

use of org.logicng.transformations.cnf.TseitinTransformation 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 2 with TseitinTransformation

use of org.logicng.transformations.cnf.TseitinTransformation in project LogicNG by logic-ng.

the class FormulaFactoryImporterTest method testAdjustCounters.

@Test
public void testAdjustCounters() throws ParserException {
    final FormulaFactory f = new FormulaFactory(FormulaFactoryConfig.builder().name("Factory").build());
    final PseudoBooleanParser p = new PseudoBooleanParser(f);
    final Formula cc = p.parse("A + B + C + D + E <= 2").cnf();
    final Formula pbc = p.parse("2*A + -2*B + 3*C + D + 2*E <= 3").cnf();
    final Formula cnf = p.parse("A & B & C | C & D & ~A").transform(new TseitinTransformation(0));
    final FormulaFactory g = new FormulaFactory();
    g.newCNFVariable();
    g.newCNFVariable();
    g.newCCVariable();
    g.newCCVariable();
    g.newCCVariable();
    g.newPBVariable();
    g.newPBVariable();
    g.newPBVariable();
    g.importFormula(cc);
    g.importFormula(pbc);
    g.importFormula(cnf);
    assertThat(g.statistics().cnfCounter()).isEqualTo(2);
    assertThat(g.statistics().ccCounter()).isEqualTo(13);
    assertThat(g.statistics().pbCounter()).isEqualTo(25);
}
Also used : PseudoBooleanParser(org.logicng.io.parsers.PseudoBooleanParser) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) TseitinTransformation(org.logicng.transformations.cnf.TseitinTransformation) Test(org.junit.jupiter.api.Test)

Example 3 with TseitinTransformation

use of org.logicng.transformations.cnf.TseitinTransformation in project LogicNG by logic-ng.

the class ModelTest method testCNFWithAuxiliaryVars.

@ParameterizedTest
@MethodSource("solvers")
public void testCNFWithAuxiliaryVars(final MiniSat 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();
    assertThat(formula.evaluate(model)).isTrue();
    final List<Assignment> allModels = solver.enumerateAllModels();
    assertThat(allModels).hasSize(4);
    if (solver.getConfig().isAuxiliaryVariablesInModels()) {
        assertThat(model.formula(f).variables()).isEqualTo(cnf.variables());
        for (final Assignment assignment : allModels) {
            assertThat(formula.evaluate(assignment)).isTrue();
            assertThat(assignment.formula(f).variables()).isEqualTo(cnf.variables());
        }
    } else {
        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)

Aggregations

Formula (org.logicng.formulas.Formula)3 TseitinTransformation (org.logicng.transformations.cnf.TseitinTransformation)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Assignment (org.logicng.datastructures.Assignment)2 Test (org.junit.jupiter.api.Test)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 PseudoBooleanParser (org.logicng.io.parsers.PseudoBooleanParser)1