Search in sources :

Example 1 with MSClause

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

the class GlucoseSyrup method propagate.

@Override
protected MSClause propagate() {
    MSClause confl = null;
    int numProps = 0;
    while (this.qhead < this.trail.size()) {
        final int p = this.trail.get(this.qhead++);
        final LNGVector<MSWatcher> ws = this.watches.get(p);
        int iInd = 0;
        int jInd = 0;
        numProps++;
        final LNGVector<MSWatcher> wbin = this.watchesBin.get(p);
        for (int k = 0; k < wbin.size(); k++) {
            final int imp = wbin.get(k).blocker();
            if (value(imp) == Tristate.FALSE) {
                return wbin.get(k).clause();
            }
            if (value(imp) == Tristate.UNDEF) {
                uncheckedEnqueue(imp, wbin.get(k).clause());
            }
        }
        while (iInd < ws.size()) {
            final MSWatcher i = ws.get(iInd);
            final int blocker = i.blocker();
            if (value(blocker) == Tristate.TRUE) {
                ws.set(jInd++, i);
                iInd++;
                continue;
            }
            final MSClause c = i.clause();
            assert !c.oneWatched();
            final int falseLit = not(p);
            if (c.get(0) == falseLit) {
                c.set(0, c.get(1));
                c.set(1, falseLit);
            }
            assert c.get(1) == falseLit;
            iInd++;
            final int first = c.get(0);
            final MSWatcher w = new MSWatcher(c, first);
            if (first != blocker && value(first) == Tristate.TRUE) {
                ws.set(jInd++, w);
                continue;
            }
            boolean foundWatch = false;
            if (this.incremental) {
                int choosenPos = -1;
                for (int k = 2; k < c.size(); k++) {
                    if (value(c.get(k)) != Tristate.FALSE) {
                        if (decisionLevel() > this.assumptions.size()) {
                            choosenPos = k;
                            break;
                        } else {
                            choosenPos = k;
                            if (value(c.get(k)) == Tristate.TRUE || !isSelector(var(c.get(k)))) {
                                break;
                            }
                        }
                    }
                }
                if (choosenPos != -1) {
                    c.set(1, c.get(choosenPos));
                    c.set(choosenPos, falseLit);
                    this.watches.get(not(c.get(1))).push(w);
                    foundWatch = true;
                }
            } else {
                for (int k = 2; k < c.size() && !foundWatch; k++) {
                    if (value(c.get(k)) != Tristate.FALSE) {
                        c.set(1, c.get(k));
                        c.set(k, falseLit);
                        this.watches.get(not(c.get(1))).push(w);
                        foundWatch = true;
                    }
                }
            }
            if (!foundWatch) {
                ws.set(jInd++, w);
                if (value(first) == Tristate.FALSE) {
                    confl = c;
                    this.qhead = this.trail.size();
                    while (iInd < ws.size()) {
                        ws.set(jInd++, ws.get(iInd++));
                    }
                } else {
                    uncheckedEnqueue(first, c);
                }
            }
        }
        ws.removeElements(iInd - jInd);
    }
    this.simpDBProps -= numProps;
    return confl;
}
Also used : MSClause(org.logicng.solvers.datastructures.MSClause) MSWatcher(org.logicng.solvers.datastructures.MSWatcher)

Example 2 with MSClause

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

the class GlucoseSyrup method litRedundant.

