Search in sources :

Example 21 with Literal

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++;
    }
}
Also used : Literal(org.logicng.formulas.Literal) BitSet(java.util.BitSet)

Example 22 with Literal

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());
    }
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Literal(org.logicng.formulas.Literal)

Example 23 with Literal

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;
}
Also used : TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal)

Example 24 with Literal

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);
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with Literal

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);
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList)

Aggregations

Literal (org.logicng.formulas.Literal)115 Formula (org.logicng.formulas.Formula)51 ArrayList (java.util.ArrayList)38 Test (org.junit.jupiter.api.Test)32 Variable (org.logicng.formulas.Variable)29 FormulaFactory (org.logicng.formulas.FormulaFactory)25 PBConstraint (org.logicng.formulas.PBConstraint)21 TreeSet (java.util.TreeSet)17 SATSolver (org.logicng.solvers.SATSolver)14 Assignment (org.logicng.datastructures.Assignment)13 LNGVector (org.logicng.collections.LNGVector)12 HashMap (java.util.HashMap)11 LogicNGTest (org.logicng.LogicNGTest)11 LNGIntVector (org.logicng.collections.LNGIntVector)8 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)8 PropositionalParser (org.logicng.io.parsers.PropositionalParser)8 BufferedReader (java.io.BufferedReader)7 FileReader (java.io.FileReader)7 BinaryOperator (org.logicng.formulas.BinaryOperator)7 Not (org.logicng.formulas.Not)7