13 typedef uint32_t AdjWord;
14 #define PRADJ "08"PRIx32
16 static int n, m, maxhamweight;
17 static AdjWord *adjmatrix;
18 static AdjWord adjall;
21 static glp_prob *best_prob;
22 static AdjWord *best_adjmatrix;
24 static unsigned printcounter;
26 static AdjWord *xalloc_adjmatrix(void) {
27 return xmalloc(sizeof(*adjmatrix)*n);
30 static void prep(void) {
31 adjall = ~((~(AdjWord)0) << m);
32 adjmatrix = xalloc_adjmatrix();
33 glp_term_out(GLP_OFF);
36 static AdjWord one_adj_bit(int bitnum) {
37 return (AdjWord)1 << bitnum;
40 static int count_set_adj_bits(AdjWord w) {
42 for (j=0, total=0; j<m; j++)
43 total += !!(w & one_adj_bit(j));
47 static void optimise(int doprint) {
51 #define HAVE_PRINTED ({ \
52 if (!doprint) { doprint = 1; goto retry_with_print; } \
56 glp_delete_prob(prob);
60 #define PRINTF if (!doprint) ; else printf /* bodgy */
62 PRINTF("%2d ", maxhamweight);
65 for (i=0, totalfrags=0; i<n; i++) {
66 int frags = count_set_adj_bits(adjmatrix[i]);
67 had_max += (frags == maxhamweight);
69 PRINTF("%"PRADJ" ", adjmatrix[i]);
70 double maxminsize = (double)m / frags;
71 if (maxminsize <= best) {
82 * We formulate our problem as an LP problem as follows.
83 * In this file "n" and "m" are the matchstick numbers.
85 * Each set bit in the adjacency matrix corresponds to taking a
86 * fragment from old match i and making it part of new match j.
88 * The structural variables (columns) are:
89 * x_minimum minimum size of any fragment (bounded below by 0)
90 * x_morefrag_i_j the amount by which the size of the fragment
91 * i,j exceeds the minimum size (bounded below by 0)
93 * The auxiliary variables (rows) are:
94 * x_total_i total length for each input match (fixed variable)
95 * x_total_j total length for each output match (fixed variable)
97 * The objective function is simply
100 * We use X_ and Y_ to refer to GLPK's (1-based) column and row indices.
101 * ME_ refers to entries in the list of constraint matrix elements
102 * which we build up as we go.
105 prob = glp_create_prob();
107 int Y_totals_i = glp_add_rows(prob, n);
108 int Y_totals_j = glp_add_rows(prob, m);
109 int X_minimum = glp_add_cols(prob, 1);
112 int next_matrix_entry = 1; /* wtf GLPK! */
113 int matrix_entries_size = next_matrix_entry + n + m + totalfrags*2;
114 double matrix_entries[matrix_entries_size];
115 int matrix_entries_XY[2][matrix_entries_size];
117 #define ADD_MATRIX_ENTRY(Y,X) ({ \
118 assert(next_matrix_entry < matrix_entries_size); \
119 matrix_entries_XY[0][next_matrix_entry] = (X); \
120 matrix_entries_XY[1][next_matrix_entry] = (Y); \
121 matrix_entries[next_matrix_entry] = 0; \
122 next_matrix_entry++; \
125 int ME_totals_i__minimum = next_matrix_entry;
126 for (i=0; i<n; i++) ADD_MATRIX_ENTRY(Y_totals_i+i, X_minimum);
128 int ME_totals_j__minimum = next_matrix_entry;
129 for (j=0; j<m; j++) ADD_MATRIX_ENTRY(Y_totals_j+j, X_minimum);
131 /* \forall_i x_totals_i = m */
132 /* \forall_i x_totals_j = n */
133 for (i=0; i<n; i++) glp_set_row_bnds(prob, Y_totals_i+i, GLP_FX, m,m);
134 for (j=0; j<m; j++) glp_set_row_bnds(prob, Y_totals_j+j, GLP_FX, n,n);
137 glp_set_col_bnds(prob, X_minimum, GLP_LO, 0, 0);
138 glp_set_col_name(prob, X_minimum, "minimum");
140 /* objective is maximising x_minimum */
141 glp_set_obj_dir(prob, GLP_MAX);
142 glp_set_obj_coef(prob, X_minimum, 1);
144 for (i=0; i<n; i++) {
145 for (j=0; j<m; j++) {
146 if (!(adjmatrix[i] & one_adj_bit(j)))
148 /* x_total_i += x_minimum */
149 /* x_total_j += x_minimum */
150 matrix_entries[ ME_totals_i__minimum + i ] ++;
151 matrix_entries[ ME_totals_j__minimum + j ] ++;
153 /* x_morefrag_i_j >= 0 */
154 int X_morefrag_i_j = glp_add_cols(prob, 1);
155 glp_set_col_bnds(prob, X_morefrag_i_j, GLP_LO, 0, 0);
158 snprintf(buf,sizeof(buf),"mf %d,%d",i,j);
159 glp_set_col_name(prob, X_morefrag_i_j, buf);
162 /* x_total_i += x_morefrag_i_j */
163 /* x_total_j += x_morefrag_i_j */
164 int ME_totals_i__mf_i_j = ADD_MATRIX_ENTRY(Y_totals_i+i, X_morefrag_i_j);
165 int ME_totals_j__mf_i_j = ADD_MATRIX_ENTRY(Y_totals_j+j, X_morefrag_i_j);
166 matrix_entries[ME_totals_i__mf_i_j] = 1;
167 matrix_entries[ME_totals_j__mf_i_j] = 1;
171 assert(next_matrix_entry == matrix_entries_size);
173 glp_load_matrix(prob, matrix_entries_size-1,
174 matrix_entries_XY[1], matrix_entries_XY[0],
177 int r = glp_simplex(prob, NULL);
178 PRINTF(" glp=%d", r);
181 case e: PRINTF(" " #e ); goto out;
183 case e: HAVE_PRINTED; printf(" " #e " CRASHING\n"); exit(-1);
185 default: HAVE_PRINTED; printf(" ! CRASHING\n"); exit(-1);
205 r = glp_get_status(prob);
206 PRINTF(" status=%d", r);
218 double got = glp_get_obj_val(prob);
227 if (best_prob) glp_delete_prob(best_prob);
230 free(best_adjmatrix);
231 best_adjmatrix = xalloc_adjmatrix();
232 memcpy(best_adjmatrix, adjmatrix, sizeof(*adjmatrix)*n);
240 glp_delete_prob(prob);
241 if (doprint) { printf(" \r"); fflush(stdout); }
244 static void iterate_recurse(int i, AdjWord min) {
247 optimise(!(printcounter & 0xfff));
250 for (adjmatrix[i] = min;
253 if (count_set_adj_bits(adjmatrix[i]) > maxhamweight)
256 iterate_recurse(i+1, adjmatrix[i]);
259 if (adjmatrix[i] == adjall)
264 static void iterate(void) {
265 for (maxhamweight=1; maxhamweight<=m; maxhamweight++) {
266 double maxminsize = (double)m / maxhamweight;
267 if (maxminsize <= best)
270 iterate_recurse(0, 1);
274 int main(int argc, char **argv) {
282 glp_print_sol(best_prob,"/dev/stdout");
283 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }