chiark / gitweb /
routesearch: make absolute/perleague into 2-element arrays rather than macros
[ypp-sc-tools.db-live.git] / yarrg / rssearch.c
index f14b0cd7a87670e6785d6a34bcc2d0adca4e3a22..646c796196d69a364034eed7f302a216fe31d040 100644 (file)
@@ -3,6 +3,7 @@
 #include "rscommon.h"
 
 DEBUG_DEFINE_DEBUGF(search);
+DEBUG_DEFINE_SOME_DEBUGF(filter,fildebugf);
 
 typedef struct Neighbour {
   struct Neighbour *next;
@@ -14,6 +15,7 @@ static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
 static sqlite3_stmt *ss_neigh;
 
 static int ports[MAX_ROUTELEN];
+static int final_isle;
 
 static Neighbour *get_neighbours(int isle) {
   Neighbour **np= &neighbours[isle];
@@ -22,7 +24,8 @@ static Neighbour *get_neighbours(int isle) {
 
   SQL_BIND(ss_neigh, 1, isle);
   while (SQL_STEP(ss_neigh)) {
-    Neighbour *add= mmalloc(sizeof(*add));
+    Neighbour *add;
+    NEW(add);
     add->islandid= sqlite3_column_int(ss_neigh, 0);
     add->dist= sqlite3_column_int(ss_neigh, 1);
     add->next= head;
@@ -34,41 +37,138 @@ static Neighbour *get_neighbours(int isle) {
   return head;
 }
 
-static double best_absolute, best_perleague;
 
-static void process_route(int nports, int totaldist) {
-  int i;
+static PotentialResult ***strat_base;
+
+
+static double process_route(int nports, int totaldist,
+                           double overestimate_excepting_tail) {
+  int i, ap;
+  int leagues_divisor= totaldist + nports;
+
+  ctr_routes_considered++;
+
+  int wrong_final= final_isle && ports[nports-1] != final_isle;
 
   debugf("========== ROUTE");
   for (i=0; i<nports; i++)
     debugf(" %d",ports[i]);
   debugf("\n");
 
-  double absolute= value_route(nports, ports);
-  double perleague= absolute / (totaldist + nports);
+  double guess[AP]={0,0};
+  if (nports>=2) {
+    int pair[2], i;
+    pair[1]= ports[nports-1];
+    guess[A]= 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[A] += ip->route_tail_value;
+    }
+    guess[P]= guess[A] / leagues_divisor;
+
+    if (wrong_final) {
+      ctr_routes_wrongfinalelim++;
+      debugf(" WFELIM\n");
+      return guess[A];
+    }
+
+    if (guess[A] <= highscores[A][0].value &&
+       guess[P] <= highscores[P][0].value) {
+      ctr_routes_quickelim++;
+      debugf(" QELIM %f %f\n", guess[A], guess[P]);
+      return guess[A];
+    }
+  }
 
-  if (absolute < best_absolute && perleague < best_perleague) return;
+  int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
+  int midarch= route2midarch(ports,nports);
 
-#define CHK(absperl)                                   \
-  fprintf(stderr,#absperl " %15f", absperl);           \
-  if (absperl < best_##absperl) fputs("   ",stderr);   \
-  else { best_##absperl= absperl; fputs("** ",stderr); }
+  PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
+  PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
 
-  CHK(absolute)
-  CHK(perleague)
+  if (nports>=2) {
+    if (guess[A] <= strat->value[A] &&
+       guess[P] <= strat->value[P]) {
+      ctr_routes_stratelim++;
+      debugf(" ELIM %f %f\n", guess[A], guess[P]);
+      return guess[A];
+    }
+    debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
+  }
+
+  ctr_routes_valued++;
 
-  fputs(" route",stderr);
+  double value[AP];
+  value[A]= value_route(nports, ports, 0);
+  value[P]= value[A] / leagues_divisor;
+
+  if (wrong_final) {
+    ctr_routes_wrongfinal++;
+    return value[0];
+  }
+
+  if (value[A] <= strat->value[A] &&
+      value[P] <= strat->value[P])
+    return value[A];
+
+  debugf(" SOMEHOW BEST\n");
+
+  fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
+
+  for (ap=0; ap<AP; ap++) {
+    fildebugf("ap=%d %15f", ap, value[ap]);
+    if (value[ap] < strat->value[ap]) {
+      debugf("      ");
+    } else {
+      int pos;
+      ctr_newbests_strat[ap]++;
+      strat->value[ap]= value[ap];
+      memcpy(strat->ports[ap], ports, sizeof(*ports) * nports);
+      if (nports < MAX_ROUTELEN-1) strat->ports[ap][nports]= -1;
+      fildebugf("** ");
+      for (pos=0; pos < nhighscores[ap]; pos++)
+       if (highscores[ap][pos].pr == strat) goto found;
+      /* not found */
+      pos= -1;
+    found:
+      for (;;) {
+       pos++;
+       if (pos >= nhighscores[ap]-1) break; /* new top */
+       if (highscores[ap][pos].value >= value[ap]) break; /* found spot */
+       if (pos>0)
+         highscores[ap][pos-1]= highscores[ap][pos];
+      }
+      pos--;
+      if (pos>0) {
+       highscores[ap][pos].value= value[ap];
+       highscores[ap][pos].pr= strat;
+      }
+      fildebugf("@%2d", pos);
+    }
+  }
+
+  fildebugf(" route");
 
   for (i=0; i<nports; i++)
-    fprintf(stderr," %d",ports[i]);
-  putc('\n',stderr);
+    fildebugf(" %d",ports[i]);
+  fildebugf("\n");
+
+  return value[0];
 }
 
 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;
@@ -76,17 +176,54 @@ 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, int final_isle_spec,
+           PotentialResult ****strat_base_io) {
+  strat_base= ONDEMAND(*strat_base_io, narches);
+  final_isle= final_isle_spec <= 0 ? 0 : final_isle_spec;
+  recurse(start_isle,0,0,1e6);
 }
 
+int nhighscores[AP];
+HighScoreEntry *highscores[AP];
+
+int narches;
+char **archnames;
+int *islandid2arch;
+
 void setup_search(void) {
-  neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
+  MCALLOC(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"
+                  "  )");
+  MCALLOC(archnames, max_narches);
+  MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-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);
 }