Search in sources :

Example 1 with Lbool

use of org.sat4j.specs.Lbool 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

Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 IConstr (org.sat4j.specs.IConstr)1 IteratorInt (org.sat4j.specs.IteratorInt)1 Lbool (org.sat4j.specs.Lbool)1 TimeoutException (org.sat4j.specs.TimeoutException)1