11 typedef uint32_t AdjWord;
12 #define PRADJ "08"PRIx32
15 static AdjWord *adjmatrix;
16 static AdjWord adjall;
20 static void prep(void) {
21 adjall = ~((~(AdjWord)0) << m);
22 adjmatrix = xmalloc(sizeof(*adjmatrix)*n);
25 static AdjWord one_adj_bit(int bitnum) {
26 return (AdjWord)1 << bitnum;
29 static int count_set_adj_bits(AdjWord w) {
31 for (j=0, total=0; j<m; j++)
32 total += !!(w & one_adj_bit(j));
36 static void optimise(void) {
39 for (i=0, totalfrags=0; i<n; i++) {
40 int frags = count_set_adj_bits(adjmatrix[i]);
42 printf("%"PRADJ" ", adjmatrix[i]);
43 double maxminsize = (double)m / frags;
44 if (maxminsize < best) {
45 printf(" too fine\n");
51 * We formulate our problem as an LP problem as follows.
52 * In this file "n" and "m" are the matchstick numbers.
54 * Each set bit in the adjacency matrix corresponds to taking a
55 * fragment from old match i and making it part of new match j.
57 * The structural variables (columns) are:
58 * x_minimum minimum size of any fragment (bounded below by 0)
59 * x_morefrag_i_j the amount by which the size of the fragment
60 * i,j exceeds the minimum size (bounded below by 0)
62 * The auxiliary variables (rows) are:
63 * x_total_i total length for each input match (fixed variable)
64 * x_total_j total length for each output match (fixed variable)
66 * The objective function is simply
69 * We use X_ and Y_ to refer to GLPK's (1-based) column and row indices.
70 * ME_ refers to entries in the list of constraint matrix elements
71 * which we build up as we go.
74 glp_prob *prob = glp_create_prob();
76 int Y_totals_i = glp_add_rows(prob, n);
77 int Y_totals_j = glp_add_rows(prob, m);
78 int X_minimum = glp_add_cols(prob, 1);
80 int next_matrix_entry = 1; /* wtf GLPK! */
81 int matrix_entries_size = next_matrix_entry + n + m + totalfrags*2;
82 double matrix_entries[matrix_entries_size];
83 int matrix_entries_XY[2][matrix_entries_size];
85 #define ADD_MATRIX_ENTRY(Y,X) ({ \
86 assert(matrix_entries_size < next_matrix_entry); \
87 matrix_entries_XY[0][next_matrix_entry] = X; \
88 matrix_entries_XY[1][next_matrix_entry] = Y; \
89 matrix_entries[next_matrix_entry] = 0; \
90 next_matrix_entry++; \
93 int ME_totals_i__minimum = next_matrix_entry;
94 for (i=0; i<n; i++) ADD_MATRIX_ENTRY(Y_totals_i+i, X_minimum);
96 int ME_totals_j__minimum = next_matrix_entry;
97 for (j=0; j<m; j++) ADD_MATRIX_ENTRY(Y_totals_j+j, X_minimum);
99 /* \forall_i x_totals_i = m */
100 /* \forall_i x_totals_j = n */
101 for (i=0; i<n; i++) glp_set_row_bnds(prob, Y_totals_i+i, GLP_FX, m,m);
102 for (j=0; j<m; j++) glp_set_row_bnds(prob, Y_totals_j+j, GLP_FX, n,n);
105 glp_set_col_bnds(prob, X_minimum, GLP_LO, 0, 0);
107 /* objective is maximising x_minimum */
108 glp_set_obj_dir(prob, GLP_MAX);
109 glp_set_obj_coef(prob, X_minimum, 1);
111 for (i=0; i<n; j++) {
112 for (j=0; j<m; j++) {
113 if (!(adjmatrix[i] & one_adj_bit(j)))
115 /* x_total_i += x_minimum */
116 /* x_total_j += x_minimum */
117 matrix_entries[ ME_totals_i__minimum + i ] ++;
118 matrix_entries[ ME_totals_j__minimum + j ] ++;
120 /* x_morefrag_i_j >= 0 */
121 int X_morefrag_i_j = glp_add_cols(prob, 1);
122 glp_set_col_bnds(prob, X_morefrag_i_j, GLP_LO, 0, 0);
124 /* x_total_i += x_morefrag_i_j */
125 /* x_total_j += x_morefrag_i_j */
126 int ME_totals_i__mf_i_j = ADD_MATRIX_ENTRY(Y_totals_i+i, X_morefrag_i_j);
127 int ME_totals_j__mf_i_j = ADD_MATRIX_ENTRY(Y_totals_j+j, X_morefrag_i_j);
128 matrix_entries[ME_totals_i__mf_i_j] = 1;
129 matrix_entries[ME_totals_j__mf_i_j] = 1;
133 assert(next_matrix_entry == matrix_entries_size);
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); }