Search in sources :

Example 11 with Formula

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

the class FormulaDepthFunction method apply.

@Override
public Integer apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(DEPTH);
    if (cached != null) {
        return (Integer) cached;
    }
    final int result;
    if (formula.isAtomicFormula()) {
        result = 0;
    } else {
        int maxDepth = 0;
        for (final Formula op : formula) {
            maxDepth = Math.max(maxDepth, apply(op, cache));
        }
        result = maxDepth + 1;
    }
    if (cache) {
        formula.setFunctionCacheEntry(DEPTH, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula)

Example 12 with Formula

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

the class LiteralProfileFunction method nonCachingRecursion.

/**
 * Recursive function for the non-caching literal profile computation.
 * @param formula the formula
 * @param map     the literal profile
 */
private static void nonCachingRecursion(final Formula formula, final Map<Literal, Integer> map) {
    if (formula.type() == FType.LITERAL) {
        final Literal lit = (Literal) formula;
        map.merge(lit, 1, Integer::sum);
    } else if (formula.type() == FType.PBC) {
        for (final Literal l : formula.literals()) {
            nonCachingRecursion(l, map);
        }
    } else {
        for (final Formula op : formula) {
            nonCachingRecursion(op, map);
        }
    }
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal)

Example 13 with Formula

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

the class MinimumPrimeImplicantFunction method apply.

@Override
public SortedSet<Literal> apply(final Formula formula, final boolean cache) {
    final Formula nnf = formula.nnf();
    final Map<Variable, Literal> newVar2oldLit = new HashMap<>();
    final LiteralSubstitution substitution = new LiteralSubstitution();
    for (final Literal literal : nnf.literals()) {
        final Variable newVar = formula.factory().variable(literal.name() + (literal.phase() ? POS : NEG));
        newVar2oldLit.put(newVar, literal);
        substitution.addSubstitution(literal, newVar);
    }
    final Formula substitued = nnf.transform(substitution);
    final SATSolver solver = MiniSat.miniSat(formula.factory(), MiniSatConfig.builder().cnfMethod(MiniSatConfig.CNFMethod.PG_ON_SOLVER).build());
    solver.add(substitued);
    for (final Literal literal : newVar2oldLit.values()) {
        if (literal.phase() && newVar2oldLit.containsValue(literal.negate())) {
            solver.add(formula.factory().amo(formula.factory().variable(literal.name() + POS), formula.factory().variable(literal.name() + NEG)));
        }
    }
    if (solver.sat() != Tristate.TRUE) {
        return null;
    }
    final Assignment minimumModel = solver.execute(OptimizationFunction.minimize(newVar2oldLit.keySet()));
    final SortedSet<Literal> primeImplicant = new TreeSet<>();
    for (final Variable variable : minimumModel.positiveVariables()) {
        final Literal literal = newVar2oldLit.get(variable);
        if (literal != null) {
            primeImplicant.add(literal);
        }
    }
    return primeImplicant;
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) LiteralSubstitution(org.logicng.transformations.LiteralSubstitution) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal)

Example 14 with Formula

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

the class SubNodeFunction method apply.

@Override
@SuppressWarnings("unchecked")
public LinkedHashSet<Formula> apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(SUBFORMULAS);
    if (cached != null) {
        return (LinkedHashSet<Formula>) cached;
    }
    final LinkedHashSet<Formula> result = new LinkedHashSet<>();
    for (final Formula op : formula) {
        if (!result.contains(op)) {
            result.addAll(apply(op, cache));
        }
    }
    result.add(formula);
    if (cache) {
        formula.setFunctionCacheEntry(SUBFORMULAS, result);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula)

Example 15 with Formula

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

the class VariableProfileFunction method nonCachingRecursion.

/**
 * Recursive function for the non-caching variable profile computation.
 * @param formula the formula
 * @param map     the variable profile
 */
private static void nonCachingRecursion(final Formula formula, final Map<Variable, Integer> map) {
    if (formula.type() == FType.LITERAL) {
        final Literal lit = (Literal) formula;
        map.merge(lit.variable(), 1, Integer::sum);
    } else if (formula.type() == FType.PBC) {
        for (final Literal l : formula.literals()) {
            nonCachingRecursion(l.variable(), map);
        }
    } else {
        for (final Formula op : formula) {
            nonCachingRecursion(op, map);
        }
    }
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal)

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