Search in sources :

Example 26 with PBConstraint

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

the class PBSolvingTest method testPBGreater.

@Test
public void testPBGreater() {
    for (final PBEncoder encoder : this.encoders) {
        for (final SATSolver solver : this.solvers) {
            solver.reset();
            final int[] coeffs10 = new int[] { 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 17, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(47).allMatch(model -> model.positiveVariables().size() >= 8);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GT, 16, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(47).allMatch(model -> model.positiveVariables().size() >= 8);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 21, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 22, this.literals10, coeffs10)));
            assertSolverUnsat(solver);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GT, 42, this.literals10, coeffs10)));
            assertSolverUnsat(solver);
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 27 with PBConstraint

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

the class ConstraintGraphGeneratorTest method testRealExample.

@Test
public void testRealExample() throws IOException, ParserException {
    final FormulaFactory f = new FormulaFactory();
    f.putConfiguration(CCConfig.builder().amoEncoding(CCConfig.AMO_ENCODER.PURE).build());
    final Formula parsed = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/formula1.txt", f);
    final List<Formula> formulas = new ArrayList<>();
    for (final Formula formula : parsed) {
        if (formula instanceof PBConstraint) {
            formulas.add(formula);
        } else {
            formulas.add(formula.transform(new CNFFactorization()));
        }
    }
    final Graph<Variable> constraintGraph = ConstraintGraphGenerator.generateFromCnf(formulas);
    final Set<Set<Node<Variable>>> ccs = ConnectedComponentsComputation.compute(constraintGraph);
    assertThat(ccs).hasSize(4);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) Set(java.util.Set) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint) CNFFactorization(org.logicng.transformations.cnf.CNFFactorization) Test(org.junit.jupiter.api.Test)

Example 28 with PBConstraint

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

the class DnnfCompilerTest method testLargeFormula.

@Test
@LongRunningTag
public void testLargeFormula() throws IOException, ParserException {
    final FormulaFactory f = new FormulaFactory();
    f.putConfiguration(CCConfig.builder().amoEncoding(CCConfig.AMO_ENCODER.PURE).build());
    final Formula parsed = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/formula1.txt", f);
    final DnnfFactory dnnfFactory = new DnnfFactory();
    Dnnf dnnf = dnnfFactory.compile(parsed);
    final BigInteger dnnfCount = dnnf.execute(DnnfModelCountFunction.get());
    final List<Formula> formulas = new ArrayList<>();
    final List<Formula> originalFormulas = new ArrayList<>();
    for (final Formula formula : parsed) {
        originalFormulas.add(formula);
        if (formula instanceof PBConstraint) {
            formulas.add(formula);
        } else {
            formulas.add(formula.transform(new CNFFactorization()));
        }
    }
    final Graph<Variable> constraintGraph = ConstraintGraphGenerator.generateFromCnf(formulas);
    final Set<Set<Node<Variable>>> ccs = ConnectedComponentsComputation.compute(constraintGraph);
    final List<List<Formula>> split = ConnectedComponentsComputation.splitFormulasByComponent(originalFormulas, ccs);
    BigInteger multipliedCount = BigInteger.ONE;
    for (final List<Formula> component : split) {
        dnnf = dnnfFactory.compile(f.and(component));
        multipliedCount = multipliedCount.multiply(dnnf.execute(DnnfModelCountFunction.get()));
    }
    assertThat(dnnfCount).isEqualTo(multipliedCount);
}
Also used : Dnnf(org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf) Variable(org.logicng.formulas.Variable) Set(java.util.Set) ArrayList(java.util.ArrayList) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) PBConstraint(org.logicng.formulas.PBConstraint) CNFFactorization(org.logicng.transformations.cnf.CNFFactorization) LongRunningTag(org.logicng.LongRunningTag) Test(org.junit.jupiter.api.Test)

Example 29 with PBConstraint

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

the class ModelCounterTest method testCornerCases.

@Test
public void testCornerCases() {
    final FormulaFactory f = new FormulaFactory();
    final FormulaCornerCases cornerCases = new FormulaCornerCases(f);
    for (final Formula formula : cornerCases.cornerCases()) {
        if (formula.type() == FType.PBC) {
            final PBConstraint pbc = (PBConstraint) formula;
            if (!pbc.isAmo() && !pbc.isExo()) {
                assertThatThrownBy(() -> ModelCounter.count(Collections.singletonList(formula), formula.variables())).isInstanceOf(UnsupportedOperationException.class);
                continue;
            }
        }
        final BigInteger expCount = enumerationBasedModelCount(Collections.singletonList(formula), f);
        final BigInteger count = ModelCounter.count(Collections.singleton(formula), formula.variables());
        assertThat(count).isEqualTo(expCount);
    }
}
Also used : FormulaCornerCases(org.logicng.util.FormulaCornerCases) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) BigInteger(java.math.BigInteger) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test)

Example 30 with PBConstraint

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

the class PureExpansionTransformationTest method testCornerCases.

@Test
public void testCornerCases() {
    final FormulaFactory f = new FormulaFactory();
    final FormulaCornerCases cornerCases = new FormulaCornerCases(f);
    for (final Formula formula : cornerCases.cornerCases()) {
        if (formula.type() == FType.PBC) {
            final PBConstraint pbc = (PBConstraint) formula;
            if (!pbc.isAmo() && !pbc.isExo()) {
                assertThatThrownBy(() -> ModelCounter.count(Collections.singletonList(formula), formula.variables())).isInstanceOf(UnsupportedOperationException.class);
                continue;
            }
        }
        computeAndVerify(formula, transformation);
    }
}
Also used : FormulaCornerCases(org.logicng.util.FormulaCornerCases) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test)

Aggregations

PBConstraint (org.logicng.formulas.PBConstraint)35 Formula (org.logicng.formulas.Formula)22 Test (org.junit.jupiter.api.Test)21 Literal (org.logicng.formulas.Literal)15 Variable (org.logicng.formulas.Variable)11 ArrayList (java.util.ArrayList)9 LogicNGTest (org.logicng.LogicNGTest)9 FormulaFactory (org.logicng.formulas.FormulaFactory)9 BinaryOperator (org.logicng.formulas.BinaryOperator)8 Not (org.logicng.formulas.Not)8 SATSolver (org.logicng.solvers.SATSolver)8 NAryOperator (org.logicng.formulas.NAryOperator)6 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)5 List (java.util.List)3 Set (java.util.Set)3 CNFFactorization (org.logicng.transformations.cnf.CNFFactorization)3 BigInteger (java.math.BigInteger)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 EncodingResult (org.logicng.datastructures.EncodingResult)2