-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolver.cpp
604 lines (557 loc) · 14.3 KB
/
solver.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_set>
#include <set>
#include <map>
#include <unordered_map>
#include <string>
#include <ctime>
//#include <chrono>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
//using namespace std::chrono;
void helperMsg(){
cout<<"input format: solver <input_file>"<<endl;
}
void formatIllegal(){
cout<<"file format illegal"<<endl;
}
void showMatrix(vector<vector<int>>& matrix){
for (int i = 0; i < matrix.size(); i++)
{
for (int j = 0; j < matrix[0].size(); j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
/**
* return true if at current state, the clauses is true or undecidable
*
**/
bool isValid(vector<vector<int>>& clauses, vector<int>& model){
for (int i = 0; i < clauses.size(); i++)
{
bool isClouseValid = false;
for (int j = 1; j < clauses[0].size(); j++)
{
int oper1 = clauses[i][j];
int oper2 = model[j];
// one element in the clause returns true
if(oper1 * oper2 == 1){
isClouseValid = true;
break;
}
// one element in the clause is undecidable, return true
else if(oper1 != 0 && oper2 == 0){
isClouseValid = true;
break;
}
}
// current clouse is not satisfiable
if(isClouseValid == false){
return false;
}
}
return true;
}
bool findAModel(vector<vector<int>>& clauses, vector<int>& model, int pos){
// base condition
int options[] = {-1, 1}; // -1 means false, 1 means true
if(pos == model.size()){
return true;
}
for (int i = 0; i < 2; i++)
{
model[pos] = options[i];
if(isValid(clauses, model)){
bool isFound = findAModel(clauses, model, pos + 1);
if(isFound == true){
return true;
}
}
}
model[pos] = 0; // mark as undecided
return false;
}
struct SetCmp
{
bool operator()(const pair<int, int>&a, const pair<int, int>&b){
if(a.second > b.second){
return true;
}else if(a.second < b.second){
return false;
}else{
if(a.first < b.first){
return true;
}else{
return false;
}
}
}
};
class VariableCounter
{
public:
VariableCounter();
VariableCounter(vector<int> counterdata);
VariableCounter(const VariableCounter & a);
~VariableCounter();
int next();
bool hasNext();
void reset();
void print();
private:
// first - index
// second - count
set<pair<int, int>, SetCmp> counter;
set<pair<int, int>, SetCmp>::iterator it;
};
VariableCounter::VariableCounter()
{
}
VariableCounter::VariableCounter(vector<int> counterdata)
{
for(int i = 2; i < counterdata.size(); i++){
this->counter.insert(make_pair(i, counterdata[i]));
}
// reset iterator
reset();
}
void VariableCounter::print(){
for (auto it = counter.begin(); it != counter.end(); it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
}
VariableCounter::~VariableCounter()
{
}
bool VariableCounter::hasNext(){
return this->it != this->counter.end();
}
int VariableCounter::next(){
if(!hasNext()){
return 0;
}
int v = this->it->first;
this->it++;
return v;
}
void VariableCounter::reset(){
this->it = this->counter.begin();
}
class Chaff
{
public:
/**
* 0 1 2 -3 4
* 0 2 3
* 0 -1 4
* column 0 is always set to 0, reserved
*
**/
vector<map<int, int>> clausesRow;
vector<map<int, int>> clausesCol;
int numOfClauses;
int numOfVariables;
bool isConflict;
/**
* count and sort variables in each polarity in clauses by frequency in descending order
*
**/
VariableCounter* vc;
/**
* decision stack,
* second - true means tried both sides
* second - false means tried one side
* first - sign indicates polarity
**/
stack<pair<int, bool>> decisionStack;
/**
* decisionTable is used for quick access by index
* it is consistent to decisionStack in information
* column 0 is reserved.
* 1 - true
* -1 - false
* 0 - unassigned (may be stored in implicationTable)
**/
vector<int> decisionTable;
/**
* implication table
* vector index indicate the variable index
* first - 1 means true, 0 means unassigned, -1 means false
* second - indicates the stack height when the variable is implied
**/
vector<pair<int, int>> implicationTable;
/**
* index of two watched variables in each clause
* watchVariable[i][j] , 0 <= i < numOfClouses, j = 0, 1
**/
vector<vector<int>> watchVariable;
/**
* try to assign an value to an unassigned variable based on the algorithm elaborated in section 3, p532
* return value:
* true - a variable is assigned successfully
* false - no unassigned variables
**/
bool decide();
/**
* try to find an implication and assign it to model
* return value:
* true - no implication can be found
* false - conflict happens
**/
bool bcp();
/**
* backtracking algorithm, flip the last assigned not tried both ways variables
* return value:
* true - resolved a conflict
* false - can not resolve conflict
**/
bool resolveConflict();
/**
* initialize
*
**/
void init(vector<vector<int>>& clauses);
/**
* populate model from decision table and implication table
**/
void genModel();
public:
/**
* 0 1 -1 1 1 -1 -1
* column 0 is always set to 0, reserved
* 0 means unassigned
* 1 means true
* -1 means false
**/
vector<int> model;
void printModel();
void printVectorSet(vector<map<int, int>>& mp);
Chaff(vector<vector<int>>& clauses);
~Chaff();
bool solve();
vector<int> getModel();
};
void Chaff::printVectorSet(vector<map<int, int>>& mp){
for(int i = 0; i < mp.size(); i++){
for(auto it = mp[i].begin(); it != mp[i].end(); it++){
cout<<"idx = "<<i<<", key = "<<it->first<<", value = "<<it->second<<endl;
}
}
}
Chaff::Chaff(vector<vector<int>>& clauses)
{
this->numOfClauses = clauses.size();
if(this->numOfClauses == 0){
throw "clauses can not be empty.";
}
this->numOfVariables = clauses[0].size() - 1;
if(this->numOfVariables <= 0){
throw "number of variables in a clause should be equal or more than 1";
}
init(clauses);
}
void Chaff::init(vector<vector<int>>& clauses){
isConflict = false;
// init clausesRow, clausesCol
this->clausesRow = vector<map<int, int>>(numOfClauses, map<int, int>());
this->clausesCol = vector<map<int, int>>(numOfVariables + 1, map<int, int>());
for (int i = 0; i < numOfClauses; ++i)
{
for (int j = 1; j < numOfVariables + 1; ++j)
{
if(clauses[i][j] == 0){
continue;
}
this->clausesRow[i][j] = clauses[i][j];
this->clausesCol[j][i] = clauses[i][j];
}
}
// init decisionTable and implicationTable
this->decisionTable = vector<int>(numOfVariables + 1, 0);
this->implicationTable = vector<pair<int, int>>(numOfVariables + 1, make_pair(0, 0));
// init watchVariables
this->watchVariable = vector<vector<int>>(numOfClauses, vector<int>(2, 0));
for (int i = 0; i < watchVariable.size(); i++)
{
if(this->clausesRow[i].size() < 1){
throw "clause input illegal";
}
auto it = this->clausesRow[i].begin();
this->watchVariable[i][0] = it->first;
it++;
// there may be only one literal in the clause, so watchVariable[1] may be 0
if(it != this->clausesRow[i].end()){
this->watchVariable[i][1] = it->first;
}else{
// only one literal in clauses[i]
// push the variable in implication table, current stack height is 0
int oldLiteral = this->implicationTable[this->clausesRow[i].begin()->first].first;
if(oldLiteral != 0 && oldLiteral != this->clausesRow[i].begin()->second){
// conflict, return unsatisfiable
isConflict = true;
}
this->implicationTable[this->clausesRow[i].begin()->first] = make_pair(this->clausesRow[i].begin()->second, 0);
// TODO: do we need to call bcp() here?
this->watchVariable[i][0] = 0;// do not watch variables in this clause anymore
}
}
// init model
this->model = vector<int>(numOfVariables + 1, 0);
// initialize variable counter
// j * 2 stores the number occurence of variable negate j
// j * 2 + 1 stores the number of occurnece of variable j
vector<int> counter = vector<int>(numOfVariables * 2 + 2, 0);
for (int i = 0; i < numOfClauses; i++)
{
for (int j = 1; j < numOfVariables + 1; j++)
{
// TODO: traverse clausesRow rather than clauses
// variable j is true
if(clauses[i][j] == 1){
counter[j*2+1]++;
}else if(clauses[i][j] == -1){
counter[j*2]++;
}
}
}
this->vc = new VariableCounter(counter);
//vc->print();
}
Chaff::~Chaff()
{
delete this->vc;
}
bool Chaff::decide(){
while((*vc).hasNext()){
int v = (*vc).next();
int idx = v / 2;
int polarity = v % 2 == 1? 1: -1;
if(decisionTable[idx] != 0 || implicationTable[idx].first != 0){
continue;
}
// new unassinged varibles found
decisionTable[idx] = polarity;//if polarity is true, set true; else set false. In order to make the literal true
// try the first side of variable
decisionStack.push(make_pair(idx * polarity, false));
return true;
}
// no unassigned variables
return false;
}
bool Chaff::bcp(){
// last assigned variable
int idx = abs(decisionStack.top().first);
int polarity = decisionStack.top().first > 0? 1: -1;
queue<pair<int, int>> impliedVariables;// first - index, second - polarity
impliedVariables.push(make_pair(idx, polarity));
//BFS search
while(impliedVariables.empty() == false){
int idx = impliedVariables.front().first;
int polarity = impliedVariables.front().second;
impliedVariables.pop();
for(auto it = clausesCol[idx].begin(); it != clausesCol[idx].end(); it++)
{
int row = it->first;
int clausePolarity = it->second;
if(watchVariable[row][0] == idx || watchVariable[row][1] == idx){
int updateWatchidx, lastWatchidx;
if(watchVariable[row][0] == idx){
updateWatchidx = 0;
lastWatchidx = 1;
}else{
updateWatchidx = 1;
lastWatchidx = 0;
}
//watched variable is set to 0
if(clausePolarity * polarity == -1){
bool updateWatchVariable = false;
for (auto itc = clausesRow[row].begin(); itc != clausesRow[row].end(); itc++)
{
int col = itc->first;
// the other watched variable
if(col == watchVariable[row][1] || col == watchVariable[row][0]){
continue;
}
int variable = decisionTable[col] != 0? decisionTable[col]: implicationTable[col].first;
// find a variable that is unassigned or literal is 1
if(variable == 0 || variable * clausesRow[row][col] == 1){
watchVariable[row][updateWatchidx] = col;
updateWatchVariable = true;
break;
}
}
// watchVariable can be implied
if(updateWatchVariable == false){
int lastLiteralIdx = watchVariable[row][lastWatchidx];
int lastLiteralPolarity = decisionTable[lastLiteralIdx] != 0? decisionTable[lastLiteralIdx]: implicationTable[lastLiteralIdx].first;
if(lastLiteralPolarity == 0){
// unassinged, imply it!
implicationTable[lastLiteralIdx] = make_pair(clausesRow[row][lastLiteralIdx], decisionStack.size());
impliedVariables.push(make_pair(lastLiteralIdx, implicationTable[lastLiteralIdx].first));
}else{
// assgined, check it!
if(lastLiteralPolarity * clausesRow[row][lastLiteralIdx] != 1){
// conflict
return false;
}
}
}
}
}
}
}
return true;
}
bool Chaff::resolveConflict(){
while(!decisionStack.empty()){
auto decision = decisionStack.top();
decisionStack.pop();
int idx = abs(decision.first);
int polarity = decision.first > 0? 1: -1;
// delete assignment in decision table
decisionTable[idx] = 0;
if(decision.second == true){
// tried both sides, go to next iteration
continue;
}
// tried both ways
decision.second = true;
// flip the decision
polarity *= -1;
decision.first = idx * polarity;
// push it back
decisionStack.push(decision);
// update decision table
decisionTable[idx] = polarity;
// undo all invalid implications
int height = decisionStack.size();
for(int i = 1; i < implicationTable.size(); i++){
if(implicationTable[i].second >= height){
implicationTable[i].first = 0;
implicationTable[i].second = 0;
}
}
// seek variable pointer to the top of assignment stack
(*vc).reset();
return true;
}
// can not found "not tried both sides variables"
return false;
}
void Chaff::genModel(){
for(int i = 1; i < numOfVariables + 1; i++){
int value = decisionTable[i] != 0? decisionTable[i]: implicationTable[i].first;
model[i] = value;
}
}
bool Chaff::solve(){
if(isConflict){
// conflict in single literal clauses
return false;
}
while(true){
if(!decide()){
// satisfiable
genModel();
return true;
}
while(!bcp()){
if(!resolveConflict()){
return false;
}
}
}
}
void Chaff::printModel(){
for (int i = 0; i < model.size(); i++)
{
cout<<model[i]<<" ";
}
cout<<endl;
}
int main(int argc, char const *argv[])
{
int numVariables, numClauses;
char p[1000], cnf[100];
if(argc != 2){
helperMsg();
return -1;
}
string spath = argv[0];
string sinput = argv[1];
FILE * fp = fopen(sinput.c_str(), "r");
if(fp == nullptr){
cout<<"can not open input file"<<endl;
return -1;
}
if(!fscanf(fp,"%s %s %d %d",p, cnf, &numVariables, &numClauses)){
cout<<"1"<<endl;
formatIllegal();
return -1;
}
if(strcmp(p,"p") || strcmp(cnf, "cnf")){
cout<<p<<endl;
cout<<cnf<<endl;
formatIllegal();
return -1;
}
if(numVariables <= 0 || numClauses <= 0){
cout<<"3"<<endl;
formatIllegal();
return -1;
}
/**
* Input clauses
* -1 - negate
* column 0 is reserved, its value is always 0
**/
vector<vector<int>> clauses = vector<vector<int>>(numClauses, vector<int>(numVariables + 1, 0));
/**
* model
* 0 means undecided
* 1 means true
* -1 means false
**/
vector<int> model = vector<int>(numVariables + 1, 0);
for (int i = 0; i < numClauses; i++)
{
int num;
fscanf(fp, "%d", &num);
while(num != 0){
int col = abs(num);
clauses[i][col] = num < 0? -1: 1;
fscanf(fp, "%d", &num);
}
}
fclose(fp);
Chaff chaff(clauses);
// milliseconds ms_start = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
// cout<<"start solving"<<endl;
bool isFound = chaff.solve();
if(isFound == true && isValid(clauses, chaff.model)){
cout<<"s SATISFIABLE"<<endl;
cout<<"v";
for (int j = 1; j < numVariables + 1; j++)
{
int ele = chaff.model[j] * j;
cout<<" "<<ele;
}
cout<<endl;
}
else{
cout<<"s UNSATISFIABLE"<<endl;
}
// milliseconds ms_end = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
// cout<<"time elapse: "<<((double)ms_end.count() - ms_start.count()) / 1000<<"s"<<endl;
// getchar();
return 0;
}