@Override
protected boolean litRedundant(final int p, final int abstractLevels) {
    this.analyzeStack.clear();
    this.analyzeStack.push(p);
    final int top = this.analyzeToClear.size();
    while (this.analyzeStack.size() > 0) {
        assert v(this.analyzeStack.back()).reason() != null;
        final MSClause c = v(this.analyzeStack.back()).reason();
        this.analyzeStack.pop();
        if (c.size() == 2 && value(c.get(0)) == Tristate.FALSE) {
            assert value(c.get(1)) == Tristate.TRUE;
            final int tmp = c.get(0);
            c.set(0, c.get(1));
            c.set(1, tmp);
        }
        for (int i = 1; i < c.size(); i++) {
            final int q = c.get(i);
            if (!this.seen.get(var(q)) && v(q).level() > 0) {
                if (v(q).reason() != null && (abstractLevel(var(q)) & abstractLevels) != 0) {
                    this.seen.set(var(q), true);
                    this.analyzeStack.push(q);
                    this.analyzeToClear.push(q);
                } else {
                    for (int j = top; j < this.analyzeToClear.size(); j++) {
                        this.seen.set(var(this.analyzeToClear.get(j)), false);
                    }
                    this.analyzeToClear.removeElements(this.analyzeToClear.size() - top);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : MSClause(org.logicng.solvers.datastructures.MSClause)

Example 3 with MSClause

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

the class MiniCard method litRedundant.

@Override
protected boolean litRedundant(final int p, final int abstractLevels) {
    this.analyzeStack.clear();
    this.analyzeStack.push(p);
    final int top = this.analyzeToClear.size();
    while (this.analyzeStack.size() > 0) {
        assert v(this.analyzeStack.back()).reason() != null;
        final MSClause c = v(this.analyzeStack.back()).reason();
        this.analyzeStack.pop();
        if (c.isAtMost()) {
            for (int i = 0; i < c.size(); i++) {
                if (value(c.get(i)) != Tristate.TRUE) {
                    continue;
                }
                final int q = not(c.get(i));
                if (!this.seen.get(var(q)) && v(q).level() > 0) {
                    if (v(q).reason() != null && (abstractLevel(var(q)) & abstractLevels) != 0) {
                        this.seen.set(var(q), true);
                        this.analyzeStack.push(q);
                        this.analyzeToClear.push(q);
                    } else {
                        for (int j = top; j < this.analyzeToClear.size(); j++) {
                            this.seen.set(var(this.analyzeToClear.get(j)), false);
                        }
                        this.analyzeToClear.removeElements(this.analyzeToClear.size() - top);
                        return false;
                    }
                }
            }
        } else {
            for (int i = 1; i < c.size(); i++) {
                final int q = c.get(i);
                if (!this.seen.get(var(q)) && v(q).level() > 0) {
                    if (v(q).reason() != null && (abstractLevel(var(q)) & abstractLevels) != 0) {
                        this.seen.set(var(q), true);
                        this.analyzeStack.push(q);
                        this.analyzeToClear.push(q);
                    } else {
                        for (int j = top; j < this.analyzeToClear.size(); j++) {
                            this.seen.set(var(this.analyzeToClear.get(j)), false);
                        }
                        this.analyzeToClear.removeElements(this.analyzeToClear.size() - top);
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : MSClause(org.logicng.solvers.datastructures.MSClause)

Example 4 with MSClause

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

the class MiniCard method analyzeFinal.

@Override
protected void analyzeFinal(final int p, final LNGIntVector outConflict) {
    outConflict.clear();
    outConflict.push(p);
    if (decisionLevel() == 0) {
        return;
    }
    this.seen.set(var(p), true);
    int x;
    MSVariable v;
    for (int i = this.trail.size() - 1; i >= this.trailLim.get(0); i--) {
        x = var(this.trail.get(i));
        if (this.seen.get(x)) {
            v = this.vars.get(x);
            if (v.reason() == null) {
                assert v.level() > 0;
                outConflict.push(not(this.trail.get(i)));
            } else {
                final MSClause c = v.reason();
                if (!c.isAtMost()) {
                    for (int j = 1; j < c.size(); j++) {
                        if (v(c.get(j)).level() > 0) {
                            this.seen.set(var(c.get(j)), true);
                        }
                    }
                } else {
                    for (int j = 0; j < c.size(); j++) {
                        if (value(c.get(j)) == Tristate.TRUE && v(c.get(j)).level() > 0) {
                            this.seen.set(var(c.get(j)), true);
                        }
                    }
                }
            }
            this.seen.set(x, false);
        }
    }
    this.seen.set(var(p), false);
}
Also used : MSClause(org.logicng.solvers.datastructures.MSClause) MSVariable(org.logicng.solvers.datastructures.MSVariable)

Example 5 with MSClause

use of org.logicng.solvers.datastructures.MSClause 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)

Aggregations

MSClause (org.logicng.solvers.datastructures.MSClause)31 LNGIntVector (org.logicng.collections.LNGIntVector)8 MSVariable (org.logicng.solvers.datastructures.MSVariable)5 MSWatcher (org.logicng.solvers.datastructures.MSWatcher)3 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Formula (org.logicng.formulas.Formula)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 Literal (org.logicng.formulas.Literal)1 Variable (org.logicng.formulas.Variable)1