Search in sources :

Example 1 with CNFFactorization

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

the class ConnectedComponentsComputerTest method testFormulaSplit.

@Test
public void testFormulaSplit() 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<>();
    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);
    assertThat(split).hasSize(4);
    assertThat(split.get(0)).hasSize(1899);
    assertThat(split.get(1)).hasSize(3);
    assertThat(split.get(2)).hasSize(3);
    assertThat(split.get(3)).hasSize(3);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PBConstraint(org.logicng.formulas.PBConstraint) CNFFactorization(org.logicng.transformations.cnf.CNFFactorization) GraphTest(org.logicng.graphs.datastructures.GraphTest) Test(org.junit.jupiter.api.Test)

Example 2 with CNFFactorization

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

use of org.logicng.transformations.cnf.CNFFactorization 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)

Aggregations

ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 Test (org.junit.jupiter.api.Test)3 Formula (org.logicng.formulas.Formula)3 FormulaFactory (org.logicng.formulas.FormulaFactory)3 PBConstraint (org.logicng.formulas.PBConstraint)3 Variable (org.logicng.formulas.Variable)3 CNFFactorization (org.logicng.transformations.cnf.CNFFactorization)3 List (java.util.List)2 BigInteger (java.math.BigInteger)1 HashSet (java.util.HashSet)1 LongRunningTag (org.logicng.LongRunningTag)1 GraphTest (org.logicng.graphs.datastructures.GraphTest)1 Dnnf (org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf)1