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);
}
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);
}
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");
}
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);
}
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;
}
Aggregations