Search in sources :

Example 1 with LiteralSubstitution

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;
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) LiteralSubstitution(org.logicng.transformations.LiteralSubstitution) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal)

Example 2 with LiteralSubstitution

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));
}
Also used : Formula(org.logicng.formulas.Formula) LiteralSubstitution(org.logicng.transformations.LiteralSubstitution) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList)

Aggregations

HashMap (java.util.HashMap)2 Formula (org.logicng.formulas.Formula)2 Literal (org.logicng.formulas.Literal)2 Variable (org.logicng.formulas.Variable)2 LiteralSubstitution (org.logicng.transformations.LiteralSubstitution)2 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Assignment (org.logicng.datastructures.Assignment)1 SATSolver (org.logicng.solvers.SATSolver)1