Search in sources :

Example 81 with Formula

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

the class FormulaDepthFunctionTest method testCache.

@Test
public void testCache() throws ParserException {
    final FormulaFactory f = new FormulaFactory();
    final Formula formula = f.parse("A & B | C");
    assertThat(formula.functionCacheEntry(FunctionCacheEntry.DEPTH)).isNull();
    assertThat(formula.apply(new FormulaDepthFunction())).isEqualTo(2);
    assertThat(formula.functionCacheEntry(FunctionCacheEntry.DEPTH)).isEqualTo(2);
    assertThat(f.variable("A").functionCacheEntry(FunctionCacheEntry.DEPTH)).isEqualTo(0);
    formula.setFunctionCacheEntry(FunctionCacheEntry.DEPTH, 3);
    assertThat(formula.apply(new FormulaDepthFunction())).isEqualTo(3);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Test(org.junit.jupiter.api.Test)

Example 82 with Formula

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

the class FormulaDepthFunctionTest method testDeeperFormulas.

@Test
public void testDeeperFormulas() {
    Formula formula = this.PBC1;
    for (int i = 0; i < 10; i++) {
        final Variable var = this.f.variable("X" + i);
        formula = i % 2 == 0 ? this.f.or(formula, var) : this.f.and(formula, var);
    }
    assertThat(formula.apply(new FormulaDepthFunction())).isEqualTo(10);
}
Also used : Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) Test(org.junit.jupiter.api.Test)

Example 83 with Formula

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

the class FactorOutSimplifier method applyRec.

private Formula applyRec(final Formula formula, final boolean cache) {
    switch(formula.type()) {
        case OR:
        case AND:
            final List<Formula> newOps = new ArrayList<>();
            for (final Formula op : formula) {
                newOps.add(apply(op, cache));
            }
            final Formula newFormula = formula.factory().naryOperator(formula.type(), newOps);
            return newFormula instanceof NAryOperator ? simplify((NAryOperator) newFormula) : newFormula;
        case NOT:
            return apply(((Not) formula).operand(), cache).negate();
        case FALSE:
        case TRUE:
        case LITERAL:
        case IMPL:
        case EQUIV:
        case PBC:
            return formula;
        default:
            throw new IllegalStateException("Unknown formula type: " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) ArrayList(java.util.ArrayList) NAryOperator(org.logicng.formulas.NAryOperator)

Example 84 with Formula

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

the class FactorOutSimplifier method computeMaxOccurringSubformula.

private static Formula computeMaxOccurringSubformula(final NAryOperator formula) {
    final Map<Formula, Integer> formulaCounts = new HashMap<>();
    for (final Formula operand : formula) {
        if (operand.type() == FType.LITERAL) {
            formulaCounts.merge(operand, 1, Integer::sum);
        } else if (operand.type() == FType.AND || operand.type() == FType.OR) {
            for (final Formula subOperand : operand) {
                formulaCounts.merge(subOperand, 1, Integer::sum);
            }
        }
    }
    final Pair<Formula, Integer> max = formulaCounts.entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue)).map(e -> new Pair<>(e.getKey(), e.getValue())).orElse(new Pair<>(null, 0));
    return max.second() < 2 ? null : max.first();
}
Also used : FType(org.logicng.formulas.FType) NAryOperator(org.logicng.formulas.NAryOperator) Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) FormulaFactory(org.logicng.formulas.FormulaFactory) Map(java.util.Map) Pair(org.logicng.util.Pair) FormulaTransformation(org.logicng.formulas.FormulaTransformation) FType.dual(org.logicng.formulas.FType.dual) Comparator(java.util.Comparator) Not(org.logicng.formulas.Not) Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.logicng.util.Pair)

Example 85 with Formula

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

the class FactorOutSimplifier method factorOut.

private static Formula factorOut(final NAryOperator formula) {
    final Formula factorOutFormula = computeMaxOccurringSubformula(formula);
    if (factorOutFormula == null) {
        return null;
    }
    final FormulaFactory f = formula.factory();
    final FType type = formula.type();
    final List<Formula> formulasWithRemoved = new ArrayList<>();
    final List<Formula> unchangedFormulas = new ArrayList<>();
    for (final Formula operand : formula) {
        if (operand.type() == FType.LITERAL) {
            if (operand.equals(factorOutFormula)) {
                formulasWithRemoved.add(f.constant(type == FType.OR));
            } else {
                unchangedFormulas.add(operand);
            }
        } else if (operand.type() == FType.AND || operand.type() == FType.OR) {
            boolean removed = false;
            final List<Formula> newOps = new ArrayList<>();
            for (final Formula op : operand) {
                if (!op.equals(factorOutFormula)) {
                    newOps.add(op);
                } else {
                    removed = true;
                }
            }
            (removed ? formulasWithRemoved : unchangedFormulas).add(f.naryOperator(operand.type(), newOps));
        } else {
            unchangedFormulas.add(operand);
        }
    }
    return f.naryOperator(type, f.naryOperator(type, unchangedFormulas), f.naryOperator(dual(type), factorOutFormula, f.naryOperator(type, formulasWithRemoved)));
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) FType(org.logicng.formulas.FType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16