Search in sources :

Example 6 with LNGIntVector

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

the class PBBinaryMerge method encode.

@Override
public List<Formula> encode(final LNGVector<Literal> lts, final LNGIntVector cffs, final int rhs, final List<Formula> formula) {
    final LNGVector<Literal> lits = new LNGVector<>(lts.size());
    for (final Literal lit : lts) {
        lits.push(lit);
    }
    final LNGIntVector coeffs = new LNGIntVector(cffs);
    final int maxWeight = maxWeight(coeffs);
    if (!this.config.binaryMergeUseGAC) {
        binary_merge(lts, new LNGIntVector(cffs), rhs, maxWeight, lits.size(), formula, null);
    } else {
        Literal x;
        boolean encode_complete_constraint = false;
        for (int i = 0; i < lits.size(); i++) {
            if (this.config.binaryMergeNoSupportForSingleBit && Double.compare(Math.floor(Math.log(coeffs.get(i)) / Math.log(2)), Math.log(coeffs.get(i)) / Math.log(2)) == 0) {
                encode_complete_constraint = true;
                continue;
            }
            final Literal tmpLit = lits.get(i);
            final int tmpCoeff = coeffs.get(i);
            lits.set(i, lits.back());
            coeffs.set(i, coeffs.back());
            lits.pop();
            coeffs.pop();
            x = tmpLit;
            if (maxWeight == tmpCoeff) {
                int mw = 0;
                for (int j = 0; j < coeffs.size(); j++) {
                    mw = Math.max(mw, coeffs.get(j));
                }
                if (rhs - tmpCoeff <= 0) {
                    for (int j = 0; j < lits.size(); j++) {
                        formula.add(this.f.clause(x.negate(), lits.get(j).negate()));
                    }
                } else {
                    binary_merge(lits, coeffs, rhs - tmpCoeff, mw, lits.size(), formula, x.negate());
                }
            } else {
                if (rhs - tmpCoeff <= 0) {
                    for (int j = 0; j < lits.size(); j++) {
                        formula.add(this.f.clause(x.negate(), lits.get(j).negate()));
                    }
                }
                binary_merge(lits, coeffs, rhs - tmpCoeff, maxWeight, lits.size(), formula, x.negate());
            }
            if (i < lits.size()) {
                lits.push(lits.get(i));
                lits.set(i, tmpLit);
                coeffs.push(coeffs.get(i));
                coeffs.set(i, tmpCoeff);
            }
        }
        if (this.config.binaryMergeNoSupportForSingleBit && encode_complete_constraint) {
            binary_merge(lts, new LNGIntVector(cffs), rhs, maxWeight, lits.size(), formula, null);
        }
    }
    return formula;
}
Also used : Literal(org.logicng.formulas.Literal) LNGVector(org.logicng.collections.LNGVector) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 7 with LNGIntVector

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

the class SequentialWeightCounter method encode.

/**
 * Incremental construction of the SWC encoding.
 * @param s           the solver
 * @param lits        the literals of the constraint
 * @param coeffs      the coefficients of the constraint
 * @param rhs         the right hand size of the constraint
 * @param assumptions the current assumptions
 * @param size        the size
 */
public void encode(final MiniSatStyleSolver s, final LNGIntVector lits, final LNGIntVector coeffs, final int rhs, final LNGIntVector assumptions, final int size) {
    if (rhs == Integer.MAX_VALUE) {
        throw new IllegalArgumentException("Overflow in the encoding.");
    }
    this.hasEncoding = false;
    final LNGIntVector simpLits = new LNGIntVector(lits);
    final LNGIntVector simpCoeffs = new LNGIntVector(coeffs);
    lits.clear();
    coeffs.clear();
    final LNGIntVector simpUnitLits = new LNGIntVector(this.unitLits);
    final LNGIntVector simpUnitCoeffs = new LNGIntVector(this.unitCoeffs);
    this.unitLits.clear();
    this.unitCoeffs.clear();
    for (int i = 0; i < simpUnitLits.size(); i++) {
        if (simpUnitCoeffs.get(i) <= rhs) {
            lits.push(simpUnitLits.get(i));
            coeffs.push(simpUnitCoeffs.get(i));
        } else {
            this.unitLits.push(simpUnitLits.get(i));
            this.unitCoeffs.push(simpUnitCoeffs.get(i));
        }
    }
    for (int i = 0; i < simpLits.size(); i++) {
        if (simpCoeffs.get(i) <= rhs) {
            lits.push(simpLits.get(i));
            coeffs.push(simpCoeffs.get(i));
        } else {
            this.unitLits.push(simpLits.get(i));
            this.unitCoeffs.push(simpCoeffs.get(i));
        }
    }
    if (lits.size() == 1) {
        for (int i = 0; i < this.unitLits.size(); i++) {
            assumptions.push(not(this.unitLits.get(i)));
        }
        this.unitLits.push(lits.get(0));
        this.unitCoeffs.push(coeffs.get(0));
        return;
    }
    if (lits.size() == 0) {
        for (int i = 0; i < this.unitLits.size(); i++) {
            assumptions.push(not(this.unitLits.get(i)));
        }
        return;
    }
    final int n = lits.size();
    this.seqAuxiliaryInc = new LNGVector<>(size + 1);
    for (int i = 0; i <= n; i++) {
        this.seqAuxiliaryInc.set(i, new LNGIntVector());
        this.seqAuxiliaryInc.get(i).growTo(rhs + 1, -1);
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= rhs; ++j) {
            this.seqAuxiliaryInc.get(i).set(j, mkLit(s.nVars(), false));
            newSATVariable(s);
        }
    }
    final int blocking = mkLit(s.nVars(), false);
    newSATVariable(s);
    this.currentLitBlocking = blocking;
    assumptions.push(not(blocking));
    for (int i = 1; i <= n; i++) {
        final int wi = coeffs.get(i - 1);
        assert rhs >= wi;
        for (int j = 1; j <= rhs; j++) {
            if (i >= 2 && i <= n && j <= rhs) {
                addBinaryClause(s, not(this.seqAuxiliaryInc.get(i - 1).get(j)), this.seqAuxiliaryInc.get(i).get(j));
            }
            if (i <= n && j <= wi) {
                addBinaryClause(s, not(lits.get(i - 1)), this.seqAuxiliaryInc.get(i).get(j));
            }
            if (i >= 2 && i <= n && j <= rhs - wi) {
                addTernaryClause(s, not(this.seqAuxiliaryInc.get(i - 1).get(j)), not(lits.get(i - 1)), this.seqAuxiliaryInc.get(i).get(j + wi));
            }
        }
        if (i >= 2) {
            addBinaryClause(s, not(this.seqAuxiliaryInc.get(i - 1).get(rhs + 1 - wi)), not(lits.get(i - 1)), blocking);
        }
    }
    for (int i = 0; i < this.unitLits.size(); i++) {
        assumptions.push(not(this.unitLits.get(i)));
    }
    this.currentPbRhs = rhs;
    this.hasEncoding = true;
    this.litsInc = new LNGIntVector(lits);
    this.coeffsInc = new LNGIntVector(coeffs);
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Example 8 with LNGIntVector

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

