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