Search in sources :

Example 31 with Literal

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

Example 32 with Literal

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

Example 33 with Literal

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());
}
Also used : Backbone(org.logicng.backbones.Backbone) SortedSet(java.util.SortedSet) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Literal(org.logicng.formulas.Literal) PrimeResult(org.logicng.primecomputation.PrimeResult)

Example 34 with Literal

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

Example 35 with Literal

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

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