Search in sources :

Example 51 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class AdvancedSimplifier method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    start(this.handler);
    final FormulaFactory f = formula.factory();
    final Backbone backbone = BackboneGeneration.compute(Collections.singletonList(formula), formula.variables(), BackboneType.POSITIVE_AND_NEGATIVE, satHandler(this.handler));
    if (backbone == null || aborted(this.handler)) {
        return null;
    }
    if (!backbone.isSat()) {
        return f.falsum();
    }
    final SortedSet<Literal> backboneLiterals = backbone.getCompleteBackbone();
    final Formula restrictedFormula = formula.restrict(new Assignment(backboneLiterals));
    final PrimeResult primeResult = PrimeCompiler.getWithMinimization().compute(restrictedFormula, PrimeResult.CoverageType.IMPLICANTS_COMPLETE, this.handler);
    if (primeResult == null || aborted(this.handler)) {
        return null;
    }
    final List<SortedSet<Literal>> primeImplicants = primeResult.getPrimeImplicants();
    final List<Formula> minimizedPIs = SmusComputation.computeSmusForFormulas(negateAllLiterals(primeImplicants, f), Collections.singletonList(restrictedFormula), f, this.handler);
    if (minimizedPIs == null || aborted(this.handler)) {
        return null;
    }
    final Formula minDnf = f.or(negateAllLiteralsInFormulas(minimizedPIs, f).stream().map(f::and).collect(Collectors.toList()));
    final Formula fullFactor = minDnf.transform(new FactorOutSimplifier(this.ratingFunction));
    return f.and(f.and(backboneLiterals), fullFactor).transform(new NegationSimplifier());
}
Also used : Backbone(org.logicng.backbones.Backbone) SortedSet(java.util.SortedSet) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Literal(org.logicng.formulas.Literal) PrimeResult(org.logicng.primecomputation.PrimeResult)

Example 52 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class BackboneSimplifier method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    final SATSolver solver = MiniSat.miniSat(formula.factory());
    solver.add(formula);
    final Backbone backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
    if (!backbone.isSat()) {
        return formula.factory().falsum();
    }
    if (!backbone.getNegativeBackbone().isEmpty() || !backbone.getPositiveBackbone().isEmpty()) {
        final Formula backboneFormula = backbone.toFormula(formula.factory());
        final Assignment assignment = new Assignment(backbone.getCompleteBackbone());
        final Formula restrictedFormula = formula.restrict(assignment);
        return formula.factory().and(backboneFormula, restrictedFormula);
    } else {
        return formula;
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Backbone(org.logicng.backbones.Backbone) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula)

Example 53 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class PlaistedGreenbaumTransformationSolver method handleNary.