the class Totalizer method toCNF.

protected void toCNF(final MiniSatStyleSolver s, final LNGIntVector lits) {
    final LNGIntVector left = new LNGIntVector();
    final LNGIntVector right = new LNGIntVector();
    assert lits.size() > 1;
    final int split = lits.size() / 2;
    for (int i = 0; i < lits.size(); i++) {
        if (i < split) {
            if (split == 1) {
                assert this.cardinalityInlits.size() > 0;
                left.push(this.cardinalityInlits.back());
                this.cardinalityInlits.pop();
            } else {
                final int p = mkLit(s.nVars(), false);
                MaxSAT.newSATVariable(s);
                left.push(p);
            }
        } else {
            if (lits.size() - split == 1) {
                assert this.cardinalityInlits.size() > 0;
                right.push(this.cardinalityInlits.back());
                this.cardinalityInlits.pop();
            } else {
                final int p = mkLit(s.nVars(), false);
                MaxSAT.newSATVariable(s);
                right.push(p);
            }
        }
    }
    this.adder(s, left, right, lits);
    if (left.size() > 1) {
        this.toCNF(s, left);
    }
    if (right.size() > 1) {
        this.toCNF(s, right);
    }
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Example 9 with LNGIntVector

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

the class Totalizer method build.

/**
 * Builds the cardinality constraint.
 * @param s    the solver
 * @param lits the literals of the constraint
 * @param rhs  the right hand side of the constraint
 */
public void build(final MiniSatStyleSolver s, final LNGIntVector lits, final int rhs) {
    this.cardinalityOutlits.clear();
    this.hasEncoding = false;
    if (rhs == 0) {
        for (int i = 0; i < lits.size(); i++) {
            addUnitClause(s, not(lits.get(i)));
        }
        return;
    }
    assert rhs >= 1 && rhs <= lits.size();
    if (this.incrementalStrategy == MaxSATConfig.IncrementalStrategy.NONE && rhs == lits.size()) {
        return;
    }
    if (rhs == lits.size() && !this.joinMode) {
        return;
    }
    for (int i = 0; i < lits.size(); i++) {
        final int p = mkLit(s.nVars(), false);
        MaxSAT.newSATVariable(s);
        this.cardinalityOutlits.push(p);
    }
    this.cardinalityInlits = new LNGIntVector(lits);
    this.currentCardinalityRhs = rhs;
    this.toCNF(s, this.cardinalityOutlits);
    assert this.cardinalityInlits.size() == 0;
    if (!this.joinMode) {
        this.joinMode = true;
    }
    this.hasEncoding = true;
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Example 10 with LNGIntVector

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

the class GlucoseSyrup method removeClause.

@Override
protected void removeClause(final MSClause c) {
    if (this.config.proofGeneration) {
        final LNGIntVector vec = new LNGIntVector(c.size());
        vec.push(-1);
        for (int i = 0; i < c.size(); i++) {
            vec.push((var(c.get(i)) + 1) * (-2 * (sign(c.get(i)) ? 1 : 0) + 1));
        }
        this.pgProof.push(vec);
    }
    detachClause(c);
    if (locked(c)) {
        v(c.get(0)).setReason(null);
    }
}
Also used : 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