Search in sources :

Example 11 with LNGIntVector

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

the class GlucoseSyrup method solve.

@Override
public Tristate solve(final SATHandler handler) {
    if (this.config.incremental && this.config.proofGeneration) {
        throw new IllegalStateException("Cannot use incremental and proof generation at the same time");
    }
    this.handler = handler;
    start(handler);
    this.model.clear();
    this.conflict.clear();
    if (!this.ok) {
        return Tristate.FALSE;
    }
    for (int i = 0; i < this.assumptions.size(); i++) {
        this.assump.set(var(this.assumptions.get(i)), !sign(this.assumptions.get(i)));
    }
    Tristate status = Tristate.UNDEF;
    while (status == Tristate.UNDEF && !this.canceledByHandler) {
        status = search();
    }
    if (this.config.proofGeneration && this.assumptions.empty()) {
        if (status == Tristate.FALSE) {
            this.pgProof.push(new LNGIntVector(1, 0));
        }
    }
    if (status == Tristate.TRUE) {
        this.model = new LNGBooleanVector(this.vars.size());
        for (final MSVariable v : this.vars) {
            this.model.push(v.assignment() == Tristate.TRUE);
        }
    } else if (status == Tristate.FALSE && this.conflict.size() == 0) {
        this.ok = false;
    }
    finishSolving(handler);
    cancelUntil(0);
    this.handler = null;
    this.canceledByHandler = false;
    for (int i = 0; i < this.assumptions.size(); i++) {
        this.assump.set(var(this.assumptions.get(i)), false);
    }
    return status;
}
Also used : MSVariable(org.logicng.solvers.datastructures.MSVariable) Tristate(org.logicng.datastructures.Tristate) LNGBooleanVector(org.logicng.collections.LNGBooleanVector) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 12 with LNGIntVector

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

the class MiniCard method simplifyClause.

/**
 * Minimizes a given learnt clause depending on the minimization method of the solver configuration.
 * @param outLearnt the learnt clause which should be minimized
 */
protected void simplifyClause(final LNGIntVector outLearnt) {
    int i;
    int j;
    this.analyzeToClear = new LNGIntVector(outLearnt);
    if (this.ccminMode == MiniSatConfig.ClauseMinimization.DEEP) {
        int abstractLevel = 0;
        for (i = 1; i < outLearnt.size(); i++) {
            abstractLevel |= abstractLevel(var(outLearnt.get(i)));
        }
        for (i = j = 1; i < outLearnt.size(); i++) {
            if (v(outLearnt.get(i)).reason() == null || !litRedundant(outLearnt.get(i), abstractLevel)) {
                outLearnt.set(j++, outLearnt.get(i));
            }
        }
    } else if (this.ccminMode == MiniSatConfig.ClauseMinimization.BASIC) {
        for (i = j = 1; i < outLearnt.size(); i++) {
            if (v(outLearnt.get(i)).reason() == null) {
                outLearnt.set(j++, outLearnt.get(i));
            } else {
                final MSClause c = v(outLearnt.get(i)).reason();
                assert !c.isAtMost();
                for (int k = 1; k < c.size(); k++) {
                    if (!this.seen.get(var(c.get(k))) && v(c.get(k)).level() > 0) {
                        outLearnt.set(j++, outLearnt.get(i));
                        break;
                    }
                }
            }
        }
    } else {
        i = j = outLearnt.size();
    }
    outLearnt.removeElements(i - j);
    this.analyzeBtLevel = 0;
    if (outLearnt.size() > 1) {
        int max = 1;
        for (int k = 2; k < outLearnt.size(); k++) {
            if (v(outLearnt.get(k)).level() > v(outLearnt.get(max)).level()) {
                max = k;
            }
        }
        final int p = outLearnt.get(max);
        outLearnt.set(max, outLearnt.get(1));
        outLearnt.set(1, p);
        this.analyzeBtLevel = v(p).level();
    }
    for (int l = 0; l < this.analyzeToClear.size(); l++) {
        this.seen.set(var(this.analyzeToClear.get(l)), false);
    }
}
Also used : MSClause(org.logicng.solvers.datastructures.MSClause) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 13 with LNGIntVector

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

the class MiniCard method initializeMiniSAT.

/**
 * Initializes the additional parameters.
 */
protected void initializeMiniSAT() {
    this.unitClauses = new LNGIntVector();
    this.learntsizeAdjustConfl = 0;
    this.learntsizeAdjustCnt = 0;
    this.learntsizeAdjustStartConfl = 100;
    this.learntsizeAdjustInc = 1.5;
    this.maxLearnts = 0;
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Example 14 with LNGIntVector

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

the class MiniSat2Solver method search.

/**
 * The main search procedure of the CDCL algorithm.
 * @param nofConflicts the number of conflicts till the next restart
 * @return a {@link Tristate} representing the result.  {@code FALSE} if the formula is UNSAT, {@code TRUE} if the
 * formula is SAT, and {@code UNDEF} if the state is not known yet (restart) or the handler canceled the computation
 */
protected Tristate search(final int nofConflicts) {
    if (!this.ok) {
        return Tristate.FALSE;
    }
    int conflictC = 0;
    this.selectionOrderIdx = 0;
    while (true) {
        final MSClause confl = propagate();
        if (confl != null) {
            if (this.handler != null && !this.handler.detectedConflict()) {
                this.canceledByHandler = true;
                return Tristate.UNDEF;
            }
            conflictC++;
            if (decisionLevel() == 0) {
                return Tristate.FALSE;
            }
            final LNGIntVector learntClause = new LNGIntVector();
            analyze(confl, learntClause);
            cancelUntil(this.analyzeBtLevel);
            if (this.analyzeBtLevel < this.selectionOrder.size()) {
                this.selectionOrderIdx = this.analyzeBtLevel;
            }
            if (this.config.proofGeneration) {
                final LNGIntVector vec = new LNGIntVector(learntClause.size());
                vec.push(1);
                for (int i = 0; i < learntClause.size(); i++) {
                    vec.push((var(learntClause.get(i)) + 1) * (-2 * (sign(learntClause.get(i)) ? 1 : 0) + 1));
                }
                this.pgProof.push(vec);
            }
            if (learntClause.size() == 1) {
                uncheckedEnqueue(learntClause.get(0), null);
                this.unitClauses.push(learntClause.get(0));
            } else {
                final MSClause cr = new MSClause(learntClause, true);
                this.learnts.push(cr);
                attachClause(cr);
                if (!this.incremental) {
                    claBumpActivity(cr);
                }
                uncheckedEnqueue(learntClause.get(0), cr);
            }
            decayActivities();
        } else {
            if (nofConflicts >= 0 && conflictC >= nofConflicts) {
                cancelUntil(0);
                return Tristate.UNDEF;
            }
            if (!this.incremental) {
                if (decisionLevel() == 0 && !simplify()) {
                    return Tristate.FALSE;
                }
                if (this.learnts.size() - nAssigns() >= this.maxLearnts) {
                    reduceDB();
                }
            }
            int next = LIT_UNDEF;
            while (decisionLevel() < this.assumptions.size()) {
                final int p = this.assumptions.get(decisionLevel());
                if (value(p) == Tristate.TRUE) {
                    this.trailLim.push(this.trail.size());
                } else if (value(p) == Tristate.FALSE) {
                    analyzeFinal(not(p), this.conflict);
                    return Tristate.FALSE;
                } else {
                    next = p;
                    break;
                }
            }
            if (next == LIT_UNDEF) {
                next = pickBranchLit();
                if (next == LIT_UNDEF) {
                    return Tristate.TRUE;
                }
            }
            this.trailLim.push(this.trail.size());
            uncheckedEnqueue(next, null);
        }
    }
}
Also used : MSClause(org.logicng.solvers.datastructures.MSClause) LNGIntVector(org.logicng.collections.LNGIntVector)

Example 15 with LNGIntVector

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

the class MiniSat2Solver method initializeMiniSAT.

/**
 * Initializes the additional parameters.
 */
protected void initializeMiniSAT() {
    this.unitClauses = new LNGIntVector();
    this.learntsizeAdjustConfl = 0;
    this.learntsizeAdjustCnt = 0;
    this.learntsizeAdjustStartConfl = 100;
    this.learntsizeAdjustInc = 1.5;
    this.maxLearnts = 0;
}
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