Search in sources :

Example 1 with SATHandler

use of org.logicng.handlers.SATHandler in project LogicNG by logic-ng.

the class IncWBO method normalSearch.

@Override
protected MaxSATResult normalSearch() {
    final Tristate unsatResult = unsatSearch();
    if (unsatResult == UNDEF) {
        return MaxSATResult.UNDEF;
    } else if (unsatResult == FALSE) {
        return MaxSATResult.UNSATISFIABLE;
    }
    initAssumptions(this.assumptions);
    this.solver = rebuildSolver();
    this.incSoft.growTo(nSoft(), false);
    while (true) {
        this.assumptions.clear();
        for (int i = 0; i < this.incSoft.size(); i++) {
            if (!this.incSoft.get(i)) {
                this.assumptions.push(not(this.softClauses.get(i).assumptionVar()));
            }
        }
        final SATHandler satHandler = satHandler();
        final Tristate res = searchSATSolver(this.solver, satHandler, this.assumptions);
        if (aborted(satHandler)) {
            return MaxSATResult.UNDEF;
        } else if (res == FALSE) {
            this.nbCores++;
            assert this.solver.conflict().size() > 0;
            final int coreCost = computeCostCore(this.solver.conflict());
            this.lbCost += coreCost;
            if (this.verbosity != Verbosity.NONE) {
                this.output.printf("c LB : %d CS : %d W : %d%n", this.lbCost, this.solver.conflict().size(), coreCost);
            }
            if (this.lbCost == this.ubCost) {
                if (this.verbosity != Verbosity.NONE) {
                    this.output.println("c LB = UB");
                }
                return MaxSATResult.OPTIMUM;
            }
            if (!foundLowerBound(this.lbCost, null)) {
                return MaxSATResult.UNDEF;
            }
            this.relaxCore(this.solver.conflict(), coreCost);
        } else {
            this.nbSatisfiable++;
            this.ubCost = this.incComputeCostModel(this.solver.model());
            assert this.lbCost == this.ubCost;
            if (this.verbosity != Verbosity.NONE) {
                this.output.println("o " + this.lbCost);
            }
            saveModel(this.solver.model());
            return MaxSATResult.OPTIMUM;
        }
    }
}
Also used : SATHandler(org.logicng.handlers.SATHandler) Tristate(org.logicng.datastructures.Tristate)

Example 2 with SATHandler

use of org.logicng.handlers.SATHandler 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 3 with SATHandler

use of org.logicng.handlers.SATHandler in project LogicNG by logic-ng.

the class LinearSU method normalSearch.

protected MaxSATResult normalSearch() {
    Tristate res;
    this.initRelaxation();
    this.solver = this.rebuildSolver(1);
    while (true) {
        final SATHandler satHandler = satHandler();
        res = searchSATSolver(this.solver, satHandler);
        if (aborted(satHandler)) {
            return MaxSATResult.UNDEF;
        } else if (res == Tristate.TRUE) {
            this.nbSatisfiable++;
            final int newCost = computeCostModel(this.solver.model(), Integer.MAX_VALUE);
            saveModel(this.solver.model());
            if (this.verbosity != Verbosity.NONE) {
                this.output.println("o " + newCost);
            }
            if (newCost == 0) {
                this.ubCost = newCost;
                return MaxSATResult.OPTIMUM;
            } else {
                if (this.problemType == ProblemType.WEIGHTED) {
                    if (!this.encoder.hasPBEncoding()) {
                        this.encoder.encodePB(this.solver, this.objFunction, this.coeffs, newCost - 1);
                    } else {
                        this.encoder.updatePB(this.solver, newCost - 1);
                    }
                } else {
                    if (!this.encoder.hasCardEncoding()) {
                        this.encoder.encodeCardinality(this.solver, this.objFunction, newCost - 1);
                    } else {
                        this.encoder.updateCardinality(this.solver, newCost - 1);
                    }
                }
                this.ubCost = newCost;
                if (!foundUpperBound(this.ubCost, null)) {
                    return MaxSATResult.UNDEF;
                }
            }
        } else {
            this.nbCores++;
            if (this.model.size() == 0) {
                assert this.nbSatisfiable == 0;
                return MaxSATResult.UNSATISFIABLE;
            } else {
                return MaxSATResult.OPTIMUM;
            }
        }
    }
}
Also used : SATHandler(org.logicng.handlers.SATHandler) Tristate(org.logicng.datastructures.Tristate)

Example 4 with SATHandler

use of org.logicng.handlers.SATHandler in project LogicNG by logic-ng.

the class MSU3 method none.

