Search in sources :

Example 46 with Formula

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

the class ExistentialQuantifierElimination method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    Formula result = formula;
    final FormulaFactory f = formula.factory();
    for (final Variable var : this.elimination) {
        result = f.or(result.restrict(new Assignment(var)), result.restrict(new Assignment(var.negate())));
    }
    return result;
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable)

Example 47 with Formula

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

the class UniversalQuantifierElimination method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    Formula result = formula;
    final FormulaFactory f = formula.factory();
    for (final Variable var : this.elimination) {
        result = f.and(result.restrict(new Assignment(var)), result.restrict(new Assignment(var.negate())));
    }
    return result;
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable)

Example 48 with Formula

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

the class QuineMcCluskeyAlgorithm method chooseSatBased.

/**
 * Choose a minimal set of prime implicants from the final prime implicant table of QMC.  This implementation uses
 * a MaxSAT formulation for this variant of the SET COVER problem which should be more efficient than e.g. Petrick's
 * method for all cases in which this Quine-McCluskey implementation yields a result in reasonable time.
 * @param table the final prime term table
 * @param f     the formula factory
 * @return the list of chosen prime implicants which are minimal wrt. to their number
 */
static List<Term> chooseSatBased(final TermTable table, final FormulaFactory f) {
    final LinkedHashMap<Variable, Term> var2Term = new LinkedHashMap<>();
    final LinkedHashMap<Formula, Variable> formula2VarMapping = new LinkedHashMap<>();
    final SATSolver satSolver = initializeSolver(table, f, var2Term, formula2VarMapping);
    if (satSolver.sat() == Tristate.FALSE) {
        throw new IllegalStateException("Solver must be satisfiable after adding the initial formula.");
    }
    return minimize(satSolver, var2Term, f);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) LinkedHashMap(java.util.LinkedHashMap)

Example 49 with Formula

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

the class QuineMcCluskeyAlgorithm method minimize.

/**
 * Performs the minimization via incremental cardinality constraints.
 * @param satSolver the SAT solver
 * @param var2Term  a mapping from selector variable to prime implicant
 * @param f         the formula factory
 * @return a minimal set of prime implicants to cover all minterms
 */
static List<Term> minimize(final SATSolver satSolver, final LinkedHashMap<Variable, Term> var2Term, final FormulaFactory f) {
    final Assignment initialModel = satSolver.model();
    List<Variable> currentTermVars = computeCurrentTermVars(initialModel, var2Term.keySet());
    if (currentTermVars.size() == 2) {
        satSolver.add(f.amo(var2Term.keySet()));
        if (satSolver.sat() == Tristate.TRUE) {
            currentTermVars = computeCurrentTermVars(satSolver.model(), var2Term.keySet());
        }
    } else {
        final Formula cc = f.cc(CType.LE, currentTermVars.size() - 1, var2Term.keySet());
        assert cc instanceof CardinalityConstraint;
        final CCIncrementalData incData = satSolver.addIncrementalCC((CardinalityConstraint) cc);
        while (satSolver.sat() == Tristate.TRUE) {
            currentTermVars = computeCurrentTermVars(satSolver.model(), var2Term.keySet());
            incData.newUpperBoundForSolver(currentTermVars.size() - 1);
        }
    }
    return computeTerms(currentTermVars, var2Term);
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) CCIncrementalData(org.logicng.cardinalityconstraints.CCIncrementalData) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint)

Example 50 with Formula

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

the class QuineMcCluskeyAlgorithm method computeFormula.

/**
 * Computes the formula for a given list of chosen terms and a variable order.
 * @param chosenTerms the chosen terms
 * @param varOrder    the variable order
 * @return the formula for this term list and variable ordering
 */
static Formula computeFormula(final List<Term> chosenTerms, final List<Variable> varOrder) {
    final FormulaFactory f = varOrder.get(0).factory();
    final List<Formula> operands = new ArrayList<>(chosenTerms.size());
    for (final Term term : chosenTerms) {
        operands.add(term.translateToFormula(varOrder));
    }
    return f.or(operands);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) ArrayList(java.util.ArrayList)

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