use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class DFSOrdering method dfs.
private void dfs(final Formula formula, final LinkedHashSet<Variable> variables) {
switch(formula.type()) {
case LITERAL:
variables.add(((Literal) formula).variable());
break;
case NOT:
dfs(((Not) formula).operand(), variables);
break;
case IMPL:
case EQUIV:
final BinaryOperator op = (BinaryOperator) formula;
dfs(op.left(), variables);
dfs(op.right(), variables);
break;
case AND:
case OR:
for (final Formula o : formula) {
dfs(o, variables);
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
for (final Literal lit : pbc.operands()) {
variables.add(lit.variable());
}
break;
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PBAdderNetworks method faCarry.
private Literal faCarry(final Literal a, final Literal b, final Literal c) {
final Literal x = this.f.newPBVariable();
this.formula.add(this.f.clause(b, c, x.negate()));
this.formula.add(this.f.clause(a, c, x.negate()));
this.formula.add(this.f.clause(a, b, x.negate()));
this.formula.add(this.f.clause(b.negate(), c.negate(), x));
this.formula.add(this.f.clause(a.negate(), c.negate(), x));
this.formula.add(this.f.clause(a.negate(), b.negate(), x));
return x;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PBBinaryMerge method binary_merge.
private void binary_merge(final LNGVector<Literal> literals, final LNGIntVector coefficients, final int leq, final int maxWeight, final int n, final List<Formula> formula, final Literal gac_lit) {
final int less_then = leq + 1;
final int p = (int) Math.floor(Math.log(maxWeight) / Math.log(2));
final int m = (int) Math.ceil(less_then / Math.pow(2, p));
final int new_less_then = (int) (m * Math.pow(2, p));
final int T = (int) ((m * Math.pow(2, p)) - less_then);
final Literal true_lit = this.f.newPBVariable();
formula.add(true_lit);
final LNGVector<LNGVector<Literal>> buckets = new LNGVector<>();
int bit = 1;
for (int i = 0; i <= p; i++) {
buckets.push(new LNGVector<>());
if ((T & bit) != 0) {
buckets.get(i).push(true_lit);
}
for (int j = 0; j < n; j++) {
if ((coefficients.get(j) & bit) != 0) {
if (gac_lit != null && coefficients.get(j) >= less_then) {
formula.add(this.f.clause(gac_lit, literals.get(j).negate()));
} else {
buckets.get(i).push(literals.get(j));
}
}
}
bit = bit << 1;
}
final LNGVector<LNGVector<Literal>> bucket_card = new LNGVector<>(p + 1);
final LNGVector<LNGVector<Literal>> bucket_merge = new LNGVector<>(p + 1);
for (int i = 0; i < p + 1; i++) {
bucket_card.push(new LNGVector<>());
bucket_merge.push(new LNGVector<>());
}
assert bucket_card.size() == buckets.size();
final LNGVector<Literal> carries = new LNGVector<>();
final EncodingResult tempResul = EncodingResult.resultForFormula(this.f);
for (int i = 0; i < buckets.size(); i++) {
final int k = (int) Math.ceil(new_less_then / Math.pow(2, i));
if (this.config.binaryMergeUseWatchDog) {
totalizer(buckets.get(i), bucket_card.get(i), formula);
} else {
this.sorting.sort(k, buckets.get(i), tempResul, bucket_card.get(i), INPUT_TO_OUTPUT);
formula.addAll(tempResul.result());
}
if (k <= buckets.get(i).size()) {
assert k == bucket_card.get(i).size() || this.config.binaryMergeUseWatchDog;
if (gac_lit != null) {
formula.add(this.f.clause(gac_lit, bucket_card.get(i).get(k - 1).negate()));
} else {
formula.add(this.f.clause(bucket_card.get(i).get(k - 1).negate()));
}
}
if (i > 0) {
if (carries.size() > 0) {
if (bucket_card.get(i).size() == 0) {
bucket_merge.set(i, carries);
} else {
if (this.config.binaryMergeUseWatchDog) {
unary_adder(bucket_card.get(i), carries, bucket_merge.get(i), formula);
} else {
this.sorting.merge(k, bucket_card.get(i), carries, tempResul, bucket_merge.get(i), INPUT_TO_OUTPUT);
formula.addAll(tempResul.result());
}
if (k == bucket_merge.get(i).size() || (this.config.binaryMergeUseWatchDog && k <= bucket_merge.get(i).size())) {
if (gac_lit != null) {
formula.add(this.f.clause(gac_lit, bucket_merge.get(i).get(k - 1).negate()));
} else {
formula.add(this.f.clause(bucket_merge.get(i).get(k - 1).negate()));
}
}
}
} else {
bucket_merge.get(i).replaceInplace(bucket_card.get(i));
}
}
carries.clear();
if (i == 0) {
for (int j = 1; j < bucket_card.get(0).size(); j = j + 2) {
carries.push(bucket_card.get(0).get(j));
}
} else {
for (int j = 1; j < bucket_merge.get(i).size(); j = j + 2) {
carries.push(bucket_merge.get(i).get(j));
}
}
}
}
use of org.logicng.formulas.Literal 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;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class Subsumption method generateSubsumedUBTree.
/**
* Generates a UBTree from the formulas operands (clauses in CNF, minterms in DNF)
* where all subsumed operands are already deleted.
* @param formula the formula (must be an n-ary operator and CNF or DNF)
* @return the UBTree with the operands and deleted subsumed operands
*/
protected static UBTree<Literal> generateSubsumedUBTree(final Formula formula) {
final SortedMap<Integer, List<SortedSet<Literal>>> mapping = new TreeMap<>();
for (final Formula term : formula) {
mapping.computeIfAbsent(term.literals().size(), k -> new ArrayList<>()).add(term.literals());
}
final UBTree<Literal> ubTree = new UBTree<>();
for (final Map.Entry<Integer, List<SortedSet<Literal>>> entry : mapping.entrySet()) {
for (final SortedSet<Literal> set : entry.getValue()) {
if (ubTree.firstSubset(set) == null) {
ubTree.addSet(set);
}
}
}
return ubTree;
}
Aggregations