Search in sources :

Example 1 with Dnnf

use of org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf in project LogicNG by logic-ng.

the class DnnfFactory method compile.

/**
 * Compiles the given formula to a DNNF instance.
 * @param formula the formula
 * @return the compiled DNNF
 */
public Dnnf compile(final Formula formula) {
    final SortedSet<Variable> originalVariables = new TreeSet<>(formula.variables());
    final Formula cnf = formula.cnf();
    originalVariables.addAll(cnf.variables());
    final Formula simplifedFormula = simplifyFormula(cnf);
    final DnnfCompiler compiler = new DnnfCompiler(simplifedFormula);
    final Formula dnnf = compiler.compile(new MinFillDTreeGenerator());
    return new Dnnf(originalVariables, dnnf);
}
Also used : Formula(org.logicng.formulas.Formula) MinFillDTreeGenerator(org.logicng.knowledgecompilation.dnnf.datastructures.dtree.MinFillDTreeGenerator) Dnnf(org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet)

Example 2 with Dnnf

use of org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf in project LogicNG by logic-ng.

the class DnnfCompilerTest method testFormula.

private void testFormula(final Formula formula, final boolean withEquivalence) {
    final DnnfFactory dnnfFactory = new DnnfFactory();
    final Dnnf dnnf = dnnfFactory.compile(formula);
    final BigInteger dnnfCount = dnnf.execute(DnnfModelCountFunction.get());
    if (withEquivalence) {
        final Formula equivalence = formula.factory().equivalence(formula, dnnf.formula());
        assertThat(equivalence.holds(new TautologyPredicate(formula.factory()))).isTrue();
    }
    final BigInteger bddCount = countWithBdd(formula);
    assertThat(dnnfCount).isEqualTo(bddCount);
}
Also used : Formula(org.logicng.formulas.Formula) Dnnf(org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf) TautologyPredicate(org.logicng.predicates.satisfiability.TautologyPredicate) BigInteger(java.math.BigInteger)

Example 3 with Dnnf

use of org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf in project LogicNG by logic-ng.

the class DnnfCompilerTest method testDnnfProperties.

@Test
public void testDnnfProperties() throws ParserException {
    final Dnnf dnnf = new DnnfFactory().compile(this.parser.parse("a | ((b & ~c) | (c & (~d | ~a & b)) & e)"));
    assertThat(dnnf.getOriginalVariables()).extracting(Variable::name).containsExactlyInAnyOrder("a", "b", "c", "d", "e");
}
Also used : Dnnf(org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf) Test(org.junit.jupiter.api.Test)

Example 4 with Dnnf

use of org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf 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 5 with Dnnf

use of org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf in project LogicNG by logic-ng.

the class ModelCounter method count.

private static BigInteger count(final Collection<Formula> formulas, final FormulaFactory f) {
    final Graph<Variable> constraintGraph = ConstraintGraphGenerator.generateFromFormulas(formulas);
    final Set<Set<Node<Variable>>> ccs = ConnectedComponentsComputation.compute(constraintGraph);
    final List<List<Formula>> components = ConnectedComponentsComputation.splitFormulasByComponent(formulas, ccs);
    final DnnfFactory factory = new DnnfFactory();
    BigInteger count = BigInteger.ONE;
    for (final List<Formula> component : components) {
        final Dnnf dnnf = factory.compile(f.and(component));
        count = count.multiply(dnnf.execute(DnnfModelCountFunction.get()));
    }
    return count;
}
Also used : Formula(org.logicng.formulas.Formula) Dnnf(org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf) Variable(org.logicng.formulas.Variable) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) Set(java.util.Set) DnnfFactory(org.logicng.knowledgecompilation.dnnf.DnnfFactory) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Dnnf (org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf)5 Formula (org.logicng.formulas.Formula)4 BigInteger (java.math.BigInteger)3 Variable (org.logicng.formulas.Variable)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 Test (org.junit.jupiter.api.Test)2 SortedSet (java.util.SortedSet)1 LongRunningTag (org.logicng.LongRunningTag)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 PBConstraint (org.logicng.formulas.PBConstraint)1 DnnfFactory (org.logicng.knowledgecompilation.dnnf.DnnfFactory)1 MinFillDTreeGenerator (org.logicng.knowledgecompilation.dnnf.datastructures.dtree.MinFillDTreeGenerator)1 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)1 CNFFactorization (org.logicng.transformations.cnf.CNFFactorization)1