chiark / gitweb /
wip lp, problem setup compiles
[matchsticks-search.git] / main.c
1
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 #include <inttypes.h>
7
8 #include <publib.h>
9 #include <glpk.h>
10
11 typedef uint32_t AdjWord;
12 #define PRADJ "08"PRIx32
13
14 static int n, m;
15 static AdjWord *adjmatrix;
16 static AdjWord adjall;
17
18 static double best;
19
20 static void prep(void) {
21   adjall = ~((~(AdjWord)0) << m);
22   adjmatrix = xmalloc(sizeof(*adjmatrix)*n);
23 }
24
25 static AdjWord one_adj_bit(int bitnum) {
26   return (AdjWord)1 << bitnum;
27 }
28
29 static int count_set_adj_bits(AdjWord w) {
30   int j, total;
31   for (j=0, total=0; j<m; j++)
32     total += !!(w & one_adj_bit(j));
33   return total;
34 }
35
36 static void optimise(void) {
37   int i, j, totalfrags;
38
39   for (i=0, totalfrags=0; i<n; i++) {
40     int frags = count_set_adj_bits(adjmatrix[i]);
41     totalfrags += frags;
42     printf("%"PRADJ" ", adjmatrix[i]);
43     double maxminsize = (double)m / frags;
44     if (maxminsize < best) {
45       printf(" too fine\n");
46       return;
47     }
48   }
49
50   /*
51    * We formulate our problem as an LP problem as follows.
52    * In this file "n" and "m" are the matchstick numbers.
53    *
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.
56    *
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)
61    *
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)
65    *
66    * The objective function is simply
67    *   maximise x_minimum
68    *
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.
72    */
73
74   glp_prob *prob = glp_create_prob();
75
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);
79
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];
84
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++;                              \
91     })
92
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);
95
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);
98
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);
103
104   /* x_minimum >= 0 */
105   glp_set_col_bnds(prob, X_minimum, GLP_LO, 0, 0);
106
107   /* objective is maximising x_minimum */
108   glp_set_obj_dir(prob, GLP_MAX);
109   glp_set_obj_coef(prob, X_minimum, 1);
110
111   for (i=0; i<n; j++) {
112     for (j=0; j<m; j++) {
113       if (!(adjmatrix[i] & one_adj_bit(j)))
114         continue;
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 ] ++;
119
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);
123
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;
130     }
131   }
132
133   assert(next_matrix_entry == matrix_entries_size);
134
135   glp_load_matrix(prob, next_matrix_entry,
136                   matrix_entries_XY[1], matrix_entries_XY[0],
137                   matrix_entries);
138
139   printf("nyi\n");
140 }
141
142 static void iterate_recurse(int i, AdjWord min) {
143   if (i >= n) {
144     optimise();
145     return;
146   }
147   for (adjmatrix[i] = min;
148        ;
149        adjmatrix[i]++) {
150     iterate_recurse(i+1, adjmatrix[i]);
151     if (adjmatrix[i] == adjall)
152       return;
153   }
154 }
155
156 static void iterate(void) {
157   iterate_recurse(0, 1);
158 }
159
160 int main(int argc, char **argv) {
161   n = atoi(argv[1]);
162   m = atoi(argv[2]);
163   prep();
164   iterate();
165   if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }
166   return 0;
167 }