Search in sources :

Example 26 with Formula

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

the class EvaluatesToConstantPredicate method handleOr.

private Formula handleOr(final Or formula, final boolean topLevel) {
    final FormulaFactory f = formula.factory();
    final List<Formula> nops = new ArrayList<>();
    for (final Formula op : formula) {
        final Formula opResult = innerTest(op, !this.evaluatesToTrue && topLevel);
        if (isVerum(opResult)) {
            return f.verum();
        }
        if (!opResult.isConstantFormula()) {
            if (!this.evaluatesToTrue && topLevel) {
                return f.verum();
            }
            nops.add(opResult);
        }
    }
    return f.or(nops);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) ArrayList(java.util.ArrayList)

Example 27 with Formula

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

the class NNFPredicate method test.

@Override
public boolean test(final Formula formula, final boolean cache) {
    final Tristate cached = formula.predicateCacheEntry(IS_NNF);
    if (cached != Tristate.UNDEF) {
        return cached == Tristate.TRUE;
    }
    boolean result;
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
            result = true;
            break;
        case AND:
        case OR:
            result = true;
            for (final Formula op : formula) {
                if (!test(op, cache)) {
                    result = false;
                    break;
                }
            }
            break;
        case NOT:
        case IMPL:
        case EQUIV:
        case PBC:
            result = false;
            break;
        default:
            throw new IllegalArgumentException("Cannot compute NNF predicate on " + formula.type());
    }
    if (cache) {
        formula.setPredicateCacheEntry(IS_NNF, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) Tristate(org.logicng.datastructures.Tristate)

Example 28 with Formula

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

the class TautologyPredicate method test.

@Override
public boolean test(final Formula formula, final boolean cache) {
    final Tristate cached = formula.predicateCacheEntry(IS_TAUTOLOGY);
    if (cached != Tristate.UNDEF) {
        return cached == Tristate.TRUE;
    }
    final boolean result;
    final Formula negation = formula.negate();
    result = !negation.holds(this.satPredicate);
    if (cache) {
        formula.setPredicateCacheEntry(IS_TAUTOLOGY, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) Tristate(org.logicng.datastructures.Tristate)

Example 29 with Formula

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

the class PrimeCompiler method compute.

/**
 * Computes prime implicants and prime implicates for a given formula.
 * The coverage type specifies if the implicants or the implicates will
 * be complete, the other one will still be a cover of the given formula.
 * <p>
 * The prime compiler can be called with an {@link OptimizationHandler}.
 * The given handler instance will be used for every subsequent
 * {@link org.logicng.solvers.functions.OptimizationFunction} call and
 * the handler's SAT handler is used for every subsequent SAT call.
 * @param formula the formula
 * @param type    the coverage type
 * @param handler an optimization handler, can be {@code null}
 * @return the prime result or null if the computation was aborted by the handler
 */
public PrimeResult compute(final Formula formula, final PrimeResult.CoverageType type, final OptimizationHandler handler) {
    start(handler);
    final boolean completeImplicants = type == PrimeResult.CoverageType.IMPLICANTS_COMPLETE;
    final Formula formulaForComputation = completeImplicants ? formula : formula.negate();
    final Pair<List<SortedSet<Literal>>, List<SortedSet<Literal>>> result = computeGeneric(formulaForComputation, handler);
    if (result == null || aborted(handler)) {
        return null;
    }
    return new PrimeResult(completeImplicants ? result.first() : negateAll(result.second()), completeImplicants ? result.second() : negateAll(result.first()), type);
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) List(java.util.List)

Example 30 with Formula

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

the class ModelCounter method simplify.

private static SimplificationResult simplify(final Collection<Formula> formulas) {
    final Assignment simpleBackbone = new Assignment();
    final SortedSet<Variable> backboneVariables = new TreeSet<>();
    for (final Formula formula : formulas) {
        if (formula.type() == FType.LITERAL) {
            final Literal lit = (Literal) formula;
            simpleBackbone.addLiteral(lit);
            backboneVariables.add(lit.variable());
        }
    }
    final List<Formula> simplified = new ArrayList<>();
    for (final Formula formula : formulas) {
        final Formula restrict = formula.restrict(simpleBackbone);
        if (restrict.type() != FType.TRUE) {
            simplified.add(restrict);
        }
    }
    return new SimplificationResult(simplified, backboneVariables);
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList)

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