yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SimpSolver.h
Go to the documentation of this file.
1 /************************************************************************************[SimpSolver.h]
2 Copyright (c) 2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
20 
21 #ifndef Minisat_SimpSolver_h
22 #define Minisat_SimpSolver_h
23 
24 #include "Queue.h"
25 #include "Solver.h"
26 
27 
28 namespace Minisat {
29 
30 //=================================================================================================
31 
32 
33 class SimpSolver : public Solver {
34  public:
35  // Constructor/Destructor:
36  //
37  SimpSolver();
38  ~SimpSolver();
39 
40  // Problem specification:
41  //
42  Var newVar (lbool upol = l_Undef, bool dvar = true);
43  void releaseVar(Lit l);
44  bool addClause (const vec<Lit>& ps);
45  bool addEmptyClause(); // Add the empty clause to the solver.
46  bool addClause (Lit p); // Add a unit clause to the solver.
47  bool addClause (Lit p, Lit q); // Add a binary clause to the solver.
48  bool addClause (Lit p, Lit q, Lit r); // Add a ternary clause to the solver.
49  bool addClause (Lit p, Lit q, Lit r, Lit s); // Add a quaternary clause to the solver.
50  bool addClause_( vec<Lit>& ps);
51  bool substitute(Var v, Lit x); // Replace all occurences of v with x (may cause a contradiction).
52 
53  // Variable mode:
54  //
55  void setFrozen (Var v, bool b); // If a variable is frozen it will not be eliminated.
56  bool isEliminated(Var v) const;
57 
58  // Alternative freeze interface (may replace 'setFrozen()'):
59  void freezeVar (Var v); // Freeze one variable so it will not be eliminated.
60  void thaw (); // Thaw all frozen variables.
61 
62 
63  // Solving:
64  //
65  bool solve (const vec<Lit>& assumps, bool do_simp = true, bool turn_off_simp = false);
66  lbool solveLimited(const vec<Lit>& assumps, bool do_simp = true, bool turn_off_simp = false);
67  bool solve ( bool do_simp = true, bool turn_off_simp = false);
68  bool solve (Lit p , bool do_simp = true, bool turn_off_simp = false);
69  bool solve (Lit p, Lit q, bool do_simp = true, bool turn_off_simp = false);
70  bool solve (Lit p, Lit q, Lit r, bool do_simp = true, bool turn_off_simp = false);
71  bool eliminate (bool turn_off_elim = false); // Perform variable elimination based simplification.
72 
73  // Memory managment:
74  //
75  virtual void garbageCollect();
76 
77 
78  // Generate a (possibly simplified) DIMACS file:
79  //
80 #if 0
81  void toDimacs (const char* file, const vec<Lit>& assumps);
82  void toDimacs (const char* file);
83  void toDimacs (const char* file, Lit p);
84  void toDimacs (const char* file, Lit p, Lit q);
85  void toDimacs (const char* file, Lit p, Lit q, Lit r);
86 #endif
87 
88  // Mode of operation:
89  //
90  int grow; // Allow a variable elimination step to grow by a number of clauses (default to zero).
91  int clause_lim; // Variables are not eliminated if it produces a resolvent with a length above this limit.
92  // -1 means no limit.
93  int subsumption_lim; // Do not check if subsumption against a clause larger than this. -1 means no limit.
94  double simp_garbage_frac; // A different limit for when to issue a GC during simplification (Also see 'garbage_frac').
95 
96  bool use_asymm; // Shrink clauses by asymmetric branching.
97  bool use_rcheck; // Check if a clause is already implied. Prett costly, and subsumes subsumptions :)
98  bool use_elim; // Perform variable elimination.
99  bool extend_model; // Flag to indicate whether the user needs to look at the full model.
100 
101  // Statistics:
102  //
103  int merges;
106 
107  protected:
108 
109  // Helper structures:
110  //
111  struct ElimLt {
112  const LMap<int>& n_occ;
113  explicit ElimLt(const LMap<int>& no) : n_occ(no) {}
114 
115  // TODO: are 64-bit operations here noticably bad on 32-bit platforms? Could use a saturating
116  // 32-bit implementation instead then, but this will have to do for now.
117  uint64_t cost (Var x) const { return (uint64_t)n_occ[mkLit(x)] * (uint64_t)n_occ[~mkLit(x)]; }
118  bool operator()(Var x, Var y) const { return cost(x) < cost(y); }
119 
120  // TODO: investigate this order alternative more.
121  // bool operator()(Var x, Var y) const {
122  // int c_x = cost(x);
123  // int c_y = cost(y);
124  // return c_x < c_y || c_x == c_y && x < y; }
125  };
126 
127  struct ClauseDeleted {
129  explicit ClauseDeleted(const ClauseAllocator& _ca) : ca(_ca) {}
130  bool operator()(const CRef& cr) const { return ca[cr].mark() == 1; } };
131 
132  // Solver state:
133  //
136  Var max_simp_var; // Max variable at the point simplification was turned off.
149 
150  // Temporaries:
151  //
153 
154  // Main internal methods:
155  //
156  lbool solve_ (bool do_simp = true, bool turn_off_simp = false);
157  bool asymm (Var v, CRef cr);
158  bool asymmVar (Var v);
159  void updateElimHeap (Var v);
160  void gatherTouchedClauses ();
161  bool merge (const Clause& _ps, const Clause& _qs, Var v, vec<Lit>& out_clause);
162  bool merge (const Clause& _ps, const Clause& _qs, Var v, int& size);
163  bool backwardSubsumptionCheck (bool verbose = false);
164  bool eliminateVar (Var v);
165  void extendModel ();
166 
167  void removeClause (CRef cr);
168  bool strengthenClause (CRef cr, Lit l);
169  bool implied (const vec<Lit>& c);
170  void relocAll (ClauseAllocator& to);
171 };
172 
173 
174 //=================================================================================================
175 // Implementation of inline methods:
176 
177 
178 inline bool SimpSolver::isEliminated (Var v) const { return eliminated[v]; }
180  assert(use_simplification);
181  // if (!frozen[v] && !isEliminated(v) && value(v) == l_Undef)
182  if (elim_heap.inHeap(v) || (!frozen[v] && !isEliminated(v) && value(v) == l_Undef))
183  elim_heap.update(v); }
184 
185 
186 inline bool SimpSolver::addClause (const vec<Lit>& ps) { ps.copyTo(add_tmp); return addClause_(add_tmp); }
189 inline bool SimpSolver::addClause (Lit p, Lit q) { add_tmp.clear(); add_tmp.push(p); add_tmp.push(q); return addClause_(add_tmp); }
190 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); }
191 inline bool SimpSolver::addClause (Lit p, Lit q, Lit r, Lit s){ add_tmp.clear(); add_tmp.push(p); add_tmp.push(q); add_tmp.push(r); add_tmp.push(s); return addClause_(add_tmp); }
192 inline void SimpSolver::setFrozen (Var v, bool b) { frozen[v] = (char)b; if (use_simplification && !b) { updateElimHeap(v); } }
193 
194 inline void SimpSolver::freezeVar(Var v){
195  if (!frozen[v]){
196  frozen[v] = 1;
197  frozen_vars.push(v);
198  } }
199 
200 inline void SimpSolver::thaw(){
201  for (int i = 0; i < frozen_vars.size(); i++){
202  Var v = frozen_vars[i];
203  frozen[v] = 0;
204  if (use_simplification)
205  updateElimHeap(v);
206  }
207  frozen_vars.clear(); }
208 
209 inline bool SimpSolver::solve ( bool do_simp, bool turn_off_simp) { budgetOff(); assumptions.clear(); return solve_(do_simp, turn_off_simp) == l_True; }
210 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; }
211 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; }
212 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; }
213 inline bool SimpSolver::solve (const vec<Lit>& assumps, bool do_simp, bool turn_off_simp){
214  budgetOff(); assumps.copyTo(assumptions); return solve_(do_simp, turn_off_simp) == l_True; }
215 
216 inline lbool SimpSolver::solveLimited (const vec<Lit>& assumps, bool do_simp, bool turn_off_simp){
217  assumps.copyTo(assumptions); return solve_(do_simp, turn_off_simp); }
218 
219 //=================================================================================================
220 }
221 
222 #endif
void freezeVar(Var v)
Definition: SimpSolver.h:194
RegionAllocator< uint32_t >::Ref CRef
Definition: SolverTypes.h:137
void removeClause(CRef cr)
Definition: SimpSolver.cc:190
ElimLt(const LMap< int > &no)
Definition: SimpSolver.h:113
void budgetOff()
Definition: Solver.h:373
void releaseVar(Lit l)
Definition: SimpSolver.cc:95
void gatherTouchedClauses()
Definition: SimpSolver.cc:294
LMap< int > n_occ
Definition: SimpSolver.h:141
OccLists< Var, vec< CRef >, ClauseDeleted > occurs
Definition: SimpSolver.h:140
Lit mkLit(Var var, bool sign=false)
Definition: SolverTypes.h:63
void setFrozen(Var v, bool b)
Definition: SimpSolver.h:192
Queue< CRef > subsumption_queue
Definition: SimpSolver.h:143
vec< uint32_t > elimclauses
Definition: SimpSolver.h:137
const lbool l_Undef((uint8_t) 2)
void copyTo(vec< T > &copy) const
Definition: Vec.h:92
bool operator()(const CRef &cr) const
Definition: SimpSolver.h:130
Var newVar(lbool upol=l_Undef, bool dvar=true)
Definition: SimpSolver.cc:79
void updateElimHeap(Var v)
Definition: SimpSolver.h:179
virtual void garbageCollect()
Definition: SimpSolver.cc:714
bool merge(const Clause &_ps, const Clause &_qs, Var v, vec< Lit > &out_clause)
Definition: SimpSolver.cc:232
VMap< char > eliminated
Definition: SimpSolver.h:146
bool eliminateVar(Var v)
Definition: SimpSolver.cc:487
bool asymm(Var v, CRef cr)
Definition: SimpSolver.cc:410
bool implied(const vec< Lit > &c)
Definition: SimpSolver.cc:322
vec< Lit > assumptions
Definition: Solver.h:194
const ClauseAllocator & ca
Definition: SimpSolver.h:128
vec< Var > frozen_vars
Definition: SimpSolver.h:145
bool operator()(Var x, Var y) const
Definition: SimpSolver.h:118
void push(void)
Definition: Vec.h:74
bool addClause(const vec< Lit > &ps)
Definition: SimpSolver.h:186
lbool solveLimited(const vec< Lit > &assumps, bool do_simp=true, bool turn_off_simp=false)
Definition: SimpSolver.h:216
Heap< Var, ElimLt > elim_heap
Definition: SimpSolver.h:142
Size size(void) const
Definition: Vec.h:64
bool strengthenClause(CRef cr, Lit l)
Definition: SimpSolver.cc:205
bool asymmVar(Var v)
Definition: SimpSolver.cc:437
bool eliminate(bool turn_off_elim=false)
Definition: SimpSolver.cc:597
VMap< char > frozen
Definition: SimpSolver.h:144
void relocAll(ClauseAllocator &to)
Definition: SimpSolver.cc:686
void toDimacs(FILE *f, const vec< Lit > &assumps)
Definition: Solver.cc:951
bool backwardSubsumptionCheck(bool verbose=false)
Definition: SimpSolver.cc:343
bool isEliminated(Var v) const
Definition: SimpSolver.h:178
const lbool l_True((uint8_t) 0)
ClauseDeleted(const ClauseAllocator &_ca)
Definition: SimpSolver.h:129
lbool value(Var x) const
Definition: Solver.h:350
lbool solve_()
Definition: Solver.cc:841
void clear(bool dealloc=false)
Definition: Vec.h:125
bool addClause_(vec< Lit > &ps)
Definition: SimpSolver.cc:150
int Var
Definition: SolverTypes.h:43
vec< Lit > add_tmp
Definition: Solver.h:227
uint64_t cost(Var x) const
Definition: SimpSolver.h:117
bool substitute(Var v, Lit x)
Definition: SimpSolver.cc:548
double simp_garbage_frac
Definition: SimpSolver.h:94
const LMap< int > & n_occ
Definition: SimpSolver.h:112
bool solve()
Definition: Solver.h:382
VMap< char > touched
Definition: SimpSolver.h:138