1 /************************************************************************************[SimpSolver.h]
2 Copyright (c) 2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
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:
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
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 **************************************************************************************************/
21 #ifndef Minisat_SimpSolver_h
22 #define Minisat_SimpSolver_h
30 //=================================================================================================
33 class SimpSolver
: public Solver
{
35 // Constructor/Destructor:
40 // Problem specification:
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).
55 void setFrozen (Var v
, bool b
); // If a variable is frozen it will not be eliminated.
56 bool isEliminated(Var v
) const;
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.
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.
75 virtual void garbageCollect();
78 // Generate a (possibly simplified) DIMACS file:
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
);
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.
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').
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.
109 // Helper structures:
112 const LMap
<int>& n_occ
;
113 explicit ElimLt(const LMap
<int>& no
) : n_occ(no
) {}
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
); }
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; }
127 struct ClauseDeleted
{
128 const ClauseAllocator
& ca
;
129 explicit ClauseDeleted(const ClauseAllocator
& _ca
) : ca(_ca
) {}
130 bool operator()(const CRef
& cr
) const { return ca
[cr
].mark() == 1; } };
135 bool use_simplification
;
136 Var max_simp_var
; // Max variable at the point simplification was turned off.
137 vec
<uint32_t> elimclauses
;
139 OccLists
<Var
, vec
<CRef
>, ClauseDeleted
>
142 Heap
<Var
,ElimLt
> elim_heap
;
143 Queue
<CRef
> subsumption_queue
;
145 vec
<Var
> frozen_vars
;
146 VMap
<char> eliminated
;
154 // Main internal methods:
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
);
167 void removeClause (CRef cr
);
168 bool strengthenClause (CRef cr
, Lit l
);
169 bool implied (const vec
<Lit
>& c
);
170 void relocAll (ClauseAllocator
& to
);
174 //=================================================================================================
175 // Implementation of inline methods:
178 inline bool SimpSolver::isEliminated (Var v
) const { return eliminated
[v
]; }
179 inline void SimpSolver::updateElimHeap(Var 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
); }
186 inline bool SimpSolver::addClause (const vec
<Lit
>& ps
) { ps
.copyTo(add_tmp
); return addClause_(add_tmp
); }
187 inline bool SimpSolver::addEmptyClause() { add_tmp
.clear(); return addClause_(add_tmp
); }
188 inline bool SimpSolver::addClause (Lit p
) { add_tmp
.clear(); add_tmp
.push(p
); 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
); } }
194 inline void SimpSolver::freezeVar(Var v
){
200 inline void SimpSolver::thaw(){
201 for (int i
= 0; i
< frozen_vars
.size(); i
++){
202 Var v
= frozen_vars
[i
];
204 if (use_simplification
)
207 frozen_vars
.clear(); }
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
; }
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
); }
219 //=================================================================================================