private LNGIntVector handleNary(final Formula formula, final boolean polarity, final Proposition proposition, final boolean topLevel) {
    final boolean skipPg = topLevel || formula.type() == FType.AND && !polarity || formula.type() == FType.OR && polarity;
    final Pair<Boolean, Integer> pgVarResult = skipPg ? new Pair<>(false, null) : getPgVar(formula, polarity);
    if (pgVarResult.first()) {
        return polarity ? vector(pgVarResult.second()) : vector(pgVarResult.second() ^ 1);
    }
    final int pgVar = skipPg ? -1 : pgVarResult.second();
    switch(formula.type()) {
        case AND:
            {
                if (polarity) {
                    // pg => (v1 & ... & vk) = (~pg | v1) & ... & (~pg | vk)
                    for (final Formula op : formula) {
                        final LNGIntVector opPgVars = computeTransformation(op, true, proposition, topLevel);
                        if (topLevel) {
                            if (opPgVars != null) {
                                this.solver.addClause(opPgVars, proposition);
                            }
                        } else {
                            this.solver.addClause(vector(pgVar ^ 1, opPgVars), proposition);
                        }
                    }
                    if (topLevel) {
                        return null;
                    }
                } else {
                    // (v1 & ... & vk) => pg = ~v1 | ... | ~vk | pg
                    // Speed-Up: Skip pg var
                    final LNGIntVector singleClause = new LNGIntVector();
                    for (final Formula op : formula) {
                        final LNGIntVector opPgVars = computeTransformation(op, false, proposition, false);
                        for (int i = 0; i < opPgVars.size(); i++) {
                            singleClause.push(opPgVars.get(i));
                        }
                    }
                    return singleClause;
                }
                break;
            }
        case OR:
            {
                if (polarity) {
                    // pg => (v1 | ... | vk) = ~pg | v1 | ... | vk
                    // Speed-Up: Skip pg var
                    final LNGIntVector singleClause = new LNGIntVector();
                    for (final Formula op : formula) {
                        final LNGIntVector opPgVars = computeTransformation(op, true, proposition, false);
                        for (int i = 0; i < opPgVars.size(); i++) {
                            singleClause.push(opPgVars.get(i));
                        }
                    }
                    return singleClause;
                } else {
                    // (v1 | ... | vk) => pg = (~v1 | pg) & ... & (~vk | pg)
                    for (final Formula op : formula) {
                        final LNGIntVector opPgVars = computeTransformation(op, false, proposition, topLevel);
                        if (topLevel) {
                            if (opPgVars != null) {
                                this.solver.addClause(opPgVars, proposition);
                            }
                        } else {
                            this.solver.addClause(vector(pgVar, opPgVars), proposition);
                        }
                    }
                    if (topLevel) {
                        return null;
                    }
                }
                break;
            }
        default:
            throw new IllegalArgumentException("Unexpected type: " + formula.type());
    }
    return polarity ? vector(pgVar) : vector(pgVar ^ 1);
}
Also used : Formula(org.logicng.formulas.Formula) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 54 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class DNFFactorization method distribute.

/**
 * Computes the distribution (factorization) of two formulas.
 * @param f1 the first formula
 * @param f2 the second formula
 * @return the distribution of the two formulas
 */
private Formula distribute(final Formula f1, final Formula f2) {
    if (this.handler != null) {
        this.proceed = this.handler.performedDistribution();
    }
    if (this.proceed) {
        final FormulaFactory f = f1.factory();
        if (f1.type() == FType.OR || f2.type() == FType.OR) {
            final LinkedHashSet<Formula> nops = new LinkedHashSet<>();
            for (final Formula op : f1.type() == FType.OR ? f1 : f2) {
                final Formula distribute = this.distribute(op, f1.type() == FType.OR ? f2 : f1);
                if (!this.proceed) {
                    return null;
                }
                nops.add(distribute);
            }
            return f.or(nops);
        }
        final Formula clause = f.and(f1, f2);
        if (this.handler != null) {
            this.proceed = this.handler.createdClause(clause);
        }
        return clause;
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory)

Example 55 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class CNFEncoder method singleAdvancedEncoding.

protected Formula singleAdvancedEncoding(final Formula formula) {
    Formula result = formula.transform(this.advancedFactorization);
    if (result == null) {
        switch(this.config().fallbackAlgorithmForAdvancedEncoding) {
            case TSEITIN:
                if (this.tseitin == null || this.currentAtomBoundary != this.config().atomBoundary) {
                    this.currentAtomBoundary = this.config().atomBoundary;
                    this.tseitin = new TseitinTransformation(this.config().atomBoundary);
                }
                result = formula.transform(this.tseitin);
                break;
            case PLAISTED_GREENBAUM:
                if (this.plaistedGreenbaum == null || this.currentAtomBoundary != this.config().atomBoundary) {
                    this.currentAtomBoundary = this.config().atomBoundary;
                    this.plaistedGreenbaum = new PlaistedGreenbaumTransformation(this.config().atomBoundary);
                }
                result = formula.transform(this.plaistedGreenbaum);
                break;
            default:
                throw new IllegalStateException("Invalid fallback CNF encoding algorithm: " + this.config().fallbackAlgorithmForAdvancedEncoding);
        }
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16