Search in sources :

Example 16 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class EncodingResult method addClause.

/**
 * Adds a clause to the result
 * @param literals the literals of the clause
 */
public void addClause(final Literal... literals) {
    if (this.miniSat == null) {
        this.result.add(this.f.clause(literals));
    } else {
        final LNGIntVector clauseVec = new LNGIntVector(literals.length);
        for (final Literal literal : literals) {
            addLiteral(clauseVec, literal);
        }
        this.miniSat.underlyingSolver().addClause(clauseVec, this.proposition);
        this.miniSat.setSolverToUndef();
    }
}
Also used : Literal(org.logicng.formulas.Literal) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 17 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class CCAMONested method encodeIntern.

/**
 * Internal recursive encoding.
 * @param vars the variables of the constraint
 */
private void encodeIntern(final LNGVector<Literal> vars) {
    if (vars.size() <= this.groupSize) {
        for (int i = 0; i + 1 < vars.size(); i++) {
            for (int j = i + 1; j < vars.size(); j++) {
                this.result.addClause(vars.get(i).negate(), vars.get(j).negate());
            }
        }
    } else {
        final LNGVector<Literal> l1 = new LNGVector<>(vars.size() / 2);
        final LNGVector<Literal> l2 = new LNGVector<>(vars.size() / 2);
        int i = 0;
        for (; i < vars.size() / 2; i++) {
            l1.push(vars.get(i));
        }
        for (; i < vars.size(); i++) {
            l2.push(vars.get(i));
        }
        final Variable newVariable = this.result.newVariable();
        l1.push(newVariable);
        l2.push(newVariable.negate());
        this.encodeIntern(l1);
        this.encodeIntern(l2);
    }
}
Also used : Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) LNGVector(org.logicng.collections.LNGVector)

Example 18 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class DnnfCompiler method cnf2Ddnnf.

protected Formula cnf2Ddnnf(final DTree tree, final int currentShannons) throws TimeoutException {
    final BitSet separator = tree.dynamicSeparator();
    final Formula implied = this.newlyImpliedLiterals(tree.staticVarSet());
    if (separator.isEmpty()) {
        if (tree instanceof DTreeLeaf) {
            return this.f.and(implied, leaf2Ddnnf((DTreeLeaf) tree));
        } else {
            return conjoin(implied, (DTreeNode) tree, currentShannons);
        }
    } else {
        final int var = chooseShannonVariable(tree, separator, currentShannons);
        if (this.handler != null && !this.handler.shannonExpansion()) {
            throw new TimeoutException();
        }
        /* Positive branch */
        Formula positiveDnnf = this.f.falsum();
        if (this.solver.decide(var, true)) {
            positiveDnnf = cnf2Ddnnf(tree, currentShannons + 1);
        }
        this.solver.undoDecide(var);
        if (positiveDnnf == this.f.falsum()) {
            if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
                return cnf2Ddnnf(tree);
            } else {
                return this.f.falsum();
            }
        }
        /* Negative branch */
        Formula negativeDnnf = this.f.falsum();
        if (this.solver.decide(var, false)) {
            negativeDnnf = cnf2Ddnnf(tree, currentShannons + 1);
        }
        this.solver.undoDecide(var);
        if (negativeDnnf == this.f.falsum()) {
            if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
                return cnf2Ddnnf(tree);
            } else {
                return this.f.falsum();
            }
        }
        final Literal lit = this.solver.litForIdx(var);
        final Formula positiveBranch = this.f.and(lit, positiveDnnf);
        final Formula negativeBranch = this.f.and(lit.negate(), negativeDnnf);
        return this.f.and(implied, this.f.or(positiveBranch, negativeBranch));
    }
}
Also used : Formula(org.logicng.formulas.Formula) DTreeLeaf(org.logicng.knowledgecompilation.dnnf.datastructures.dtree.DTreeLeaf) Literal(org.logicng.formulas.Literal) BitSet(java.util.BitSet) TimeoutException(java.util.concurrent.TimeoutException)

Example 19 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class DnnfCompiler method leaf2Ddnnf.

protected Formula leaf2Ddnnf(final DTreeLeaf leaf) {
    final Iterator<Literal> literals = leaf.clause().literals().iterator();
    this.leafResultOperands.clear();
    this.leafCurrentLiterals.clear();
    Literal lit;
    int index = 0;
    while (literals.hasNext()) {
        lit = literals.next();
        switch(this.solver.valueOf(MiniSatStyleSolver.mkLit(this.solver.variableIndex(lit), !lit.phase()))) {
            case TRUE:
                return this.f.verum();
            case UNDEF:
                this.leafCurrentLiterals.add(lit);
                this.leafResultOperands.add(this.f.and(this.leafCurrentLiterals));
                this.leafCurrentLiterals.set(index, lit.negate());
                index++;
        }
    }
    return this.f.or(this.leafResultOperands);
}
Also used : Literal(org.logicng.formulas.Literal)

Example 20 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class DnnfMiniSatStyleSolver method generateClauseVector.

protected LNGIntVector generateClauseVector(final Collection<Literal> literals) {
    final LNGIntVector clauseVec = new LNGIntVector(literals.size());
    for (final Literal lit : literals) {
        int index = idxForName(lit.name());
        if (index == -1) {
            index = newVar(false, true);
            addName(lit.name(), index);
        }
        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)

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