use of org.logicng.formulas.Literal 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.Literal 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.Literal 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);
}
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class VariablesFunction method apply.
@Override
public SortedSet<Variable> apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(VARIABLES);
if (cached != null) {
return (SortedSet<Variable>) cached;
}
SortedSet<Variable> result = new TreeSet<>();
switch(formula.type()) {
case FALSE:
case TRUE:
result = new TreeSet<>();
break;
case LITERAL:
final Literal lit = (Literal) formula;
result.add(lit.variable());
break;
case NOT:
final Not not = (Not) formula;
result = apply(not.operand(), cache);
break;
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
result.addAll(apply(binary.left(), cache));
result.addAll(apply(binary.right(), cache));
break;
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
for (final Formula op : nary) {
result.addAll(apply(op, cache));
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
result = FormulaHelper.variables(pbc.literals());
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
result = Collections.unmodifiableSortedSet(result);
if (cache) {
formula.setFunctionCacheEntry(VARIABLES, result);
}
return result;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class CCCardinalityNetworks method buildALK.
void buildALK(final EncodingResult result, final Variable[] vars, final int rhs) {
result.reset();
final LNGVector<Literal> input = new LNGVector<>();
final LNGVector<Literal> output = new LNGVector<>();
final int newRHS = vars.length - rhs;
if (newRHS > vars.length / 2) {
final int geq = vars.length - newRHS;
for (final Variable v : vars) {
input.push(v);
}
this.sorting.sort(geq, input, result, output, OUTPUT_TO_INPUT);
for (int i = 0; i < geq; i++) {
result.addClause(output.get(i));
}
} else {
for (final Variable v : vars) {
input.push(v.negate());
}
this.sorting.sort(newRHS + 1, input, result, output, INPUT_TO_OUTPUT);
assert output.size() > newRHS;
result.addClause(output.get(newRHS).negate());
}
}
Aggregations