chiark / gitweb /
WIP routesearch; route high table
[ypp-sc-tools.db-test.git] / yarrg / rssearch.c
index 0a17adfd8d79f564afcd1d98549baa8be541f341..2c530fd70eeab4219fa4534726129458ff47d474 100644 (file)
@@ -23,7 +23,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;
@@ -51,16 +52,11 @@ static double process_route(int nports, int totaldist,
     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);
-
+  double guess_absolute=0, guess_perleague=0;
   if (nports>=2) {
     int pair[2], i;
     pair[1]= ports[nports-1];
-    double guess_absolute= overestimate_excepting_tail;
+    guess_absolute= overestimate_excepting_tail;
     
     for (i=0; i<nports; i++) {
       pair[0]= ports[i];
@@ -72,11 +68,26 @@ static double process_route(int nports, int totaldist,
       }
       guess_absolute += ip->route_tail_value;
     }
-    double guess_perleague= guess_absolute / leagues_divisor;
+    guess_perleague= guess_absolute / leagues_divisor;
+
+    if (guess_absolute <= highscores_absolute[0].value &&
+       guess_perleague <= highscores_perleague[0].value) {
+      ctr_routes_quickelim++;
+      debugf(" QELIM %f %f\n", guess_absolute, guess_perleague);
+      return guess_absolute;
+    }
+  }
+
+  int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
+  int midisle= ports[nports/2]; int midarch= isle2arch(midisle);
 
+  PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
+  PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
+
+  if (nports>=2) {
     if (guess_absolute <= strat->absolute &&
        guess_perleague <= strat->perleague) {
-      ctr_routes_eliminated++;
+      ctr_routes_stratelim++;
       debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
       return guess_absolute;
     }
@@ -94,16 +105,37 @@ static double process_route(int nports, int totaldist,
 
   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("** ");                                                  \
+  fildebugf("final %d:%3d mid %d:%3d ",finarch,finisle,midarch,midisle);
+
+#define CHK(absperl)                                                         \
+  fildebugf(#absperl " %15f", absperl);                                              \
+  if (absperl < strat->absperl) {                                            \
+    debugf("      ");                                                        \
+  } else {                                                                   \
+    int pos;                                                                 \
+    ctr_newbests_strat_##absperl++;                                          \
+    strat->absperl= absperl;                                                 \
+    memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports);          \
+    if (nports < MAX_ROUTELEN-1) strat->absperl##_ports[nports]= -1;         \
+    fildebugf("** ");                                                        \
+    for (pos=0; pos < nhighscores_##absperl; pos++)                          \
+      if (highscores_##absperl[pos].pr == strat) goto found_##absperl;       \
+    /* not found */                                                          \
+    pos= -1;                                                                 \
+   found_##absperl:                                                          \
+    for (;;) {                                                               \
+      pos++;                                                                 \
+      if (pos >= nhighscores_##absperl-1) break; /* new top */               \
+      if (highscores_##absperl[pos].value >= absperl) break; /* found spot */ \
+      if (pos>0)                                                             \
+       highscores_##absperl[pos-1]= highscores_##absperl[pos];               \
+    }                                                                        \
+    pos--;                                                                   \
+    if (pos>0) {                                                             \
+      highscores_##absperl[pos].value= absperl;                                      \
+      highscores_##absperl[pos].pr= strat;                                   \
+    }                                                                        \
+    fildebugf("@%2d", pos);                                                  \
   }
 
   CHK(absolute)
@@ -136,13 +168,48 @@ static void recurse(int last_isle,
 }
 
 void search(int start_isle, PotentialResult ****strat_base_io) {
-  strat_base= ONDEMAND(*strat_base_io, islandtablesz);
+  strat_base= ONDEMAND(*strat_base_io, narches);
   recurse(start_isle,0,0,1e6);
 }
 
+int nhighscores_absolute, nhighscores_perleague;
+HighScoreEntry *highscores_absolute;
+HighScoreEntry *highscores_perleague;
+
+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);
 }