use of org.logicng.transformations.LiteralSubstitution 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.transformations.LiteralSubstitution in project LogicNG by logic-ng.
the class PrimeCompiler method createSubstitution.
private SubstitutionResult createSubstitution(final Formula formula) {
final Map<Variable, Literal> newVar2oldLit = new HashMap<>();
final LiteralSubstitution substitution = new LiteralSubstitution();
final List<Formula> constraintOps = new ArrayList<>();
for (final Variable variable : formula.variables()) {
final Variable posVar = formula.factory().variable(variable.name() + POS);
newVar2oldLit.put(posVar, variable);
substitution.addSubstitution(variable, posVar);
final Variable negVar = formula.factory().variable(variable.name() + NEG);
newVar2oldLit.put(negVar, variable.negate());
substitution.addSubstitution(variable.negate(), negVar);
constraintOps.add(formula.factory().amo(posVar, negVar));
}
return new SubstitutionResult(newVar2oldLit, substitution, formula.factory().and(constraintOps));
}
Aggregations