-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnf.cpp
More file actions
303 lines (255 loc) · 7.13 KB
/
cnf.cpp
File metadata and controls
303 lines (255 loc) · 7.13 KB
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
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <set>
#include "lexer.hpp"
#include "parser.hpp"
#include "eval.hpp"
#include "cnf.hpp"
bool operator> (const literal_t &left, const literal_t &right) {
return (left.name_ > right.name_);
}
bool operator< (const literal_t &left, const literal_t &right) {
return (left.name_ < right.name_);
}
bool operator> (const disjunct_t &left, const disjunct_t &right){
return (left.elem_.size() > right.elem_.size());
}
bool operator< (const disjunct_t &left, const disjunct_t &right){
return (left.elem_.size() < right.elem_.size());
}
std::string add(int a, int b, int i);
std::string sumremainder(int a,int b,int i);
cnf_t::cnf_t(const cnf_t &other) {
variables_ = other.variables_;
disjuncts_ = other.disjuncts_;
int sz = variables_.size();
val_ = new char [sz] ;
visited_ = new int [sz] ;
balance_ = new int [sz] ;
for (int i = 0; i < sz; ++i) {
val_[i] = other.val_[i];
visited_[i] = other.visited_[i];
balance_[i] = other.balance_[i];
}
}
cnf_t::cnf_t() {
val_ = new char [1024] ;
visited_ = new int [1024] ;
balance_ = new int [1024] ;
}
void create_disjunct_(disjunct_t *disjunct, std::vector <std::string> *variables, int *visited, int *balance, node_t *c_root) {
if (c_root == nullptr) return;
literal_t new_literal;
switch (c_root->data.k) {
case NODE_OP : {
if (c_root->data.u.op == OR) {
create_disjunct_(disjunct, variables, visited, balance, c_root->left );
create_disjunct_(disjunct, variables, visited, balance, c_root->right);
}
else if (c_root->data.u.op == NOT) {
new_literal.sign_ = 1;
std::string name = std::string(c_root->left->data.u.var);
auto it_cur = std::find(variables->begin(), variables->end(), name);
new_literal.name_ = std::distance(variables->begin(), it_cur);
++visited[new_literal.name_];
if (it_cur == variables->end()) {
variables->push_back(name);
}
disjunct->elem_.insert(new_literal);
}
break;
}
case NODE_VAR: {
new_literal.sign_ = 0;
std::string name = std::string(c_root->data.u.var);
auto it_cur = std::find(variables->begin(), variables->end(), name);
new_literal.name_ = std::distance(variables->begin(), it_cur);
++visited[new_literal.name_];
++balance[new_literal.name_];
if (it_cur == variables->end())
variables->push_back(name);
disjunct->elem_.insert(new_literal);
break;
}
case NODE_VAL: {
int num;
if (c_root->data.value == 1) num = -2 ;
else num = -1;
new_literal.name_ = num;
new_literal.sign_ = 0;
disjunct->elem_.insert(new_literal);
break;
}
default:
printf("default?\n");
abort();
}
}
void create_cnf_(cnf_t *cnf, node_t *c_root) {
if (c_root == nullptr) return;
for (int i = 0; i < 255; ++i)
cnf->val_[i] = -1;
if (c_root->data.k == NODE_OP && c_root->data.u.op == AND) {
create_cnf_(cnf, c_root->left );
create_cnf_(cnf, c_root->right);
}
else {
disjunct_t new_disjunct;
create_disjunct_(&new_disjunct, &cnf->variables_, cnf->visited_, cnf->balance_, c_root);
cnf->disjuncts_.push_back(new_disjunct);
}
}
char disjunct_t::eval_disjunct(char *eval) {
int prec = 0;
for (auto it: elem_) {
if (it.name_ == -2) return 1;
if (it.name_ == -1) continue;
if (eval[it.name_] == -1) ++prec;
if (eval[it.name_] != -1 && (eval[it.name_] ^ it.sign_)) return 1;
}
if (prec != 0) return -1;
return 0;
}
char cnf_t::eval() {
int temp ;
for (auto it: disjuncts_) {
if (it.elem_.size() == 0) continue;
temp = it.eval_disjunct(val_);
if (temp == -1) return -1;
if (temp == 0 ) return 0;
}
return 1;
}
void disjunct_t::print() {
if (elem_.size() == 0) return ;
for (auto it: elem_) {
std::cout << it.name_ << " with sign = " << it.sign_ << "; ";
}
std::cout << std::endl;
}
void cnf_t::print() {
for (auto it: disjuncts_) {
it.print();
}
}
void cnf_t::_3form() {
int count = 0;
for (auto state: disjuncts_) {
state = *(disjuncts_.begin() + count);
if (state.elem_.size() == 0) continue;
int temp_state_size = state.elem_.size();
if (temp_state_size < 3) {
for (int i = 0; i < 3 - temp_state_size; ++i)
state.elem_.insert({-1, 0});
}
if (temp_state_size > 3) {
int num_of_var = variables_.size();
variables_.push_back("_");
literal_t insert_literal = {num_of_var, 0};
literal_t new_literal;
while (temp_state_size > 2) {
disjunct_t new_disjunct = {};
variables_.push_back("_");
new_literal = {num_of_var, 1};
new_disjunct.elem_.insert(new_literal);
new_disjunct.elem_.insert({state.elem_.begin()->name_, state.elem_.begin()->sign_});
state.elem_.erase(state.elem_.begin());
--temp_state_size;
++num_of_var;
variables_.push_back("_");
new_literal = {num_of_var, 0};
new_disjunct.elem_.insert(new_literal);
disjuncts_.push_back(new_disjunct);
}
state.elem_.insert(insert_literal);
disjuncts_.push_back(state);
disjuncts_[count] = {};
}
++count;
}
}
void cnf_t::unit_propagate() {
bool isPropagate = false;
literal_t literal = {-3, 0};
for (auto it:disjuncts_) {
if (it.elem_.size() == 0) continue;
int counter = 0;
if (it.eval_disjunct(val_) == -1)
for (auto i: it.elem_) {
if (val_[i.name_] == -1) {
++counter;
literal = i;
}
if (counter > 1) break;
}
if (counter == 1) {
isPropagate = true;
val_[literal.name_] = 1 ^ literal.sign_;
}
}
if (isPropagate) unit_propagate();
}
void cnf_t::pure_literal() {
for (long unsigned int i = 0; i < variables_.size(); ++i) {
if (visited_[i] == 0) continue;
if (visited_[i] == balance_[i] || !balance_[i]) {
if (!balance_[i]) val_[i] = 0;
else val_[i] = 1;
}
}
}
bool DPLL(cnf_t cnf) {
char isEnd = cnf.eval();
if (isEnd == 1) return true ;
if (isEnd == 0) return false;
cnf.unit_propagate();
cnf.pure_literal();
for (auto state: cnf.disjuncts_) {
if (state.elem_.size() == 0) continue;
char temp = state.eval_disjunct(cnf.val_);
if (temp == 1) {
for (auto it: state.elem_) {
--cnf.visited_[it.name_];
if (it.sign_ == 0) --cnf.balance_[it.name_];
}
state = {};
}
if (temp == 0) return false;
}
int new_var = 0;
for (long unsigned int i = 0 ; i < cnf.variables_.size(); ++i) {
if (cnf.val_[i] == -1) {
isEnd = 0;
new_var = i;
break;
}
}
if (!isEnd) return cnf.eval();
cnf.val_[new_var] = 1;
bool temp_1 = DPLL(cnf);
if (temp_1) return true;
cnf.val_[new_var] = 0;
return DPLL(cnf);
}
std::string sumremainder(int a,int b,int i) {
if (i < 1) return "0";
std::string res = "(((" ;
std::string sub_a = "avar", sub_b = "bvar";
sub_a += std::to_string(i);
sub_b += std::to_string(i);
res += (sub_a + "&" + sub_b + ")|(" + sub_a + "|" + sub_b + ")&" + sumremainder(a, b, i - 1) + "))");
return res;
}
std::string add(int a, int b, int i) {
std::string sub_a = "avar", sub_b = "bvar";
sub_a += std::to_string(i);
sub_b += std::to_string(i);
std::cout << sub_a << " " << sub_b << std::endl;
std::string res = "";
res += (sub_a + "&" + sub_b + "|(" + sub_a + "|" +sub_b + ")&" + sumremainder(a, b, i - 1)) ;
return res;
}
//s_i = (a_i & b_i || (a_i || b_i) & p_i)
//p_i = (((a_(i-1) && b_(i-1)) || (a_(i-1) || b_(i-1)) & p_(i-1)))