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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations