Search in sources :

Example 1 with LNGIntVector

use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.

the class PBConstraint method normalize.

/**
 * Normalizes this constraint s.t. it can be converted to CNF.
 * @return the normalized constraint
 */
public Formula normalize() {
    final LNGVector<Literal> normPs = new LNGVector<>(this.literals.length);
    final LNGIntVector normCs = new LNGIntVector(this.literals.length);
    int normRhs;
    switch(this.comparator) {
        case EQ:
            for (int i = 0; i < this.literals.length; i++) {
                normPs.push(this.literals[i]);
                normCs.push(this.coefficients[i]);
            }
            normRhs = this.rhs;
            final Formula f1 = this.normalize(normPs, normCs, normRhs);
            normPs.clear();
            normCs.clear();
            for (int i = 0; i < this.literals.length; i++) {
                normPs.push(this.literals[i]);
                normCs.push(-this.coefficients[i]);
            }
            normRhs = -this.rhs;
            final Formula f2 = this.normalize(normPs, normCs, normRhs);
            return this.f.and(f1, f2);
        case LT:
        case LE:
            for (int i = 0; i < this.literals.length; i++) {
                normPs.push(this.literals[i]);
                normCs.push(this.coefficients[i]);
            }
            normRhs = this.comparator == CType.LE ? this.rhs : this.rhs - 1;
            return this.normalize(normPs, normCs, normRhs);
        case GT:
        case GE:
            for (int i = 0; i < this.literals.length; i++) {
                normPs.push(this.literals[i]);
                normCs.push(-this.coefficients[i]);
            }
            normRhs = this.comparator == CType.GE ? -this.rhs : -this.rhs - 1;
            return this.normalize(normPs, normCs, normRhs);
        default:
            throw new IllegalStateException("Unknown pseudo-Boolean comparator: " + this.comparator);
    }
}
Also used : LNGVector(org.logicng.collections.LNGVector) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 2 with LNGIntVector

use of org.logicng.collections.LNGIntVector 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 LNGVector<Literal> literals) {
    if (this.miniSat == null) {
        this.result.add(this.vec2clause(literals));
    } else {
        final LNGIntVector clauseVec = new LNGIntVector(literals.size());
        for (final Literal l : literals) {
            addLiteral(clauseVec, l);
        }
        this.miniSat.underlyingSolver().addClause(clauseVec, this.proposition);
        this.miniSat.setSolverToUndef();
    }
}
Also used : Literal(org.logicng.formulas.Literal) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 3 with LNGIntVector

use of org.logicng.collections.LNGIntVector 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 4 with LNGIntVector

use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.

the class DnnfMiniSatStyleSolver method handleConflict.

protected void handleConflict(final MSClause conflict) {
    if (decisionLevel() > 0) {
        this.lastLearnt = new LNGIntVector();
        analyze(conflict, this.lastLearnt);
        this.assertionLevel = this.analyzeBtLevel;
    } else {
        // solver unsat
        cancelUntil(0);
        this.lastLearnt = null;
        this.assertionLevel = -1;
    }
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Example 5 with LNGIntVector

use of org.logicng.collections.LNGIntVector 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

LNGIntVector (org.logicng.collections.LNGIntVector)79 Tristate (org.logicng.datastructures.Tristate)10 Literal (org.logicng.formulas.Literal)8 MSClause (org.logicng.solvers.datastructures.MSClause)8 LNGBooleanVector (org.logicng.collections.LNGBooleanVector)7 SATHandler (org.logicng.handlers.SATHandler)7 MiniSatStyleSolver (org.logicng.solvers.sat.MiniSatStyleSolver)7 LNGVector (org.logicng.collections.LNGVector)6 Test (org.junit.jupiter.api.Test)5 Formula (org.logicng.formulas.Formula)4 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)3 PBConstraint (org.logicng.formulas.PBConstraint)3 MSVariable (org.logicng.solvers.datastructures.MSVariable)3 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 Pair (org.logicng.util.Pair)2 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1