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;
}
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);
}
}
Aggregations