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