Search in sources :

Example 1 with IteratorInt

use of org.sat4j.specs.IteratorInt in project spoon by INRIA.

the class Solver method addAtMost.

public IConstr addAtMost(IVecInt literals, int degree) throws ContradictionException {
    int n = literals.size();
    IVecInt opliterals = new VecInt(n);
    for (IteratorInt iterator = literals.iterator(); iterator.hasNext(); ) {
        opliterals.push(-iterator.next());
    }
    return addAtLeast(opliterals, n - degree);
}
Also used : IVecInt(org.sat4j.specs.IVecInt) IteratorInt(org.sat4j.specs.IteratorInt) IVecInt(org.sat4j.specs.IVecInt) VecInt(org.sat4j.core.VecInt)

Example 2 with IteratorInt

use of org.sat4j.specs.IteratorInt in project spoon by INRIA.

the class Solver method primeImplicant.

public int[] primeImplicant() {
    assert this.qhead == this.trail.size() + this.learnedLiterals.size();
    if (this.learnedLiterals.size() > 0) {
        this.qhead = trail.size();
    }
    if (isVerbose()) {
        System.out.printf("%s implied: %d, decision: %d %n", getLogPrefix(), implied.size(), decisions.size());
    }
    this.prime = new int[realNumberOfVariables() + 1];
    int p, d;
    for (int i = 0; i < this.prime.length; i++) {
        this.prime[i] = 0;
    }
    boolean noproblem;
    for (IteratorInt it = this.implied.iterator(); it.hasNext(); ) {
        d = it.next();
        p = toInternal(d);
        this.prime[Math.abs(d)] = d;
        noproblem = setAndPropagate(p);
        assert noproblem;
    }
    boolean canBeRemoved;
    int rightlevel;
    int removed = 0;
    int propagated = 0;
    int tested = 0;
    int l2propagation = 0;
    for (int i = 0; i < this.decisions.size(); i++) {
        d = this.decisions.get(i);
        assert !this.voc.isFalsified(toInternal(d));
        if (this.voc.isSatisfied(toInternal(d))) {
            // d has been propagated
            this.prime[Math.abs(d)] = d;
            propagated++;
        } else if (setAndPropagate(toInternal(-d))) {
            canBeRemoved = true;
            tested++;
            rightlevel = currentDecisionLevel();
            for (int j = i + 1; j < this.decisions.size(); j++) {
                l2propagation++;
                if (!setAndPropagate(toInternal(this.decisions.get(j)))) {
                    canBeRemoved = false;
                    break;
                }
            }
            cancelUntil(rightlevel);
            if (canBeRemoved) {
                // it is not a necessary literal
                forget(Math.abs(d));
                IConstr confl = propagate();
                assert confl == null;
                removed++;
            } else {
                this.prime[Math.abs(d)] = d;
                cancel();
                assert voc.isUnassigned(toInternal(d));
                noproblem = setAndPropagate(toInternal(d));
                assert noproblem;
            }
        } else {
            // conflict, literal is necessary
            this.prime[Math.abs(d)] = d;
            cancel();
            noproblem = setAndPropagate(toInternal(d));
            assert noproblem;
        }
    }
    cancelUntil(0);
    int[] implicant = new int[this.prime.length - removed - 1];
    int index = 0;
    for (int i : this.prime) {
        if (i != 0) {
            implicant[index++] = i;
        }
    }
    if (isVerbose()) {
        System.out.printf("%s prime implicant computation statistics%n", getLogPrefix());
        System.out.printf("%s implied: %d, decision: %d (removed %d, tested %d, propagated %d), l2 propagation:%d%n", getLogPrefix(), implied.size(), decisions.size(), removed, tested, propagated, l2propagation);
    }
    return implicant;
}
Also used : IteratorInt(org.sat4j.specs.IteratorInt) IConstr(org.sat4j.specs.IConstr)

Example 3 with IteratorInt

use of org.sat4j.specs.IteratorInt in project spoon by INRIA.

the class Solver method isSatisfiable.

