chiark / gitweb /
WIP routesearch; construct archipelago lookup table
[ypp-sc-tools.db-live.git] / yarrg / rssearch.c
index 88ea45132fcb5ae1c5448ceb3e1e7364b21dfef6..fa43d8939d26a250c61c3108c9b30194d79c058a 100644 (file)
@@ -2,6 +2,9 @@
 
 #include "rscommon.h"
 
+DEBUG_DEFINE_DEBUGF(search);
+DEBUG_DEFINE_SOME_DEBUGF(filter,fildebugf);
+
 typedef struct Neighbour {
   struct Neighbour *next;
   int islandid;
@@ -32,35 +35,95 @@ static Neighbour *get_neighbours(int isle) {
   return head;
 }
 
-static double best_absolute, best_perleague;
 
-static void process_route(int nports, int totaldist) {
-  double absolute= value_route(nports, ports);
-  double perleague= absolute / totaldist;
+static PotentialResult ***strat_base;
+
+
+static double process_route(int nports, int totaldist,
+                           double overestimate_excepting_tail) {
+  int i;
+  int leagues_divisor= totaldist + nports;
+
+  ctr_routes_considered++;
+
+  debugf("========== ROUTE");
+  for (i=0; i<nports; i++)
+    debugf(" %d",ports[i]);
+  debugf("\n");
+
+  int finalisle= ports[nports-1];
+  int midisle= ports[nports/2];
+
+  PotentialResult **strat_fin= ONDEMAND(strat_base[finalisle], islandtablesz);
+  PotentialResult *strat= ONDEMAND(strat_fin[midisle], 1);
+
+  if (nports>=2) {
+    int pair[2], i;
+    pair[1]= ports[nports-1];
+    double guess_absolute= overestimate_excepting_tail;
+    
+    for (i=0; i<nports; i++) {
+      pair[0]= ports[i];
+      IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
+      if (!ip) continue;
+      if (ip->route_tail_value < 0) {
+       ctr_subroute_tails_valued++;
+       ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
+      }
+      guess_absolute += ip->route_tail_value;
+    }
+    double guess_perleague= guess_absolute / leagues_divisor;
+
+    if (guess_absolute <= strat->absolute &&
+       guess_perleague <= strat->perleague) {
+      ctr_routes_eliminated++;
+      debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
+      return guess_absolute;
+    }
+    debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
+  }
+
+  ctr_routes_valued++;
+
+  double absolute= value_route(nports, ports, 0);
+  double perleague= absolute / leagues_divisor;
 
-  if (absolute < best_absolute && perleague < best_perleague) return;
+  if (absolute <= strat->absolute &&
+      perleague <= strat->perleague)
+    return absolute;
 
-#define CHK(absperl)                                   \
-  fprintf(stderr,#absperl " %15f", absperl);           \
-  if (absperl < best_##absperl) fputs("   ",stderr);   \
-  else { best_##absperl= absperl; fputs("** ",stderr); }
+  debugf(" SOMEHOW BEST\n");
+
+  fildebugf("final %3d mid %3d ",finalisle,midisle);
+
+#define CHK(absperl)                                                   \
+  fildebugf(#absperl " %15f", absperl);                                        \
+  if (absperl < strat->absperl) {                                      \
+    debugf("   ");                                                     \
+  } else {                                                             \
+    strat->absperl= absperl;                                           \
+    memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports);    \
+    fildebugf("** ");                                                  \
+  }
 
   CHK(absolute)
   CHK(perleague)
 
-  fputs(" route",stderr);
+  fildebugf(" route");
 
-  int i;
   for (i=0; i<nports; i++)
-    fprintf(stderr," %d",ports[i]);
-  putc('\n',stderr);
+    fildebugf(" %d",ports[i]);
+  fildebugf("\n");
+
+  return absolute;
 }
 
 static void recurse(int last_isle,
                    int nports, /* excluding last_isle */
-                   int totaldist /* including last_isle */) {
+                   int totaldist /* including last_isle */,
+                   double last_estimate) {
   ports[nports++]= last_isle;
-  process_route(nports, totaldist);
+  double estimate= process_route(nports, totaldist, last_estimate);
   if (nports >= MAX_ROUTELEN) return;
 
   Neighbour *add;
@@ -68,17 +131,52 @@ static void recurse(int last_isle,
     int newdist= totaldist + add->dist;
     if (newdist > max_dist) continue;
 
-    recurse(add->islandid, nports, newdist);
+    recurse(add->islandid, nports, newdist, estimate);
   }
 }
 
-void search(int start_isle) {
-  recurse(start_isle,0,0);
+void search(int start_isle, PotentialResult ****strat_base_io) {
+  strat_base= ONDEMAND(*strat_base_io, islandtablesz);
+  recurse(start_isle,0,0,1e6);
 }
 
+
+int narches;
+char **archnames;
+int *islandid2arch;
+
 void setup_search(void) {
   neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
 
   SQL_PREPARE(ss_neigh,
              "SELECT biid, dist FROM routes WHERE aiid=?");
+
+  int max_narches=
+    sql_single_int(" SELECT count(*) FROM (\n"
+                  "  SELECT DISTINCT archipelago\n"
+                  "   FROM islands\n"
+                  "  )");
+  archnames= mcalloc(sizeof(*archnames) * max_narches);
+  islandid2arch= mmalloc(sizeof(*islandid2arch) * islandtablesz);
+  int i;
+  for (i=0; i<islandtablesz; i++) islandid2arch[i]=-1;
+
+  sqlite3_stmt *archs;
+  SQL_PREPARE(archs,
+             " SELECT islandid, archipelago\n"
+             "  FROM islands\n"
+             "  ORDER BY archipelago");
+  while (SQL_STEP(archs)) {
+    int isle= sqlite3_column_int(archs,0);
+    const char *archname= (const char*)sqlite3_column_text(archs,1);
+    int arch;
+    for (arch=0; arch<narches; arch++)
+      if (!strcmp(archnames[arch], archname)) goto found;
+    assert(narches < max_narches);
+    arch= narches++;
+    archnames[arch]= masprintf("%s",archname);
+  found:
+    islandid2arch[isle]= arch;
+  }
+  sqlite3_finalize(archs);
 }