00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef Solver_h
00021 #define Solver_h
00022
00023 #include <cstdio>
00024 #include <string>
00025
00026 using namespace std;
00027
00028 #include "Vec.h"
00029 #include "Heap.h"
00030 #include "Alg.h"
00031
00032 #include "SolverTypes.h"
00033
00034
00035
00036
00037
00038 namespace minisat
00039 {
00040
00041
00042 class Solver {
00043
00044 friend class SkeletonSolver;
00045
00046 public:
00047
00048
00049
00050 Solver();
00051 ~Solver();
00052
00053
00054
00055
00056
00057 Var newVar (bool polarity = true, bool dvar = true);
00058
00059
00060 void reserveVars(int num_vars);
00061
00062 bool addClause (vec<Lit>& ps);
00063
00064
00065
00066
00067
00068
00069 bool simplify ();
00070 bool solve (const vec<Lit>& assumps);
00071 bool solve ();
00072 bool okay () const;
00073
00074
00075
00076 void setPolarity (Var v, bool b);
00077 void setDecisionVar (Var v, bool b);
00078
00079
00080
00081 lbool value (Var x) const;
00082 lbool value (Lit p) const;
00083 lbool modelValue (Lit p) const;
00084 int nAssigns () const;
00085 int nClauses () const;
00086 int nLearnts () const;
00087 int nVars () const;
00088
00089
00090
00091 vec<lbool> model;
00092 vec<Lit> conflict;
00093
00094
00095
00096
00097 double var_decay;
00098 double clause_decay;
00099 double random_var_freq;
00100 int restart_first;
00101 double restart_inc;
00102 double learntsize_factor;
00103 double learntsize_inc;
00104 bool expensive_ccmin;
00105 int polarity_mode;
00106 int verbosity;
00107
00108 enum { polarity_true = 0, polarity_false = 1, polarity_user = 2, polarity_rnd = 3 };
00109
00110
00111
00112 uint64_t starts, decisions, rnd_decisions, propagations, conflicts;
00113 uint64_t clauses_literals, learnts_literals, max_literals, tot_literals;
00114
00115
00116
00117
00118 struct VarOrderLt {
00119 const vec<double>& activity;
00120 bool operator () (Var x, Var y) const { return activity[x] > activity[y]; }
00121 VarOrderLt(const vec<double>& act) : activity(act) { }
00122 };
00123
00124 friend class VarFilter;
00125 struct VarFilter {
00126 const Solver& s;
00127 VarFilter(const Solver& _s) : s(_s) {}
00128 bool operator()(Var v) const { return toLbool(s.assigns[v]) == l_Undef && s.decision_var[v]; }
00129 };
00130
00131
00132
00133 bool ok;
00134 vec<Clause*> clauses;
00135 vec<Clause*> learnts;
00136 double cla_inc;
00137 vec<double> activity;
00138 double var_inc;
00139 vec<vec<Clause*> > watches;
00140 vec<char> assigns;
00141 vec<char> polarity;
00142 vec<char> decision_var;
00143 vec<Lit> trail;
00144 vec<int> trail_lim;
00145 vec<Clause*> reason;
00146 vec<int> level;
00147 int qhead;
00148 int simpDB_assigns;
00149 int64_t simpDB_props;
00150 vec<Lit> assumptions;
00151 Heap<VarOrderLt> order_heap;
00152 double random_seed;
00153 double progress_estimate;
00154 bool remove_satisfied;
00155
00156
00157
00158
00159 vec<char> seen;
00160 vec<Lit> analyze_stack;
00161 vec<Lit> analyze_toclear;
00162 vec<Lit> add_tmp;
00163
00164
00165
00166 void insertVarOrder (Var x);
00167 Lit pickBranchLit (int polarity_mode, double random_var_freq);
00168 void newDecisionLevel ();
00169 void uncheckedEnqueue (Lit p, Clause* from = NULL);
00170 bool enqueue (Lit p, Clause* from = NULL);
00171 Clause* propagate ();
00172 void cancelUntil (int level);
00173 void analyze (Clause* confl, vec<Lit>& out_learnt, int& out_btlevel);
00174 void analyzeFinal (Lit p, vec<Lit>& out_conflict);
00175 bool litRedundant (Lit p, uint32_t abstract_levels);
00176 lbool search (int nof_conflicts, int nof_learnts);
00177 void reduceDB ();
00178 void removeSatisfied (vec<Clause*>& cs);
00179
00180
00181
00182 void varDecayActivity ();
00183 void varBumpActivity (Var v);
00184 void claDecayActivity ();
00185 void claBumpActivity (Clause& c);
00186
00187
00188
00189 void attachClause (Clause& c);
00190 void detachClause (Clause& c);
00191 void removeClause (Clause& c);
00192 bool locked (const Clause& c) const;
00193 bool satisfied (const Clause& c) const;
00194
00195
00196
00197 int decisionLevel () const;
00198 uint32_t abstractLevel (Var x) const;
00199 double progressEstimate () const;
00200
00201
00202 void printLit (Lit l);
00203 template<class C>
00204 void printClause (const C& c);
00205 void verifyModel ();
00206 void checkLiteralCount();
00207 string clause_to_string(Clause& c);
00208
00209
00210
00211
00212
00213 static inline double drand(double& seed) {
00214 seed *= 1389796;
00215 int q = (int)(seed / 2147483647);
00216 seed -= (double)q * 2147483647;
00217 return seed / 2147483647; }
00218
00219
00220 static inline int irand(double& seed, int size) {
00221 return (int)(drand(seed) * size); }
00222 };
00223
00224
00225
00226
00227
00228
00229 inline void Solver::insertVarOrder(Var x) {
00230 if (!order_heap.inHeap(x) && decision_var[x]) order_heap.insert(x); }
00231
00232 inline void Solver::varDecayActivity() { var_inc *= var_decay; }
00233 inline void Solver::varBumpActivity(Var v) {
00234 if ( (activity[v] += var_inc) > 1e100 ) {
00235
00236 for (int i = 0; i < nVars(); i++)
00237 activity[i] *= 1e-100;
00238 var_inc *= 1e-100; }
00239
00240
00241 if (order_heap.inHeap(v))
00242 order_heap.decrease(v); }
00243
00244 inline void Solver::claDecayActivity() { cla_inc *= clause_decay; }
00245 inline void Solver::claBumpActivity (Clause& c) {
00246 if ( (c.activity() += cla_inc) > 1e20 ) {
00247
00248 for (int i = 0; i < learnts.size(); i++)
00249 learnts[i]->activity() *= 1e-20;
00250 cla_inc *= 1e-20; } }
00251
00252 inline bool Solver::enqueue (Lit p, Clause* from) { return value(p) != l_Undef ? value(p) != l_False : (uncheckedEnqueue(p, from), true); }
00253 inline bool Solver::locked (const Clause& c) const { return reason[var(c[0])] == &c && value(c[0]) == l_True; }
00254 inline void Solver::newDecisionLevel() { trail_lim.push(trail.size()); }
00255
00256 inline int Solver::decisionLevel () const { return trail_lim.size(); }
00257 inline uint32_t Solver::abstractLevel (Var x) const { return 1 << (level[x] & 31); }
00258 inline lbool Solver::value (Var x) const { return toLbool(assigns[x]); }
00259 inline lbool Solver::value (Lit p) const { return toLbool(assigns[var(p)]) ^ sign(p); }
00260 inline lbool Solver::modelValue (Lit p) const { return model[var(p)] ^ sign(p); }
00261 inline int Solver::nAssigns () const { return trail.size(); }
00262 inline int Solver::nClauses () const { return clauses.size(); }
00263 inline int Solver::nLearnts () const { return learnts.size(); }
00264 inline int Solver::nVars () const { return assigns.size(); }
00265 inline void Solver::setPolarity (Var v, bool b) { polarity [v] = (char)b; }
00266 inline void Solver::setDecisionVar(Var v, bool b) { decision_var[v] = (char)b; if (b) { insertVarOrder(v); } }
00267 inline bool Solver::solve () { vec<Lit> tmp; return solve(tmp); }
00268 inline bool Solver::okay () const { return ok; }
00269
00270
00271
00272
00273
00274
00275
00276 #define reportf(format, args...) ( fflush(stdout), fprintf(stderr, format, ## args), fflush(stderr) )
00277
00278 static inline void logLit(FILE* f, Lit l)
00279 {
00280 fprintf(f, "%sx%d", sign(l) ? "~" : "", var(l)+1);
00281 }
00282
00283 static inline void logLits(FILE* f, const vec<Lit>& ls)
00284 {
00285 fprintf(f, "[ ");
00286 if (ls.size() > 0){
00287 logLit(f, ls[0]);
00288 for (int i = 1; i < ls.size(); i++){
00289 fprintf(f, ", ");
00290 logLit(f, ls[i]);
00291 }
00292 }
00293 fprintf(f, "] ");
00294 }
00295
00296 static inline const char* showBool(bool b) { return b ? "true" : "false"; }
00297
00298
00299
00300 static inline void check(bool expr) { assert(expr); }
00301
00302
00303 inline void Solver::printLit(Lit l)
00304 {
00305 reportf("%s%d:%c", sign(l) ? "-" : "", var(l)+1, value(l) == l_True ? '1' : (value(l) == l_False ? '0' : 'X'));
00306 }
00307
00308
00309 template<class C>
00310 inline void Solver::printClause(const C& c)
00311 {
00312 for (int i = 0; i < c.size(); i++){
00313 printLit(c[i]);
00314 fprintf(stderr, " ");
00315 }
00316 }
00317 }
00318
00319
00320 #endif