#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#include <stdbool.h>
#include <inttypes.h>
#include <publib.h>
typedef uint32_t AdjWord;
#define PRADJ "08"PRIx32
-static int n, m;
+static int n, m, maxhamweight;
static AdjWord *adjmatrix;
static AdjWord adjall;
glp_prob *prob = 0;
int i, j, totalfrags;
+#define HAVE_PRINTED ({ \
+ if (!doprint) { doprint = 1; goto retry_with_print; } \
+ })
+ retry_with_print:
+ if (prob) {
+ glp_delete_prob(prob);
+ prob = 0;
+ }
+
+#define PRINTF if (!doprint) ; else printf /* bodgy */
+
+ PRINTF("%2d ", maxhamweight);
+
+ bool had_max = 0;
for (i=0, totalfrags=0; i<n; i++) {
int frags = count_set_adj_bits(adjmatrix[i]);
+ had_max += (frags == maxhamweight);
totalfrags += frags;
- if (doprint) printf("%"PRADJ" ", adjmatrix[i]);
+ PRINTF("%"PRADJ" ", adjmatrix[i]);
double maxminsize = (double)m / frags;
- if (maxminsize < best) {
- if (doprint) printf(" too fine");
+ if (maxminsize <= best) {
+ PRINTF(" too fine");
goto out;
}
}
+ if (!had_max) {
+ PRINTF(" nomaxham");
+ goto out;
+ }
/*
* We formulate our problem as an LP problem as follows.
/* x_minimum >= 0 */
glp_set_col_bnds(prob, X_minimum, GLP_LO, 0, 0);
+ glp_set_col_name(prob, X_minimum, "minimum");
/* objective is maximising x_minimum */
glp_set_obj_dir(prob, GLP_MAX);
/* x_morefrag_i_j >= 0 */
int X_morefrag_i_j = glp_add_cols(prob, 1);
glp_set_col_bnds(prob, X_morefrag_i_j, GLP_LO, 0, 0);
+ if (doprint) {
+ char buf[255];
+ snprintf(buf,sizeof(buf),"mf %d,%d",i,j);
+ glp_set_col_name(prob, X_morefrag_i_j, buf);
+ }
/* x_total_i += x_morefrag_i_j */
/* x_total_j += x_morefrag_i_j */
matrix_entries);
int r = glp_simplex(prob, NULL);
- if (doprint) printf(" simplex=%d", r);
+ PRINTF(" glp=%d", r);
#define OKERR(e) \
- case e: if (doprint) printf(" " #e ); goto out;
+ case e: PRINTF(" " #e ); goto out;
#define BADERR(e) \
- case e: if (doprint) printf(" " #e " CRASHING"); exit(-1);
+ case e: HAVE_PRINTED; printf(" " #e " CRASHING\n"); exit(-1);
#define DEFAULT \
- default: if (doprint) printf(" ! CRASHING"); exit(-1);
+ default: HAVE_PRINTED; printf(" ! CRASHING\n"); exit(-1);
switch (r) {
OKERR(GLP_ESING);
BADERR(GLP_EOBJUL);
BADERR(GLP_EITLIM);
BADERR(GLP_ETMLIM);
+ BADERR(GLP_EINSTAB);
+ BADERR(GLP_ENOCVG);
case 0: break;
DEFAULT;
}
r = glp_get_status(prob);
- if (doprint) printf(" status=%d", r);
+ PRINTF(" status=%d", r);
switch (r) {
OKERR(GLP_NOFEAS);
}
double got = glp_get_obj_val(prob);
- if (doprint) printf(" %g", got);
+ PRINTF(" %g", got);
if (got <= best)
goto out;
- if (!doprint) {
- optimise(1);
- goto out;
- }
+ HAVE_PRINTED;
best = got;
for (adjmatrix[i] = min;
;
adjmatrix[i]++) {
+ if (count_set_adj_bits(adjmatrix[i]) > maxhamweight)
+ goto again;
+
iterate_recurse(i+1, adjmatrix[i]);
+
+ again:
if (adjmatrix[i] == adjall)
return;
}
}
static void iterate(void) {
- iterate_recurse(0, 1);
+ for (maxhamweight=1; maxhamweight<=m; maxhamweight++) {
+ double maxminsize = (double)m / maxhamweight;
+ if (maxminsize <= best)
+ continue;
+
+ iterate_recurse(0, 1);
+ }
}
int main(int argc, char **argv) {
+ assert(argc==3);
n = atoi(argv[1]);
m = atoi(argv[2]);
prep();
iterate();
printf("\n");
+ if (best_prob)
+ glp_print_sol(best_prob,"/dev/stdout");
if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }
return 0;
}