Search in sources :

Example 11 with LNGVector

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

the class LinearSU method bmoSearch.

protected MaxSATResult bmoSearch() {
    assert this.orderWeights.size() > 0;
    Tristate res;
    this.initRelaxation();
    int currentWeight = this.orderWeights.get(0);
    final int minWeight = this.orderWeights.get(this.orderWeights.size() - 1);
    int posWeight = 0;
    final LNGVector<LNGIntVector> functions = new LNGVector<>();
    final LNGIntVector weights = new LNGIntVector();
    this.solver = this.rebuildBMO(functions, weights, currentWeight);
    int localCost = 0;
    this.ubCost = 0;
    while (true) {
        final SATHandler satHandler = satHandler();
        res = searchSATSolver(this.solver, satHandler);
        if (aborted(satHandler)) {
            return MaxSATResult.UNDEF;
        }
        if (res == Tristate.TRUE) {
            this.nbSatisfiable++;
            final int newCost = computeCostModel(this.solver.model(), currentWeight);
            if (currentWeight == minWeight) {
                saveModel(this.solver.model());
                if (this.verbosity != Verbosity.NONE) {
                    this.output.println("o " + (newCost + this.lbCost));
                }
                this.ubCost = newCost + this.lbCost;
                if (newCost > 0 && !foundUpperBound(this.ubCost, null)) {
                    return MaxSATResult.UNDEF;
                }
            } else if (this.verbosity != Verbosity.NONE) {
                this.output.printf("c BMO-UB : %d (Function %d/%d)%n", newCost, posWeight + 1, this.orderWeights.size());
            }
            if (newCost == 0 && currentWeight == minWeight) {
                return MaxSATResult.OPTIMUM;
            } else {
                if (newCost == 0) {
                    functions.push(new LNGIntVector(this.objFunction));
                    localCost = newCost;
                    weights.push(localCost / currentWeight);
                    posWeight++;
                    currentWeight = this.orderWeights.get(posWeight);
                    localCost = 0;
                    this.solver = this.rebuildBMO(functions, weights, currentWeight);
                    if (this.verbosity != Verbosity.NONE) {
                        this.output.println("c LB : " + this.lbCost);
                    }
                } else {
                    if (localCost == 0) {
                        this.encoder.encodeCardinality(this.solver, this.objFunction, newCost / currentWeight - 1);
                    } else {
                        this.encoder.updateCardinality(this.solver, newCost / currentWeight - 1);
                    }
                    localCost = newCost;
                }
            }
        } else {
            this.nbCores++;
            if (currentWeight == minWeight) {
                if (this.model.size() == 0) {
                    assert this.nbSatisfiable == 0;
                    return MaxSATResult.UNSATISFIABLE;
                } else {
                    return MaxSATResult.OPTIMUM;
                }
            } else {
                functions.push(new LNGIntVector(this.objFunction));
                weights.push(localCost / currentWeight);
                this.lbCost += localCost;
                posWeight++;
                currentWeight = this.orderWeights.get(posWeight);
                localCost = 0;
                if (!foundLowerBound(this.lbCost, null)) {
                    return MaxSATResult.UNDEF;
                }
                this.solver = this.rebuildBMO(functions, weights, currentWeight);
                if (this.verbosity != Verbosity.NONE) {
                    this.output.println("c LB : " + this.lbCost);
                }
            }
        }
    }
}
Also used : SATHandler(org.logicng.handlers.SATHandler) Tristate(org.logicng.datastructures.Tristate) LNGIntVector(org.logicng.collections.LNGIntVector) LNGVector(org.logicng.collections.LNGVector)

Example 12 with LNGVector

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

the class CCModularTotalizer method finalAdder.