protected MaxSATResult none() {
    this.nbInitialVariables = nVars();
    Tristate res;
    this.initRelaxation();
    this.solver = this.rebuildSolver();
    final LNGIntVector assumptions = new LNGIntVector();
    final LNGIntVector currentObjFunction = new LNGIntVector();
    this.encoder.setIncremental(IncrementalStrategy.NONE);
    this.activeSoft.growTo(nSoft(), false);
    for (int i = 0; i < nSoft(); i++) {
        this.coreMapping.put(this.softClauses.get(i).assumptionVar(), i);
    }
    while (true) {
        final SATHandler satHandler = satHandler();
        res = searchSATSolver(this.solver, satHandler, assumptions);
        if (aborted(satHandler)) {
            return MaxSATResult.UNDEF;
        } else if (res == Tristate.TRUE) {
            this.nbSatisfiable++;
            final int newCost = computeCostModel(this.solver.model(), Integer.MAX_VALUE);
            saveModel(this.solver.model());
            if (this.verbosity != Verbosity.NONE) {
                this.output.println("o " + newCost);
            }
            this.ubCost = newCost;
            if (this.nbSatisfiable == 1) {
                if (!foundUpperBound(this.ubCost, null)) {
                    return MaxSATResult.UNDEF;
                }
                for (int i = 0; i < this.objFunction.size(); i++) {
                    assumptions.push(not(this.objFunction.get(i)));
                }
            } else {
                return MaxSATResult.OPTIMUM;
            }
        } else {
            this.lbCost++;
            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();
            for (int i = 0; i < this.solver.conflict().size(); i++) {
                assert !this.activeSoft.get(this.coreMapping.get(this.solver.conflict().get(i)));
                this.activeSoft.set(this.coreMapping.get(this.solver.conflict().get(i)), true);
            }
            currentObjFunction.clear();
            assumptions.clear();
            for (int i = 0; i < nSoft(); i++) {
                if (this.activeSoft.get(i)) {
                    currentObjFunction.push(this.softClauses.get(i).relaxationVars().get(0));
                } else {
                    assumptions.push(not(this.softClauses.get(i).assumptionVar()));
                }
            }
            if (this.verbosity != Verbosity.NONE) {
                this.output.printf("c Relaxed soft clauses %d / %d%n", currentObjFunction.size(), this.objFunction.size());
            }
            this.solver = this.rebuildSolver();
            this.encoder.encodeCardinality(this.solver, currentObjFunction, this.lbCost);
        }
    }
}
Also used : SATHandler(org.logicng.handlers.SATHandler) Tristate(org.logicng.datastructures.Tristate) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 5 with SATHandler

use of org.logicng.handlers.SATHandler in project LogicNG by logic-ng.

the class WBO method normalSearch.

protected MaxSATResult normalSearch() {
    final Tristate unsatResult = this.unsatSearch();
    if (unsatResult == UNDEF) {
        return MaxSATResult.UNDEF;
    } else if (unsatResult == FALSE) {
        return MaxSATResult.UNSATISFIABLE;
    }
    this.initAssumptions(this.assumptions);
    this.solver = this.rebuildSolver();
    while (true) {
        final SATHandler satHandler = satHandler();
        final Tristate res = searchSATSolver(this.solver, satHandler, this.assumptions);
        if (aborted(satHandler)) {
            return MaxSATResult.UNDEF;
        } else if (res == FALSE) {
            this.nbCores++;
            assert this.solver.conflict().size() > 0;
            final int coreCost = this.computeCostCore(this.solver.conflict());
            this.lbCost += coreCost;
            if (this.verbosity != Verbosity.NONE) {
                this.output.printf("c LB : %d CS : %d W : %d%n", this.lbCost, this.solver.conflict().size(), coreCost);
            }
            if (this.lbCost == this.ubCost) {
                if (this.verbosity != Verbosity.NONE) {
                    this.output.println("c LB = UB");
                }
                return MaxSATResult.OPTIMUM;
            } else if (!foundLowerBound(this.lbCost, null)) {
                return MaxSATResult.UNDEF;
            }
            this.relaxCore(this.solver.conflict(), coreCost, this.assumptions);
            this.solver = this.rebuildSolver();
        } else {
            this.nbSatisfiable++;
            this.ubCost = computeCostModel(this.solver.model(), Integer.MAX_VALUE);
            assert this.lbCost == this.ubCost;
            if (this.verbosity != Verbosity.NONE) {
                this.output.println("o " + this.lbCost);
            }
            saveModel(this.solver.model());
            return MaxSATResult.OPTIMUM;
        }
    }
}
Also used : SATHandler(org.logicng.handlers.SATHandler) Tristate(org.logicng.datastructures.Tristate)

Aggregations

SATHandler (org.logicng.handlers.SATHandler)19 Tristate (org.logicng.datastructures.Tristate)14 LNGIntVector (org.logicng.collections.LNGIntVector)7 Test (org.junit.jupiter.api.Test)5 BoundedSatHandler (org.logicng.handlers.BoundedSatHandler)5 Formula (org.logicng.formulas.Formula)3 LNGVector (org.logicng.collections.LNGVector)2 StandardProposition (org.logicng.propositions.StandardProposition)2 LNGBooleanVector (org.logicng.collections.LNGBooleanVector)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 Encoder (org.logicng.solvers.maxsat.encodings.Encoder)1