Search in sources :

Example 16 with LNGVector

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

the class PBAdderNetworks method encode.

@Override
public List<Formula> encode(final LNGVector<Literal> lits, final LNGIntVector coeffs, final int rhs, final List<Formula> formula) {
    this.formula = formula;
    final LNGVector<Literal> result = new LNGVector<>();
    final LNGVector<LinkedList<Literal>> buckets = new LNGVector<>();
    final int nb = ldInt(rhs);
    for (int iBit = 0; iBit < nb; ++iBit) {
        buckets.push(new LinkedList<>());
        result.push(null);
        for (int iVar = 0; iVar < lits.size(); iVar++) {
            if (((1 << iBit) & coeffs.get(iVar)) != 0) {
                buckets.back().push(lits.get(iVar));
            }
        }
    }
    this.adderTree(buckets, result);
    final LNGBooleanVector kBits = this.numToBits(buckets.size(), rhs);
    this.lessThanOrEqual(result, kBits, formula);
    return formula;
}
Also used : Literal(org.logicng.formulas.Literal) LNGBooleanVector(org.logicng.collections.LNGBooleanVector) LNGVector(org.logicng.collections.LNGVector) LinkedList(java.util.LinkedList)

Example 17 with LNGVector

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

the class PBEncoder method encode.

/**
 * Builds a pseudo Boolean constraint of the form {@code c_1 * lit_1 + c_2 * lit_2 + ... + c_n * lit_n >= k}.
 * @param lits   the literals {@code lit_1 ... lit_n}
 * @param coeffs the coefficients {@code c_1 ... c_n}
 * @param rhs    the right hand side {@code k} of the constraint
 * @return the CNF encoding of the pseudo Boolean constraint
 * @throws IllegalArgumentException if the right hand side of the cardinality constraint is negative or
 *                                  larger than the number of literals
 */
protected List<Formula> encode(final Literal[] lits, final int[] coeffs, final int rhs) {
    if (rhs == Integer.MAX_VALUE) {
        throw new IllegalArgumentException("Overflow in the Encoding");
    }
    if (rhs < 0) {
        return Collections.singletonList(this.f.falsum());
    }
    final LNGVector<Literal> simplifiedLits = new LNGVector<>();
    final LNGIntVector simplifiedCoeffs = new LNGIntVector();
    final List<Formula> result = new ArrayList<>();
    if (rhs == 0) {
        for (final Literal lit : lits) {
            result.add(lit.negate());
        }
        return result;
    }
    for (int i = 0; i < lits.length; i++) {
        if (coeffs[i] <= rhs) {
            simplifiedLits.push(lits[i]);
            simplifiedCoeffs.push(coeffs[i]);
        } else {
            result.add(lits[i].negate());
        }
    }
    if (simplifiedLits.size() <= 1) {
        return result;
    }
    switch(this.config().pbEncoder) {
        case SWC:
        case BEST:
            if (this.swc == null) {
                this.swc = new PBSWC(this.f);
            }
            return this.swc.encode(simplifiedLits, simplifiedCoeffs, rhs, result);
        case BINARY_MERGE:
            return new PBBinaryMerge(this.f, this.config()).encode(simplifiedLits, simplifiedCoeffs, rhs, result);
        case ADDER_NETWORKS:
            if (this.adderNetworks == null) {
                this.adderNetworks = new PBAdderNetworks(this.f);
            }
            return this.adderNetworks.encode(simplifiedLits, simplifiedCoeffs, rhs, result);
        default:
            throw new IllegalStateException("Unknown pseudo-Boolean encoder: " + this.config().pbEncoder);
    }
}
Also used : ArrayList(java.util.ArrayList) LNGVector(org.logicng.collections.LNGVector) LNGIntVector(org.logicng.collections.LNGIntVector) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal)

Aggregations

LNGVector (org.logicng.collections.LNGVector)17 Literal (org.logicng.formulas.Literal)12 Variable (org.logicng.formulas.Variable)8 LNGIntVector (org.logicng.collections.LNGIntVector)6 LNGBooleanVector (org.logicng.collections.LNGBooleanVector)2 Tristate (org.logicng.datastructures.Tristate)2 Formula (org.logicng.formulas.Formula)2 SATHandler (org.logicng.handlers.SATHandler)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 EncodingResult (org.logicng.datastructures.EncodingResult)1 UNSATCore (org.logicng.explanations.UNSATCore)1 DRUPTrim (org.logicng.explanations.drup.DRUPTrim)1 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)1 PBConstraint (org.logicng.formulas.PBConstraint)1 Proposition (org.logicng.propositions.Proposition)1 StandardProposition (org.logicng.propositions.StandardProposition)1 Encoder (org.logicng.solvers.maxsat.encodings.Encoder)1