summaryrefslogtreecommitdiffstats
path: root/src/misc/espresso/unate.c
blob: bd71207f5c3a62676229e66e88e37b6d169fdd41 (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
/*
 * Revision Control Information
 *
 * $Source$
 * $Author$
 * $Revision$
 * $Date$
 *
 */
/*
 *  unate.c -- routines for dealing with unate functions
 */

#include "espresso.h"

static pset_family abs_covered();
static pset_family abs_covered_many();
static int abs_select_restricted();

pcover map_cover_to_unate(T)
pcube *T;
{
    register unsigned int word_test, word_set, bit_test, bit_set;
    register pcube p, pA;
    pset_family A;
    pcube *T1;
    int ncol, i;

    A = sf_new(CUBELISTSIZE(T), cdata.vars_unate);
    A->count = CUBELISTSIZE(T);
    foreachi_set(A, i, p) {
    (void) set_clear(p, A->sf_size);
    }
    ncol = 0;

    for(i = 0; i < cube.size; i++) {
    if (cdata.part_zeros[i] > 0) {
        assert(ncol <= cdata.vars_unate);

        /* Copy a column from T to A */
        word_test = WHICH_WORD(i);
        bit_test = 1 << WHICH_BIT(i);
        word_set = WHICH_WORD(ncol);
        bit_set = 1 << WHICH_BIT(ncol);

        pA = A->data;
        for(T1 = T+2; (p = *T1++) != 0; ) {
        if ((p[word_test] & bit_test) == 0) {
            pA[word_set] |= bit_set;
        }
        pA += A->wsize;
        }

        ncol++;
    }
    }

    return A;
}

pcover map_unate_to_cover(A)
pset_family A;
{
    register int i, ncol, lp;
    register pcube p, pB;
    int var, nunate, *unate;
    pcube last;
    pset_family B;

    B = sf_new(A->count, cube.size);
    B->count = A->count;

    /* Find the unate variables */
    unate = ALLOC(int, cube.num_vars);
    nunate = 0;
    for(var = 0; var < cube.num_vars; var++) {
    if (cdata.is_unate[var]) {
        unate[nunate++] = var;
    }
    }

    /* Loop for each set of A */
    pB = B->data;
    foreach_set(A, last, p) {

    /* Initialize this set of B */
    INLINEset_fill(pB, cube.size);

    /* Now loop for the unate variables; if the part is in A,
     * then this variable of B should be a single 1 in the unate
     * part.
     */
    for(ncol = 0; ncol < nunate; ncol++) {
        if (is_in_set(p, ncol)) {
        lp = cube.last_part[unate[ncol]];
        for(i = cube.first_part[unate[ncol]]; i <= lp; i++) {
            if (cdata.part_zeros[i] == 0) {
            set_remove(pB, i);
            }
        }
        }
    }
    pB += B->wsize;
    }

    FREE(unate);
    return B;
}

/*
 *  unate_compl
 */

pset_family unate_compl(A)
pset_family A;
{
    register pset p, last;

    /* Make sure A is single-cube containment minimal */
/*    A = sf_rev_contain(A);*/

    foreach_set(A, last, p) {
    PUTSIZE(p, set_ord(p));
    }

    /* Recursively find the complement */
    A = unate_complement(A);

    /* Now, we can guarantee a minimal result by containing the result */
    A = sf_rev_contain(A);
    return A;
}


/*
 *  Assume SIZE(p) records the size of each set
 */
pset_family unate_complement(A)
pset_family A;            /* disposes of A */
{
    pset_family Abar;
    register pset p, p1, restrict;
    register int i;
    int max_i, min_set_ord, j;

    /* Check for no sets in the matrix -- complement is the universe */
    if (A->count == 0) {
    sf_free(A);
    Abar = sf_new(1, A->sf_size);
    (void) set_clear(GETSET(Abar, Abar->count++), A->sf_size);

    /* Check for a single set in the maxtrix -- compute de Morgan complement */
    } else if (A->count == 1) {
    p = A->data;
    Abar = sf_new(A->sf_size, A->sf_size);
    for(i = 0; i < A->sf_size; i++) {
        if (is_in_set(p, i)) {
        p1 = set_clear(GETSET(Abar, Abar->count++), A->sf_size);
        set_insert(p1, i);
        }
    }
    sf_free(A);

    } else {

    /* Select splitting variable as the variable which belongs to a set
     * of the smallest size, and which has greatest column count
     */
    restrict = set_new(A->sf_size);
    min_set_ord = A->sf_size + 1;
    foreachi_set(A, i, p) {
        if (SIZE(p) < min_set_ord) {
        set_copy(restrict, p);
        min_set_ord = SIZE(p);
        } else if (SIZE(p) == min_set_ord) {
        set_or(restrict, restrict, p);
        }
    }

    /* Check for no data (shouldn't happen ?) */
    if (min_set_ord == 0) {
        A->count = 0;
        Abar = A;

    /* Check for "essential" columns */
    } else if (min_set_ord == 1) {
        Abar = unate_complement(abs_covered_many(A, restrict));
        sf_free(A);
        foreachi_set(Abar, i, p) {
        set_or(p, p, restrict);
        }

    /* else, recur as usual */
    } else {
        max_i = abs_select_restricted(A, restrict);

        /* Select those rows of A which are not covered by max_i,
         * recursively find all minimal covers of these rows, and
         * then add back in max_i
         */
        Abar = unate_complement(abs_covered(A, max_i));
        foreachi_set(Abar, i, p) {
        set_insert(p, max_i);
        }

        /* Now recur on A with all zero's on column max_i */
        foreachi_set(A, i, p) {
        if (is_in_set(p, max_i)) {
            set_remove(p, max_i);
            j = SIZE(p) - 1;
            PUTSIZE(p, j);
        }
        }

        Abar = sf_append(Abar, unate_complement(A));
    }
    set_free(restrict);
    }

    return Abar;
}

pset_family exact_minimum_cover(T)
IN pset_family T;
{
    register pset p, last, p1;
    register int i, n;
    int lev, lvl;
    pset nlast;
    pset_family temp;
    long start = ptime();
    struct {
    pset_family sf;
    int level;
    } stack[32];                /* 32 suffices for 2 ** 32 cubes ! */

    if (T->count <= 0)
    return sf_new(1, T->sf_size);
    for(n = T->count, lev = 0; n != 0; n >>= 1, lev++)   ;

    /* A simple heuristic ordering */
    T = lex_sort(sf_save(T));

    /* Push a full set on the stack to get things started */
    n = 1;
    stack[0].sf = sf_new(1, T->sf_size);
    stack[0].level = lev;
    set_fill(GETSET(stack[0].sf, stack[0].sf->count++), T->sf_size);

    nlast = GETSET(T, T->count - 1);
    foreach_set(T, last, p) {

    /* "unstack" the set into a family */
    temp = sf_new(set_ord(p), T->sf_size);
    for(i = 0; i < T->sf_size; i++)
        if (is_in_set(p, i)) {
        p1 = set_fill(GETSET(temp, temp->count++), T->sf_size);
        set_remove(p1, i);
        }
    stack[n].sf = temp;
    stack[n++].level = lev;

    /* Pop the stack and perform (leveled) intersections */
    while (n > 1 && (stack[n-1].level==stack[n-2].level || p == nlast)) {
        temp = unate_intersect(stack[n-1].sf, stack[n-2].sf, FALSE);
        lvl = MIN(stack[n-1].level, stack[n-2].level) - 1;
        if (debug & MINCOV && lvl < 10) {
        printf("# EXACT_MINCOV[%d]: %4d = %4d x %4d, time = %s\n",
            lvl, temp->count, stack[n-1].sf->count,
            stack[n-2].sf->count, print_time(ptime() - start));
        (void) fflush(stdout);
        }
        sf_free(stack[n-2].sf);
        sf_free(stack[n-1].sf);
        stack[n-2].sf = temp;
        stack[n-2].level = lvl;
        n--;
    }
    }

    temp = stack[0].sf;
    p1 = set_fill(set_new(T->sf_size), T->sf_size);
    foreach_set(temp, last, p)
    INLINEset_diff(p, p1, p);
    set_free(p1);
    if (debug & MINCOV1) {
    printf("MINCOV: family of all minimal coverings is\n");
    sf_print(temp);
    }
    sf_free(T);         /* this is the copy of T we made ... */
    return temp;
}

/*
 *  unate_intersect -- intersect two unate covers
 *
 *  If largest_only is TRUE, then only the largest cube(s) are returned
 */

#define MAGIC 500               /* save 500 cubes before containment */

pset_family unate_intersect(A, B, largest_only)
pset_family A, B;
bool largest_only;
{
    register pset pi, pj, lasti, lastj, pt;
    pset_family T, Tsave;
    bool save;
    int maxord, ord;

    /* How large should each temporary result cover be ? */
    T = sf_new(MAGIC, A->sf_size);
    Tsave = NULL;
    maxord = 0;
    pt = T->data;

    /* Form pairwise intersection of each set of A with each cube of B */
    foreach_set(A, lasti, pi) {

    foreach_set(B, lastj, pj) {

        save = set_andp(pt, pi, pj);

        /* Check if we want the largest only */
        if (save && largest_only) {
        if ((ord = set_ord(pt)) > maxord) {
            /* discard Tsave and T */
            if (Tsave != NULL) {
            sf_free(Tsave);
            Tsave = NULL;
            }
            pt = T->data;
            T->count = 0;
            /* Re-create pt (which was just thrown away) */
            (void) set_and(pt, pi, pj);
            maxord = ord;
        } else if (ord < maxord) {
            save = FALSE;
        }
        }

        if (save) {
        if (++T->count >= T->capacity) {
            T = sf_contain(T);
            Tsave = (Tsave == NULL) ? T : sf_union(Tsave, T);
            T = sf_new(MAGIC, A->sf_size);
            pt = T->data;
        } else {
            pt += T->wsize;
        }
        }
    }
    }


    /* Contain the final result and merge it into Tsave */
    T = sf_contain(T);
    Tsave = (Tsave == NULL) ? T : sf_union(Tsave, T);

    return Tsave;
}

/*
 *  abs_covered -- after selecting a new column for the selected set,
 *  create a new matrix which is only those rows which are still uncovered
 */
static pset_family
abs_covered(A, pick)
pset_family A;
register int pick;
{
    register pset last, p, pdest;
    register pset_family Aprime;

    Aprime = sf_new(A->count, A->sf_size);
    pdest = Aprime->data;
    foreach_set(A, last, p)
    if (! is_in_set(p, pick)) {
        INLINEset_copy(pdest, p);
        Aprime->count++;
        pdest += Aprime->wsize;
    }
    return Aprime;
}


/*
 *  abs_covered_many -- after selecting many columns for ther selected set,
 *  create a new matrix which is only those rows which are still uncovered
 */
static pset_family
abs_covered_many(A, pick_set)
pset_family A;
register pset pick_set;
{
    register pset last, p, pdest;
    register pset_family Aprime;

    Aprime = sf_new(A->count, A->sf_size);
    pdest = Aprime->data;
    foreach_set(A, last, p)
    if (setp_disjoint(p, pick_set)) {
        INLINEset_copy(pdest, p);
        Aprime->count++;
        pdest += Aprime->wsize;
    }
    return Aprime;
}


/*
 *  abs_select_restricted -- select the column of maximum column count which
 *  also belongs to the set "restrict"; weight each column of a set as
 *  1 / (set_ord(p) - 1).
 */
static int
abs_select_restricted(A, restrict)
pset_family A;
pset restrict;
{
    register int i, best_var, best_count, *count;

    /* Sum the elements in these columns */
    count = sf_count_restricted(A, restrict);

    /* Find which variable has maximum weight */
    best_var = -1;
    best_count = 0;
    for(i = 0; i < A->sf_size; i++) {
    if (count[i] > best_count) {
        best_var = i;
        best_count = count[i];
    }
    }
    FREE(count);

    if (best_var == -1)
    fatal("abs_select_restricted: should not have best_var == -1");

    return best_var;
}