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