SimpSolver.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /************************************************************************************[SimpSolver.h]
  2. MiniSat -- Copyright (c) 2006, Niklas Een, Niklas Sorensson
  3. Copyright (c) 2007-2010, Niklas Sorensson
  4. Chanseok Oh's MiniSat Patch Series -- Copyright (c) 2015, Chanseok Oh
  5. Maple_LCM, Based on MapleCOMSPS_DRUP -- Copyright (c) 2017, Mao Luo, Chu-Min LI, Fan Xiao: implementing a learnt clause minimisation approach
  6. Reference: M. Luo, C.-M. Li, F. Xiao, F. Manya, and Z. L. , “An effective learnt clause minimization approach for cdcl sat solvers,” in IJCAI-2017, 2017, pp. to–appear.
  7. Maple_LCM_Dist, Based on Maple_LCM -- Copyright (c) 2017, Fan Xiao, Chu-Min LI, Mao Luo: using a new branching heuristic called Distance at the beginning of search
  8. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  9. associated documentation files (the "Software"), to deal in the Software without restriction,
  10. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  11. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all copies or
  14. substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  16. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  19. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. **************************************************************************************************/
  21. #ifndef Minisat_SimpSolver_h
  22. #define Minisat_SimpSolver_h
  23. #include "mtl/Queue.h"
  24. #include "core/Solver.h"
  25. namespace Minisat {
  26. //=================================================================================================
  27. class SimpSolver : public Solver {
  28. public:
  29. // Constructor/Destructor:
  30. //
  31. SimpSolver();
  32. ~SimpSolver();
  33. // Problem specification:
  34. //
  35. Var newVar (bool polarity = true, bool dvar = true);
  36. bool addClause (const vec<Lit>& ps);
  37. bool addEmptyClause(); // Add the empty clause to the solver.
  38. bool addClause (Lit p); // Add a unit clause to the solver.
  39. bool addClause (Lit p, Lit q); // Add a binary clause to the solver.
  40. bool addClause (Lit p, Lit q, Lit r); // Add a ternary clause to the solver.
  41. bool addClause_( vec<Lit>& ps);
  42. bool substitute(Var v, Lit x); // Replace all occurences of v with x (may cause a contradiction).
  43. // Variable mode:
  44. //
  45. void setFrozen (Var v, bool b); // If a variable is frozen it will not be eliminated.
  46. bool isEliminated(Var v) const;
  47. // Solving:
  48. //
  49. bool solve (const vec<Lit>& assumps, bool do_simp = true, bool turn_off_simp = false);
  50. lbool solveLimited(const vec<Lit>& assumps, bool do_simp = true, bool turn_off_simp = false);
  51. bool solve ( bool do_simp = true, bool turn_off_simp = false);
  52. bool solve (Lit p , bool do_simp = true, bool turn_off_simp = false);
  53. bool solve (Lit p, Lit q, bool do_simp = true, bool turn_off_simp = false);
  54. bool solve (Lit p, Lit q, Lit r, bool do_simp = true, bool turn_off_simp = false);
  55. bool eliminate (bool turn_off_elim = false); // Perform variable elimination based simplification.
  56. bool eliminate_ ();
  57. void removeSatisfied();
  58. // Memory managment:
  59. //
  60. virtual void garbageCollect();
  61. // Generate a (possibly simplified) DIMACS file:
  62. //
  63. #if 0
  64. void toDimacs (const char* file, const vec<Lit>& assumps);
  65. void toDimacs (const char* file);
  66. void toDimacs (const char* file, Lit p);
  67. void toDimacs (const char* file, Lit p, Lit q);
  68. void toDimacs (const char* file, Lit p, Lit q, Lit r);
  69. #endif
  70. // Mode of operation:
  71. //
  72. bool parsing;
  73. int grow; // Allow a variable elimination step to grow by a number of clauses (default to zero).
  74. int clause_lim; // Variables are not eliminated if it produces a resolvent with a length above this limit.
  75. // -1 means no limit.
  76. int subsumption_lim; // Do not check if subsumption against a clause larger than this. -1 means no limit.
  77. double simp_garbage_frac; // A different limit for when to issue a GC during simplification (Also see 'garbage_frac').
  78. bool use_asymm; // Shrink clauses by asymmetric branching.
  79. bool use_rcheck; // Check if a clause is already implied. Prett costly, and subsumes subsumptions :)
  80. bool use_elim; // Perform variable elimination.
  81. // Statistics:
  82. //
  83. int merges;
  84. int asymm_lits;
  85. int eliminated_vars;
  86. protected:
  87. // Helper structures:
  88. //
  89. struct ElimLt {
  90. const vec<int>& n_occ;
  91. explicit ElimLt(const vec<int>& no) : n_occ(no) {}
  92. // TODO: are 64-bit operations here noticably bad on 32-bit platforms? Could use a saturating
  93. // 32-bit implementation instead then, but this will have to do for now.
  94. uint64_t cost (Var x) const { return (uint64_t)n_occ[toInt(mkLit(x))] * (uint64_t)n_occ[toInt(~mkLit(x))]; }
  95. bool operator()(Var x, Var y) const { return cost(x) < cost(y); }
  96. // TODO: investigate this order alternative more.
  97. // bool operator()(Var x, Var y) const {
  98. // int c_x = cost(x);
  99. // int c_y = cost(y);
  100. // return c_x < c_y || c_x == c_y && x < y; }
  101. };
  102. struct ClauseDeleted {
  103. const ClauseAllocator& ca;
  104. explicit ClauseDeleted(const ClauseAllocator& _ca) : ca(_ca) {}
  105. bool operator()(const CRef& cr) const { return ca[cr].mark() == 1; } };
  106. // Solver state:
  107. //
  108. int elimorder;
  109. bool use_simplification;
  110. vec<uint32_t> elimclauses;
  111. vec<char> touched;
  112. OccLists<Var, vec<CRef>, ClauseDeleted>
  113. occurs;
  114. vec<int> n_occ;
  115. Heap<ElimLt> elim_heap;
  116. Queue<CRef> subsumption_queue;
  117. vec<char> frozen;
  118. vec<char> eliminated;
  119. int bwdsub_assigns;
  120. int n_touched;
  121. // Temporaries:
  122. //
  123. CRef bwdsub_tmpunit;
  124. // Main internal methods:
  125. //
  126. lbool solve_ (bool do_simp = true, bool turn_off_simp = false);
  127. bool asymm (Var v, CRef cr);
  128. bool asymmVar (Var v);
  129. void updateElimHeap (Var v);
  130. void gatherTouchedClauses ();
  131. bool merge (const Clause& _ps, const Clause& _qs, Var v, vec<Lit>& out_clause);
  132. bool merge (const Clause& _ps, const Clause& _qs, Var v, int& size);
  133. bool backwardSubsumptionCheck (bool verbose = false);
  134. bool eliminateVar (Var v);
  135. void extendModel ();
  136. void removeClause (CRef cr);
  137. bool strengthenClause (CRef cr, Lit l);
  138. bool implied (const vec<Lit>& c);
  139. void relocAll (ClauseAllocator& to);
  140. };
  141. //=================================================================================================
  142. // Implementation of inline methods:
  143. inline bool SimpSolver::isEliminated (Var v) const { return eliminated[v]; }
  144. inline void SimpSolver::updateElimHeap(Var v) {
  145. assert(use_simplification);
  146. // if (!frozen[v] && !isEliminated(v) && value(v) == l_Undef)
  147. if (elim_heap.inHeap(v) || (!frozen[v] && !isEliminated(v) && value(v) == l_Undef))
  148. elim_heap.update(v); }
  149. inline bool SimpSolver::addClause (const vec<Lit>& ps) { ps.copyTo(add_tmp); return addClause_(add_tmp); }
  150. inline bool SimpSolver::addEmptyClause() { add_tmp.clear(); return addClause_(add_tmp); }
  151. inline bool SimpSolver::addClause (Lit p) { add_tmp.clear(); add_tmp.push(p); return addClause_(add_tmp); }
  152. inline bool SimpSolver::addClause (Lit p, Lit q) { add_tmp.clear(); add_tmp.push(p); add_tmp.push(q); return addClause_(add_tmp); }
  153. inline bool SimpSolver::addClause (Lit p, Lit q, Lit r) { add_tmp.clear(); add_tmp.push(p); add_tmp.push(q); add_tmp.push(r); return addClause_(add_tmp); }
  154. inline void SimpSolver::setFrozen (Var v, bool b) { frozen[v] = (char)b; if (use_simplification && !b) { updateElimHeap(v); } }
  155. inline bool SimpSolver::solve ( bool do_simp, bool turn_off_simp) { budgetOff(); assumptions.clear(); return solve_(do_simp, turn_off_simp) == l_True; }
  156. inline bool SimpSolver::solve (Lit p , bool do_simp, bool turn_off_simp) { budgetOff(); assumptions.clear(); assumptions.push(p); return solve_(do_simp, turn_off_simp) == l_True; }
  157. inline bool SimpSolver::solve (Lit p, Lit q, bool do_simp, bool turn_off_simp) { budgetOff(); assumptions.clear(); assumptions.push(p); assumptions.push(q); return solve_(do_simp, turn_off_simp) == l_True; }
  158. inline bool SimpSolver::solve (Lit p, Lit q, Lit r, bool do_simp, bool turn_off_simp) { budgetOff(); assumptions.clear(); assumptions.push(p); assumptions.push(q); assumptions.push(r); return solve_(do_simp, turn_off_simp) == l_True; }
  159. inline bool SimpSolver::solve (const vec<Lit>& assumps, bool do_simp, bool turn_off_simp){
  160. budgetOff(); assumps.copyTo(assumptions); return solve_(do_simp, turn_off_simp) == l_True; }
  161. inline lbool SimpSolver::solveLimited (const vec<Lit>& assumps, bool do_simp, bool turn_off_simp){
  162. assumps.copyTo(assumptions); return solve_(do_simp, turn_off_simp); }
  163. //=================================================================================================
  164. }
  165. #endif