fix minisat compile on modern c++ compilers
[cl-satwrap.git] / backends / minisat / core / Solver.h
blob0137b6ff335c9b21e1bb353b63eaf707559ff66e
1 /****************************************************************************************[Solver.h]
2 MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 associated documentation files (the "Software"), to deal in the Software without restriction,
6 including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
10 The above copyright notice and this permission notice shall be included in all copies or
11 substantial portions of the Software.
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 **************************************************************************************************/
20 #ifndef Solver_h
21 #define Solver_h
23 #include <cstdio>
25 #include "Vec.h"
26 #include "Heap.h"
27 #include "Alg.h"
29 #include "SolverTypes.h"
32 //=================================================================================================
33 // Solver -- the main class:
36 class Solver {
37 public:
39 // Constructor/Destructor:
41 Solver();
42 ~Solver();
44 // Problem specification:
46 Var newVar (bool polarity = true, bool dvar = true); // Add a new variable with parameters specifying variable mode.
47 bool addClause (vec<Lit>& ps); // Add a clause to the solver. NOTE! 'ps' may be shrunk by this method!
49 // Solving:
51 bool simplify (); // Removes already satisfied clauses.
52 bool solve (const vec<Lit>& assumps); // Search for a model that respects a given set of assumptions.
53 bool solve (); // Search without assumptions.
54 bool okay () const; // FALSE means solver is in a conflicting state
56 // Variable mode:
57 //
58 void setPolarity (Var v, bool b); // Declare which polarity the decision heuristic should use for a variable. Requires mode 'polarity_user'.
59 void setDecisionVar (Var v, bool b); // Declare if a variable should be eligible for selection in the decision heuristic.
61 // Read state:
63 lbool value (Var x) const; // The current value of a variable.
64 lbool value (Lit p) const; // The current value of a literal.
65 lbool modelValue (Lit p) const; // The value of a literal in the last model. The last call to solve must have been satisfiable.
66 int nAssigns () const; // The current number of assigned literals.
67 int nClauses () const; // The current number of original clauses.
68 int nLearnts () const; // The current number of learnt clauses.
69 int nVars () const; // The current number of variables.
71 // Extra results: (read-only member variable)
73 vec<lbool> model; // If problem is satisfiable, this vector contains the model (if any).
74 vec<Lit> conflict; // If problem is unsatisfiable (possibly under assumptions),
75 // this vector represent the final conflict clause expressed in the assumptions.
77 // Mode of operation:
79 double var_decay; // Inverse of the variable activity decay factor. (default 1 / 0.95)
80 double clause_decay; // Inverse of the clause activity decay factor. (1 / 0.999)
81 double random_var_freq; // The frequency with which the decision heuristic tries to choose a random variable. (default 0.02)
82 int restart_first; // The initial restart limit. (default 100)
83 double restart_inc; // The factor with which the restart limit is multiplied in each restart. (default 1.5)
84 double learntsize_factor; // The intitial limit for learnt clauses is a factor of the original clauses. (default 1 / 3)
85 double learntsize_inc; // The limit for learnt clauses is multiplied with this factor each restart. (default 1.1)
86 bool expensive_ccmin; // Controls conflict clause minimization. (default TRUE)
87 int polarity_mode; // Controls which polarity the decision heuristic chooses. See enum below for allowed modes. (default polarity_false)
88 int verbosity; // Verbosity level. 0=silent, 1=some progress report (default 0)
90 enum { polarity_true = 0, polarity_false = 1, polarity_user = 2, polarity_rnd = 3 };
92 // Statistics: (read-only member variable)
94 uint64_t starts, decisions, rnd_decisions, propagations, conflicts;
95 uint64_t clauses_literals, learnts_literals, max_literals, tot_literals;
97 protected:
99 // Helper structures:
101 struct VarOrderLt {
102 const vec<double>& activity;
103 bool operator () (Var x, Var y) const { return activity[x] > activity[y]; }
104 VarOrderLt(const vec<double>& act) : activity(act) { }
107 friend class VarFilter;
108 struct VarFilter {
109 const Solver& s;
110 VarFilter(const Solver& _s) : s(_s) {}
111 bool operator()(Var v) const { return toLbool(s.assigns[v]) == l_Undef && s.decision_var[v]; }
114 // Solver state:
116 bool ok; // If FALSE, the constraints are already unsatisfiable. No part of the solver state may be used!
117 vec<Clause*> clauses; // List of problem clauses.
118 vec<Clause*> learnts; // List of learnt clauses.
119 double cla_inc; // Amount to bump next clause with.
120 vec<double> activity; // A heuristic measurement of the activity of a variable.
121 double var_inc; // Amount to bump next variable with.
122 vec<vec<Clause*> > watches; // 'watches[lit]' is a list of constraints watching 'lit' (will go there if literal becomes true).
123 vec<char> assigns; // The current assignments (lbool:s stored as char:s).
124 vec<char> polarity; // The preferred polarity of each variable.
125 vec<char> decision_var; // Declares if a variable is eligible for selection in the decision heuristic.
126 vec<Lit> trail; // Assignment stack; stores all assigments made in the order they were made.
127 vec<int> trail_lim; // Separator indices for different decision levels in 'trail'.
128 vec<Clause*> reason; // 'reason[var]' is the clause that implied the variables current value, or 'NULL' if none.
129 vec<int> level; // 'level[var]' contains the level at which the assignment was made.
130 int qhead; // Head of queue (as index into the trail -- no more explicit propagation queue in MiniSat).
131 int simpDB_assigns; // Number of top-level assignments since last execution of 'simplify()'.
132 int64_t simpDB_props; // Remaining number of propagations that must be made before next execution of 'simplify()'.
133 vec<Lit> assumptions; // Current set of assumptions provided to solve by the user.
134 Heap<VarOrderLt> order_heap; // A priority queue of variables ordered with respect to the variable activity.
135 double random_seed; // Used by the random variable selection.
136 double progress_estimate;// Set by 'search()'.
137 bool remove_satisfied; // Indicates whether possibly inefficient linear scan for satisfied clauses should be performed in 'simplify'.
139 // Temporaries (to reduce allocation overhead). Each variable is prefixed by the method in which it is
140 // used, exept 'seen' wich is used in several places.
142 vec<char> seen;
143 vec<Lit> analyze_stack;
144 vec<Lit> analyze_toclear;
145 vec<Lit> add_tmp;
147 // Main internal methods:
149 void insertVarOrder (Var x); // Insert a variable in the decision order priority queue.
150 Lit pickBranchLit (int polarity_mode, double random_var_freq); // Return the next decision variable.
151 void newDecisionLevel (); // Begins a new decision level.
152 void uncheckedEnqueue (Lit p, Clause* from = NULL); // Enqueue a literal. Assumes value of literal is undefined.
153 bool enqueue (Lit p, Clause* from = NULL); // Test if fact 'p' contradicts current state, enqueue otherwise.
154 Clause* propagate (); // Perform unit propagation. Returns possibly conflicting clause.
155 void cancelUntil (int level); // Backtrack until a certain level.
156 void analyze (Clause* confl, vec<Lit>& out_learnt, int& out_btlevel); // (bt = backtrack)
157 void analyzeFinal (Lit p, vec<Lit>& out_conflict); // COULD THIS BE IMPLEMENTED BY THE ORDINARIY "analyze" BY SOME REASONABLE GENERALIZATION?
158 bool litRedundant (Lit p, uint32_t abstract_levels); // (helper method for 'analyze()')
159 lbool search (int nof_conflicts, int nof_learnts); // Search for a given number of conflicts.
160 void reduceDB (); // Reduce the set of learnt clauses.
161 void removeSatisfied (vec<Clause*>& cs); // Shrink 'cs' to contain only non-satisfied clauses.
163 // Maintaining Variable/Clause activity:
165 void varDecayActivity (); // Decay all variables with the specified factor. Implemented by increasing the 'bump' value instead.
166 void varBumpActivity (Var v); // Increase a variable with the current 'bump' value.
167 void claDecayActivity (); // Decay all clauses with the specified factor. Implemented by increasing the 'bump' value instead.
168 void claBumpActivity (Clause& c); // Increase a clause with the current 'bump' value.
170 // Operations on clauses:
172 void attachClause (Clause& c); // Attach a clause to watcher lists.
173 void detachClause (Clause& c); // Detach a clause to watcher lists.
174 void removeClause (Clause& c); // Detach and free a clause.
175 bool locked (const Clause& c) const; // Returns TRUE if a clause is a reason for some implication in the current state.
176 bool satisfied (const Clause& c) const; // Returns TRUE if a clause is satisfied in the current state.
178 // Misc:
180 int decisionLevel () const; // Gives the current decisionlevel.
181 uint32_t abstractLevel (Var x) const; // Used to represent an abstraction of sets of decision levels.
182 double progressEstimate () const; // DELETE THIS ?? IT'S NOT VERY USEFUL ...
184 // Debug:
185 void printLit (Lit l);
186 template<class C>
187 void printClause (const C& c);
188 void verifyModel ();
189 void checkLiteralCount();
191 // Static helpers:
194 // Returns a random float 0 <= x < 1. Seed must never be 0.
195 static inline double drand(double& seed) {
196 seed *= 1389796;
197 int q = (int)(seed / 2147483647);
198 seed -= (double)q * 2147483647;
199 return seed / 2147483647; }
201 // Returns a random integer 0 <= x < size. Seed must never be 0.
202 static inline int irand(double& seed, int size) {
203 return (int)(drand(seed) * size); }
207 //=================================================================================================
208 // Implementation of inline methods:
211 inline void Solver::insertVarOrder(Var x) {
212 if (!order_heap.inHeap(x) && decision_var[x]) order_heap.insert(x); }
214 inline void Solver::varDecayActivity() { var_inc *= var_decay; }
215 inline void Solver::varBumpActivity(Var v) {
216 if ( (activity[v] += var_inc) > 1e100 ) {
217 // Rescale:
218 for (int i = 0; i < nVars(); i++)
219 activity[i] *= 1e-100;
220 var_inc *= 1e-100; }
222 // Update order_heap with respect to new activity:
223 if (order_heap.inHeap(v))
224 order_heap.decrease(v); }
226 inline void Solver::claDecayActivity() { cla_inc *= clause_decay; }
227 inline void Solver::claBumpActivity (Clause& c) {
228 if ( (c.activity() += cla_inc) > 1e20 ) {
229 // Rescale:
230 for (int i = 0; i < learnts.size(); i++)
231 learnts[i]->activity() *= 1e-20;
232 cla_inc *= 1e-20; } }
234 inline bool Solver::enqueue (Lit p, Clause* from) { return value(p) != l_Undef ? value(p) != l_False : (uncheckedEnqueue(p, from), true); }
235 inline bool Solver::locked (const Clause& c) const { return reason[var(c[0])] == &c && value(c[0]) == l_True; }
236 inline void Solver::newDecisionLevel() { trail_lim.push(trail.size()); }
238 inline int Solver::decisionLevel () const { return trail_lim.size(); }
239 inline uint32_t Solver::abstractLevel (Var x) const { return 1 << (level[x] & 31); }
240 inline lbool Solver::value (Var x) const { return toLbool(assigns[x]); }
241 inline lbool Solver::value (Lit p) const { return toLbool(assigns[var(p)]) ^ sign(p); }
242 inline lbool Solver::modelValue (Lit p) const { return model[var(p)] ^ sign(p); }
243 inline int Solver::nAssigns () const { return trail.size(); }
244 inline int Solver::nClauses () const { return clauses.size(); }
245 inline int Solver::nLearnts () const { return learnts.size(); }
246 inline int Solver::nVars () const { return assigns.size(); }
247 inline void Solver::setPolarity (Var v, bool b) { polarity [v] = (char)b; }
248 inline void Solver::setDecisionVar(Var v, bool b) { decision_var[v] = (char)b; if (b) { insertVarOrder(v); } }
249 inline bool Solver::solve () { vec<Lit> tmp; return solve(tmp); }
250 inline bool Solver::okay () const { return ok; }
254 //=================================================================================================
255 // Debug + etc:
258 #define reportf(format, args...) ( fflush(stdout), fprintf(stderr, format, ## args), fflush(stderr) )
260 static inline void logLit(FILE* f, Lit l)
262 fprintf(f, "%sx%d", sign(l) ? "~" : "", var(l)+1);
265 static inline void logLits(FILE* f, const vec<Lit>& ls)
267 fprintf(f, "[ ");
268 if (ls.size() > 0){
269 logLit(f, ls[0]);
270 for (int i = 1; i < ls.size(); i++){
271 fprintf(f, ", ");
272 logLit(f, ls[i]);
275 fprintf(f, "] ");
278 static inline const char* showBool(bool b) { return b ? "true" : "false"; }
281 // Just like 'assert()' but expression will be evaluated in the release version as well.
282 static inline void check(bool expr) { assert(expr); }
285 inline void Solver::printLit(Lit l)
287 reportf("%s%d:%c", sign(l) ? "-" : "", var(l)+1, value(l) == l_True ? '1' : (value(l) == l_False ? '0' : 'X'));
291 template<class C>
292 inline void Solver::printClause(const C& c)
294 for (int i = 0; i < c.size(); i++){
295 printLit(c[i]);
296 fprintf(stderr, " ");
301 //=================================================================================================
302 #endif