-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTest.h
434 lines (376 loc) · 13.7 KB
/
Test.h
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
/*!
* \author Prateek Kumar - [email protected]
*
* @section LICENSE
*
* Open-WBO Copyright (c) 2018 Saurabh Joshi, Prateek Kumar, Sukrut Rao
*
* 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.
*
*/
#ifndef TEST_H
#define TEST_H
#include <random>
#include <algorithm>
#include <vector>
#include <iostream>
#include "encodings/Enc_GTECluster.h"
#include "encodings/Enc_GTEIncremental.h"
#define NUM_CLUSTERS 3
#define MAX_PER_CLUSTER 4
#define MAX_WEIGHT 10
/*_________________________________________________________________________________________________
|
| test_encodig : (maxsat_formula : MaxSATFormula*, rhs1 : uint64_t, rhs2 : uint64_t, nsoft1 : int)
| -> [void]
|
| Description:
|
| This functions tests GTE Incremental by creating two trees and joining them.
| The clauses for both the trees are given in a file which is loaded by main
| function into maxsat_formula. The first nsoft soft clauses of maxsat_formula
| is used to create the first tree and remaining are used to create the second tree.
| The first tree has <= rhs1 constraint and second tree has <= rhs2 constraint.
| After joining, the RHS for AMK cnstraint is rhs2.
|________________________________________________________________________________________________@*/
void test_encoding(MaxSATFormula *maxsat_formula, uint64_t rhs1, uint64_t rhs2, int nsoft1) {
Solver *s = new Solver();
vec<Lit> assumptions;
vec<Lit> literals1;
vec<uint64_t> weights_vec1;
vec<Lit> literals2;
vec<uint64_t> weights_vec2;
for (int i=0; i<maxsat_formula->nSoft(); i++) {
s->newVar();
if (i<nsoft1) {
literals1.push(maxsat_formula->getSoftClause(i).clause[0]);
weights_vec1.push(maxsat_formula->getSoftClause(i).weight);
} else {
literals2.push(maxsat_formula->getSoftClause(i).clause[0]);
weights_vec2.push(maxsat_formula->getSoftClause(i).weight);
}
}
openwbo::GTEIncremental gte(_INCREMENTAL_ITERATIVE_);
gte.encode(s, literals1, weights_vec1, rhs1);
gte.update(s, rhs1, assumptions);
gte.join(s, literals2, weights_vec2, rhs2, assumptions);
gte.update(s, rhs2, assumptions);
for (int i=0; i<maxsat_formula->nHard(); i++) {
s->addClause(maxsat_formula->getHardClause(i).clause[0]);
}
bool solved = s->solve(assumptions);
if (solved) {
std::cout << "SAT" << std::endl;
} else {
std::cout << "UNSAT" << std::endl;
}
delete s;
}
/*_________________________________________________________________________________________________
|
| test_encodig_join : [void] -> [void]
|
| Description:
|
| This function randomly creates two GTE trees and joins them.
|________________________________________________________________________________________________@*/
void test_encoding_join()
{
std::random_device rd;
std::mt19937 g(rd());
std::uniform_int_distribution<unsigned int> dis(1, MAX_PER_CLUSTER);
std::uniform_int_distribution<uint64_t> dis64(1, MAX_WEIGHT);
// Generate random weights to be used by the trees
std::vector<uint64_t> weights;
for (int i=0; i<NUM_CLUSTERS; i++) {
int n = dis(g);
uint64_t w = dis64(g);
for (int j=0; j<n; j++) {
weights.push_back(w);
}
}
Solver *s = new Solver();
vec<Lit> literals1;
vec<Lit> literals2;
vec<uint64_t> weights_vec1;
vec<uint64_t> weights_vec2;
// Create literals and push into a vector
std::vector<Lit> literals_vector;
vec<uint64_t> weights_vec;
for (unsigned i=0; i<weights.size(); i++) {
literals_vector.push_back(mkLit(s->newVar(), false));
weights_vec.push(weights[i]);
}
// Shuffle the literals
std::shuffle(literals_vector.begin(), literals_vector.end(), g);
std::uniform_int_distribution<unsigned> dis_tree2(0,1);
for (unsigned i=0; i<literals_vector.size(); i++) {
// Decide randomly whether to have the literal in first tree or second tree
unsigned take_tree2 = dis_tree2(g);
if (take_tree2) {
literals2.push(literals_vector[i]);
weights_vec2.push(weights_vec[i]);
}
else {
literals1.push(literals_vector[i]);
weights_vec1.push(weights_vec[i]);
}
}
// Decide the number of unit clauses to be used as hard clauses and find the sum
// of weights of those unit clauses
std::uniform_int_distribution<unsigned> dis_unit(1, weights.size()/4+1);
unsigned num_unit_clauses = dis_unit(g);
uint64_t sum = 0;
for (unsigned i=0; i<num_unit_clauses; i++) {
sum += weights[i];
s->addClause(literals_vector[i]);
}
openwbo::GTEIncremental gte(_INCREMENTAL_ITERATIVE_);
vec<Lit> assumptions;
uint64_t rhs = 0;
std::uniform_int_distribution<unsigned> dis_rhs(0,1);
unsigned unsat = dis_rhs(g); // Randomly decide whether formula should be SAT or UNSAT
// Acoordingly randomly select RHS for atmost constraint for the final tree
if (unsat) {
std::uniform_int_distribution<unsigned> dis_rhs(0, sum-1);
rhs = dis_rhs(g);
} else {
std::uniform_int_distribution<unsigned> dis_rhs(sum, 2*sum+1);
rhs = dis_rhs(g);
}
// Randomly select RHS for the first tree
std::uniform_int_distribution<unsigned> dis_rhs1(0, 2*sum+1);
uint64_t rhs1 = dis_rhs1(g);
std::cout << "Number of lits1: " << literals1.size() << std::endl
<< "Number of lits2: " << literals2.size() << std::endl
<< "Number of unit clauses: " << num_unit_clauses << std::endl;
std::cout << "RHS1: " << rhs1 << std::endl;
std::cout << "RHS: " << rhs << std::endl;
// Encode using GTE and join the tree
gte.encode(s, literals1, weights_vec1, rhs1);
gte.update(s, rhs1, assumptions);
gte.join(s, literals2, weights_vec2, rhs, assumptions);
gte.update(s, rhs, assumptions);
std::cout << "Encoded" << std::endl;
// Solve the constraints
bool solved = s->solve(assumptions);
// Check if the answer was correct and print the case if test failed
if (unsat) {
if (solved) {
std::cout << "TEST FAILED" << std::endl;
std::cout << "SAT" << std::endl;
std::cout << "c RHS1 " << rhs1 << std::endl;
std::cout << "c RHS2 " << rhs << std::endl;
std::cout << "c Nsoft " << weights_vec1.size() << std::endl;
std::cout << "p wcnf " << weights_vec.size() << " " << weights_vec.size()+num_unit_clauses
<< " " << MAX_WEIGHT+5 << std::endl;
for (int i=0; i<weights_vec1.size(); i++) {
std::cout << weights_vec1[i] << " " << i+1 << " 0" << std::endl;
}
for (int i=0; i<weights_vec2.size(); i++) {
std::cout << weights_vec2[i] << " " << i+1 << " 0" << std::endl;
}
for (int i=0; i<num_unit_clauses; i++) {
std::cout << MAX_WEIGHT+5 << " " << i+1 << " 0" << std::endl;
}
std::cout << "c DONE" << std::endl;
} else {
std::cout << "UNSAT" << std::endl;
}
} else {
if (solved) {
std::cout << "SAT" << std::endl;
} else {
std::cout << "TEST FAILED" << std::endl;
std::cout << "UNSAT" << std::endl;
std::cout << "c RHS1 " << rhs1 << std::endl;
std::cout << "c RHS2 " << rhs << std::endl;
std::cout << "c Nsoft " << weights_vec1.size() << std::endl;
std::cout << "p wcnf " << weights_vec.size() << " " << weights_vec.size()+num_unit_clauses
<< " " << MAX_WEIGHT+5 << std::endl;
for (int i=0; i<weights_vec1.size(); i++) {
std::cout << weights_vec1[i] << " " << i+1 << " 0" << std::endl;
}
for (int i=0; i<weights_vec2.size(); i++) {
std::cout << weights_vec2[i] << " " << i+1 << " 0" << std::endl;
}
for (int i=0; i<num_unit_clauses; i++) {
std::cout << MAX_WEIGHT+5 << " " << i+1 << " 0" << std::endl;
}
std::cout << "c DONE" << std::endl;
}
}
delete s;
}
/*_________________________________________________________________________________________________
|
| test_encodig : [maxsat_formula: MaxSATFormula*, rhs: uint64_t] -> [void]
|
| Description:
|
| Test for GTE Incremental encoding for the given test case in a file
| which is loaded by main function in maxsat_formula.
|________________________________________________________________________________________________@*/
void test_encoding(MaxSATFormula *maxsat_formula, uint64_t rhs) {
Solver *s = new Solver();
openwbo::GTEIncremental gte(_INCREMENTAL_ITERATIVE_);
vec<Lit> assumptions;
vec<Lit> literals;
vec<uint64_t> weights_vec;
for (int i=0; i<maxsat_formula->nSoft(); i++) {
s->newVar();
literals.push(maxsat_formula->getSoftClause(i).clause[0]);
weights_vec.push(maxsat_formula->getSoftClause(i).weight);
}
gte.encode(s, literals, weights_vec, rhs);
gte.update(s, rhs, assumptions);
std::cout << "Encoded" << std::endl;
for (int i=0; i<maxsat_formula->nHard(); i++) {
s->addClause(maxsat_formula->getHardClause(i).clause[0]);
}
bool solved = s->solve(assumptions);
if (solved) {
std::cout << "SAT" << std::endl;
} else {
std::cout << "UNSAT" << std::endl;
}
delete s;
}
/*_________________________________________________________________________________________________
|
| test_encodig : [void] -> [void]
|
| Description:
|
| Randomly create a AMK constraint, encode using GTE and check SAT/UNSAT.
|________________________________________________________________________________________________@*/
void test_encoding()
{
std::random_device rd;
std::mt19937 g(rd());
std::uniform_int_distribution<unsigned int> dis(1, MAX_PER_CLUSTER);
std::uniform_int_distribution<uint64_t> dis64(1, MAX_WEIGHT);
// Create Random weights to be used for the soft clauses
std::vector<uint64_t> weights;
for (int i=0; i<NUM_CLUSTERS; i++) {
int n = dis(g);
uint64_t w = dis64(g);
for (int j=0; j<n; j++) {
weights.push_back(w);
}
}
Solver *s = new Solver();
std::vector<Lit> literals_vector;
vec<Lit> literals;
vec<uint64_t> weights_vec;
// Get literals from the solvers
for (unsigned i=0; i<weights.size(); i++) {
literals_vector.push_back(mkLit(s->newVar(), false));
weights_vec.push(weights[i]);
}
// Shuffle the literals
std::shuffle(literals_vector.begin(), literals_vector.end(), g);
for (unsigned i=0; i<literals_vector.size(); i++) {
literals.push(literals_vector[i]);
}
// Decide number of unit clauses as hard clauses
std::uniform_int_distribution<unsigned> dis_unit(1, weights.size()/4+1);
unsigned num_unit_clauses = dis_unit(g);
// Find the sum of weights of the unit hard clauses
uint64_t sum = 0;
for (unsigned i=0; i<num_unit_clauses; i++) {
sum += weights[i];
s->addClause(literals[i]);
}
openwbo::GTEIncremental gte(_INCREMENTAL_ITERATIVE_);
vec<Lit> assumptions;
uint64_t rhs = 0;
std::uniform_int_distribution<unsigned> dis_rhs(0,1);
// Randomly decide SAT/UNSAT and choose RHS of AMK constraint
unsigned unsat = dis_rhs(g);
if (unsat) {
std::uniform_int_distribution<unsigned> dis_rhs(0, sum-1);
rhs = dis_rhs(g);
} else {
std::uniform_int_distribution<unsigned> dis_rhs(sum, 2*sum+1);
rhs = dis_rhs(g);
}
std::cout << "Number of lits: " << literals.size() << std::endl
<< "Number of unit clauses: " << num_unit_clauses << std::endl;
std::cout << "RHS: " << rhs << std::endl;
// Encode the AMK constraint
gte.encode(s, literals, weights_vec, rhs);
// Randomly choose number of times to update the RHS
std::uniform_int_distribution<unsigned> dis_num_inc(1, 100);
unsigned num_inc = dis_num_inc(g);
for (int k=0; k < num_inc; k++) {
std::cout << "RHS : " << rhs << std::endl;
// Update RHS and solve
gte.update(s, rhs, assumptions);
std::cout << "Encoded" << std::endl;
bool solved = s->solve(assumptions);
// Check SAT/UNSAT and print the test case if failed
if (unsat) {
if (solved) {
std::cout << "TEST FAILED" << std::endl;
std::cout << "SAT" << std::endl;
std::cout << "c RHS " << rhs << std::endl;
std::cout << "p wcnf " << weights_vec.size() << " " << weights_vec.size()+num_unit_clauses
<< " " << MAX_WEIGHT+5 << std::endl;
for (int i=0; i<weights_vec.size(); i++) {
std::cout << weights_vec[i] << " " << i+1 << " 0" << std::endl;
}
for (int i=0; i<num_unit_clauses; i++) {
std::cout << MAX_WEIGHT+5 << " " << i+1 << " 0" << std::endl;
}
std::cout << "c DONE" << std::endl;
} else {
std::cout << "UNSAT" << std::endl;
}
} else {
if (solved) {
std::cout << "SAT" << std::endl;
} else {
std::cout << "TEST FAILED" << std::endl;
std::cout << "UNSAT" << std::endl;
std::cout << "c RHS " << rhs << std::endl;
std::cout << "p wcnf " << weights_vec.size() << " " << weights_vec.size()+num_unit_clauses
<< " " << MAX_WEIGHT+5 << std::endl;
for (int i=0; i<weights_vec.size(); i++) {
std::cout << weights_vec[i] << " " << i+1 << " 0" << std::endl;
}
for (int i=0; i<num_unit_clauses; i++) {
std::cout << MAX_WEIGHT+5 << " " << i+1 << " 0" << std::endl;
}
std::cout << "c DONE" << std::endl;
}
}
std::uniform_int_distribution<unsigned> dis_rhs(0,1);
unsat = dis_rhs(g);
if (unsat) {
std::uniform_int_distribution<unsigned> dis_rhs(0, sum-1);
rhs = dis_rhs(g);
} else {
std::uniform_int_distribution<unsigned> dis_rhs(sum, 2*sum+1);
rhs = dis_rhs(g);
}
}
delete s;
}
#endif