00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "espresso.h"
00011
00012
00013 static void dump_irredundant();
00014 static pcover do_minimize();
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 pcover
00027 minimize_exact(F, D, R, exact_cover)
00028 pcover F, D, R;
00029 int exact_cover;
00030 {
00031 return do_minimize(F, D, R, exact_cover, 0);
00032 }
00033
00034
00035 pcover
00036 minimize_exact_literals(F, D, R, exact_cover)
00037 pcover F, D, R;
00038 int exact_cover;
00039 {
00040 return do_minimize(F, D, R, exact_cover, 1);
00041 }
00042
00043
00044
00045 static pcover
00046 do_minimize(F, D, R, exact_cover, weighted)
00047 pcover F, D, R;
00048 int exact_cover;
00049 int weighted;
00050 {
00051 pcover newF, E, Rt, Rp;
00052 pset p, last;
00053 int heur, level, *weights, i;
00054 sm_matrix *table;
00055 sm_row *cover;
00056 sm_element *pe;
00057 int debug_save = debug;
00058
00059 if (debug & EXACT) {
00060 debug |= (IRRED | MINCOV);
00061 }
00062 #if defined(sun) || defined(bsd4_2)
00063 if (debug & MINCOV) {
00064 setlinebuf(stdout);
00065 }
00066 #endif
00067 level = (debug & MINCOV) ? 4 : 0;
00068 heur = ! exact_cover;
00069
00070
00071 EXEC(F = primes_consensus(cube2list(F, D)), "PRIMES ", F);
00072
00073
00074 EXEC(irred_split_cover(F, D, &E, &Rt, &Rp), "ESSENTIALS ", E);
00075 EXEC(table = irred_derive_table(D, E, Rp), "PI-TABLE ", Rp);
00076
00077
00078 if (weighted) {
00079
00080 weights = ALLOC(int, F->count);
00081 foreach_set(Rp, last, p) {
00082 weights[SIZE(p)] = cube.size - set_ord(p);
00083
00084
00085 for (i = cube.first_part[cube.output];
00086 i <= cube.last_part[cube.output]; i++) {
00087 is_in_set(p, i) ? weights[SIZE(p)]++ : weights[SIZE(p)]--;
00088 }
00089 }
00090 } else {
00091 weights = NIL(int);
00092 }
00093 EXEC(cover=sm_minimum_cover(table,weights,heur,level), "MINCOV ", F);
00094 if (weights != 0) {
00095 FREE(weights);
00096 }
00097
00098 if (debug & EXACT) {
00099 dump_irredundant(E, Rt, Rp, table);
00100 }
00101
00102
00103 newF = new_cover(100);
00104 foreach_set(E, last, p) {
00105 newF = sf_addset(newF, p);
00106 }
00107 sm_foreach_row_element(cover, pe) {
00108 newF = sf_addset(newF, GETSET(F, pe->col_num));
00109 }
00110
00111 free_cover(E);
00112 free_cover(Rt);
00113 free_cover(Rp);
00114 sm_free(table);
00115 sm_row_free(cover);
00116 free_cover(F);
00117
00118
00119 debug &= ~ (IRRED | SHARP | MINCOV);
00120 if (! skip_make_sparse && R != 0) {
00121 newF = make_sparse(newF, D, R);
00122 }
00123
00124 debug = debug_save;
00125 return newF;
00126 }
00127
00128 static void
00129 dump_irredundant(E, Rt, Rp, table)
00130 pcover E, Rt, Rp;
00131 sm_matrix *table;
00132 {
00133 FILE *fp_pi_table, *fp_primes;
00134 pPLA PLA;
00135 pset last, p;
00136 char *file;
00137
00138 if (filename == 0 || strcmp(filename, "(stdin)") == 0) {
00139 fp_pi_table = fp_primes = stdout;
00140 } else {
00141 file = ALLOC(char, strlen(filename)+20);
00142 (void) sprintf(file, "%s.primes", filename);
00143 if ((fp_primes = fopen(file, "w")) == NULL) {
00144 (void) fprintf(stderr, "espresso: Unable to open %s\n", file);
00145 fp_primes = stdout;
00146 }
00147 (void) sprintf(file, "%s.pi", filename);
00148 if ((fp_pi_table = fopen(file, "w")) == NULL) {
00149 (void) fprintf(stderr, "espresso: Unable to open %s\n", file);
00150 fp_pi_table = stdout;
00151 }
00152 FREE(file);
00153 }
00154
00155 PLA = new_PLA();
00156 PLA_labels(PLA);
00157
00158 fpr_header(fp_primes, PLA, F_type);
00159 free_PLA(PLA);
00160
00161 (void) fprintf(fp_primes, "# Essential primes are\n");
00162 foreach_set(E, last, p) {
00163 (void) fprintf(fp_primes, "%s\n", pc1(p));
00164 }
00165 (void) fprintf(fp_primes, "# Totally redundant primes are\n");
00166 foreach_set(Rt, last, p) {
00167 (void) fprintf(fp_primes, "%s\n", pc1(p));
00168 }
00169 (void) fprintf(fp_primes, "# Partially redundant primes are\n");
00170 foreach_set(Rp, last, p) {
00171 (void) fprintf(fp_primes, "%s\n", pc1(p));
00172 }
00173 if (fp_primes != stdout) {
00174 (void) fclose(fp_primes);
00175 }
00176
00177 sm_write(fp_pi_table, table);
00178 if (fp_pi_table != stdout) {
00179 (void) fclose(fp_pi_table);
00180 }
00181 }