chiark / gitweb /
Add -std=gnu99 to Makefile
[matchsticks-search.git] / main.c
diff --git a/main.c b/main.c
index 43cfc3e1181379e899034933ada16b37b9ab120a..da9e5fa089503f9c3dfdbb44507316824f113fbd 100644 (file)
--- a/main.c
+++ b/main.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <unistd.h>
 #include <stdbool.h>
 #include <inttypes.h>
 
@@ -88,6 +89,8 @@ static AdjWord *best_adjmatrix;
 
 static unsigned printcounter;
 
+static int ncpus = 1;
+
 static AdjWord *xalloc_adjmatrix(void) {
   return xmalloc(sizeof(*adjmatrix)*n);
 }
@@ -130,7 +133,7 @@ static void optimise(int doprint) {
     prob = 0;
   }
 
-#define PRINTF if (!doprint) ; else printf /* bodgy */
+#define PRINTF(...) if (!doprint) ; else fprintf(stderr, __VA_ARGS__) /* bodgy */
 
   PRINTF("%2d ", maxhamweight);
 
@@ -308,14 +311,14 @@ static void optimise(int doprint) {
   best_adjmatrix = xalloc_adjmatrix();
   memcpy(best_adjmatrix, adjmatrix, sizeof(*adjmatrix)*n);
 
-  printf(" BEST        \n");
+  PRINTF(" BEST        \n");
   return;
 
   }
  out:
   if (prob)
     glp_delete_prob(prob);
-  if (doprint) { printf("        \r"); fflush(stdout); }
+  if (doprint) { PRINTF("        \r"); fflush(stdout); }
 }
 
 static void iterate_recurse(int i, AdjWord min) {
@@ -329,6 +332,8 @@ static void iterate_recurse(int i, AdjWord min) {
        adjmatrix[i]++) {
     if (count_set_adj_bits(adjmatrix[i]) > maxhamweight)
       goto again;
+    if (i == 0 && (adjmatrix[i] & (1+adjmatrix[i])))
+      goto again;
 
     iterate_recurse(i+1, adjmatrix[i]);
 
@@ -348,15 +353,53 @@ static void iterate(void) {
   }
 }
 
+static void report(void) {
+  fprintf(stderr, "\n");
+  if (best_prob) {
+    double min = glp_get_obj_val(best_prob);
+    double a[n][m];
+    int i, j, cols;
+    for (i = 0; i < n; i++)
+      for (j = 0; j < m; j++)
+        a[i][j] = 0;
+    cols = glp_get_num_cols(best_prob);
+    for (i = 1; i <= cols; i++) {
+      int x, y;
+      if (2 != sscanf(glp_get_col_name(best_prob, i), "mf %d,%d", &x, &y))
+        continue;
+      a[x][y] = min + glp_get_col_prim(best_prob, i);
+    }
+    printf("%d into %d: min fragment %g\n", n, m, min);
+    for (i = 0; i < n; i++) {
+      for (j = 0; j < m; j++) {
+        if (a[i][j])
+          printf(" %9.3f", a[i][j]);
+        else
+          printf("          ");
+      }
+      printf("\n");
+    }
+  }
+  if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }
+}
 int main(int argc, char **argv) {
+  int opt;
+  while ((opt = getopt(argc,argv,"j:")) >= 0) {
+    switch (opt) {
+    case 'j': ncpus = atoi(optarg); break;
+    case '+': assert(!"bad option");
+    default: abort();
+    }
+  }
+  argc -= optind-1;
+  argv += optind-1;
   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); }
+  report();
   return 0;
 }