9 typedef uint32_t AdjWord;
10 #define PRADJ "08"PRIx32
13 static AdjWord *adjmatrix;
14 static AdjWord adjall;
18 static void prep(void) {
19 adjall = ~((~(AdjWord)0) << m);
20 adjmatrix = xmalloc(sizeof(*adjmatrix)*n);
23 static AdjWord one_adj_bit(int bitnum) {
24 return (AdjWord)1 << j;
27 static int count_set_adj_bits(AdjWord w) {
29 for (j=0, total=0; j<m; j++)
30 total += !!(w & one_adj_bit(j));
34 static void optimise(void) {
36 for (i=0, totalfrags=0; i<n; i++) {
37 int frags = count_set_adj_bits(adjmatrix[i]);
39 printf("%"PRADJ" ", adjmatrix[i]);
40 double maxminsize = (double)m / frags;
41 if (maxminsize < best) {
42 printf(" too fine\n");
48 * We formulate our problem as an LP problem as follows.
49 * In this file "n" and "m" are the matchstick numbers.
51 * Each set bit in the adjacency matrix corresponds to taking a
52 * fragment from old match i and making it part of new match j.
54 * The structural variables (columns) are:
55 * x_minimum minimum size of any fragment (bounded below by 0)
56 * x_morefrag_i_j the amount by which the size of the fragment
57 * i,j exceeds the minimum size (bounded below by 0)
59 * The auxiliary variables (rows) are:
60 * x_total_i total length for each input match (fixed variable)
61 * x_total_j total length for each output match (fixed variable)
63 * The objective function is simply
66 * We use X_ and Y_ to refer to GLPK's (1-based) column and row indices.
67 * ME_ refers to entries in the list of constraint matrix elements
68 * which we build up as we go.
71 glp_prob *prob = glp_create_prob();
73 int Y_totals_i = glp_add_rows(prob, i);
74 int Y_totals_j = glp_add_rows(prob, j);
75 int X_minimum = glp_add_cols(prob, 1);
76 int rows = glp_get_num_rows(prob);
77 int cols = glp_get_num_rows(cols);
79 int next_matrix_entry = 1; /* wtf GLPK! */
80 int matrix_entries_size = next_matrix_entry + i + j + totalfrags*2;
81 double matrix_entries[matrix_entries_size];
82 int matrix_entries_XY[2][matrix_entries_size];
84 #define ADD_MATRIX_ENTRY(Y,X) ({ \
85 assert(matrix_entries_size < next_matrix_entry); \
86 matrix_entries_XY[0][next_matrix_entry] = X; \
87 matrix_entries_XY[1][next_matrix_entry] = Y; \
88 matrix_entries[next_matrix_entry] = 0; \
89 next_matrix_entry++; \
92 int ME_totals_i__minimum = next_matrix_entry;
93 for (i=0; i<n; i++) ADD_MATRIX_ENTRY(Y_totals_i+i, X_minimum);
95 int ME_totals_j__minimum = next_matrix_entry;
96 for (j=0; j<m; j++) ADD_MATRIX_ENTRY(Y_totals_j+j, X_minimum);
98 /* \forall_i x_totals_i = m */
99 /* \forall_i x_totals_j = n */
100 for (i=0; i<n; i++) glp_set_row_bounds(prob, Y_totals_i+i, GLP_FX, m,m);
101 for (j=0; j<m; j++) glp_set_row_bounds(prob, Y_totals_j+j, GLP_FX, n,n);
104 glp_set_col_bounds(prob, X_minimum, GLP_LB, 0, 0);
106 /* objective is maximising x_minimum */
107 glp_set_obj_dir(prob, GLP_MAX);
108 glp_set_obj_coef(prob, X_minimum, 1);
110 for (i=0; i<n; j++) {
111 for (j=0; j<m; j++) {
112 if (!(adjmatrix[i] & one_adj_bit(j)))
114 /* x_total_i += x_minimum */
115 /* x_total_j += x_minimum */
116 matrix_entries[ ME_totals_i__minimum + i ] ++;
117 matrix_entries[ ME_totals_j__minimum + j ] ++;
119 /* x_morefrag_i_j >= 0 */
120 int X_morefrag_i_j = glp_add_cols(prob, 1);
121 glp_set_col_bnds(prob, X_morefrag_i_j, GLP_LO, 0, 0);
123 /* x_total_i += x_morefrag_i_j */
124 /* x_total_j += x_morefrag_i_j */
125 int ME_totals_i__mf_i_j = ADD_MATRIX_ENTRY(Y_totals_i+i, X_morefrag_i_j);
126 int ME_totals_j__mf_i_j = ADD_MATRIX_ENTRY(Y_totals_j+j, X_morefrag_i_j);
127 matrix_entries[ME_totals_i__mf_i_j] = 1;
128 matrix_entries[ME_totals_j__mf_i_j] = 1;
132 assert(next_matrix_entry == matrix_entries_size);
134 for (row=1; row<=rows; row++) {
135 glp_load_matrix(prob, next_matrix_entry,
136 matrix_entries_XY[1], matrix_entries_XY[0],
142 static void iterate_recurse(int i, AdjWord min) {
147 for (adjmatrix[i] = min;
150 iterate_recurse(i+1, adjmatrix[i]);
151 if (adjmatrix[i] == adjall)
156 static void iterate(void) {
157 iterate_recurse(0, 1);
160 int main(int argc, char **argv) {
165 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }