-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cc
358 lines (323 loc) · 12.4 KB
/
Main.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*****************************************************************************************[Main.cc]
Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
Copyright (c) 2007-2011, Niklas Sorensson
Copyright 2019 Richard Sanger, Wand Network Research Group
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 <algorithm>
#include <array>
#include <errno.h>
#include <iostream>
#include <signal.h>
#include <sstream>
#include <string>
#include <vector>
#include <zmqpp/zmqpp.hpp>
#include <minisat/core/Dimacs.h>
#include <minisat/simp/SimpSolver.h>
#include <minisat/utils/Options.h>
#include <minisat/utils/ParseUtils.h>
#include <minisat/utils/System.h>
#define VERSION "0.1"
using namespace Minisat;
//=================================================================================================
void printStats(Solver &solver) {
double cpu_time = cpuTime();
double mem_used = memUsedPeak();
printf("restarts : %" PRIu64 "\n", solver.starts);
printf("conflicts : %-12" PRIu64 " (%.0f /sec)\n", solver.conflicts, solver.conflicts / cpu_time);
printf("decisions : %-12" PRIu64 " (%4.2f %% random) (%.0f /sec)\n", solver.decisions,
(float)solver.rnd_decisions * 100 / (float)solver.decisions, solver.decisions / cpu_time);
printf("propagations : %-12" PRIu64 " (%.0f /sec)\n", solver.propagations,
solver.propagations / cpu_time);
printf("conflict literals : %-12" PRIu64 " (%4.2f %% deleted)\n", solver.tot_literals,
(solver.max_literals - solver.tot_literals) * 100 / (double)solver.max_literals);
if (mem_used != 0)
printf("Memory used : %.2f MB\n", mem_used);
printf("CPU time : %g s\n", cpu_time);
}
//=================================================================================================
// Main:
static Solver *solver;
// Note that '_exit()' rather than 'exit()' has to be used. The reason is that 'exit()' calls
// destructors and may cause deadlocks if a malloc/free function happens to be running (these
// functions are guarded by locks for multithreaded use).
static void SIGINT_exit(int) {
printf("\n");
printf("*** INTERRUPTED ***\n");
if (solver->verbosity > 0) {
printStats(*solver);
printf("\n");
printf("*** INTERRUPTED ***\n");
}
_exit(1);
}
//=================================================================================================
// Main:
using namespace std;
template <class T> void add_clause(T &s, string &clause, string &full_dimacs) {
// void add_clause(Solver& s, string &clause) {
int var;
stringstream ss(clause);
vec<Lit> lits;
full_dimacs += clause + " 0\n";
// Loop all vars included
while (ss >> var) {
if (var == 0) { // Allow DIMACS style, with 0 terminator
s.addClause(lits);
lits.clear();
continue;
}
// minisat is 0 indexed dimcas is 1
int abs_var = abs(var) - 1;
while (abs_var >= s.nVars())
s.newVar();
lits.push((var > 0) ? mkLit(abs_var) : ~mkLit(abs_var));
}
s.addClause(lits);
}
void collect_clause(string &clause, vector<vector<Lit>> &full_problem) {
int var;
stringstream ss(clause);
vector<Lit> lits;
// Loop all vars included
while (ss >> var) {
// minisat is 0 indexed dimcas is 1
int abs_var = abs(var) - 1;
lits.push_back((var > 0) ? mkLit(abs_var) : ~mkLit(abs_var));
}
full_problem.push_back(lits);
}
vector<Var> parse_vars(string &var_string) {
// Parse a string of variable numbers in list of Var's
int var;
vector<Var> ret;
stringstream ss(var_string);
while (ss >> var)
ret.push_back(var - 1);
return ret;
}
void print_solution(Solver &solv) {
cout << "SAT\n";
for (int i = 0; i < solv.nVars(); i++)
if (solv.model[i] != l_Undef)
cout << ((i == 0) ? "" : " ") << ((solv.model[i] == l_True) ? "" : "-") << i + 1;
cout << " 0\n";
}
/*
o: (O)utput the full problem in the dimacs format
d: Add clauses from a (d)imacs file (only incremental), now UNUSED
r: Add a clause incrementally [(r)eceive]
f: (F)inish and exit
s: (S)olve and return a string of true variables
p: Register (p)lacement variables which used to generate the result.
These are disallowed on the next iteration.
c: Register variables which you (c)are about. I.e. 's' only returns these
*/
template <class T> void handle_requests(T &s, bool incr, const char *ipc_path, int verb) {
string endpoint = "ipc:///tmp/";
string full_dimacs;
endpoint += ipc_path;
zmqpp::context context;
vector<vector<Lit>> full_problem;
vector<Lit> blocking_clause;
zmqpp::socket_type type = zmqpp::socket_type::rep;
zmqpp::socket socket(context, type);
if (verb)
cout << "Connecting to " << endpoint << endl;
socket.connect(endpoint);
vector<Var> cares;
vector<Var> places;
while (1) {
uint8_t type;
zmqpp::message message_recv;
zmqpp::message message_send;
string a;
gzFile in;
T new_solver;
int n_clauses;
socket.receive(message_recv);
message_recv >> type;
switch (type) {
case 'o':
// Return in dimacs format
n_clauses = count(full_dimacs.begin(), full_dimacs.end(), '\n');
a += "p cnf ";
a += to_string(s.nVars()); // Number of vars
a += " ";
a += to_string(n_clauses);
a += "\n";
a += full_dimacs;
message_send.add(a);
break;
case 'd':
if (incr) {
message_recv >> a;
if (verb)
cout << "Loading file: " << a;
in = gzopen(a.c_str(), "rb");
parse_DIMACS(in, s);
gzclose(in);
message_send.add("OK");
} else {
message_send.add("NOT_SUPPORTED");
}
break;
case 'p':
message_recv >> a;
if (verb)
cout << "Place vars are : " << a << endl;
places = parse_vars(a);
// What do we care about?
message_send.add("OK");
break;
case 'c':
message_recv >> a;
if (verb)
cout << "We care about : " << a << endl;
cares = parse_vars(a);
// What do we care about?
message_send.add("OK");
// s.eliminate(false);
s.simplify();
if (verb)
cout << "Simplify" << endl;
break;
case 'r':
// Sadly only new versions of zmqpp have remaining()
// 1st part was the 'type'
for (size_t i = 1; i < message_recv.parts(); i++) {
message_recv >> a;
if (incr) {
add_clause(s, a, full_dimacs);
} else {
collect_clause(a, full_problem);
}
}
message_send.add("OK");
break;
case 'f':
if (verb)
cout << "Closing, goodbye" << endl;
message_send.add("OK");
socket.send(message_send);
return;
case 's':
T &solv = incr ? s : new_solver;
if (!incr) {
for (auto &a : full_problem) {
vec<Lit> vs;
for (auto &l : a) {
vs.push(l);
while (l.x / 2 >= solv.nVars())
solv.newVar();
}
solv.addClause_(vs);
}
}
if (std::is_same<SimpSolver, T>::value) {
SimpSolver &solv2 = static_cast<SimpSolver &>(solv);
if (verb)
cout << "Eliminate" << endl;
solv2.eliminate(true);
} else {
// solv.simplify();
// if (verb)
// cout<<"Simplify"<<endl;
}
// Give me a solution
vec<Lit> dummy;
if (solv.solveLimited(dummy) == l_True) {
// printStats(s);
// Collect the result
string out;
if (cares.empty()) {
for (Var i = 0; i < solv.nVars(); i++) {
if (solv.modelValue(i) == l_True) {
if (!out.empty())
out += " ";
out += to_string(i + 1);
}
}
} else {
for (Var i : cares) {
if (solv.modelValue(i) == l_True) {
if (!out.empty())
out += " ";
out += to_string(i + 1);
}
blocking_clause.push_back(mkLit(i, solv.modelValue(i) == l_True));
}
}
// Return the result
message_send.add(out);
if (verb >= 2)
print_solution(solv);
// Add a clause to stop this combination
// Collect only the placement clauses
blocking_clause.clear();
if (places.empty()) {
for (Var i = 0; i < solv.nVars(); i++) {
blocking_clause.push_back(mkLit(i, solv.modelValue(i) == l_True));
}
} else {
for (Var i : places) {
blocking_clause.push_back(mkLit(i, solv.modelValue(i) == l_True));
}
}
// Add counter clause for next time
if (incr) {
vec<Lit> a;
for (auto &l : blocking_clause) {
a.push(l);
full_dimacs += sign(l) ? "-" : "";
full_dimacs += to_string(var(l) + 1);
full_dimacs += " ";
}
full_dimacs += "0\n";
solv.addClause_(a);
} else {
full_problem.push_back(blocking_clause);
}
} else {
if (verb)
cout << "UNSAT" << endl;
message_send.add("UNSAT");
}
break;
}
socket.send(message_send);
}
}
int main(int argc, char **argv) {
try {
setUsageHelp("USAGE: %s [options] -ipc-name=<name>\nVERSION: " VERSION "\n");
// Extra options:
//
IntOption verb("MAIN", "verb", "Verbosity level (0=silent, 1=some, 2=more).", 0, IntRange(0, 2));
StringOption ipc_name("MAIN", "ipc-name", "zmq socket name ipc:/tmp/<ipc-path>", "dminisat");
parseOptions(argc, argv, true);
Solver S;
solver = &S;
// Use signal handlers that forcibly quit:
signal(SIGINT, SIGINT_exit);
signal(SIGXCPU, SIGINT_exit);
handle_requests(S, true, ipc_name, verb);
return 0;
} catch (OutOfMemoryException &) {
printf("===============================================================================\n");
printf("INDETERMINATE\n");
exit(0);
}
}