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