chiark / gitweb /
split up stdout heading output line printing (no functional change)
[matchsticks-search.git] / main.c
diff --git a/main.c b/main.c
index 97b4bbbcc2071972bbc5ce38d7e1b09ef2bc6479..08bf5cb6ff524caf3fc84a62fc5e113bdc5d86f9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -8,6 +8,10 @@
  * The arguments must be ordered so that n > m:
  *   n is the number of (more, shorter) input matches of length m
  *   m is the number of (fewer, longer) output matches of length n
+ *
+ * Options:
+ *  -j<jobs>     run in parallel on <jobs> cores
+ *  -b<best>     search only for better than <best>
  */
 
 /*
@@ -138,9 +142,13 @@ static void set_best(double new_best) {
    *   <=>       frags <   |  n / best  |
    *                        _          _
    *   <=>       frags <=  |  n / best  | - 1
+   *
+   * But best values from glpk are slightly approximate, so we
+   * subtract a fudge factor from our target.
    */
-  n_max_frags = ceil(n / best) - 1;
-  m_max_frags = ceil(m / best) - 1;
+  double near_best = best * 0.98 - 0.02;
+  n_max_frags = ceil(n / near_best) - 1;
+  m_max_frags = ceil(m / near_best) - 1;
 }
 
 /*----- multicore support -----*/
@@ -458,13 +466,6 @@ static void optimise(bool doprint) {
   if (!ok)
     goto out;
 
-  for (j=0; j<m; j++)
-    if (weight[j] < 2) {
-      PRINTF(" nominham");
-      goto out;
-    }
-
-
   /*
    * We formulate our problem as an LP problem as follows.
    * In this file "n" and "m" are the matchstick numbers.
@@ -635,6 +636,10 @@ static void iterate_recurse(int i, AdjWord min) {
   AdjWord jbit;
 
   if (i >= n) {
+    for (j=0; j<m; j++)
+      if (weight[j] < 2)
+       return;
+
     printcounter++;
     optimise(!(printcounter & 0xfff));
     return;
@@ -682,6 +687,13 @@ static void iterate(void) {
 
 static void report(void) {
   fprintf(stderr, "\n");
+  if (best_adjmatrix) {
+    int i;
+    fprintf(stderr,"  ");
+    for (i=0; i<n; i++) fprintf(stderr, " %"PRADJ, best_adjmatrix[i]);
+    fprintf(stderr," OK\n");
+  }
+  printf("%d into %d: ", n, m);
   if (best_prob) {
     double min = glp_get_obj_val(best_prob);
     double a[n][m];
@@ -696,7 +708,7 @@ static void report(void) {
         continue;
       a[x][y] = min + glp_get_col_prim(best_prob, i);
     }
-    printf("%d into %d: min fragment %g   [%s]\n", n, m, min, VERSION);
+    printf("min fragment %g", min);
     for (i = 0; i < n; i++) {
       for (j = 0; j < m; j++) {
         if (a[i][j])
@@ -707,6 +719,7 @@ static void report(void) {
       printf("\n");
     }
   }
+  printf("   [%s]\n", VERSION);
   if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }
 }
  
@@ -715,6 +728,7 @@ int main(int argc, char **argv) {
   while ((opt = getopt(argc,argv,"j:")) >= 0) {
     switch (opt) {
     case 'j': ncpus = atoi(optarg); break;
+    case 'b': set_best(atof(optarg)); break;
     case '+': assert(!"bad option");
     default: abort();
     }