summaryrefslogtreecommitdiff
path: root/libs/ezsat/testbench.cc
blob: cc0fe5734063f09573c28c1c796dc4ea931e5b8c (plain)
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
/*
 *  ezSAT -- A simple and easy to use CNF generator for SAT solvers
 *
 *  Copyright (C) 2013  Clifford Wolf <clifford@clifford.at>
 *  
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "ezminisat.h"
#include <stdio.h>

struct xorshift128 {
	uint32_t x, y, z, w;
	xorshift128() {
		x = 123456789;
		y = 362436069;
		z = 521288629;
		w = 88675123;
	}
	uint32_t operator()() {
		uint32_t t = x ^ (x << 11);
		x = y; y = z; z = w;
		w ^= (w >> 19) ^ t ^ (t >> 8);
		return w;
	}
};

bool test(ezSAT &sat, int assumption = 0)
{
	for (auto id : sat.assumed())
		printf("%s\n", sat.to_string(id).c_str());
	if (assumption)
		printf("%s\n", sat.to_string(assumption).c_str());

	std::vector<int> modelExpressions;
	std::vector<bool> modelValues;

	for (int id = 1; id <= sat.numLiterals(); id++)
		if (sat.bound(id))
			modelExpressions.push_back(id);

	if (sat.solve(modelExpressions, modelValues, assumption)) {
		printf("satisfiable:");
		for (int i = 0; i < int(modelExpressions.size()); i++)
			printf(" %s=%d", sat.to_string(modelExpressions[i]).c_str(), int(modelValues[i]));
		printf("\n\n");
		return true;
	} else {
		printf("not satisfiable.\n\n");
		return false;
	}
}

// ------------------------------------------------------------------------------------------------------------

void test_simple()
{
	printf("==== %s ====\n\n", __PRETTY_FUNCTION__);

	ezSAT sat;
	sat.assume(sat.OR("A", "B"));
	sat.assume(sat.NOT(sat.AND("A", "B")));
	test(sat);
}

// ------------------------------------------------------------------------------------------------------------

void test_basic_operators(ezSAT &sat, xorshift128 &rng, int iter, bool buildTrees, bool buildClusters, std::vector<bool> &log)
{
	int vars[6] = {
		sat.VAR("A"), sat.VAR("B"), sat.VAR("C"),
		sat.NOT("A"), sat.NOT("B"), sat.NOT("C")
	};
	for (int i = 0; i < iter; i++) {
		int assumption = 0, op = rng() % 6, to = rng() % 6;
		int a = vars[rng() % 6], b = vars[rng() % 6], c = vars[rng() % 6];
		// printf("--> %d %d:%s %d:%s %d:%s\n", op, a, sat.to_string(a).c_str(), b, sat.to_string(b).c_str(), c, sat.to_string(c).c_str());
		switch (op)
		{
		case 0:
			assumption = sat.NOT(a);
			break;
		case 1:
			assumption = sat.AND(a, b);
			break;
		case 2:
			assumption = sat.OR(a, b);
			break;
		case 3:
			assumption = sat.XOR(a, b);
			break;
		case 4:
			assumption = sat.IFF(a, b);
			break;
		case 5:
			assumption = sat.ITE(a, b, c);
			break;
		}
		// printf("  --> %d:%s\n", to, sat.to_string(assumption).c_str());
		if (buildTrees)
			vars[to] = assumption;
		if (!buildClusters)
			sat.clear();
		sat.assume(assumption);
		if (sat.numCnfVariables() < 15) {
			printf("%d:\n", int(log.size()));
			log.push_back(test(sat));
		} else {
			// printf("** skipping large problem **\n");
		}
	}
}

void test_basic_operators(ezSAT &sat, std::vector<bool> &log)
{
	printf("-- %s --\n\n", __PRETTY_FUNCTION__);

	xorshift128 rng;
	test_basic_operators(sat, rng, 1000, false, false, log);
	for (int i = 0; i < 100; i++)
		test_basic_operators(sat, rng, 10, true, false, log);
	for (int i = 0; i < 100; i++)
		test_basic_operators(sat, rng, 10, false, true, log);
}

void test_basic_operators()
{
	printf("==== %s ====\n\n", __PRETTY_FUNCTION__);

	ezSAT sat;
	ezMiniSAT miniSat;
	std::vector<bool> logSat, logMiniSat;

	test_basic_operators(sat, logSat);
	test_basic_operators(miniSat, logMiniSat);

	if (logSat != logMiniSat) {
		printf("Differences between logSat and logMiniSat:");
		for (int i = 0; i < int(std::max(logSat.size(), logMiniSat.size())); i++)
			if (i >= int(logSat.size()) || i >= int(logMiniSat.size()) || logSat[i] != logMiniSat[i])
				printf(" %d", i);
		printf("\n");
		abort();
	} else {
		printf("Completed %d tests with identical results with ezSAT and ezMiniSAT.\n\n", int(logSat.size()));
	}
}

// ------------------------------------------------------------------------------------------------------------

void test_xorshift32_try(ezSAT &sat, uint32_t input_pattern)
{
	uint32_t output_pattern = input_pattern;
	output_pattern ^= output_pattern << 13;
	output_pattern ^= output_pattern >> 17;
	output_pattern ^= output_pattern << 5;

	std::vector<int> modelExpressions;
	std::vector<int> forwardAssumptions, backwardAssumptions;
	std::vector<bool> forwardModel, backwardModel;

	sat.vec_append(modelExpressions, sat.vec_var("i", 32));
	sat.vec_append(modelExpressions, sat.vec_var("o", 32));

	sat.vec_append_unsigned(forwardAssumptions,  sat.vec_var("i", 32), input_pattern);
	sat.vec_append_unsigned(backwardAssumptions,  sat.vec_var("o", 32), output_pattern);

	if (!sat.solve(modelExpressions, backwardModel, backwardAssumptions)) {
		printf("backward solving failed!\n");
		abort();
	}

	if (!sat.solve(modelExpressions, forwardModel, forwardAssumptions)) {
		printf("forward solving failed!\n");
		abort();
	}

	printf("xorshift32 test with input pattern 0x%08x:\n", input_pattern);

	printf("forward  solution: input=0x%08x output=0x%08x\n",
			(unsigned int)sat.vec_model_get_unsigned(modelExpressions, forwardModel, sat.vec_var("i", 32)),
			(unsigned int)sat.vec_model_get_unsigned(modelExpressions, forwardModel, sat.vec_var("o", 32)));

	printf("backward solution: input=0x%08x output=0x%08x\n",
			(unsigned int)sat.vec_model_get_unsigned(modelExpressions, backwardModel, sat.vec_var("i", 32)),
			(unsigned int)sat.vec_model_get_unsigned(modelExpressions, backwardModel, sat.vec_var("o", 32)));

	if (forwardModel != backwardModel) {
		printf("forward and backward results are inconsistend!\n");
		abort();
	}

	printf("passed.\n\n");
}

void test_xorshift32()
{
	printf("==== %s ====\n\n", __PRETTY_FUNCTION__);

	ezMiniSAT sat;
	xorshift128 rng;

	std::vector<int> bits = sat.vec_var("i", 32);

	bits = sat.vec_xor(bits, sat.vec_shl(bits, 13));
	bits = sat.vec_xor(bits, sat.vec_shr(bits, 17));
	bits = sat.vec_xor(bits, sat.vec_shl(bits,  5));

	sat.vec_set(bits, sat.vec_var("o", 32));

	test_xorshift32_try(sat, 0);
	test_xorshift32_try(sat, 314159265);
	test_xorshift32_try(sat, rng());
	test_xorshift32_try(sat, rng());
	test_xorshift32_try(sat, rng());
	test_xorshift32_try(sat, rng());
}

// ------------------------------------------------------------------------------------------------------------

#define CHECK(_expr1, _expr2) check(#_expr1, _expr1, #_expr2, _expr2)

void check(const char *expr1_str, bool expr1, const char *expr2_str, bool expr2)
{
	if (expr1 == expr2) {
		printf("[ %s ] == [ %s ] .. ok (%s == %s)\n", expr1_str, expr2_str, expr1 ? "true" : "false", expr2 ? "true" : "false");
	} else {
		printf("[ %s ] != [ %s ] .. ERROR (%s != %s)\n", expr1_str, expr2_str, expr1 ? "true" : "false", expr2 ? "true" : "false");
		abort();
	}
}

void test_signed(int8_t a, int8_t b, int8_t c)
{
	ezSAT sat;

	std::vector<int> av = sat.vec_const_signed(a, 8);
	std::vector<int> bv = sat.vec_const_signed(b, 8);
	std::vector<int> cv = sat.vec_const_signed(c, 8);

	printf("Testing signed arithmetic using: a=%+d, b=%+d, c=%+d\n", int(a), int(b), int(c));

	CHECK(a <  b+c, sat.solve(sat.vec_lt_signed(av, sat.vec_add(bv, cv))));
	CHECK(a <= b-c, sat.solve(sat.vec_le_signed(av, sat.vec_sub(bv, cv))));

	CHECK(a >  b+c, sat.solve(sat.vec_gt_signed(av, sat.vec_add(bv, cv))));
	CHECK(a >= b-c, sat.solve(sat.vec_ge_signed(av, sat.vec_sub(bv, cv))));

	printf("\n");
}

void test_unsigned(uint8_t a, uint8_t b, uint8_t c)
{
	ezSAT sat;

	if (b < c)
		b ^= c, c ^= b, b ^= c;

	std::vector<int> av = sat.vec_const_unsigned(a, 8);
	std::vector<int> bv = sat.vec_const_unsigned(b, 8);
	std::vector<int> cv = sat.vec_const_unsigned(c, 8);

	printf("Testing unsigned arithmetic using: a=%d, b=%d, c=%d\n", int(a), int(b), int(c));

	CHECK(a <  b+c, sat.solve(sat.vec_lt_unsigned(av, sat.vec_add(bv, cv))));
	CHECK(a <= b-c, sat.solve(sat.vec_le_unsigned(av, sat.vec_sub(bv, cv))));

	CHECK(a >  b+c, sat.solve(sat.vec_gt_unsigned(av, sat.vec_add(bv, cv))));
	CHECK(a >= b-c, sat.solve(sat.vec_ge_unsigned(av, sat.vec_sub(bv, cv))));

	printf("\n");
}

void test_count(uint32_t x)
{
	ezSAT sat;

	int count = 0;
	for (int i = 0; i < 32; i++)
		if (((x >> i) & 1) != 0)
			count++;

	printf("Testing bit counting using x=0x%08x (%d set bits) .. ", x, count);

	std::vector<int> v = sat.vec_const_unsigned(x, 32);

	std::vector<int> cv6 = sat.vec_const_unsigned(count, 6);
	std::vector<int> cv4 = sat.vec_const_unsigned(count <= 15 ? count : 15, 4);

	if (cv6 != sat.vec_count(v, 6, false)) {
		fprintf(stderr, "FAILED 6bit-no-clipping test!\n");
		abort();
	}
	
	if (cv4 != sat.vec_count(v, 4, true)) {
		fprintf(stderr, "FAILED 4bit-clipping test!\n");
		abort();
	}
	
	printf("ok.\n");
}

void test_arith()
{
	printf("==== %s ====\n\n", __PRETTY_FUNCTION__);

	xorshift128 rng;

	for (int i = 0; i < 100; i++)
		test_signed(rng() % 19 - 10, rng() % 19 - 10, rng() % 19 - 10);

	for (int i = 0; i < 100; i++)
		test_unsigned(rng() % 10, rng() % 10, rng() % 10);

	test_count(0x00000000);
	test_count(0xffffffff);
	for (int i = 0; i < 30; i++)
		test_count(rng());

	printf("\n");
}

// ------------------------------------------------------------------------------------------------------------

void test_onehot()
{
	printf("==== %s ====\n\n", __PRETTY_FUNCTION__);
	ezMiniSAT ez;

	int a = ez.literal("a");
	int b = ez.literal("b");
	int c = ez.literal("c");
	int d = ez.literal("d");

	std::vector<int> abcd;
	abcd.push_back(a);
	abcd.push_back(b);
	abcd.push_back(c);
	abcd.push_back(d);

	ez.assume(ez.onehot(abcd));

	int solution_counter = 0;
	while (1)
	{
		std::vector<bool> modelValues;
		bool ok = ez.solve(abcd, modelValues);

		if (!ok)
			break;

		printf("Solution: %d %d %d %d\n", int(modelValues[0]), int(modelValues[1]), int(modelValues[2]), int(modelValues[3]));

		int count_hot = 0;
		std::vector<int> sol;
		for (int i = 0; i < 4; i++) {
			if (modelValues[i])
				count_hot++;
			sol.push_back(modelValues[i] ? abcd[i] : ez.NOT(abcd[i]));
		}
		ez.assume(ez.NOT(ez.expression(ezSAT::OpAnd, sol)));

		if (count_hot != 1) {
			fprintf(stderr, "Wrong number of hot bits!\n");
			abort();
		}

		solution_counter++;
	}

	if (solution_counter != 4) {
		fprintf(stderr, "Wrong number of one-hot solutions!\n");
		abort();
	}

	printf("\n");
}

void test_manyhot()
{
	printf("==== %s ====\n\n", __PRETTY_FUNCTION__);
	ezMiniSAT ez;

	int a = ez.literal("a");
	int b = ez.literal("b");
	int c = ez.literal("c");
	int d = ez.literal("d");

	std::vector<int> abcd;
	abcd.push_back(a);
	abcd.push_back(b);
	abcd.push_back(c);
	abcd.push_back(d);

	ez.assume(ez.manyhot(abcd, 1, 2));

	int solution_counter = 0;
	while (1)
	{
		std::vector<bool> modelValues;
		bool ok = ez.solve(abcd, modelValues);

		if (!ok)
			break;

		printf("Solution: %d %d %d %d\n", int(modelValues[0]), int(modelValues[1]), int(modelValues[2]), int(modelValues[3]));

		int count_hot = 0;
		std::vector<int> sol;
		for (int i = 0; i < 4; i++) {
			if (modelValues[i])
				count_hot++;
			sol.push_back(modelValues[i] ? abcd[i] : ez.NOT(abcd[i]));
		}
		ez.assume(ez.NOT(ez.expression(ezSAT::OpAnd, sol)));

		if (count_hot != 1 && count_hot != 2) {
			fprintf(stderr, "Wrong number of hot bits!\n");
			abort();
		}

		solution_counter++;
	}

	if (solution_counter != 4 + 4*3/2) {
		fprintf(stderr, "Wrong number of one-hot solutions!\n");
		abort();
	}

	printf("\n");
}

void test_ordered()
{
	printf("==== %s ====\n\n", __PRETTY_FUNCTION__);
	ezMiniSAT ez;

	int a = ez.literal("a");
	int b = ez.literal("b");
	int c = ez.literal("c");

	int x = ez.literal("x");
	int y = ez.literal("y");
	int z = ez.literal("z");

	std::vector<int> abc;
	abc.push_back(a);
	abc.push_back(b);
	abc.push_back(c);

	std::vector<int> xyz;
	xyz.push_back(x);
	xyz.push_back(y);
	xyz.push_back(z);

	ez.assume(ez.ordered(abc, xyz));

	int solution_counter = 0;

	while (1)
	{
		std::vector<int> modelVariables;
		std::vector<bool> modelValues;

		modelVariables.push_back(a);
		modelVariables.push_back(b);
		modelVariables.push_back(c);

		modelVariables.push_back(x);
		modelVariables.push_back(y);
		modelVariables.push_back(z);

		bool ok = ez.solve(modelVariables, modelValues);

		if (!ok)
			break;

		printf("Solution: %d %d %d | %d %d %d\n",
				int(modelValues[0]), int(modelValues[1]), int(modelValues[2]),
				int(modelValues[3]), int(modelValues[4]), int(modelValues[5]));

		std::vector<int> sol;
		for (size_t i = 0; i < modelVariables.size(); i++)
			sol.push_back(modelValues[i] ? modelVariables[i] : ez.NOT(modelVariables[i]));
		ez.assume(ez.NOT(ez.expression(ezSAT::OpAnd, sol)));

		solution_counter++;
	}

	if (solution_counter != 8+7+6+5+4+3+2+1) {
		fprintf(stderr, "Wrong number of solutions!\n");
		abort();
	}

	printf("\n");
}

// ------------------------------------------------------------------------------------------------------------


int main()
{
	test_simple();
	test_basic_operators();
	test_xorshift32();
	test_arith();
	test_onehot();
	test_manyhot();
	test_ordered();
	printf("Passed all tests.\n\n");
	return 0;
}