summaryrefslogtreecommitdiffstats
path: root/src/misc/espresso/pair.c
blob: a8077176a6e10cb91655caaea6c7e25d1de4510f (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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
 * Revision Control Information
 *
 * $Source$
 * $Author$
 * $Revision$
 * $Date$
 *
 */
#include "espresso.h"

void set_pair(PLA)
pPLA PLA;
{
    set_pair1(PLA, TRUE);
}

void set_pair1(PLA, adjust_labels)
pPLA PLA;
bool adjust_labels;
{
    int i, var, *paired, newvar;
    int old_num_vars, old_num_binary_vars, old_size, old_mv_start;
    int *new_part_size, new_num_vars, new_num_binary_vars, new_mv_start;
    ppair pair = PLA->pair;
    char scratch[1000], **oldlabel, *var1, *var1bar, *var2, *var2bar;

    if (adjust_labels)
    makeup_labels(PLA);

    /* Check the pair structure for valid entries and see which binary
    variables are left unpaired
    */
    paired = ALLOC(bool, cube.num_binary_vars);
    for(var = 0; var < cube.num_binary_vars; var++)
    paired[var] = FALSE;
    for(i = 0; i < pair->cnt; i++)
    if ((pair->var1[i] > 0 && pair->var1[i] <= cube.num_binary_vars) &&
         (pair->var2[i] > 0 && pair->var2[i] <= cube.num_binary_vars)) {
        paired[pair->var1[i]-1] = TRUE;
        paired[pair->var2[i]-1] = TRUE;
    } else
        fatal("can only pair binary-valued variables");

    PLA->F = delvar(pairvar(PLA->F, pair), paired);
    PLA->R = delvar(pairvar(PLA->R, pair), paired);
    PLA->D = delvar(pairvar(PLA->D, pair), paired);

    /* Now painfully adjust the cube size */
    old_size = cube.size;
    old_num_vars = cube.num_vars;
    old_num_binary_vars = cube.num_binary_vars;
    old_mv_start = cube.first_part[cube.num_binary_vars];
    /* Create the new cube.part_size vector and setup the cube structure */
    new_num_binary_vars = 0;
    for(var = 0; var < old_num_binary_vars; var++)
    new_num_binary_vars += (paired[var] == FALSE);
    new_num_vars = new_num_binary_vars + pair->cnt;
    new_num_vars += old_num_vars - old_num_binary_vars;
    new_part_size = ALLOC(int, new_num_vars);
    for(var = 0; var < pair->cnt; var++)
    new_part_size[new_num_binary_vars + var] = 4;
    for(var = 0; var < old_num_vars - old_num_binary_vars; var++)
    new_part_size[new_num_binary_vars + pair->cnt + var] =
        cube.part_size[old_num_binary_vars + var];
    setdown_cube();
    FREE(cube.part_size);
    cube.num_vars = new_num_vars;
    cube.num_binary_vars = new_num_binary_vars;
    cube.part_size = new_part_size;
    cube_setup();

    /* hack with the labels to get them correct */
    if (adjust_labels) {
    oldlabel = PLA->label;
    PLA->label = ALLOC(char *, cube.size);
    for(var = 0; var < pair->cnt; var++) {
        newvar = cube.num_binary_vars*2 + var*4;
        var1 = oldlabel[ (pair->var1[var]-1) * 2 + 1];
        var2 = oldlabel[ (pair->var2[var]-1) * 2 + 1];
        var1bar = oldlabel[ (pair->var1[var]-1) * 2];
        var2bar = oldlabel[ (pair->var2[var]-1) * 2];
        (void) sprintf(scratch, "%s+%s", var1bar, var2bar);
        PLA->label[newvar] = util_strsav(scratch);
        (void) sprintf(scratch, "%s+%s", var1bar, var2);
        PLA->label[newvar+1] = util_strsav(scratch);
        (void) sprintf(scratch, "%s+%s", var1, var2bar);
        PLA->label[newvar+2] = util_strsav(scratch);
        (void) sprintf(scratch, "%s+%s", var1, var2);
        PLA->label[newvar+3] = util_strsav(scratch);
    }
    /* Copy the old labels for the unpaired binary vars */
    i = 0;
    for(var = 0; var < old_num_binary_vars; var++) {
        if (paired[var] == FALSE) {
        PLA->label[2*i] = oldlabel[2*var];
        PLA->label[2*i+1] = oldlabel[2*var+1];
        oldlabel[2*var] = oldlabel[2*var+1] = (char *) NULL;
        i++;
        }
    }
    /* Copy the old labels for the remaining unpaired vars */
    new_mv_start = cube.num_binary_vars*2 + pair->cnt*4;
    for(i = old_mv_start; i < old_size; i++) {
        PLA->label[new_mv_start + i - old_mv_start] = oldlabel[i];
        oldlabel[i] = (char *) NULL;
    }
    /* free remaining entries in oldlabel */
    for(i = 0; i < old_size; i++)
        if (oldlabel[i] != (char *) NULL)
        FREE(oldlabel[i]);
    FREE(oldlabel);
    }

    /* the paired variables should not be sparse (cf. mv_reduce/raise_in)*/
    for(var = 0; var < pair->cnt; var++)
    cube.sparse[cube.num_binary_vars + var] = 0;
    FREE(paired);
}

pcover pairvar(A, pair)
pcover A;
ppair pair;
{
    register pcube last, p;
    register int val, p1, p2, b1, b0;
    int insert_col, pairnum;

    insert_col = cube.first_part[cube.num_vars - 1];

    /* stretch the cover matrix to make room for the paired variables */
    A = sf_delcol(A, insert_col, -4*pair->cnt);

    /* compute the paired values */
    foreach_set(A, last, p) {
    for(pairnum = 0; pairnum < pair->cnt; pairnum++) {
        p1 = cube.first_part[pair->var1[pairnum] - 1];
        p2 = cube.first_part[pair->var2[pairnum] - 1];
        b1 = is_in_set(p, p2+1);
        b0 = is_in_set(p, p2);
        val = insert_col + pairnum * 4;
        if (/* a0 */ is_in_set(p, p1)) {
        if (b0)
            set_insert(p, val + 3);
        if (b1)
            set_insert(p, val + 2);
        }
        if (/* a1 */ is_in_set(p, p1+1)) {
        if (b0)
            set_insert(p, val + 1);
        if (b1)
            set_insert(p, val);
        }
    }
    }
    return A;
}


/* delvar -- delete variables from A, minimize the number of column shifts */
pcover delvar(A, paired)
pcover A;
bool paired[];
{
    bool run;
    int first_run, run_length, var, offset = 0;

    run = FALSE; run_length = 0;
    for(var = 0; var < cube.num_binary_vars; var++)
    if (paired[var])
        if (run)
        run_length += cube.part_size[var];
        else {
        run = TRUE;
        first_run = cube.first_part[var];
        run_length = cube.part_size[var];
        }
    else
        if (run) {
        A = sf_delcol(A, first_run-offset, run_length);
        run = FALSE;
        offset += run_length;
        }
    if (run)
    A = sf_delcol(A, first_run-offset, run_length);
    return A;
}

/*
    find_optimal_pairing -- find which binary variables should be paired
    to maximally reduce the number of terms

    This is essentially the technique outlined by T. Sasao in the
    Trans. on Comp., Oct 1984.  We estimate the cost of pairing each
    pair individually using 1 of 4 strategies: (1) algebraic division
    of F by the pair (exactly T. Sasao technique); (2) strong division
    of F by the paired variables (using REDUCE/EXPAND/ IRREDUNDANT from
    espresso); (3) full minimization using espresso; (4) exact
    minimization.  These are in order of both increasing accuracy and
    increasing difficulty (!)

    Once the n squared pairs have been evaluated, T. Sasao proposes a
    graph covering of nodes by disjoint edges.  For now, I solve this
    problem exhaustively (complexity = (n-1)*(n-3)*...*3*1 for n
    variables when n is even).  Note that solving this problem exactly
    is the same as evaluating the cost function for all possible
    pairings.

                   n       pairs

                 1, 2           1
                 3, 4           3
                 5, 6          15
                 7, 8         105
                 9,10         945
                11,12      10,395
                13,14     135,135
                15,16   2,027,025
                17,18  34,459,425
                19,20 654,729,075
*/
void find_optimal_pairing(PLA, strategy)
pPLA PLA;
int strategy;
{
    int i, j, **cost_array;

    cost_array = find_pairing_cost(PLA, strategy);

    if (summary) {
    printf("    ");
    for(i = 0; i < cube.num_binary_vars; i++)
        printf("%3d ", i+1);
    printf("\n");
    for(i = 0; i < cube.num_binary_vars; i++) {
        printf("%3d ", i+1);
        for(j = 0; j < cube.num_binary_vars; j++)
        printf("%3d ", cost_array[i][j]);
        printf("\n");
    }
    }

    if (cube.num_binary_vars <= 14) {
    PLA->pair = pair_best_cost(cost_array);
    } else {
    (void) greedy_best_cost(cost_array, &(PLA->pair));
    }
    printf("# ");
    print_pair(PLA->pair);
    
    for(i = 0; i < cube.num_binary_vars; i++)
    FREE(cost_array[i]);
    FREE(cost_array);

    set_pair(PLA);
    EXEC_S(PLA->F=espresso(PLA->F,PLA->D,PLA->R),"ESPRESSO  ",PLA->F);
}

int **find_pairing_cost(PLA, strategy)
pPLA PLA;
int strategy;
{
    int var1, var2, **cost_array;
    int i, j, xnum_binary_vars, xnum_vars, *xpart_size, cost;
    pcover T, Fsave, Dsave, Rsave;
    pset mask;
/*    char *s;*/

    /* data is returned in the cost array */
    cost_array = ALLOC(int *, cube.num_binary_vars);
    for(i = 0; i < cube.num_binary_vars; i++)
    cost_array[i] = ALLOC(int, cube.num_binary_vars);
    for(i = 0; i < cube.num_binary_vars; i++)
    for(j = 0; j < cube.num_binary_vars; j++)
        cost_array[i][j] = 0;

    /* Setup the pair structure for pairing variables together */
    PLA->pair = pair_new(1);
    PLA->pair->cnt = 1;

    for(var1 = 0; var1 < cube.num_binary_vars-1; var1++) {
    for(var2 = var1+1; var2 < cube.num_binary_vars; var2++) {
        /* if anything but simple strategy, perform setup */
        if (strategy > 0) {
        /* save the original covers */
        Fsave = sf_save(PLA->F);
        Dsave = sf_save(PLA->D);
        Rsave = sf_save(PLA->R);

        /* save the original cube structure */
        xnum_binary_vars = cube.num_binary_vars;
        xnum_vars = cube.num_vars;
        xpart_size = ALLOC(int, cube.num_vars);
        for(i = 0; i < cube.num_vars; i++)
            xpart_size[i] = cube.part_size[i];

        /* pair two variables together */
        PLA->pair->var1[0] = var1 + 1;
        PLA->pair->var2[0] = var2 + 1;
        set_pair1(PLA, /* adjust_labels */ FALSE);
        }


        /* decide how to best estimate worth of this pairing */
        switch(strategy) {
        case 3:
            /*s = "exact minimization";*/
            PLA->F = minimize_exact(PLA->F, PLA->D, PLA->R, 1);
            cost = Fsave->count - PLA->F->count;
            break;
        case 2:
            /*s = "full minimization";*/
            PLA->F = espresso(PLA->F, PLA->D, PLA->R);
            cost = Fsave->count - PLA->F->count;
            break;
        case 1:
            /*s = "strong division";*/
            PLA->F = reduce(PLA->F, PLA->D);
            PLA->F = expand(PLA->F, PLA->R, FALSE);
            PLA->F = irredundant(PLA->F, PLA->D);
            cost = Fsave->count - PLA->F->count;
            break;
        case 0:
            /*s = "weak division";*/
            mask = new_cube();
            set_or(mask, cube.var_mask[var1], cube.var_mask[var2]);
            T = dist_merge(sf_save(PLA->F), mask);
            cost = PLA->F->count - T->count;
            sf_free(T);
            set_free(mask);
        }

        cost_array[var1][var2] = cost;

        if (strategy > 0) {
        /* restore the original cube structure -- free the new ones */
        setdown_cube();
        FREE(cube.part_size);
        cube.num_binary_vars = xnum_binary_vars;
        cube.num_vars = xnum_vars;
        cube.part_size = xpart_size;
        cube_setup();

        /* restore the original cover(s) -- free the new ones */
        sf_free(PLA->F);
        sf_free(PLA->D);
        sf_free(PLA->R);
        PLA->F = Fsave;
        PLA->D = Dsave;
        PLA->R = Rsave;
        }
    }
    }

    pair_free(PLA->pair);
    PLA->pair = NULL;
    return cost_array;
}

static int best_cost;
static int **cost_array;
static ppair best_pair;
static pset best_phase;
static pPLA global_PLA;
static pcover best_F, best_D, best_R;
static int pair_minim_strategy;


print_pair(pair)
ppair pair;
{
    int i;

    printf("pair is");
    for(i = 0; i < pair->cnt; i++)
    printf (" (%d %d)", pair->var1[i], pair->var2[i]);
    printf("\n");
}


int greedy_best_cost(cost_array_local, pair_p)
int **cost_array_local;
ppair *pair_p;
{
    int i, j, besti, bestj, maxcost, total_cost;
    pset cand;
    ppair pair;

    pair = pair_new(cube.num_binary_vars);
    cand = set_full(cube.num_binary_vars);
    total_cost = 0;

    while (set_ord(cand) >= 2) {
    maxcost = -1;
    for(i = 0; i < cube.num_binary_vars; i++) {
        if (is_in_set(cand, i)) {
        for(j = i+1; j < cube.num_binary_vars; j++) {
            if (is_in_set(cand, j)) {
            if (cost_array_local[i][j] > maxcost) {
                maxcost = cost_array_local[i][j];
                besti = i;
                bestj = j;
            }
            }
        }
        }
    }
    pair->var1[pair->cnt] = besti+1;
    pair->var2[pair->cnt] = bestj+1;
    pair->cnt++;
    set_remove(cand, besti);
    set_remove(cand, bestj);
    total_cost += maxcost;
    }
    set_free(cand);
    *pair_p = pair;
    return total_cost;
}


ppair pair_best_cost(cost_array_local)
int **cost_array_local;
{
    ppair pair;
    pset candidate;

    best_cost = -1;
    best_pair = NULL;
    cost_array = cost_array_local;

    pair = pair_new(cube.num_binary_vars);
    candidate = set_full(cube.num_binary_vars);
    generate_all_pairs(pair, cube.num_binary_vars, candidate, find_best_cost);
    pair_free(pair);
    set_free(candidate);
    return best_pair;
}


int find_best_cost(pair)
register ppair pair;
{
    register int i, cost;

    cost = 0;
    for(i = 0; i < pair->cnt; i++)
    cost += cost_array[pair->var1[i]-1][pair->var2[i]-1];
    if (cost > best_cost) {
    best_cost = cost;
    best_pair = pair_save(pair, pair->cnt);
    }
    if ((debug & MINCOV) && trace) {
    printf("cost is %d ", cost);
    print_pair(pair);
    }
}

/*
    pair_all: brute-force approach to try all possible pairings

    pair_strategy is:
    2) for espresso
    3) for minimize_exact
    4) for phase assignment
*/

pair_all(PLA, pair_strategy)
pPLA PLA;
int pair_strategy;
{
    ppair pair;
    pset candidate;

    global_PLA = PLA;
    pair_minim_strategy = pair_strategy;
    best_cost = PLA->F->count + 1;
    best_pair = NULL;
    best_phase = NULL;
    best_F = best_D = best_R = NULL;
    pair = pair_new(cube.num_binary_vars);
    candidate = set_fill(set_new(cube.num_binary_vars), cube.num_binary_vars);

    generate_all_pairs(pair, cube.num_binary_vars, candidate, minimize_pair);

    pair_free(pair);
    set_free(candidate);

    PLA->pair = best_pair;
    PLA->phase = best_phase;
/* not really necessary
    if (phase != NULL)
    (void) set_phase(PLA->phase);
*/
    set_pair(PLA);
    printf("# ");
    print_pair(PLA->pair);

    sf_free(PLA->F);
    sf_free(PLA->D);
    sf_free(PLA->R);
    PLA->F = best_F;
    PLA->D = best_D;
    PLA->R = best_R;
}


/*
 *  minimize_pair -- called as each pair is generated
 */
int minimize_pair(pair)
ppair pair;
{
    pcover Fsave, Dsave, Rsave;
    int i, xnum_binary_vars, xnum_vars, *xpart_size;

    /* save the original covers */
    Fsave = sf_save(global_PLA->F);
    Dsave = sf_save(global_PLA->D);
    Rsave = sf_save(global_PLA->R);

    /* save the original cube structure */
    xnum_binary_vars = cube.num_binary_vars;
    xnum_vars = cube.num_vars;
    xpart_size = ALLOC(int, cube.num_vars);
    for(i = 0; i < cube.num_vars; i++)
    xpart_size[i] = cube.part_size[i];

    /* setup the paired variables */
    global_PLA->pair = pair;
    set_pair1(global_PLA, /* adjust_labels */ FALSE);

    /* call the minimizer */
    if (summary)
    print_pair(pair);
    switch(pair_minim_strategy) {
    case 2:
        EXEC_S(phase_assignment(global_PLA,0), "OPO       ", global_PLA->F);
        if (summary)
        printf("# phase is %s\n", pc1(global_PLA->phase));
        break;
    case 1:
        EXEC_S(global_PLA->F = minimize_exact(global_PLA->F, global_PLA->D,
        global_PLA->R, 1), "EXACT     ", global_PLA->F);
        break;
    case 0:
        EXEC_S(global_PLA->F = espresso(global_PLA->F, global_PLA->D,
        global_PLA->R), "ESPRESSO  ", global_PLA->F);
        break;
    default:
        break;
    }

    /* see if we have a new best solution */
    if (global_PLA->F->count < best_cost) {
    best_cost = global_PLA->F->count;
    best_pair = pair_save(pair, pair->cnt);
    best_phase = global_PLA->phase!=NULL?set_save(global_PLA->phase):NULL;
    if (best_F != NULL) sf_free(best_F);
    if (best_D != NULL) sf_free(best_D);
    if (best_R != NULL) sf_free(best_R);
    best_F = sf_save(global_PLA->F);
    best_D = sf_save(global_PLA->D);
    best_R = sf_save(global_PLA->R);
    }

    /* restore the original cube structure -- free the new ones */
    setdown_cube();
    FREE(cube.part_size);
    cube.num_binary_vars = xnum_binary_vars;
    cube.num_vars = xnum_vars;
    cube.part_size = xpart_size;
    cube_setup();

    /* restore the original cover(s) -- free the new ones */
    sf_free(global_PLA->F);
    sf_free(global_PLA->D);
    sf_free(global_PLA->R);
    global_PLA->F = Fsave;
    global_PLA->D = Dsave;
    global_PLA->R = Rsave;
    global_PLA->pair = NULL;
    global_PLA->phase = NULL;
}

generate_all_pairs(pair, n, candidate, action)
ppair pair;
int n;
pset candidate;
int (*action)();
{
    int i, j;
    pset recur_candidate;
    ppair recur_pair;

    if (set_ord(candidate) < 2) {
    (*action)(pair);
    return;
    }

    recur_pair = pair_save(pair, n);
    recur_candidate = set_save(candidate);

    /* Find first variable still in the candidate set */
    for(i = 0; i < n; i++)
    if (is_in_set(candidate, i))
        break;

    /* Try all pairs of i with other variables */
    for(j = i+1; j < n; j++)
    if (is_in_set(candidate, j)) {
        /* pair (i j) -- remove from candidate set for future pairings */
        set_remove(recur_candidate, i);
        set_remove(recur_candidate, j);

        /* add to the pair array */
        recur_pair->var1[recur_pair->cnt] = i+1;
        recur_pair->var2[recur_pair->cnt] = j+1;
        recur_pair->cnt++;

        /* recur looking for the end ... */
        generate_all_pairs(recur_pair, n, recur_candidate, action);

        /* now break this pair, and restore candidate set */
        recur_pair->cnt--;
        set_insert(recur_candidate, i);
        set_insert(recur_candidate, j);
    }

    /* if odd, generate all pairs which do NOT include i */
    if ((set_ord(candidate) % 2) == 1) {
    set_remove(recur_candidate, i);
    generate_all_pairs(recur_pair, n, recur_candidate, action);
    }

    pair_free(recur_pair);
    set_free(recur_candidate);
}

ppair pair_new(n)
register int n;
{
    register ppair pair1;

    pair1 = ALLOC(pair_t, 1);
    pair1->cnt = 0;
    pair1->var1 = ALLOC(int, n);
    pair1->var2 = ALLOC(int, n);
    return pair1;
}


ppair pair_save(pair, n)
register ppair pair;
register int n;
{
    register int k;
    register ppair pair1;

    pair1 = pair_new(n);
    pair1->cnt = pair->cnt;
    for(k = 0; k < pair->cnt; k++) {
    pair1->var1[k] = pair->var1[k];
    pair1->var2[k] = pair->var2[k];
    }
    return pair1;
}


int pair_free(pair)
register ppair pair;
{
    FREE(pair->var1);
    FREE(pair->var2);
    FREE(pair);
}