use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class UpZeroLiteralsFunction method apply.
@Override
public SortedSet<Literal> apply(final MiniSat solver, final Consumer<Tristate> resultSetter) {
if (solver.getResult() == UNDEF) {
throw new IllegalStateException("Cannot get unit propagated literals on level 0 as long as the formula is not solved. Call 'sat' first.");
}
if (solver.getResult() == FALSE) {
return null;
}
final LNGIntVector literals = solver.underlyingSolver().upZeroLiterals();
final SortedSet<Literal> upZeroLiterals = new TreeSet<>();
for (int i = 0; i < literals.size(); ++i) {
final int lit = literals.get(i);
upZeroLiterals.add(solver.factory().literal(solver.underlyingSolver().nameForIdx(MiniSatStyleSolver.var(lit)), !MiniSatStyleSolver.sign(lit)));
}
return upZeroLiterals;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class Term method translateToFormula.
/**
* Translates this term to a formula for a given variable ordering
* @param varOrder the variable ordering
* @return the translation of this term to a formula
*/
Formula translateToFormula(final List<Variable> varOrder) {
final FormulaFactory f = varOrder.get(0).factory();
assert this.bits.length == varOrder.size();
final List<Literal> operands = new ArrayList<>(varOrder.size());
for (int i = 0; i < this.bits.length; i++) {
if (this.bits[i] != Tristate.UNDEF) {
operands.add(this.bits[i] == Tristate.TRUE ? varOrder.get(i) : varOrder.get(i).negate());
}
}
return f.and(operands);
}
use of org.logicng.formulas.Literal 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.Literal in project LogicNG by logic-ng.
the class MiniSat method generateClauseVector.
/**
* Generates a clause vector of a collection of literals.
* @param literals the literals
* @return the clause vector
*/
protected LNGIntVector generateClauseVector(final Collection<? extends Literal> literals) {
final LNGIntVector clauseVec = new LNGIntVector(literals.size());
for (final Literal lit : literals) {
final int index = getOrAddIndex(lit);
final int litNum = lit.phase() ? index * 2 : (index * 2) ^ 1;
clauseVec.push(litNum);
}
return clauseVec;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PlaistedGreenbaumTransformation method pgVariable.
/**
* Returns the auxiliary variable for a given formula. Either the formula is already a variable, has already an
* auxiliary variable or a new one is generated.
* @param formula the formula
* @return the old or new auxiliary variable
*/
private static Literal pgVariable(final Formula formula) {
if (formula.type() == FType.LITERAL) {
return (Literal) formula;
}
Literal var = (Literal) formula.transformationCacheEntry(PLAISTED_GREENBAUM_VARIABLE);
if (var == null) {
var = formula.factory().newCNFVariable();
formula.setTransformationCacheEntry(PLAISTED_GREENBAUM_VARIABLE, var);
}
return var;
}
Aggregations