forked from vprover/vampire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimizingSolver.cpp
264 lines (226 loc) · 6.43 KB
/
MinimizingSolver.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* File MinimizingSolver.cpp.
*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*
* In summary, you are allowed to use Vampire for non-commercial
* purposes but not allowed to distribute, modify, copy, create derivatives,
* or use in competitions.
* For other uses of Vampire please contact developers for a different
* licence, which we will make an effort to provide.
*/
/**
* @file MinimizingSolver.cpp
* Implements class MinimizingSolver.
*/
#include "SAT/SATClause.hpp"
#include "MinimizingSolver.hpp"
namespace SAT
{
MinimizingSolver::MinimizingSolver(SATSolver* inner)
: _varCnt(0), _inner(inner), _assignmentValid(false), _heap(CntComparator(_unsClCnt))
{
CALL("MinimizingSolver::MinimizingSolver");
}
void MinimizingSolver::ensureVarCount(unsigned newVarCnt)
{
CALL("MinimizingSolver::ensureVarCount");
if (newVarCnt<= _varCnt) {
return;
}
_varCnt = newVarCnt;
_inner->ensureVarCount(newVarCnt);
_asgn.expand(newVarCnt+1);
_watcher.expand(newVarCnt+1);
_unsClCnt.expand(newVarCnt+1, 0);
_heap.elMap().expand(newVarCnt+1);
_clIdx.expand(newVarCnt+1);
_assignmentValid = false;
}
void MinimizingSolver::addClause(SATClause* cl)
{
CALL("MinimizingSolver::addClause");
// pass it to inner ...
_inner->addClause(cl);
_assignmentValid = false;
// ... and also keep track for minimization
if (cl->length()!=0) {
//we need to filter out the empty clause -- it won't have any influence on our algorithm
//(as it will make the problem unsat and we process only satisfiable assignment), but it
//is a corner case that needs to be handled
_unprocessed.push(cl);
}
}
void MinimizingSolver::addClauseIgnoredInPartialModel(SATClause* cl)
{
CALL("MinimizingSolver::addClauseIgnoredInPartialModel");
// just passing to _inner, but for minimization it will be ignored
_inner->addClause(cl);
_assignmentValid = false;
}
SATSolver::Status MinimizingSolver::solve(unsigned conflictCountLimit)
{
CALL("MinimizingSolver::solve");
_assignmentValid = false;
return _inner->solve(conflictCountLimit);
}
SATSolver::VarAssignment MinimizingSolver::getAssignment(unsigned var)
{
CALL("MinimizingSolver::getAssignment");
ASS_G(var,0); ASS_LE(var,_varCnt);
if(!_assignmentValid) {
updateAssignment();
}
if(admitsDontcare(var)) {
return SATSolver::DONT_CARE;
}
return _asgn[var] ? SATSolver::TRUE : SATSolver::FALSE;
}
bool MinimizingSolver::isZeroImplied(unsigned var)
{
CALL("MinimizingSolver::isZeroImplied");
ASS_G(var,0); ASS_LE(var,_varCnt);
bool res = _inner->isZeroImplied(var);
ASS(!res || getAssignment(var)!=DONT_CARE); //zero-implied variables will not become a don't care
return res;
}
/**
* Give a concrete value (as opposed to don't-care) to the given variable.
*/
void MinimizingSolver::selectVariable(unsigned var)
{
CALL("MinimizingSolver::selectVariable");
ASS_G(var,0); ASS_LE(var,_varCnt);
ASS_G(_unsClCnt[var],0);
SATClauseStack& satisfied = _clIdx[var];
SATClauseStack& watch = _watcher[var];
while(satisfied.isNonEmpty()) {
SATClause* cl = satisfied.pop();
if(!_satisfiedClauses.insert(cl)) {
continue;
}
watch.push(cl);
SATClause::Iterator cit(*cl);
while(cit.hasNext()) {
SATLiteral cl_lit = cit.next();
unsigned cl_var = cl_lit.var();
if (cl_lit.polarity() == _asgn[cl_var]) {
ASS_G(_unsClCnt[cl_var], 0);
_unsClCnt[cl_var]--;
if (cl_var != var) { // var has been just popped
_heap.notifyIncrease(cl_var); //It was an increase wrt max-heap
}
}
}
}
}
void MinimizingSolver::putIntoIndex(SATClause* cl)
{
CALL("MinimizingSolver::putIntoIndex");
SATClause::Iterator cit(*cl);
while(cit.hasNext()) {
SATLiteral lit = cit.next();
unsigned var = lit.var();
if (lit.polarity() == _asgn[var]) {
_clIdx[var].push(cl);
_unsClCnt[var]++;
}
}
}
bool MinimizingSolver::tryPuttingToAnExistingWatch(SATClause* cl)
{
CALL("MinimizingSolver::tryPuttingToAnExistingWatch");
SATClause::Iterator cit(*cl);
while(cit.hasNext()) {
SATLiteral lit = cit.next();
unsigned var = lit.var();
if(_asgn[var]==lit.polarity() && !admitsDontcare(var)) {
ALWAYS(_satisfiedClauses.insert(cl));
_watcher[var].push(cl);
return true;
}
}
return false;
}
/**
* Move satisfied unprocessed clauses into an appropriate watch, and
* unsatisfied unprocessed clauses into _clIdx
*/
void MinimizingSolver::processUnprocessedAndFillHeap()
{
CALL("MinimizingSolver::processUnprocessed");
while(_unprocessed.isNonEmpty()) {
SATClause* cl = _unprocessed.pop();
ASS_G(cl->length(),0)
if(!tryPuttingToAnExistingWatch(cl)) {
putIntoIndex(cl);
}
}
for(unsigned var=1; var<=_varCnt; var++) {
ASS(!_heap.contains(var));
if(_unsClCnt[var]>0) {
_heap.addToEnd(var);
}
}
_heap.heapify();
}
/**
* Update the values in _asgn and move the clauses whose watch
* became unsatisfied to _unprocessed.
*/
void MinimizingSolver::processInnerAssignmentChanges()
{
CALL("MinimizingSolver::processInnerAssignmentChanges");
for(unsigned v=1; v<=_varCnt; v++) {
VarAssignment va = _inner->getAssignment(v);
bool changed;
switch(va) {
case DONT_CARE:
changed = false;
break;
case TRUE:
changed = !_asgn[v];
_asgn[v] = true;
break;
case FALSE:
changed = _asgn[v];
_asgn[v] = false;
break;
case NOT_KNOWN:
default:
ASSERTION_VIOLATION;
break;
}
if(changed) {
SATClauseStack& watch = _watcher[v];
_unprocessed.loadFromIterator(SATClauseStack::Iterator(watch));
_satisfiedClauses.removeIteratorElements(SATClauseStack::Iterator(watch));
watch.reset();
}
}
}
void MinimizingSolver::updateAssignment()
{
CALL("MinimizingSolver::updateAssignment");
TimeCounter tca(TC_MINIMIZING_SOLVER);
processInnerAssignmentChanges();
processUnprocessedAndFillHeap();
while (!_heap.isEmpty()) {
unsigned best_var = _heap.pop();
if (_unsClCnt[best_var] > 0) {
selectVariable(best_var);
ASS_EQ(_unsClCnt[best_var],0);
ASS(_clIdx[best_var].isEmpty());
} else {
_clIdx[best_var].reset();
}
}
_assignmentValid = true;
}
}