private void finalAdder(final int mod, final LNGVector<Literal> upper, final LNGVector<Literal> lupper, final LNGVector<Literal> rupper, final Variable carry) {
    for (int i = 0; i <= lupper.size(); i++) {
        for (int j = 0; j <= rupper.size(); j++) {
            Literal a = this.varError;
            Literal b = this.varError;
            Literal c = this.varError;
            Literal d = this.varError;
            int closeMod = this.currentCardinalityRhs / mod;
            if (this.currentCardinalityRhs % mod != 0) {
                closeMod++;
            }
            if (mod * (i + j) > closeMod * mod) {
                continue;
            }
            if (i != 0) {
                a = lupper.get(i - 1);
            }
            if (j != 0) {
                b = rupper.get(j - 1);
            }
            if (i + j != 0 && i + j - 1 < upper.size()) {
                c = upper.get(i + j - 1);
            }
            if (i + j < upper.size()) {
                d = upper.get(i + j);
            }
            if (c != this.varUndef && c != this.varError) {
                final LNGVector<Literal> clause = new LNGVector<>();
                if (a != this.varUndef && a != this.varError) {
                    clause.push(a.negate());
                }
                if (b != this.varUndef && b != this.varError) {
                    clause.push(b.negate());
                }
                clause.push(c);
                if (clause.size() > 1) {
                    this.result.addClause(clause);
                }
            }
            final LNGVector<Literal> clause = new LNGVector<>();
            clause.push(carry.negate());
            if (a != this.varUndef && a != this.varError) {
                clause.push(a.negate());
            }
            if (b != this.varUndef && b != this.varError) {
                clause.push(b.negate());
            }
            if (d != this.varError && d != this.varUndef) {
                clause.push(d);
            }
            if (clause.size() > 1) {
                this.result.addClause(clause);
            }
        }
    }
}
Also used : Literal(org.logicng.formulas.Literal) LNGVector(org.logicng.collections.LNGVector)

Example 13 with LNGVector

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

the class CCSorting method recursiveMerger.

private void recursiveMerger(final int c, final LNGVector<Literal> inputA, final int a, final LNGVector<Literal> inputB, final int b, final EncodingResult formula, final LNGVector<Literal> output, final ImplicationDirection direction) {
    assert inputA.size() > 0;
    assert inputB.size() > 0;
    assert c > 0;
    output.clear();
    int a2 = a;
    int b2 = b;
    if (a2 > c) {
        a2 = c;
    }
    if (b2 > c) {
        b2 = c;
    }
    if (c == 1) {
        final Variable y = formula.newVariable();
        comparator(inputA.get(0), inputB.get(0), y, formula, direction);
        output.push(y);
        return;
    }
    if (a2 == 1 && b2 == 1) {
        assert c == 2;
        final Variable y1 = formula.newVariable();
        final Variable y2 = formula.newVariable();
        comparator(inputA.get(0), inputB.get(0), y1, y2, formula, direction);
        output.push(y1);
        output.push(y2);
        return;
    }
    final LNGVector<Literal> oddMerge = new LNGVector<>();
    final LNGVector<Literal> evenMerge = new LNGVector<>();
    final LNGVector<Literal> tmpLitsOddA = new LNGVector<>();
    final LNGVector<Literal> tmpLitsOddB = new LNGVector<>();
    final LNGVector<Literal> tmpLitsEvenA = new LNGVector<>();
    final LNGVector<Literal> tmpLitsEvenB = new LNGVector<>();
    for (int i = 0; i < a2; i = i + 2) {
        tmpLitsOddA.push(inputA.get(i));
    }
    for (int i = 0; i < b2; i = i + 2) {
        tmpLitsOddB.push(inputB.get(i));
    }
    for (int i = 1; i < a2; i = i + 2) {
        tmpLitsEvenA.push(inputA.get(i));
    }
    for (int i = 1; i < b2; i = i + 2) {
        tmpLitsEvenB.push(inputB.get(i));
    }
    merge(c / 2 + 1, tmpLitsOddA, tmpLitsOddB, formula, oddMerge, direction);
    merge(c / 2, tmpLitsEvenA, tmpLitsEvenB, formula, evenMerge, direction);
    assert oddMerge.size() > 0;
    output.push(oddMerge.get(0));
    int i = 1;
    int j = 0;
    while (true) {
        if (i < oddMerge.size() && j < evenMerge.size()) {
            if (output.size() + 2 <= c) {
                final Variable z0 = formula.newVariable();
                final Variable z1 = formula.newVariable();
                comparator(oddMerge.get(i), evenMerge.get(j), z0, z1, formula, direction);
                output.push(z0);
                output.push(z1);
                if (output.size() == c) {
                    break;
                }
            } else if (output.size() + 1 == c) {
                final Variable z0 = formula.newVariable();
                comparator(oddMerge.get(i), evenMerge.get(j), z0, formula, direction);
                output.push(z0);
                break;
            }
        } else if (i >= oddMerge.size() && j >= evenMerge.size()) {
            break;
        } else if (i >= oddMerge.size()) {
            assert j == evenMerge.size() - 1;
            output.push(evenMerge.back());
            break;
        } else {
            assert i == oddMerge.size() - 1;
            output.push(oddMerge.back());
            break;
        }
        i++;
        j++;
    }
    assert output.size() == a2 + b2 || output.size() == c;
}
Also used : Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) LNGVector(org.logicng.collections.LNGVector)

