Search in sources :

Example 1 with EncodingResult

use of org.logicng.datastructures.EncodingResult in project LogicNG by logic-ng.

the class CCEncoder method encodeIncremental.

/**
 * Encodes an incremental cardinality constraint and returns its encoding.
 * @param cc the cardinality constraint
 * @return the encoding of the constraint and the incremental data
 */
public Pair<List<Formula>, CCIncrementalData> encodeIncremental(final CardinalityConstraint cc) {
    final EncodingResult result = EncodingResult.resultForFormula(this.f);
    final CCIncrementalData incData = this.encodeIncremental(cc, result);
    return new Pair<>(Collections.unmodifiableList(result.result()), incData);
}
Also used : EncodingResult(org.logicng.datastructures.EncodingResult) Pair(org.logicng.util.Pair)

Example 2 with EncodingResult

use of org.logicng.datastructures.EncodingResult 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));
            }
        }
    }
}
Also used : Literal(org.logicng.formulas.Literal) EncodingResult(org.logicng.datastructures.EncodingResult) LNGVector(org.logicng.collections.LNGVector)

Example 3 with EncodingResult

use of org.logicng.datastructures.EncodingResult in project LogicNG by logic-ng.

the class PureExpansionTransformation method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    final FormulaFactory f = formula.factory();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
            return formula;
        case NOT:
            final Not not = (Not) formula;
            return f.not(apply(not.operand(), cache));
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            final List<Formula> newOps = new ArrayList<>(nary.numberOfOperands());
            for (final Formula op : nary) {
                newOps.add(apply(op, cache));
            }
            return f.naryOperator(formula.type(), newOps);
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            final Formula newLeft = apply(binary.left(), cache);
            final Formula newRight = apply(binary.right(), cache);
            return f.binaryOperator(formula.type(), newLeft, newRight);
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            if (pbc.isAmo() || pbc.isExo()) {
                final EncodingResult encodingResult = EncodingResult.resultForFormula(f);
                final Variable[] vars = literalsAsVariables(pbc.operands());
                this.amoEncoder.build(encodingResult, vars);
                final List<Formula> encoding = encodingResult.result();
                if (pbc.isExo()) {
                    encoding.add(f.or(vars));
                }
                return f.and(encoding);
            } else {
                throw new UnsupportedOperationException("Pure encoding for a PBC of type other than AMO or EXO is currently not supported.");
            }
        default:
            throw new IllegalStateException("Unknown formula type: " + formula.type());
    }
}
Also used : Variable(org.logicng.formulas.Variable) ArrayList(java.util.ArrayList) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Not(org.logicng.formulas.Not) EncodingResult(org.logicng.datastructures.EncodingResult) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 4 with EncodingResult

use of org.logicng.datastructures.EncodingResult in project LogicNG by logic-ng.

the class MiniSat method add.

@Override
public void add(final Formula formula, final Proposition proposition) {
    this.result = UNDEF;
    if (formula.type() == FType.PBC) {
        final PBConstraint constraint = (PBConstraint) formula;
        if (constraint.isCC()) {
            if (this.style == SolverStyle.MINICARD) {
                if (constraint.comparator() == CType.LE) {
                    ((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs());
                } else if (constraint.comparator() == CType.LT && constraint.rhs() > 3) {
                    ((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs() - 1);
                } else if (constraint.comparator() == CType.EQ && constraint.rhs() == 1) {
                    ((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs());
                    this.solver.addClause(generateClauseVector(Arrays.asList(constraint.operands())), proposition);
                } else {
                    addFormulaAsCNF(constraint, proposition);
                }
            } else {
                final EncodingResult result = EncodingResult.resultForMiniSat(this.f, this, proposition);
                this.ccEncoder.encode((CardinalityConstraint) constraint, result);
            }
        } else {
            addFormulaAsCNF(constraint, proposition);
        }
    } else {
        addFormulaAsCNF(formula, proposition);
    }
}
Also used : MiniCard(org.logicng.solvers.sat.MiniCard) EncodingResult(org.logicng.datastructures.EncodingResult) PBConstraint(org.logicng.formulas.PBConstraint)

Example 5 with EncodingResult

use of org.logicng.datastructures.EncodingResult in project LogicNG by logic-ng.

the class CCEncoder method encode.

/**
 * Encodes a cardinality constraint and returns its CNF encoding.
 * @param cc the cardinality constraint
 * @return the CNF encoding of the cardinality constraint
 */
public List<Formula> encode(final CardinalityConstraint cc) {
    final EncodingResult result = EncodingResult.resultForFormula(this.f);
    this.encodeConstraint(cc, result);
    return Collections.unmodifiableList(result.result());
}
Also used : EncodingResult(org.logicng.datastructures.EncodingResult)

Aggregations

EncodingResult (org.logicng.datastructures.EncodingResult)5 PBConstraint (org.logicng.formulas.PBConstraint)2 ArrayList (java.util.ArrayList)1 LNGVector (org.logicng.collections.LNGVector)1 BinaryOperator (org.logicng.formulas.BinaryOperator)1 Formula (org.logicng.formulas.Formula)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 Literal (org.logicng.formulas.Literal)1 NAryOperator (org.logicng.formulas.NAryOperator)1 Not (org.logicng.formulas.Not)1 Variable (org.logicng.formulas.Variable)1 MiniCard (org.logicng.solvers.sat.MiniCard)1 Pair (org.logicng.util.Pair)1