chiark / gitweb /
do not leak prob when retrying
[matchsticks-search.git] / main.c
diff --git a/main.c b/main.c
index 006c071ed77920b962248aab889df35ea4197128..b9724608c9a175849e6b06dc7df0caa344318100 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <stdbool.h>
 #include <inttypes.h>
 
 #include <publib.h>
@@ -12,7 +13,7 @@
 typedef uint32_t AdjWord;
 #define PRADJ "08"PRIx32
 
-static int n, m;
+static int n, m, maxhamweight;
 static AdjWord *adjmatrix;
 static AdjWord adjall;
 
@@ -51,10 +52,19 @@ static void optimise(int doprint) {
       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;
     PRINTF("%"PRADJ" ", adjmatrix[i]);
     double maxminsize = (double)m / frags;
@@ -63,6 +73,10 @@ static void optimise(int doprint) {
       goto out;
     }
   }
+  if (!had_max) {
+    PRINTF(" nomaxham");
+    goto out;
+  }
 
   /*
    * We formulate our problem as an LP problem as follows.
@@ -121,6 +135,7 @@ static void optimise(int doprint) {
 
   /* 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);
@@ -138,6 +153,11 @@ static void optimise(int doprint) {
       /* 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 */
@@ -155,14 +175,14 @@ static void optimise(int doprint) {
                  matrix_entries);
 
   int r = glp_simplex(prob, NULL);
-  PRINTF(" simplex=%d", r);
+  PRINTF(" glp=%d", r);
 
 #define OKERR(e) \
   case e: PRINTF(" " #e ); goto out;
 #define BADERR(e) \
-  case e: HAVE_PRINTED; printf(" " #e " CRASHING"); exit(-1);
+  case e: HAVE_PRINTED; printf(" " #e " CRASHING\n"); exit(-1);
 #define DEFAULT \
-  default: HAVE_PRINTED; printf(" ! CRASHING"); exit(-1);
+  default: HAVE_PRINTED; printf(" ! CRASHING\n"); exit(-1);
 
   switch (r) {
   OKERR(GLP_ESING);
@@ -176,6 +196,8 @@ static void optimise(int doprint) {
   BADERR(GLP_EOBJUL);
   BADERR(GLP_EITLIM);
   BADERR(GLP_ETMLIM);
+  BADERR(GLP_EINSTAB);
+  BADERR(GLP_ENOCVG);
   case 0: break;
   DEFAULT;
   }
@@ -228,14 +250,25 @@ static void iterate_recurse(int i, AdjWord min) {
   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) {
@@ -244,6 +277,8 @@ int main(int argc, char **argv) {
   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;
 }