-
Notifications
You must be signed in to change notification settings - Fork 0
/
sym_exact.c
476 lines (378 loc) · 10.6 KB
/
sym_exact.c
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
#include "spm.h"
//#define DEBUG
//#define DEBUG1
//#define PRINT
//#define LEVEL
//#define TIMEF
static int bound;
static int pass;
static int *boundlevel;
static long ttime;
/* Get the first group from P and add the remaining groups to M */
pset
Get_First_Group(pcover P, pcover *M)
{
register pset result, q;
register int i;
result = set_save(GETSET(P, 0));
foreachi_set(P, i, q){
/* The be merged cube */
if (i == 0) continue;
sf_addset(*M, q);
}
return result;
}
/***********************************************************************/
/* Check the connection between a group and an input. */
/* If a group and an input are connected then return TRUE, */
/* else return FALSE. */
/* Beside, return the intersected position(inp_ind). */
/***********************************************************************/
bool
Is_Group_Connect(pcover WSS, pset group, pset inp, int *inp_ind)
{
register pset r=new_cube(), mask=new_cube();
register int ind;
register bool result;
ind = Get_Var_Ind(group, 0);
Generate_Mask(mask, inp);
if (set_andp(r, mask, GETSET(WSS, ind)))
result = TRUE;
else
result = FALSE;
*inp_ind = Get_Var_Pos(r, 0);
free_cube(r);
free_cube(mask);
return result;
}
/* Update the P to newP and they have the same cube sequence */
void
Update_Partition(pcover newP, pcover P, pset x, pset q, int pos_q)
{
register pset r, tt=set_save(x), tmp=new_cube();
register int i, first_pos;
/* The merge cube */
set_insert(tt, pos_q);
/* Let the first variable of the merge cube is positive */
first_pos = Get_Var_Pos(tt, 0);
if ((first_pos % 2) == 0)
sf_addset(newP, set_save(tt));
else
sf_addset(newP, Var_Phase_Inv(tt));
free_cube(tt);
/* Add the other inputs sequently */
foreachi_set(P, i, r){
if (set_andp(tmp, r, x)) continue;
if (set_andp(tmp, r, q)) continue;
sf_addset(newP, r);
}
free_cube(tmp);
}
/* If there are some symmetries in P then return TRUE */
/* else return FALSE. */
bool
Is_Any_Connect(pcover WSS, pcover P)
{
register int i, ind;
register pset q, x=new_cube(), r=new_cube();
int pos_p;
foreachi_set(P, i, q){
ind = Get_Var_Ind(q, 0);
set_copy(x, GETSET(WSS, ind));
set_diff(r, x, q);
if (!setp_empty(r))
return TRUE;
}
return FALSE;
}
/* Count the number of symmetries */
int
Symmetries_Count(pcover P)
{
register int n = cube.num_binary_vars;
register pset q;
register int i, j, pair=0;
register unsigned tmp;
foreachi_set(P, i, q){
if (set_ord(q) > 1){
for(j=0; j<n; j++){
tmp = var_get(q, j);
if (tmp != 0x0)
pair++;
}//end for
}
}//end foreachi_set
return pair;
}
/* Do nothing if the order of q(input_mask) is smaller than */
/* the order of the last variable of x(group_mask). */
bool
Is_Redundant(pcover record, pset group_mask, pset input_mask)
{
register int i, x=-1, y=-1;
register pset p, tmp=new_cube();
foreachi_set(record, i, p){
if (set_andp(tmp, p, group_mask))
x = i;
if (set_andp(tmp, p, input_mask))
y = i;
}
free_cube(tmp);
if (x > y)
return TRUE;
else
return FALSE;
}
/* Ignore the input column in WSS */
pcover
Clear_Col_Inputs(pcover WSS, pset set)
{
register int *walk, i, ind, nn=NUMINPUTS*2;
register pset p, s=new_cube(), u;
pcover T=sf_save(WSS);
Generate_Mask(s, set);
u = set_fill(new_cube(), nn);
set_xor(s, s, u);
foreachi_set(T, i, p)
set_and(p, p, s);
return T;
}
/* Ignore the input row in WSS */
pcover
Clear_Row_Inputs(pcover WSS, pset set)
{
register int z, n=NUMINPUTS, nn=n*2;
register pset p, emptyset;
pcover tmpW;
tmpW = sf_save(WSS);
emptyset = new_cube();
z = 0;
while((z != -1) && (z < n)){
z = Get_Var_Ind(set, z);
if (z != -1){
p = GETSET(tmpW, z);
set_and(p, p, emptyset);
z++;
}
}
free_cube(emptyset);
return tmpW;
}
/* Ignore the input row and column in WSS */
pcover
Clear_Inputs(pcover WSS, pset set)
{
pcover T, tmp;
tmp = Clear_Col_Inputs(WSS, set);
T = Clear_Row_Inputs(tmp, set);
sf_free(tmp);
return T;
}
void
Exact_Maximum_Symmetries_Step(pcover F, pcover R, pcover *bestF, pcover *bestR, pcover WSS, pcover P, pcover I, pcover *current_best, int *current_cost, int level, pcover record, int op)
{
register pset x, q, y, test, or, last_mask, add_mask, r=new_cube();
register int i, psize, cost, max_size, ind;
pcover M, tmpF, tmpR, CWSS, newP, T, IWSS, newI, S, tmpWSS;
int pos_q;
//register long start;
psize = P->count;
/* the terminal case */
if ((psize == 1) || (!Is_Any_Connect(WSS, P))){
S = sf_join(I, P);
if ((S->count) <= *current_cost){
if (Symmetries_Count(S) > Symmetries_Count(*current_best)){
/* Record the best on-set and off-set */
sf_free(*bestF);
sf_free(*bestR);
*bestR = sf_save(R);
*bestF = sf_save(F);
/* Update current_best and current_cost */
sf_free(*current_best);
*current_best = S;
*current_cost = (*current_best)->count;
#ifdef PRINT
/* Show the process */
printf("\n============== temp =============\n");
printf("level: %d\n", level);
printf("bound: %d\n", bound);
printf("pass: %d\n", pass);
Find_Partition_Count(*current_best);
printf("\n-- Partition --");
Find_Partition((pcover) Generate_Partition((pcover) Modified_SPM(sf_join(*bestF, *bestR), complement(cube1list(sf_join(*bestF, *bestR))), (pcover) Generate_Weak_SPM(*bestF, *bestR))));
printf("\n\ntime: %s", print_time(ptime()-ttime));
printf("\n");
printf("=================================\n");
#endif
}else
sf_free(S);
}
return;
}
/* Select a group "x" */
M = new_cover(psize-1);
x = Get_First_Group(P, &M);
if (op == 2)
sf_outdeg_sort(M, WSS, 1);
/* Get the symmetric situation for x */
ind = Get_Var_Ind(x, 0);
y = set_save(GETSET(WSS, ind));
set_diff(r, y, x);
free_cube(y);
if (!setp_empty(r)){ // x can't form a symmetric pair with any input
/* Used for avoiding redundant paths */
last_mask = new_cube();
Generate_Mask(last_mask, x);
foreachi_set(M, i, q){
/* We only add "one" input */
if (set_ord(q) > 1) continue;
/* Do nothing if x and q are not weakly symmetric */
if (!Is_Group_Connect(WSS, x, q, &pos_q)) continue;
/* Do nothing if the order of q is smaller than the order of the last variable of x */
add_mask = new_cube();
Generate_Mask(add_mask, q);
if (Is_Redundant(record, last_mask, add_mask)){
free_cube(add_mask);
continue;
}else
free_cube(add_mask);
/* Modified on-set and off-set */
tmpF = sf_save(F);
tmpR = sf_save(R);
tmpWSS = (pcover) Modified_WSS1(&tmpF, &tmpR, x, pos_q);
or = sf_or(I);
CWSS = (pcover) Ignore_Inputs(tmpWSS, or);
/* Update partition */
newP = new_cover(psize-1);
Update_Partition(newP, P, x, q, pos_q);
/* Evaluate the cost */
test = new_cube();
max_size = Maximal_Independent_Set_Size(CWSS, test);
cost = max_size + I->count;
free_cube(test);
if (op != 0){
if (cost > *current_cost){
bound ++;
boundlevel[level+1]++;
continue;
}
}
pass++;
sf_free(CWSS);
CWSS = Clear_Inputs(tmpWSS, or);
sf_free(tmpWSS);
free_cube(or);
Exact_Maximum_Symmetries_Step(tmpF, tmpR, bestF, bestR, CWSS, newP, I, current_best, current_cost, level+1, record, op);
/* FREE */
sf_free(newP);
sf_free(tmpF);
sf_free(tmpR);
sf_free(CWSS);
}
free_cube(last_mask);
}
free_cube(r);
IWSS = (pcover) Ignore_Inputs(WSS, x);
/* Evaluate the cost */
test = new_cube();
max_size = Maximal_Independent_Set_Size(IWSS, test);
cost = max_size + I->count + 1;
free_cube(test);
if (op != 0){
if (cost > *current_cost){
bound ++;
boundlevel[level+1]++;
return;
}
}
pass ++;
newI = sf_save(I);
sf_addset(newI, x);
sf_free(IWSS);
IWSS = Clear_Inputs(WSS, x);
cprint(IWSS);
Exact_Maximum_Symmetries_Step(F, R, bestF, bestR, IWSS, M, newI, current_best, current_cost, level+1, record, op);
/* FREE */
sf_free(newI);
free_cube(x);
sf_free(M);
}
/**************************************************/
/* Exact method for finding maximum symmetries */
/* op = 0, brute force method */
/* 1, branch and bound with only one sort */
/* 2, branch and bound with sort every level */
/**************************************************/
pcover
Exact_Maximum_Symmetries(pcover *F, pcover *R, pcover *D, int op)
{
register int psize, i;
register pset *clist;
pcover tF, tR, tD, WSS, SSS, P, I, bestF, bestR, current_best;
int current_cost, level=0;
pcover T, bestD, record;
/* Initial */
bound = 0;
pass = 0;
boundlevel = (int *) malloc(NUMINPUTS*sizeof(int));
for(i=0; i<NUMINPUTS; i++)
boundlevel[i] = 0;
tF = sf_save(*F);
tR = sf_save(*R);
tD = sf_save(*D);
bestF = sf_save(*F);
bestR = sf_save(*R);
/* Generate WSS, SSS, P */
WSS = (pcover) Generate_Weak_SPM(tF, tR);
SSS = (pcover) Modified_SPM(sf_join(tF, tR), tD, WSS);
P = (pcover) Generate_Partition(SSS);
P = (pcover) sf_size_sort(P, 0);
psize = P->count;
current_best = sf_save(P);
record = sf_save(P);
current_cost = psize;
I = new_cover(psize);
/* Print initial state */
ttime = ptime();
printf("\n********** Initial Partition ***********\n");
Find_Partition(P);
printf("\n\ntime: %s\n", print_time(ptime()-ttime));
printf("*****************************************\n");
Exact_Maximum_Symmetries_Step(tF, tR, &bestF, &bestR, WSS, P, I, ¤t_best, ¤t_cost, level, record, op);
/* Modified bestF, bestR and bestD */
T = sf_join(bestF, bestR);
clist = cube1list(T);
bestD = complement(clist);
sf_free(T);
/* Show some information */
printf("\n********** Best Partition ***********\n");
printf("\nbound = %d\n", bound);
printf("pass = %d\n", pass);
printf("bound level:\n");
for(i=0; i<NUMINPUTS; i++)
printf("\tbound[%d] = %d\n", i, boundlevel[i]);
T = (pcover) Generate_Partition((pcover) Modified_SPM(sf_join(bestF, bestR), bestD, (pcover) Generate_Weak_SPM(bestF, bestR)));
Find_Partition_Count(T);
printf("\n-- Partition --");
Find_Partition(T);
printf("\n\ntime: %s\n", print_time(ptime()-ttime));
sf_free(T);
printf("*****************************************\n");
/* Modified F R and D */
sf_free(*F);
*F = bestF;
sf_free(*R);
*R = bestR;
sf_free(*D);
*D = bestD;
/* FREE */
sf_free(tF);
sf_free(tR);
sf_free(tD);
sf_free(WSS);
sf_free(SSS);
sf_free(P);
sf_free(I);
sf_free(record);
return current_best;
}