1
- #include < stdio.h>
2
1
#include < vector>
3
2
#include < iostream>
4
- #include < fstream>
5
- #include < sstream>
6
3
#include " sat.h"
7
4
8
- std::optional<std::vector<std::vector<std::tuple<int , bool >>>> parse (std::string path) {
9
- std::ifstream in (path);
10
- std::vector<std::vector<std::tuple<int ,bool >>> pc;
11
-
12
- std::string dimacsType;
13
- int literals;
14
- int clauses;
15
- for (std::string line; std::getline (in, line);) {
16
- if (line.empty () || line[0 ] == ' c' ) continue ;
17
- // TODO: Throw exception
18
- if (line[0 ] != ' p' ) {
19
- std::cout << " NOT PARAMS" << std::endl;
20
- return std::nullopt;
21
- }
22
- else {
23
- char pch;
24
- std::stringstream ss (line);
25
- ss >> pch >> dimacsType >> literals >> clauses;
26
- if (!ss.eof ()) {
27
- // TODO: Throw exception
28
- return std::nullopt;
29
- }
30
- break ;
31
- }
32
- }
33
- std::vector<std::tuple<int , bool >> clause;
34
- for (std::string line; std::getline (in, line);) {
35
- if (line.empty () || line[0 ] == ' c' ) continue ;
36
- if (line[0 ] == ' p' ) return std::nullopt;
37
- int literal;
38
- std::stringstream ss (line);
39
- while (ss >> literal && literal != 0 )
40
- clause.emplace_back (std::make_tuple (std::abs (literal) - 1 , literal < 0 ));
41
- if (literal == 0 ) {
42
- pc.push_back (std::move (clause));
43
- clause = {};
44
- }
45
- }
46
- return std::make_optional (pc);
47
- }
48
-
49
- int main () {
50
- auto result = parse (" /Users/galileo/CLionProjects/sat-solver/dimacs.txt" );
51
- if (result.has_value ()) {
52
- for (const auto & a : result.value ()) {
53
- for (const auto & b : a)
54
- std::cout << " (" << std::get<0 >(b) << " ," << std::get<1 >(b) << " ) " ;
55
- std::cout << std::endl;
56
- }
57
- }
58
- {
59
- std::cout << " Problem 1: Answer is either 01 or 10\n Result: " ;
60
- std::vector<std::tuple<int , bool >> p1{
61
- std::make_tuple (0 , false ), std::make_tuple (1 , true )
62
- };
63
- std::vector<std::tuple<int , bool >> p2{
64
- std::make_tuple (0 , true ), std::make_tuple (1 , false )
65
- };
66
- std::vector<std::vector<std::tuple<int , bool >>> pClauses{
67
- p1, p2
68
- };
69
- auto sat = NaiveSAT (2 , pClauses);
70
- auto result = sat.solve ();
71
- if (!result.has_value ()) {
72
- std::cout << " unsatisfiable" ;
73
- }
5
+ void printResults (const std::optional<std::vector<bool >>& result) {
6
+ if (!result.has_value ()) {
7
+ std::cout << " unsatisfiable" << std::endl;
8
+ } else {
74
9
for (const auto b : result.value ()) {
75
10
std::cout << b;
76
11
}
12
+ std::cout << std::endl;
77
13
}
78
- {
79
- std::cout << " \n\n Problem 2: Unsatisfiable\n Result: " ;
80
- std::vector<std::tuple<int , bool >> q1{
81
- std::make_tuple (0 , false ), std::make_tuple (1 , false )
82
- };
83
- std::vector<std::tuple<int , bool >> q2{
84
- std::make_tuple (0 , false ), std::make_tuple (1 , true )
85
- };
86
- std::vector<std::tuple<int , bool >> q3{
87
- std::make_tuple (0 , true ), std::make_tuple (1 , false )
88
- };
89
- std::vector<std::tuple<int , bool >> q4{
90
- std::make_tuple (0 , true ), std::make_tuple (1 , true )
91
- };
92
- std::vector<std::vector<std::tuple<int , bool >>> qClauses{
93
- q1, q2, q3, q4
94
- };
95
-
96
- auto sat = NaiveSAT (4 , qClauses);
97
- auto result = sat.solve ();
98
- if (!result.has_value ()) {
99
- std::cout << " unsatisfiable" ;
100
- } else {
101
- for (const auto b : result.value ()) {
102
- std::cout << b;
103
- }
104
- }
105
- }
106
-
107
- {
108
- std::cout << " \n\n Problem 3: Answer should be 0000111\n Result: " ;
109
- std::vector<std::tuple<int , bool >> r1{
110
- std::make_tuple (2 , true ), std::make_tuple (6 , true )
111
- };
112
- std::vector<std::tuple<int , bool >> r2{
113
- std::make_tuple (1 , false ), std::make_tuple (2 , false ),
114
- std::make_tuple (4 , false )// , std::make_tuple(0, true)
115
- };
116
- std::vector<std::tuple<int , bool >> r3{
117
- std::make_tuple (0 , false ), std::make_tuple (1 , true )
118
- };
119
- std::vector<std::tuple<int , bool >> r4{
120
- std::make_tuple (0 , true ), std::make_tuple (4 , true )
121
- };
122
- std::vector<std::tuple<int , bool >> r5{
123
- std::make_tuple (0 , false ), std::make_tuple (3 , true )
124
- };
125
- std::vector<std::tuple<int , bool >> r6{
126
- std::make_tuple (5 , false ), std::make_tuple (2 , false ), std::make_tuple (3 , false )
127
- };
128
- std::vector<std::tuple<int , bool >> r7{
129
- std::make_tuple (5 , true ), std::make_tuple (0 , true )
130
- };
131
- std::vector<std::tuple<int , bool >> r8{
132
- std::make_tuple (6 , false )
133
- };
134
- std::vector<std::vector<std::tuple<int , bool >>> rClauses{
135
- r1, r2, r3, r4, r5, r6, r7, r8
136
- };
137
-
138
- auto sat = NaiveSAT (7 , rClauses);
139
- auto result = sat.solve ();
140
- if (!result.has_value ()) {
141
- std::cout << " unsatisfiable" ;
142
- } else {
143
- for (const auto b : result.value ()) {
144
- std::cout << b;
145
- }
146
- }
147
- }
14
+ }
148
15
149
- {
150
- // Taken from the DPLL Algorithm Wikipedia page
151
- std::cout << " \n\n Problem 4: Answer should be 1111 or 1101\n Result: " ;
152
- std::vector<std::tuple<int , bool >> w1{
153
- std::make_tuple (0 , true ), std::make_tuple (1 , false ), std::make_tuple (2 , false )
154
- };
155
- std::vector<std::tuple<int , bool >> w2{
156
- std::make_tuple (0 , false ), std::make_tuple (2 , false ), std::make_tuple (3 , false )
157
- };
158
- std::vector<std::tuple<int , bool >> w3{
159
- std::make_tuple (0 , false ), std::make_tuple (2 , false ), std::make_tuple (3 , true )
160
- };
161
- std::vector<std::tuple<int , bool >> w4{
162
- std::make_tuple (0 , false ), std::make_tuple (2 , true ), std::make_tuple (3 , false )
163
- };
164
- std::vector<std::tuple<int , bool >> w5{
165
- std::make_tuple (0 , false ), std::make_tuple (2 , true ), std::make_tuple (3 , true )
166
- };
167
- std::vector<std::tuple<int , bool >> w6{
168
- std::make_tuple (1 , true ), std::make_tuple (2 , true ), std::make_tuple (3 , false )
169
- };
170
- std::vector<std::tuple<int , bool >> w7{
171
- std::make_tuple (0 , true ), std::make_tuple (1 , false ), std::make_tuple (2 , true )
172
- };
173
- std::vector<std::tuple<int , bool >> w8{
174
- std::make_tuple (0 , true ), std::make_tuple (1 , true ), std::make_tuple (2 , false )
175
- };
176
- std::vector<std::vector<std::tuple<int , bool >>> wClauses{
177
- w1, w2, w3, w4, w5, w6, w7, w8
178
- };
16
+ int main () {
17
+ auto simple = NaiveSAT::parse (" examples/simple.dimacs" );
18
+ printResults (simple.solve ());
19
+ auto simple_unsat = NaiveSAT::parse (" examples/simple-unsat.dimacs" );
20
+ printResults (simple_unsat.solve ());
21
+ auto complex = NaiveSAT::parse (" examples/complex.dimacs" );
22
+ printResults (complex.solve ());
23
+ auto wiki = NaiveSAT::parse (" examples/wiki.dimacs" );
24
+ printResults (wiki.solve ());
25
+ auto format_example = NaiveSAT::parse (" examples/format-example.dimacs" );
26
+ printResults (format_example.solve ());
179
27
180
- auto sat = NaiveSAT (4 , wClauses);
181
- auto result = sat.solve ();
182
- if (!result.has_value ()) {
183
- std::cout << " unsatisfiable" ;
184
- } else {
185
- for (const auto b : result.value ()) {
186
- std::cout << b;
187
- }
188
- }
189
- }
190
28
return 0 ;
191
29
}
0 commit comments