-
Notifications
You must be signed in to change notification settings - Fork 0
/
sat.cpp
80 lines (71 loc) · 2.25 KB
/
sat.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
using namespace std;
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <sstream>
#include "parser.h"
#include "clause.h"
#include "ValliJGM.h"
#include "sat.h"
int main(int argc, char** argv) {
if (argc == 1) {
cout << "Usage: CNF file" << endl;
return 1;
}
//This constructs the clauses vector and maxVarIndex int
vector<vector<int> > clauses;
int maxVarIndex = 0;
// cout << argv[1] << endl;
parse_DIMACS_CNF(clauses, maxVarIndex, argv[1]);
// You can refer to the ith clause appearing in the CNF file using the
// expression `clauses[i]'. The jth literal of `clauses[i]' can be
// referred to using `clauses[i][j]'. The expression `clauses.size()'
// tells you the number of clauses in the benchmark.
// Insert into our data structure
vector<Clause *> set_of_clauses;
// Clause is a vector of literals
// Literals are integers either positive or negative
for (int i = 0, sz = clauses.size(); i < sz; i++) {
Clause *cl = new Clause(clauses[i]);
set_of_clauses.push_back(cl);
}
cout << "*******************************************************************" << endl;
cout << "************************ STARTING *************************" << endl;
cout << "*******************************************************************" << endl;
SAT mainSAT;
mainSAT.fillVariables(set_of_clauses);
// Solve SAT
outcome finish = mainSAT.DPLL(set_of_clauses);
// output processing
ofstream outfile;
outfile.open ("output.txt");
outfile << "c The SAT Solution is:\n";
if (finish == SATISFIED) {
outfile << "s SATISFIABLE\n";
Valli<Variable>::iterator itr = mainSAT.getVarStart();
Valli<Variable>::iterator end = mainSAT.getVarEnd();
string varString = "v ";
while (itr != end) {
unsigned var = (*itr).getValue();
int varS = ((*itr).getSetting() == 0) ? -var : var;
stringstream out;
out << varS;
varString = varString + out.str() + " ";
++itr;
}
outfile << varString << "\n";
outfile.close();
return 10;
} else if (finish == UNSATISFIED){
outfile << "s UNSATISFIABLE\n";
outfile.close();
return 20;
} else {
outfile << "s UNKNOWN\n";
}
outfile.close();
return 0;
}