public boolean isSatisfiable(IVecInt assumps, boolean global) throws TimeoutException {
    Lbool status = Lbool.UNDEFINED;
    boolean alreadylaunched = this.conflictCount != null;
    final int howmany = this.voc.nVars();
    if (this.mseen.length <= howmany) {
        this.mseen = new boolean[howmany + 1];
    }
    this.trail.ensure(howmany);
    this.trailLim.ensure(howmany);
    this.learnedLiterals.ensure(howmany);
    this.decisions.clear();
    this.implied.clear();
    this.slistener.init(this);
    this.slistener.start();
    // forget about previous model
    this.model = null;
    this.userbooleanmodel = null;
    this.prime = null;
    this.unsatExplanationInTermsOfAssumptions = null;
    if (!alreadylaunched || !this.keepHot) {
        this.order.init();
    }
    this.learnedConstraintsDeletionStrategy.init();
    int learnedLiteralsLimit = this.trail.size();
    // Fix for Bug SAT37
    this.qhead = 0;
    // again now that qhead is 0.
    for (int i = learnedLiteralsLimit - 1; i >= 0; i--) {
        int p = this.trail.get(i);
        IVec<Undoable> undos = this.voc.undos(p);
        assert undos != null;
        for (int size = undos.size(); size > 0; size--) {
            undos.last().undo(p);
            undos.pop();
        }
    }
    // push previously learned literals
    for (IteratorInt iterator = this.learnedLiterals.iterator(); iterator.hasNext(); ) {
        enqueue(iterator.next());
    }
    // propagate constraints
    Constr confl = propagate();
    if (confl != null) {
        analyzeAtRootLevel(confl);
        this.slistener.conflictFound(confl, 0, 0);
        this.slistener.end(Lbool.FALSE);
        cancelUntil(0);
        cancelLearntLiterals(learnedLiteralsLimit);
        return false;
    }
    // push incremental assumptions
    for (IteratorInt iterator = assumps.iterator(); iterator.hasNext(); ) {
        int assump = iterator.next();
        int p = this.voc.getFromPool(assump);
        if (!this.voc.isSatisfied(p) && !assume(p) || (confl = propagate()) != null) {
            if (confl == null) {
                this.slistener.conflictFound(p);
                this.unsatExplanationInTermsOfAssumptions = analyzeFinalConflictInTermsOfAssumptions(null, assumps, p);
                this.unsatExplanationInTermsOfAssumptions.push(assump);
            } else {
                this.slistener.conflictFound(confl, decisionLevel(), this.trail.size());
                this.unsatExplanationInTermsOfAssumptions = analyzeFinalConflictInTermsOfAssumptions(confl, assumps, ILits.UNDEFINED);
            }
            this.slistener.end(Lbool.FALSE);
            cancelUntil(0);
            cancelLearntLiterals(learnedLiteralsLimit);
            return false;
        }
    }
    this.rootLevel = decisionLevel();
    // assumptions.
    if (!alreadylaunched || !this.keepHot) {
        // duplicated on purpose
        this.order.init();
    }
    this.learner.init();
    if (!alreadylaunched) {
        this.conflictCount = new ConflictTimerContainer();
        this.conflictCount.add(this.restarter);
        this.conflictCount.add(this.learnedConstraintsDeletionStrategy.getTimer());
    }
    boolean firstTimeGlobal = false;
    if (this.timeBasedTimeout) {
        if (!global || this.timer == null) {
            firstTimeGlobal = true;
            this.undertimeout = true;
            TimerTask stopMe = new TimerTask() {

                @Override
                public void run() {
                    Solver.this.undertimeout = false;
                }
            };
            this.timer = new Timer(true);
            this.timer.schedule(stopMe, this.timeout);
        }
    } else {
        if (!global || !alreadylaunched) {
            firstTimeGlobal = true;
            this.undertimeout = true;
            ConflictTimer conflictTimeout = new ConflictTimerAdapter((int) this.timeout) {

                private static final long serialVersionUID = 1L;

                @Override
                public void run() {
                    Solver.this.undertimeout = false;
                }
            };
            this.conflictCount.add(conflictTimeout);
        }
    }
    if (!global || firstTimeGlobal) {
        this.restarter.init(this.params, this.stats);
        this.timebegin = System.currentTimeMillis();
    }
    this.needToReduceDB = false;
    // this is used to allow the solver to be incomplete,
    // when using a heuristics limited to a subset of variables
    this.lastConflictMeansUnsat = true;
    // Solve
    while (status == Lbool.UNDEFINED && this.undertimeout && this.lastConflictMeansUnsat) {
        int before = this.trail.size();
        unitClauseProvider.provideUnitClauses(this);
        this.stats.importedUnits += this.trail.size() - before;
        status = search(assumps);
        if (status == Lbool.UNDEFINED) {
            this.restarter.onRestart();
            this.slistener.restarting();
        }
    }
    cancelUntil(0);
    cancelLearntLiterals(learnedLiteralsLimit);
    if (!global && this.timeBasedTimeout && this.timer != null) {
        this.timer.cancel();
        this.timer = null;
    }
    this.slistener.end(status);
    if (!this.undertimeout) {
        String message = " Timeout (" + this.timeout + (this.timeBasedTimeout ? "s" : " conflicts") + ") exceeded";
        throw new TimeoutException(message);
    }
    if (status == Lbool.UNDEFINED && !this.lastConflictMeansUnsat) {
        throw new TimeoutException("Cannot decide the satisfiability");
    }
    // (the last one)
    return model != null;
}
Also used : IteratorInt(org.sat4j.specs.IteratorInt) Lbool(org.sat4j.specs.Lbool) IConstr(org.sat4j.specs.IConstr) TimerTask(java.util.TimerTask) Timer(java.util.Timer) TimeoutException(org.sat4j.specs.TimeoutException)

Aggregations

IteratorInt (org.sat4j.specs.IteratorInt)3 IConstr (org.sat4j.specs.IConstr)2 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 VecInt (org.sat4j.core.VecInt)1 IVecInt (org.sat4j.specs.IVecInt)1 Lbool (org.sat4j.specs.Lbool)1 TimeoutException (org.sat4j.specs.TimeoutException)1