⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 solver.c

📁 多核环境下运行了可满足性分析的工具软件
💻 C
字号:
/****************************************************************************************[Solver.C]MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas SorenssonPermission is hereby granted, free of charge, to any person obtaining a copy of this software andassociated documentation files (the "Software"), to deal in the Software without restriction,including without limitation the rights to use, copy, modify, merge, publish, distribute,sublicense, and/or sell copies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies orsubstantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUTNOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUTOF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**************************************************************************************************/#include "Solver.h"#include "Sort.h"#include <cmath>namespace minisat {  //=================================================================================================  // Constructor/Destructor:  Solver::Solver() :        // More parameters:    //     verbosity        (0)            , ok               (true)    , qhead            (0)    , simpDB_assigns   (-1)    , simpDB_props     (0)    , order_heap       (VarOrderLt(activity))    , random_seed      (91648253)    , remove_satisfied (true)  {}      Solver::~Solver()  {    for (int i = 0; i < learnts.size(); i++) free(learnts[i]);    for (int i = 0; i < clauses.size(); i++) free(clauses[i]);  }      //=================================================================================================  // Minor methods:      // Creates a new SAT variable in the solver. If 'decision_var' is cleared, variable will not be  // used as a decision variable (NOTE! This has effects on the meaning of a SATISFIABLE result).  //  Var Solver::newVar(bool sign, bool dvar)  {    int v = nVars();    watches   .push();          // (list for positive literal)    watches   .push();          // (list for negative literal)    reason    .push(NULL);    assigns   .push(toInt(l_Undef));    level     .push(-1);    activity  .push(0);    seen      .push(0);    polarity    .push((char)sign);    decision_var.push((char)dvar);    insertVarOrder(v);    return v;  }  bool Solver::addClause(vec<Lit>& ps)  {    assert(decisionLevel() == 0);    if (!ok)      return false;    else{      // Check if clause is satisfied and remove false/duplicate literals:      sort(ps);      Lit p; int i, j;      for (i = j = 0, p = lit_Undef; i < ps.size(); i++)	if (value(ps[i]) == l_True || ps[i] == ~p)	  return true;	else if (value(ps[i]) != l_False && ps[i] != p)	  ps[j++] = p = ps[i];      ps.shrink(i - j);    }        if (ps.size() == 0)      return ok = false;    else if (ps.size() == 1){      assert(value(ps[0]) == l_Undef);      uncheckedEnqueue(ps[0]);      return propagate() == NULL;    }else{      Clause* c = Clause_new(ps, false);      clauses.push(c);      attachClause(*c);    }        return true;  }  void Solver::attachClause(Clause& c) {    assert(c.size() > 1);    watches[toInt(~c[0])].push(&c);    watches[toInt(~c[1])].push(&c);    if (c.learnt()) learnts_literals += c.size();    else            clauses_literals += c.size(); }    void Solver::detachClause(Clause& c) {    assert(c.size() > 1);    assert(find(watches[toInt(~c[0])], &c));    assert(find(watches[toInt(~c[1])], &c));    remove(watches[toInt(~c[0])], &c);    remove(watches[toInt(~c[1])], &c);    if (c.learnt()) learnts_literals -= c.size();    else            clauses_literals -= c.size(); }  void Solver::removeClause(Clause& c) {    detachClause(c);    free(&c); }  bool Solver::satisfied(const Clause& c) const {    for (int i = 0; i < c.size(); i++)      if (value(c[i]) == l_True)	return true;    return false; }    // Revert to the state at given level (keeping all assignment at 'level' but not beyond).  //  void Solver::cancelUntil(int level) {    if (decisionLevel() > level){      for (int c = trail.size()-1; c >= trail_lim[level]; c--){	Var     x  = var(trail[c]);	assigns[x] = toInt(l_Undef);	insertVarOrder(x); }      qhead = trail_lim[level];      trail.shrink(trail.size() - trail_lim[level]);      trail_lim.shrink(trail_lim.size() - level);    } }    void Solver::uncheckedEnqueue(Lit p, Clause* from)  {    assert(value(p) == l_Undef);    assigns [var(p)] = toInt(lbool(!sign(p)));  // <<== abstract but not uttermost effecient    level   [var(p)] = decisionLevel();    reason  [var(p)] = from;    trail.push(p);  }  /*_________________________________________________________________________________________________    |    |  propagate : [void]  ->  [Clause*]    |      |  Description:    |    Propagates all enqueued facts. If a conflict arises, the conflicting clause is returned,    |    otherwise NULL.    |      |    Post-conditions:    |      * the propagation queue is empty, even if there was a conflict.    |________________________________________________________________________________________________@*/  Clause* Solver::propagate()  {    Clause* confl     = NULL;    int     num_props = 0;        while (qhead < trail.size()){      Lit            p   = trail[qhead++];     // 'p' is enqueued fact to propagate.      vec<Clause*>&  ws  = watches[toInt(p)];      Clause         **i, **j, **end;      num_props++;            for (i = j = (Clause**)ws, end = i + ws.size();  i != end;){	Clause& c = **i++;		// Make sure the false literal is data[1]:	Lit false_lit = ~p;	if (c[0] == false_lit)	  c[0] = c[1], c[1] = false_lit;		assert(c[1] == false_lit);		// If 0th watch is true, then clause is already satisfied.	Lit first = c[0];	if (value(first) == l_True){	  *j++ = &c;	}else{	  // Look for new watch:	  for (int k = 2; k < c.size(); k++)	    if (value(c[k]) != l_False){	      c[1] = c[k]; c[k] = false_lit;	      watches[toInt(~c[1])].push(&c);	      goto FoundWatch; }	  	  // Did not find watch -- clause is unit under assignment:	  *j++ = &c;	  if (value(first) == l_False){	    confl = &c;	    qhead = trail.size();	    // Copy the remaining watches:	    while (i < end)	      *j++ = *i++;	  }else	    uncheckedEnqueue(first, &c);	}      FoundWatch:;      }      ws.shrink(i - j);    }    propagations += num_props;    simpDB_props -= num_props;        return confl;  }  void Solver::removeSatisfied(vec<Clause*>& cs)  {    int i,j;    for (i = j = 0; i < cs.size(); i++){      if (satisfied(*cs[i]))	removeClause(*cs[i]);      else	cs[j++] = cs[i];    }    cs.shrink(i - j);  }  /*_________________________________________________________________________________________________    |    |  simplify : [void]  ->  [bool]    |      |  Description:    |    Simplify the clause database according to the current top-level assigment. Currently, the only    |    thing done here is the removal of satisfied clauses, but more things can be put here.    |________________________________________________________________________________________________@*/  bool Solver::simplify()  {    assert(decisionLevel() == 0);        if (!ok || propagate() != NULL)      return ok = false;        if (nAssigns() == simpDB_assigns || (simpDB_props > 0))      return true;    // Remove satisfied clauses:    removeSatisfied(learnts);    if (remove_satisfied)        // Can be turned off.      removeSatisfied(clauses);        // Remove fixed variables from the variable heap:    order_heap.filter(VarFilter(*this));        simpDB_assigns = nAssigns();    simpDB_props   = clauses_literals + learnts_literals;   // (shouldn't depend on stats really, but it will do for now)    return true;  }} // end namespace minisat

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -