abc-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 (bool polarity = true, bool dvar = true);
43  bool addClause (const vec<Lit>& ps);
44  bool addEmptyClause(); // Add the empty clause to the solver.
45  bool addClause (Lit p); // Add a unit clause to the solver.
46  bool addClause (Lit p, Lit q); // Add a binary clause to the solver.
47  bool addClause (Lit p, Lit q, Lit r); // Add a ternary clause to the solver.
48  bool addClause_( vec<Lit>& ps);
49  bool substitute(Var v, Lit x); // Replace all occurences of v with x (may cause a contradiction).
50 
51  // Variable mode:
52  //
53  void setFrozen (Var v, bool b); // If a variable is frozen it will not be eliminated.
54  bool isEliminated(Var v) const;
55 
56  // Solving:
57  //
58  bool solve (const vec<Lit>& assumps, bool do_simp = true, bool turn_off_simp = false);
59  lbool solveLimited(const vec<Lit>& assumps, bool do_simp = true, bool turn_off_simp = false);
60  bool solve ( bool do_simp = true, bool turn_off_simp = false);
61  bool solve (Lit p , bool do_simp = true, bool turn_off_simp = false);
62  bool solve (Lit p, Lit q, bool do_simp = true, bool turn_off_simp = false);
63  bool solve (Lit p, Lit q, Lit r, bool do_simp = true, bool turn_off_simp = false);
64  bool eliminate (bool turn_off_elim = false); // Perform variable elimination based simplification.
65 
66  // Memory managment:
67  //
68  virtual void garbageCollect();
69 
70 
71  // Generate a (possibly simplified) DIMACS file:
72  //
73 #if 0
74  void toDimacs (const char* file, const vec<Lit>& assumps);
75  void toDimacs (const char* file);
76  void toDimacs (const char* file, Lit p);
77  void toDimacs (const char* file, Lit p, Lit q);
78  void toDimacs (const char* file, Lit p, Lit q, Lit r);
79 #endif
80 
81  // Mode of operation:
82  //
83  int grow; // Allow a variable elimination step to grow by a number of clauses (default to zero).
84  int clause_lim; // Variables are not eliminated if it produces a resolvent with a length above this limit.
85  // -1 means no limit.
86  int subsumption_lim; // Do not check if subsumption against a clause larger than this. -1 means no limit.
87  double simp_garbage_frac; // A different limit for when to issue a GC during simplification (Also see 'garbage_frac').
88 
89  bool use_asymm; // Shrink clauses by asymmetric branching.
90  bool use_rcheck; // Check if a clause is already implied. Prett costly, and subsumes subsumptions :)
91  bool use_elim; // Perform variable elimination.
92 
93  // Statistics:
94  //
95  int merges;
98 
99  protected:
100 
101  // Helper structures:
102  //
103  struct ElimLt {
104  const vec<int>& n_occ;
105  explicit ElimLt(const vec<int>& no) : n_occ(no) {}
106 
107  // TODO: are 64-bit operations here noticably bad on 32-bit platforms? Could use a saturating
108  // 32-bit implementation instead then, but this will have to do for now.
109  uint64_t cost (Var x) const { return (uint64_t)n_occ[toInt(mkLit(x))] * (uint64_t)n_occ[toInt(~mkLit(x))]; }
110  bool operator()(Var x, Var y) const { return cost(x) < cost(y); }
111 
112  // TODO: investigate this order alternative more.
113  // bool operator()(Var x, Var y) const {
114  // int c_x = cost(x);
115  // int c_y = cost(y);
116  // return c_x < c_y || c_x == c_y && x < y; }
117  };
118 
119  struct ClauseDeleted {
121  explicit ClauseDeleted(const ClauseAllocator& _ca) : ca(_ca) {}
122  bool operator()(const CRef& cr) const { return ca[cr].mark() == 1; } };
123 
124  // Solver state:
125  //
139 
140  // Temporaries:
141  //
143 
144  // Main internal methods:
145  //
146  lbool solve_ (bool do_simp = true, bool turn_off_simp = false);
147  bool asymm (Var v, CRef cr);
148  bool asymmVar (Var v);
149  void updateElimHeap (Var v);
150  void gatherTouchedClauses ();
151  bool merge (const Clause& _ps, const Clause& _qs, Var v, vec<Lit>& out_clause);
152  bool merge (const Clause& _ps, const Clause& _qs, Var v, int& size);
153  bool backwardSubsumptionCheck (bool verbose = false);
154  bool eliminateVar (Var v);
155  void extendModel ();
156 
157  void removeClause (CRef cr);
158  bool strengthenClause (CRef cr, Lit l);
159  void cleanUpClauses ();
160  bool implied (const vec<Lit>& c);
161  void relocAll (ClauseAllocator& to);
162 };
163 
164 
165 //=================================================================================================
166 // Implementation of inline methods:
167 
168 
169 inline bool SimpSolver::isEliminated (Var v) const { return eliminated[v]; }
172  // if (!frozen[v] && !isEliminated(v) && value(v) == l_Undef)
173  if (elim_heap.inHeap(v) || (!frozen[v] && !isEliminated(v) && value(v) == l_Undef))
174  elim_heap.update(v); }
175 
176 
177 inline bool SimpSolver::addClause (const vec<Lit>& ps) { ps.copyTo(add_tmp); return addClause_(add_tmp); }
178 inline bool SimpSolver::addEmptyClause() { add_tmp.clear(); return addClause_(add_tmp); }
179 inline bool SimpSolver::addClause (Lit p) { add_tmp.clear(); add_tmp.push(p); return addClause_(add_tmp); }
180 inline bool SimpSolver::addClause (Lit p, Lit q) { add_tmp.clear(); add_tmp.push(p); add_tmp.push(q); return addClause_(add_tmp); }
181 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); }
182 inline void SimpSolver::setFrozen (Var v, bool b) { frozen[v] = (char)b; if (use_simplification && !b) { updateElimHeap(v); } }
183 
184 inline bool SimpSolver::solve ( bool do_simp, bool turn_off_simp) { budgetOff(); assumptions.clear(); return solve_(do_simp, turn_off_simp) == l_True; }
185 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; }
186 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; }
187 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; }
188 inline bool SimpSolver::solve (const vec<Lit>& assumps, bool do_simp, bool turn_off_simp){
189  budgetOff(); assumps.copyTo(assumptions); return solve_(do_simp, turn_off_simp) == l_True; }
190 
191 inline lbool SimpSolver::solveLimited (const vec<Lit>& assumps, bool do_simp, bool turn_off_simp){
192  assumps.copyTo(assumptions); return solve_(do_simp, turn_off_simp); }
193 
194 //=================================================================================================
195 }
196 
197 #endif
RegionAllocator< uint32_t >::Ref CRef
Definition: SolverTypes.h:120
void removeClause(CRef cr)
Definition: SimpSolver.cpp:175
vec< int > n_occ
Definition: SimpSolver.h:132
ElimLt(const vec< int > &no)
Definition: SimpSolver.h:105
void budgetOff()
Definition: Solver.h:343
OccLists< Var, vec< CRef >, ClauseDeleted > occurs
Definition: SimpSolver.h:131
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define l_Undef
Definition: SolverTypes.h:86
void setFrozen(Var v, bool b)
Definition: SimpSolver.h:182
Queue< CRef > subsumption_queue
Definition: SimpSolver.h:134
vec< char > polarity
Definition: Solver.h:180
vec< uint32_t > elimclauses
Definition: SimpSolver.h:128
#define l_True
Definition: SolverTypes.h:84
Lit mkLit(Var var, bool sign)
Definition: SolverTypes.h:58
bool operator()(const CRef &cr) const
Definition: SimpSolver.h:122
void copyTo(vec< T > &copy) const
Definition: Vec.h:90
void updateElimHeap(Var v)
Definition: SimpSolver.h:170
virtual void garbageCollect()
Definition: SimpSolver.cpp:706
bool merge(const Clause &_ps, const Clause &_qs, Var v, vec< Lit > &out_clause)
Definition: SimpSolver.cpp:217
bool eliminateVar(Var v)
Definition: SimpSolver.cpp:471
bool asymm(Var v, CRef cr)
Definition: SimpSolver.cpp:394
bool implied(const vec< Lit > &c)
Definition: SimpSolver.cpp:306
vec< Lit > assumptions
Definition: Solver.h:188
const ClauseAllocator & ca
Definition: SimpSolver.h:120
bool operator()(Var x, Var y) const
Definition: SimpSolver.h:110
bool addClause(const vec< Lit > &ps)
Definition: SimpSolver.h:177
lbool solveLimited(const vec< Lit > &assumps, bool do_simp=true, bool turn_off_simp=false)
Definition: SimpSolver.h:191
vec< char > touched
Definition: SimpSolver.h:129
static int size
Definition: cuddSign.c:86
bool strengthenClause(CRef cr, Lit l)
Definition: SimpSolver.cpp:190
bool asymmVar(Var v)
Definition: SimpSolver.cpp:421
bool eliminate(bool turn_off_elim=false)
Definition: SimpSolver.cpp:582
void relocAll(ClauseAllocator &to)
Definition: SimpSolver.cpp:682
Heap< ElimLt > elim_heap
Definition: SimpSolver.h:133
void toDimacs(FILE *f, const vec< Lit > &assumps)
Definition: Solver.cpp:831
bool backwardSubsumptionCheck(bool verbose=false)
Definition: SimpSolver.cpp:327
bool isEliminated(Var v) const
Definition: SimpSolver.h:169
const vec< int > & n_occ
Definition: SimpSolver.h:104
ClauseDeleted(const ClauseAllocator &_ca)
Definition: SimpSolver.h:121
lbool value(Var x) const
Definition: Solver.h:321
lbool solve_()
Definition: Solver.cpp:751
Var newVar(bool polarity=true, bool dvar=true)
Definition: SimpSolver.cpp:76
vec< char > eliminated
Definition: SimpSolver.h:136
bool addClause_(vec< Lit > &ps)
Definition: SimpSolver.cpp:135
int Var
Definition: SolverTypes.h:42
vec< Lit > add_tmp
Definition: Solver.h:201
#define assert(ex)
Definition: util_old.h:213
uint64_t cost(Var x) const
Definition: SimpSolver.h:109
bool substitute(Var v, Lit x)
Definition: SimpSolver.cpp:533
double simp_garbage_frac
Definition: SimpSolver.h:87
vec< char > frozen
Definition: SimpSolver.h:135
int toInt(Var v)
Definition: SolverTypes.h:65
bool solve()
Definition: Solver.h:352