chiark / gitweb /
apply floor to n_over_best
[matchsticks-search.git] / main.c
diff --git a/main.c b/main.c
index 38bd8541ca6d5c382aa6beb06cd5d088f18f6ed8..87cb8d70fe7eb0508567511492cd0dc063b4f67c 100644 (file)
--- a/main.c
+++ b/main.c
  * GNU General Public License for more details.
  */
 
+#define _GNU_SOURCE
+
+#include <publib.h>
+
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdbool.h>
 #include <inttypes.h>
+#include <math.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/uio.h>
 #include <sys/fcntl.h>
 
-#include <publib.h>
 #include <glpk.h>
 
 /*
@@ -91,10 +95,15 @@ static double best;
 static glp_prob *best_prob;
 static AdjWord *best_adjmatrix;
 
+static int n_over_best;
+static int *weight;
+
 static unsigned printcounter;
 
 static void iterate(void);
 static void iterate_recurse(int i, AdjWord min);
+static bool preconsider_ok(int nwords, bool doprint);
+static bool maxhamweight_ok(void);
 static void optimise(bool doprint);
 
 static void progress_eol(void) {
@@ -102,6 +111,11 @@ static void progress_eol(void) {
   fflush(stderr);
 }
 
+static void set_best(double new_best) {
+  best = new_best;
+  n_over_best = floor(n / best);
+}
+
 /*----- multicore support -----*/
 
 /*
@@ -142,6 +156,8 @@ typedef struct {
 } Worker;
 static Worker *mc_us;
 
+static void multicore_check_for_new_best(void);
+
 #define MAX_NIOVS 3
 static AdjWord mc_iter_min;
 static int mc_niovs;
@@ -195,6 +211,8 @@ static void mc_awaitpid(int wnum, pid_t pid) {
 }
 
 static void multicore_outer_iteration(int i, AdjWord min) {
+  static unsigned check_counter;
+
   assert(i == multicore_iteration_boundary);
   mc_iter_min = min;
   mc_rwvsetup_outer();
@@ -202,6 +220,9 @@ static void multicore_outer_iteration(int i, AdjWord min) {
   assert(r == mc_iovlen);
   /* effectively, this writev arranges to transfers control
    * to some worker's instance of iterate_recurse via mc_iterate_worker */
+
+  if (!(check_counter++ & 0xff))
+    multicore_check_for_new_best();
 }
 
 static void mc_iterate_worker(void) {
@@ -210,6 +231,13 @@ static void mc_iterate_worker(void) {
     ssize_t r = readv(mc_work[0], mc_iov, mc_niovs);
     if (r == 0) break;
     assert(r == mc_iovlen);
+    
+    bool ok = maxhamweight_ok();
+    if (!ok) continue;
+
+    ok = preconsider_ok(multicore_iteration_boundary, 1);
+    progress_eol();
+    if (!ok) continue;
 
     /* stop iterate_recurse from trying to run multicore_outer_iteration */
     int mc_org_it_bound = multicore_iteration_boundary;
@@ -290,7 +318,7 @@ static void multicore_check_for_new_best(void) {
     if (!got) break;
     assert(got == sizeof(msg));
     if (msg > best)
-      best = msg;
+      set_best(msg);
     mc_bus_read += sizeof(msg);
   }
 }
@@ -314,6 +342,8 @@ static void prep(void) {
   adjmatrix = xalloc_adjmatrix();
   glp_term_out(GLP_OFF);
   setlinebuf(stderr);
+  weight = calloc(sizeof(*weight), m);  assert(weight);
+  n_over_best = INT_MAX;
 }
 
 static AdjWord one_adj_bit(int bitnum) {
@@ -331,6 +361,11 @@ static int count_set_adj_bits(AdjWord w) {
 
 static int totalfrags;
 
+static bool maxhamweight_ok(void) {
+  double maxminsize = (double)m / maxhamweight;
+  return maxminsize > best;
+}
+
 static bool preconsider_ok(int nwords, bool doprint) {
   int i;
 
@@ -531,7 +566,7 @@ static void optimise(bool doprint) {
 
   HAVE_PRINTED;
 
-  best = got;
+  set_best(got);
   multicore_found_new_best();
 
   if (best_prob) glp_delete_prob(best_prob);
@@ -570,8 +605,20 @@ static void iterate_recurse(int i, AdjWord min) {
     if (i == 0 && (adjmatrix[i] & (1+adjmatrix[i])))
       goto again;
 
+    for (int j = 0; j < m; j++)
+      if (adjmatrix[i] & one_adj_bit(j))
+        weight[j]++;
+    for (int j = 0; j < m; j++)
+      if (weight[j] >= n_over_best)
+        goto takeout;
+
     iterate_recurse(i+1, adjmatrix[i]);
 
+  takeout:
+    for (int j = 0; j < m; j++)
+      if (adjmatrix[i] & one_adj_bit(j))
+        weight[j]--;
+
   again:
     if (adjmatrix[i] == adjall)
       return;
@@ -580,8 +627,7 @@ static void iterate_recurse(int i, AdjWord min) {
 
 static void iterate(void) {
   for (maxhamweight=1; maxhamweight<=m; maxhamweight++) {
-    double maxminsize = (double)m / maxhamweight;
-    if (maxminsize <= best)
+    if (!maxhamweight_ok())
       continue;
 
     iterate_recurse(0, 1);