chiark / gitweb /
fudge the {n,m}_max_frags to deal with rounding errors in glpk results
[matchsticks-search.git] / main.c
diff --git a/main.c b/main.c
index 57a979654f705bd01912fe773cd01668a92ddb07..f0bb84f497f12d91f9a1f4416643d80aee002c09 100644 (file)
--- a/main.c
+++ b/main.c
@@ -5,7 +5,9 @@
  *
  * Invoke as   ./main n m
  *
- * The algorithm is faster if the arguments are ordered so that n > m.
+ * 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
  */
 
 /*
  *
  * We further winnow the set of possible adjacency matrices, by
  * ensuring the same bit is not set in too many entries of adjmatrix
- * (ie, as above, only considering output sticks).
+ * (ie, as above, only considering output sticks); and by ensuring
+ * that it is not set in too few: each output stick must consist
+ * of at least two fragments since the output sticks are longer than
+ * the input ones.
  *
  * And, we want to do the search in order of increasing maximum
  * hamming weight.  This is because in practice optimal solutions tend
@@ -133,9 +138,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 -----*/
@@ -623,6 +632,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;
@@ -712,6 +725,7 @@ int main(int argc, char **argv) {
   assert(argc==3);
   n = atoi(argv[1]);
   m = atoi(argv[2]);
+  assert(n > m);
 
   prep();