use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class DTreeLeaf method initialize.
@Override
public void initialize(final DnnfSatSolver solver) {
this.solver = solver;
final SortedSet<Literal> lits = this.clause.literals();
final int size = lits.size();
this.staticVarSet = new BitSet();
this.staticVariables = new int[size];
this.literals = new int[size];
int i = 0;
for (final Literal literal : lits) {
final int var = solver.variableIndex(literal);
this.staticVarSet.set(var);
this.staticVariables[i] = var;
this.literals[i] = MiniSatStyleSolver.mkLit(var, !literal.phase());
i++;
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class EvaluatesToConstantPredicate method innerTest.
/**
* Restricts and possibly simplifies the formula by applying the (partial) assignment in order to decide if the restriction yields to the specified constant.
* @param formula the formula
* @param topLevel indicator if the formula is the top level operator
* @return Falsum resp. Verum if the (partial) assignment resulted not to the specified constant, otherwise the restricted and simplified formula
*/
private Formula innerTest(final Formula formula, final boolean topLevel) {
final FormulaFactory f = formula.factory();
switch(formula.type()) {
case TRUE:
case FALSE:
return formula;
case LITERAL:
final Literal lit = (Literal) formula;
final Boolean found = this.mapping.get(lit.variable());
return found == null ? lit : f.constant(lit.phase() == found);
case NOT:
return handleNot((Not) formula, topLevel);
case IMPL:
return handleImplication((Implication) formula, topLevel);
case EQUIV:
return handleEquivalence((Equivalence) formula, topLevel);
case OR:
return handleOr((Or) formula, topLevel);
case AND:
return handleAnd((And) formula, topLevel);
case PBC:
return handlePBC((PBConstraint) formula);
default:
throw new IllegalArgumentException("Unknown formula type " + formula.type());
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class NaivePrimeReduction method reduceImplicant.
/**
* Computes a prime implicant from the given implicant for the given formula.
* Assumption: Given implicant is a satisfying assignment for the formula
* @param implicant the implicant
* @param handler a SAT handler for the underlying SAT Solver
* @return a prime implicant or null if the computation was aborted by the handler
*/
public SortedSet<Literal> reduceImplicant(final SortedSet<Literal> implicant, final SATHandler handler) {
start(handler);
final SortedSet<Literal> primeImplicant = new TreeSet<>(implicant);
for (final Literal lit : implicant) {
primeImplicant.remove(lit);
final boolean sat = this.implicantSolver.sat(handler, primeImplicant) == Tristate.TRUE;
if (aborted(handler)) {
return null;
}
if (sat) {
primeImplicant.add(lit);
}
}
return primeImplicant;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PrimeCompiler method compute.
/**
* Computes prime implicants and prime implicates for a given formula.
* The coverage type specifies if the implicants or the implicates will
* be complete, the other one will still be a cover of the given formula.
* <p>
* The prime compiler can be called with an {@link OptimizationHandler}.
* The given handler instance will be used for every subsequent
* {@link org.logicng.solvers.functions.OptimizationFunction} call and
* the handler's SAT handler is used for every subsequent SAT call.
* @param formula the formula
* @param type the coverage type
* @param handler an optimization handler, can be {@code null}
* @return the prime result or null if the computation was aborted by the handler
*/
public PrimeResult compute(final Formula formula, final PrimeResult.CoverageType type, final OptimizationHandler handler) {
start(handler);
final boolean completeImplicants = type == PrimeResult.CoverageType.IMPLICANTS_COMPLETE;
final Formula formulaForComputation = completeImplicants ? formula : formula.negate();
final Pair<List<SortedSet<Literal>>, List<SortedSet<Literal>>> result = computeGeneric(formulaForComputation, handler);
if (result == null || aborted(handler)) {
return null;
}
return new PrimeResult(completeImplicants ? result.first() : negateAll(result.second()), completeImplicants ? result.second() : negateAll(result.first()), type);
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class ModelCounter method simplify.
private static SimplificationResult simplify(final Collection<Formula> formulas) {
final Assignment simpleBackbone = new Assignment();
final SortedSet<Variable> backboneVariables = new TreeSet<>();
for (final Formula formula : formulas) {
if (formula.type() == FType.LITERAL) {
final Literal lit = (Literal) formula;
simpleBackbone.addLiteral(lit);
backboneVariables.add(lit.variable());
}
}
final List<Formula> simplified = new ArrayList<>();
for (final Formula formula : formulas) {
final Formula restrict = formula.restrict(simpleBackbone);
if (restrict.type() != FType.TRUE) {
simplified.add(restrict);
}
}
return new SimplificationResult(simplified, backboneVariables);
}
Aggregations