forked from vprover/vampire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinisatInterfacingNewSimp.cpp
263 lines (211 loc) · 6.97 KB
/
MinisatInterfacingNewSimp.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
/*
* File MinisatInterfacingNewSimp.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 MinisatInterfacingNewSimp.cpp
* Implements class MinisatInterfacingNewSimp
*/
#include "Forwards.hpp"
#include "MinisatInterfacingNewSimp.hpp"
#include "Lib/System.hpp"
#include "Shell/UIHelper.hpp"
#include "Lib/ScopedLet.hpp"
#include "Lib/Environment.hpp"
#include "Shell/Statistics.hpp"
#include "Minisat/core/SolverTypes.h"
#include <limits>
namespace SAT
{
using namespace Shell;
using namespace Lib;
using namespace Minisat;
const unsigned MinisatInterfacingNewSimp::VAR_MAX = std::numeric_limits<Minisat::Var>::max() / 2;
MinisatInterfacingNewSimp::MinisatInterfacingNewSimp(const Shell::Options& opts, bool generateProofs):
_status(SATISFIABLE)
{
CALL("MinisatInterfacingNewSimp::MinisatInterfacingNewSimp");
// TODO: consider tuning minisat's options to be set for _solver
// (or even forwarding them to vampire's options)
//_solver.mem_lim(opts.memoryLimit()*2);
limitMemory(opts.memoryLimit()*1);
}
void MinisatInterfacingNewSimp::reportMinisatOutOfMemory() {
env.beginOutput();
reportSpiderStatus('m');
env.out() << "Minisat ran out of memory" << endl;
if(env.statistics) {
env.statistics->print(env.out());
}
#if VDEBUG
Debug::Tracer::printStack(env.out());
#endif
env.endOutput();
System::terminateImmediately(1);
}
/**
* Make the solver handle clauses with variables up to @b newVarCnt
* (but see vampireVar2Minisat!)
*/
void MinisatInterfacingNewSimp::ensureVarCount(unsigned newVarCnt)
{
CALL("MinisatInterfacingNewSimp::ensureVarCount");
try{
while(_solver.nVars() < (int)newVarCnt) {
_solver.newVar();
}
} catch (Minisat::OutOfMemoryException&){
reportMinisatOutOfMemory();
}
}
unsigned MinisatInterfacingNewSimp::newVar()
{
CALL("MinisatInterfacingNewSimp::ensureVarCount");
return minisatVar2Vampire(_solver.newVar());
}
SATSolver::Status MinisatInterfacingNewSimp::solveUnderAssumptions(const SATLiteralStack& assumps, unsigned conflictCountLimit, bool)
{
CALL("MinisatInterfacingNewSimp::solveUnderAssumptions");
ASS(!hasAssumptions());
// load assumptions:
SATLiteralStack::ConstIterator it(assumps);
while (it.hasNext()) {
_assumptions.push(vampireLit2Minisat(it.next()));
}
solveModuloAssumptionsAndSetStatus(conflictCountLimit);
if (_status == SATSolver::UNSATISFIABLE) {
// unload minisat's internal conflict clause to _failedAssumptionBuffer
_failedAssumptionBuffer.reset();
Minisat::LSet& conflict = _solver.conflict;
for (int i = 0; i < conflict.size(); i++) {
_failedAssumptionBuffer.push(minisatLit2Vampire(conflict[i]).opposite());
}
}
_assumptions.clear();
return _status;
}
/**
* Solve modulo assumptions and set status.
* @b conflictCountLimit as with addAssumption.
*/
void MinisatInterfacingNewSimp::solveModuloAssumptionsAndSetStatus(unsigned conflictCountLimit)
{
CALL("MinisatInterfacingNewSimp::solveModuloAssumptionsAndSetStatus");
// TODO: consider calling simplify(); or only from time to time?
try{
//int bef = _solver.nVars();
//cout << "Before: vars " << bef << ", non-unit clauses " << _solver.nClauses() << endl;
_solver.setConfBudget(conflictCountLimit); // treating UINT_MAX as \infty
lbool res = _solver.solveLimited(_assumptions,true,true);
//cout << "After: vars " << bef - _solver.eliminated_vars << ", non-unit clauses " << _solver.nClauses() << endl;
if (res == l_True) {
_status = SATISFIABLE;
} else if (res == l_False) {
_status = UNSATISFIABLE;
} else {
_status = UNKNOWN;
}
}catch(Minisat::OutOfMemoryException&){
reportMinisatOutOfMemory();
}
}
/**
* Add clause into the solver.
*
*/
void MinisatInterfacingNewSimp::addClause(SATClause* cl)
{
CALL("MinisatInterfacingNewSimp::addClause");
// TODO: consider measuring time
ASS_EQ(_assumptions.size(),0);
try {
static vec<Lit> mcl;
mcl.clear();
unsigned clen=cl->length();
for(unsigned i=0;i<clen;i++) {
SATLiteral l = (*cl)[i];
mcl.push(vampireLit2Minisat(l));
}
_solver.addClause(mcl);
} catch (Minisat::OutOfMemoryException&){
reportMinisatOutOfMemory();
}
}
/**
* Perform solving and return status.
*/
SATSolver::Status MinisatInterfacingNewSimp::solve(unsigned conflictCountLimit)
{
CALL("MinisatInterfacingNewSimp::solve");
solveModuloAssumptionsAndSetStatus(conflictCountLimit);
return _status;
}
void MinisatInterfacingNewSimp::addAssumption(SATLiteral lit)
{
CALL("MinisatInterfacingNewSimp::addAssumption");
_assumptions.push(vampireLit2Minisat(lit));
}
SATSolver::VarAssignment MinisatInterfacingNewSimp::getAssignment(unsigned var)
{
CALL("MinisatInterfacingNewSimp::getAssignment");
ASS_EQ(_status, SATISFIABLE);
ASS_G(var,0); ASS_LE(var,(unsigned)_solver.nVars());
lbool res;
Minisat::Var mvar = vampireVar2Minisat(var);
if (mvar < _solver.model.size()) {
if ((res = _solver.modelValue(mvar)) == l_True) {
return TRUE;
} else if (res == l_False) {
return FALSE;
} else {
ASSERTION_VIOLATION;
return NOT_KNOWN;
}
} else { // new vars have been added but the model didn't grow yet
return DONT_CARE;
}
}
bool MinisatInterfacingNewSimp::isZeroImplied(unsigned var)
{
CALL("MinisatInterfacingNewSimp::isZeroImplied");
ASS_G(var,0); ASS_LE(var,(unsigned)_solver.nVars());
/* between calls to _solver.solve*
value is undefined for all accept zero implied variables */
return _solver.value(vampireVar2Minisat(var)) != l_Undef;
}
void MinisatInterfacingNewSimp::collectZeroImplied(SATLiteralStack& acc)
{
CALL("MinisatInterfacingNewSimp::collectZeroImplied");
// TODO: could be made more efficient by inspecting the trail
// [new code would be needed in Minisat::solver, though]
// Minisat's variables start from 0
for (Minisat::Var v = 0; v < _solver.nVars(); v++) {
lbool val = _solver.value(v);
if (val != l_Undef) { // see isZeroImplied
// the lit needs to be negated, if the variable alone is false
acc.push(minisatLit2Vampire(mkLit(v,val == l_False)));
}
}
}
SATClause* MinisatInterfacingNewSimp::getZeroImpliedCertificate(unsigned)
{
CALL("MinisatInterfacingNewSimp::getZeroImpliedCertificate");
// Currently unused anyway.
/* The whole SATSolver interface should be revised before
implementing functions like this one properly */
return 0;
}
} // namespace SAT