Search in sources :

Example 26 with LNGIntVector

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

the class MSU3 method rebuildSolver.

protected MiniSatStyleSolver rebuildSolver() {
    final MiniSatStyleSolver s = newSATSolver();
    for (int i = 0; i < nVars(); i++) {
        newSATVariable(s);
    }
    for (int i = 0; i < nHard(); i++) {
        s.addClause(this.hardClauses.get(i).clause(), null);
    }
    LNGIntVector clause;
    for (int i = 0; i < nSoft(); i++) {
        clause = new LNGIntVector(this.softClauses.get(i).clause());
        for (int j = 0; j < this.softClauses.get(i).relaxationVars().size(); j++) {
            clause.push(this.softClauses.get(i).relaxationVars().get(j));
        }
        s.addClause(clause, null);
    }
    return s;
}
Also used : MiniSatStyleSolver(org.logicng.solvers.sat.MiniSatStyleSolver) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 27 with LNGIntVector

use of org.logicng.collections.LNGIntVector 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 28 with LNGIntVector

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

the class WBO method rebuildWeightSolver.

protected MiniSatStyleSolver rebuildWeightSolver(final WeightStrategy strategy) {
    assert strategy == WeightStrategy.NORMAL || strategy == WeightStrategy.DIVERSIFY;
    final MiniSatStyleSolver s = newSATSolver();
    for (int i = 0; i < nVars(); i++) {
        newSATVariable(s);
    }
    for (int i = 0; i < nHard(); i++) {
        s.addClause(this.hardClauses.get(i).clause(), null);
    }
    if (this.symmetryStrategy) {
        this.symmetryBreaking();
    }
    LNGIntVector clause = new LNGIntVector();
    this.nbCurrentSoft = 0;
    for (int i = 0; i < nSoft(); i++) {
        if (this.softClauses.get(i).weight() >= this.currentWeight) {
            this.nbCurrentSoft++;
            clause.clear();
            clause = new LNGIntVector(this.softClauses.get(i).clause());
            for (int j = 0; j < this.softClauses.get(i).relaxationVars().size(); j++) {
                clause.push(this.softClauses.get(i).relaxationVars().get(j));
            }
            clause.push(this.softClauses.get(i).assumptionVar());
            s.addClause(clause, null);
        }
    }
    return s;
}
Also used : MiniSatStyleSolver(org.logicng.solvers.sat.MiniSatStyleSolver) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 29 with LNGIntVector

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

the class WBO method initSymmetry.

void initSymmetry() {
    for (int i = 0; i < nSoft(); i++) {
        this.softMapping.push(new LNGIntVector());
        this.relaxationMapping.push(new LNGIntVector());
    }
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Example 30 with LNGIntVector

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

the class PlaistedGreenbaumTransformationSolver method vector.

private static LNGIntVector vector(final int elt, final LNGIntVector a) {
    final LNGIntVector result = new LNGIntVector(a.size() + 1);
    result.unsafePush(elt);
    for (int i = 0; i < a.size(); i++) {
        result.unsafePush(a.get(i));
    }
    return result;
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Aggregations

LNGIntVector (org.logicng.collections.LNGIntVector)79 Tristate (org.logicng.datastructures.Tristate)10 Literal (org.logicng.formulas.Literal)8 MSClause (org.logicng.solvers.datastructures.MSClause)8 LNGBooleanVector (org.logicng.collections.LNGBooleanVector)7 SATHandler (org.logicng.handlers.SATHandler)7 MiniSatStyleSolver (org.logicng.solvers.sat.MiniSatStyleSolver)7 LNGVector (org.logicng.collections.LNGVector)6 Test (org.junit.jupiter.api.Test)5 Formula (org.logicng.formulas.Formula)4 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)3 PBConstraint (org.logicng.formulas.PBConstraint)3 MSVariable (org.logicng.solvers.datastructures.MSVariable)3 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 Pair (org.logicng.util.Pair)2 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1