Example 14 with LNGVector

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

the class CCTotalizer method initializeConstraint.

/**
 * Initializes the constraint.
 * @param vars the variables
 * @return the auxiliary variables
 */
private LNGVector<Variable> initializeConstraint(final EncodingResult result, final Variable[] vars) {
    result.reset();
    this.result = result;
    this.cardinalityInvars = new LNGVector<>(vars.length);
    final LNGVector<Variable> cardinalityOutvars = new LNGVector<>(vars.length);
    for (final Variable var : vars) {
        this.cardinalityInvars.push(var);
        cardinalityOutvars.push(this.result.newVariable());
    }
    return cardinalityOutvars;
}
Also used : Variable(org.logicng.formulas.Variable) LNGVector(org.logicng.collections.LNGVector)

Example 15 with LNGVector

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

the class WMSU3 method iterativeBmo.

protected MaxSATResult iterativeBmo() {
    assert this.isBmo;
    this.nbInitialVariables = nVars();
    Tristate res;
    this.initRelaxation();
    this.solver = this.rebuildSolver();
    this.encoder.setIncremental(IncrementalStrategy.ITERATIVE);
    final LNGIntVector joinObjFunction = new LNGIntVector();
    final LNGIntVector encodingAssumptions = new LNGIntVector();
    final LNGIntVector joinCoeffs = new LNGIntVector();
    this.activeSoft.growTo(nSoft(), false);
    for (int i = 0; i < nSoft(); i++) {
        this.coreMapping.put(this.softClauses.get(i).assumptionVar(), i);
    }
    int minWeight = 0;
    int posWeight = 0;
    int localCost = 0;
    final LNGVector<LNGIntVector> functions = new LNGVector<>();
    final LNGIntVector weights = new LNGIntVector();
    final LNGVector<Encoder> bmoEncodings = new LNGVector<>();
    final LNGBooleanVector firstEncoding = new LNGBooleanVector();
    functions.push(new LNGIntVector());
    weights.push(0);
    assert this.objFunction.size() == 0;
    Encoder e = new Encoder(CardinalityEncoding.TOTALIZER);
    e.setIncremental(IncrementalStrategy.ITERATIVE);
    bmoEncodings.push(e);
    firstEncoding.push(true);
    while (true) {
        final SATHandler satHandler = satHandler();
        res = searchSATSolver(this.solver, satHandler, this.assumptions);
        if (aborted(satHandler)) {
            return MaxSATResult.UNDEF;
        } else if (res == TRUE) {
            this.nbSatisfiable++;
            final int newCost = computeCostModel(this.solver.model(), Integer.MAX_VALUE);
            if (newCost < this.ubCost || this.nbSatisfiable == 1) {
                saveModel(this.solver.model());
                if (this.verbosity != Verbosity.NONE) {
                    this.output.println("o " + newCost);
                }
                this.ubCost = newCost;
            }
            if (this.nbSatisfiable == 1) {
                if (this.ubCost == 0) {
                    return MaxSATResult.OPTIMUM;
                } else if (!foundUpperBound(this.ubCost, null)) {
                    return MaxSATResult.UNDEF;
                }
                assert this.orderWeights.size() > 0;
                assert this.orderWeights.get(0) > 1;
                minWeight = this.orderWeights.get(this.orderWeights.size() - 1);
                this.currentWeight = this.orderWeights.get(0);
                for (int i = 0; i < nSoft(); i++) {
                    if (this.softClauses.get(i).weight() >= this.currentWeight) {
                        this.assumptions.push(not(this.softClauses.get(i).assumptionVar()));
                    }
                }
            } else {
                if (this.currentWeight == 1 || this.currentWeight == minWeight) {
                    return MaxSATResult.OPTIMUM;
                } else {
                    if (!foundUpperBound(this.ubCost, null)) {
                        return MaxSATResult.UNDEF;
                    }
                    this.assumptions.clear();
                    final int previousWeight = this.currentWeight;
                    posWeight++;
                    assert posWeight < this.orderWeights.size();
                    this.currentWeight = this.orderWeights.get(posWeight);
                    if (this.objFunction.size() > 0) {
                        functions.set(functions.size() - 1, new LNGIntVector(this.objFunction));
                    }
                    functions.push(new LNGIntVector());
                    weights.push(0);
                    localCost = 0;
                    e = new Encoder(CardinalityEncoding.TOTALIZER);
                    e.setIncremental(IncrementalStrategy.ITERATIVE);
                    bmoEncodings.push(e);
                    firstEncoding.push(true);
                    for (int i = 0; i < encodingAssumptions.size(); i++) {
                        this.solver.addClause(encodingAssumptions.get(i), null);
                    }
                    encodingAssumptions.clear();
                    for (int i = 0; i < nSoft(); i++) {
                        if (!this.activeSoft.get(i) && previousWeight == this.softClauses.get(i).weight()) {
                            this.solver.addClause(not(this.softClauses.get(i).assumptionVar()), null);
                        }
                        if (this.currentWeight == this.softClauses.get(i).weight()) {
                            this.assumptions.push(not(this.softClauses.get(i).assumptionVar()));
                        }
                        if (this.activeSoft.get(i)) {
                            assert this.softClauses.get(i).weight() == previousWeight;
                            this.activeSoft.set(i, false);
                        }
                    }
                }
            }
        } else {
            localCost++;
            this.lbCost += this.currentWeight;
            this.nbCores++;
            if (this.verbosity != Verbosity.NONE) {
                this.output.println("c LB : " + this.lbCost);
            }
            if (this.nbSatisfiable == 0) {
                return MaxSATResult.UNSATISFIABLE;
            } else if (this.lbCost == this.ubCost) {
                assert this.nbSatisfiable > 0;
                if (this.verbosity != Verbosity.NONE) {
                    this.output.println("c LB = UB");
                }
                return MaxSATResult.OPTIMUM;
            } else if (!foundLowerBound(this.lbCost, null)) {
                return MaxSATResult.UNDEF;
            }
            this.sumSizeCores += this.solver.conflict().size();
            joinObjFunction.clear();
            joinCoeffs.clear();
            for (int i = 0; i < this.solver.conflict().size(); i++) {
                if (this.coreMapping.containsKey(this.solver.conflict().get(i))) {
                    if (this.activeSoft.get(this.coreMapping.get(this.solver.conflict().get(i)))) {
                        continue;
                    }
                    assert this.softClauses.get(this.coreMapping.get(this.solver.conflict().get(i))).weight() == this.currentWeight;
                    this.activeSoft.set(this.coreMapping.get(this.solver.conflict().get(i)), true);
                    joinObjFunction.push(this.softClauses.get(this.coreMapping.get(this.solver.conflict().get(i))).relaxationVars().get(0));
                    joinCoeffs.push(this.softClauses.get(this.coreMapping.get(this.solver.conflict().get(i))).weight());
                }
            }
            this.objFunction.clear();
            this.coeffs.clear();
            this.assumptions.clear();
            for (int i = 0; i < nSoft(); i++) {
                if (this.activeSoft.get(i)) {
                    assert this.softClauses.get(i).weight() == this.currentWeight;
                    this.objFunction.push(this.softClauses.get(i).relaxationVars().get(0));
                    this.coeffs.push(this.softClauses.get(i).weight());
                } else if (this.currentWeight == this.softClauses.get(i).weight()) {
                    this.assumptions.push(not(this.softClauses.get(i).assumptionVar()));
                }
            }
            if (this.verbosity != Verbosity.NONE) {
                this.output.printf("c Relaxed soft clauses %d / %d%n", this.objFunction.size(), nSoft());
            }
            assert posWeight < functions.size();
            functions.set(posWeight, new LNGIntVector(this.objFunction));
            weights.set(posWeight, localCost);
            if (firstEncoding.get(posWeight)) {
                if (weights.get(posWeight) != this.objFunction.size()) {
                    bmoEncodings.get(posWeight).buildCardinality(this.solver, this.objFunction, weights.get(posWeight));
                    joinObjFunction.clear();
                    bmoEncodings.get(posWeight).incUpdateCardinality(this.solver, joinObjFunction, this.objFunction, weights.get(posWeight), encodingAssumptions);
                    firstEncoding.set(posWeight, false);
                }
            } else {
                bmoEncodings.get(posWeight).incUpdateCardinality(this.solver, joinObjFunction, this.objFunction, weights.get(posWeight), encodingAssumptions);
            }
            for (int i = 0; i < encodingAssumptions.size(); i++) {
                this.assumptions.push(encodingAssumptions.get(i));
            }
        }
    }
}
Also used : SATHandler(org.logicng.handlers.SATHandler) Tristate(org.logicng.datastructures.Tristate) Encoder(org.logicng.solvers.maxsat.encodings.Encoder) LNGBooleanVector(org.logicng.collections.LNGBooleanVector) LNGIntVector(org.logicng.collections.LNGIntVector) LNGVector(org.logicng.collections.LNGVector)

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