forked from karpiu/kp-minisatp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SatSolver.cc
234 lines (213 loc) · 8.11 KB
/
SatSolver.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*************************************************************************************[PbSolver.cc]
KP-MiniSat+ based on MiniSat+ -- Copyright (c) 2018-2020 Michał Karpiński, Marek Piotrów
UWrMaxSat based on KP-MiniSat+ -- Copyright (c) 2019-2024 Marek Piotrów
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
#include "SatSolver.h"
#if !defined(CADICAL) && !defined(CRYPTOMS)
static Var mapVar(Var x, Minisat::vec<Var>& map, Var& max)
{
if (map.size() <= x || map[x] == -1){
map.growTo(x+1, -1);
map[x] = max++;
}
return map[x];
}
#endif
#if !defined(CADICAL) && !defined(CRYPTOMS)
const Minisat::Clause& ExtSimpSolver::getClause (int i, bool &is_satisfied) const
{
const Minisat::Clause& ps = ca[clauses[i]];
is_satisfied = ps.mark() != 0 || satisfied(ps);
return ps;
}
#endif
bool ExtSimpSolver::reduceProblem()
{
bool res = true;
#if !defined(CADICAL) && !defined(CRYPTOMS)
if (use_simplification) res = eliminate();
elimclauses.copyTo(elimClauses);
#endif
return res;
}
#if !defined(CADICAL) && !defined(CRYPTOMS)
void ExtSimpSolver::extendGivenModel(vec<lbool> &model)
{
for (int j, i = elimClauses.size()-1; i > 0; i -= j) {
Lit x;
for (j = elimClauses[i--]; j > 1; j--, i--) {
x = Minisat::toLit(elimClauses[i]);
if ((model[var(x)] ^ sign(x)) != l_False) goto next; // x is no false in the model
}
x = Minisat::toLit(elimClauses[i]);
model[var(x)] = lbool(!sign(x));
next:;
}
}
#else
void ExtSimpSolver::extendGivenModel(vec<lbool> &) {}
#endif
void ExtSimpSolver::printVarsCls(bool encoding, const vec<Pair<weight_t, Minisat::vec<Lit>* > > *soft_cls, int soft_cnt)
{
Minisat::vec<Var> map; Var max=0;
int cnt;
#ifdef CADICAL
max = solver->active();
cnt = solver->irredundant();
(void)soft_cls;
#elif defined(CRYPTOMS)
max = solver->nVars();
cnt = nClauses();
(void)soft_cls;
#else
if (!ok) max=1, cnt=2;
else {
cnt = 0; // assumptions.size();
for (int i = 0; i < clauses.size(); i++)
if (!satisfied(ca[clauses[i]])) {
cnt++;
Minisat::Clause& c = ca[clauses[i]];
for (int j = 0; j < c.size(); j++)
if (value(c[j]) != l_False)
mapVar(var(c[j]), map, max);
}
if (soft_cls != NULL)
for (int i = 0; i < soft_cls->size(); i++) {
Minisat::vec<Lit>& c = *(*soft_cls)[i].snd;
for (int j = 0; j < c.size(); j++)
if (value(c[j]) != l_False)
mapVar(var(c[j]), map, max);
}
}
#endif
printf("c ============================[ %s Statistics ]============================\n",
encoding ? "Encoding" : " Problem");
printf("c | Number of variables: %12d |\n", max);
if (soft_cnt == 0)
printf("c | Number of clauses: %12d |\n", cnt);
else
printf("c | Number of clauses: %12d (incl. %12d soft in queue) |\n", cnt + soft_cnt, soft_cnt);
printf("c ===============================================================================\n");
}
//=================================================================================================
// Propagate and check:
#if defined(CADICAL) || defined(CRYPTOMS)
bool ExtSimpSolver::prop_check(Lit , Minisat::vec<Lit>& props, const vec<Lit>&, int )
{
props.clear();
return okay();
}
#elif defined(MERGESAT)
bool ExtSimpSolver::prop_check(Lit lit, Minisat::vec<Lit>& props, const vec<Lit>& assumptions, int )
{
if (value(lit) != l_Undef) return value(lit) == l_True;
Solver::cancelUntil(0);
if (assumptions.size() > 0) {
while (decisionLevel() < assumptions.size()) {
Lit p = assumptions[decisionLevel()];
if (value(p) == l_False) {
Solver::cancelUntil(0);
return true;
}
Solver::newDecisionLevel();
if (value(p) == l_Undef) Solver::uncheckedEnqueue(p, decisionLevel(), Minisat::CRef_Undef);
}
if (Solver::propagate() != Minisat::CRef_Undef) {
Solver::cancelUntil(0);
return true;
}
}
bool noConflict = true;
Solver::newDecisionLevel();
Solver::uncheckedEnqueue(lit, decisionLevel(), Minisat::CRef_Undef);
int c = trail.size();
if (!ok || (Solver::propagate() != Minisat::CRef_Undef)) noConflict = false;
else // collect trail literals
while (c < trail.size()) props.push(trail[c++]);
Solver::cancelUntil(0);
return noConflict;
}
#else
bool ExtSimpSolver::prop_check(Lit lit, Minisat::vec<Lit>& props, const vec<Lit>& assumptions, int psaving)
{
using Minisat::CRef; using Minisat::CRef_Undef;
props.clear();
if (!ok) return false;
if (value(lit) != l_Undef) return value(lit) == l_True;
CRef confl = CRef_Undef;
// dealing with phase saving
int psaving_copy = phase_saving;
phase_saving = psaving;
// propagate lit at a new decision level
#ifdef MAPLE
int trailRec = trailRecord;
trailRecord = trail.size();
if (assumptions.size() > 0) {
Solver::newDecisionLevel ();
for (int i = 0; i < assumptions.size(); i++) {
Lit p = assumptions[i];
if (value(p) == l_False) {
Solver::cancelUntilTrailRecord(); // backtracking
trailRecord = trailRec;
phase_saving = psaving_copy;
return true;
}
else if (value(p) != l_True) Solver::simpleUncheckedEnqueue(p);
}
confl = Solver::simplePropagate();
}
int newTrailRec = trail.size();
Solver::simpleUncheckEnqueue(lit);
confl = Solver::simplePropagate();
if (confl == CRef_Undef && trail.size() > newTrailRecord) { // copying the result
int c = newTrailRecord;
if (trail[c] == lit) c++;
while (c < trail.size()) props.push(trail[c++]);
}
cancelUntilTrailRecord(); // backtracking
trailRecord = trailRec;
#else
int old_level = decisionLevel();
if (assumptions.size() > 0) {
Solver::newDecisionLevel ();
for (int i = 0; i < assumptions.size(); i++) {
Lit p = assumptions[i];
if (value(p) == l_False) {
Solver::cancelUntil(old_level); // backtracking
phase_saving = psaving_copy;
return true;
}
else if (value(p) != l_True) uncheckedEnqueue(p);
}
confl = Solver::propagate();
}
if (confl == CRef_Undef && value(lit) == l_Undef) {
int level = decisionLevel();
Solver::newDecisionLevel ();
Solver::uncheckedEnqueue(lit);
confl = Solver::propagate();
if (confl == CRef_Undef) { // copying the result
int c = trail_lim[level];
if (trail[c] == lit) c++;
while (c < trail.size()) props.push(trail[c++]);
}
}
Solver::cancelUntil(old_level); // backtracking
#endif
// restoring phase saving
phase_saving = psaving_copy;
return confl == CRef_Undef;
}
#endif