chiark / gitweb /
routesearch: change granu to be higher for more specific (more granularity)
[ypp-sc-tools.db-test.git] / yarrg / rssearch.c
index 6674c02..9676640 100644 (file)
@@ -15,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];
@@ -37,38 +38,29 @@ static Neighbour *get_neighbours(int isle) {
 }
 
 
-static PotentialResult ***strat_base;
+static PotentialResult ***buckets_base[GRANUS];
 
 
-static inline int isle2arch(int isle) {
-  int arch= islandid2arch[isle];
-  assert(arch>=0);
-  return arch;
-}
-
 static double process_route(int nports, int totaldist,
                            double overestimate_excepting_tail) {
-  int i;
+  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");
 
-  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);
-
+  double guess[AP]={0,0};
   if (nports>=2) {
     int pair[2], i;
     pair[1]= ports[nports-1];
-    double guess_absolute= overestimate_excepting_tail;
-    
+    guess[A]= overestimate_excepting_tail;
+
     for (i=0; i<nports; i++) {
       pair[0]= ports[i];
       IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
@@ -77,44 +69,112 @@ static double process_route(int nports, int totaldist,
        ctr_subroute_tails_valued++;
        ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
       }
-      guess_absolute += ip->route_tail_value;
+      guess[A] += ip->route_tail_value;
     }
-    double guess_perleague= guess_absolute / leagues_divisor;
+    guess[P]= guess[A] / 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;
+    if (wrong_final) {
+      ctr_routes_wrongfinalelim++;
+      debugf(" WFELIM\n");
+      return guess[A];
     }
-    debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
-  }
 
-  ctr_routes_valued++;
+    if (guess[A] <= highscores[GRANUS-1][A][0].value &&
+       guess[P] <= highscores[GRANUS-1][P][0].value) {
+      ctr_routes_quickelim++;
+      debugf(" QELIM %f %f\n", guess[A], guess[P]);
+      return guess[A];
+    }
+  }
 
-  double absolute= value_route(nports, ports, 0);
-  double perleague= absolute / leagues_divisor;
+  int finisle= ports[nports-1];
+  int finarch= isle2arch(finisle);
+
+  int midisle= ports[nports/2];
+  int midarch= route2midarch(ports,nports);
+
+  PotentialResult *buckets[GRANUS];
+  int granui;
+  for (granui=0; granui<granus; granui++) {
+    PotentialResult **buckets_fin;
+    int mid, fin;
+    switch (granui) {
+    case 0: fin=finarch; mid=midarch; break;
+    case 1: fin=finisle; mid=midarch; break;
+    case 2: fin=finisle; mid=midisle; break;
+    default: abort();
+    }
+    buckets_fin= ONDEMAND(buckets_base[granui][fin], granusz_mid[granui]);
+    buckets[granui]= ONDEMAND(buckets_fin[mid], 1);
+  }
 
-  if (absolute <= strat->absolute &&
-      perleague <= strat->perleague)
-    return absolute;
+  if (nports>=2) {
+    if (guess[A] <= buckets[0]->value[A] &&
+       guess[P] <= buckets[0]->value[P]) {
+      ctr_routes_bucketelim++;
+      debugf(" ELIM %f %f\n", guess[A], guess[P]);
+      return guess[A];
+    }
+    debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
+  }
 
-  debugf(" SOMEHOW BEST\n");
+  ctr_routes_valued++;
 
-  fildebugf("final %d:%3d mid %d:%3d ",finarch,finisle,midarch,midisle);
+  double value[AP];
+  value[A]= value_route(nports, ports, 0);
+  value[P]= value[A] / leagues_divisor;
 
-#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("** ");                                                  \
+  if (wrong_final) {
+    ctr_routes_wrongfinal++;
+    return value[0];
   }
 
-  CHK(absolute)
-  CHK(perleague)
+  for (granui=granus-1; granui>=0; granui--) {
+    PotentialResult *bucket= buckets[granui];
+
+    if (value[A] <= bucket->value[A] &&
+       value[P] <= bucket->value[P])
+      continue;
+
+    debugf(" SOMEHOW %d BEST\n",granui);
+
+    fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
+
+    for (ap=0; ap<AP; ap++) {
+      HighScoreEntry *scores= highscores[granui][ap];
+      int *nscores= &nhighscores[granui][ap];
+
+      fildebugf("ap=%d %15f", ap, value[ap]);
+      if (value[ap] < bucket->value[ap]) {
+       debugf("      ");
+      } else {
+       int pos;
+       ctr_newbests_granu[granui*AP+ap]++;
+       bucket->value[ap]= value[ap];
+       memcpy(bucket->ports[ap], ports, sizeof(*ports) * nports);
+       if (nports < MAX_ROUTELEN-1) bucket->ports[ap][nports]= -1;
+       fildebugf("** ");
+       for (pos=0; pos < *nscores; pos++)
+         if (scores[pos].pr == bucket) goto found;
+       /* not found */
+       pos= -1;
+      found:
+       for (;;) {
+         pos++;
+         if (pos >= *nscores-1) break; /* new top */
+         if (scores[pos].value >= value[ap]) break; /* found spot */
+         if (pos>0)
+           scores[pos-1]= scores[pos];
+       }
+       pos--;
+       if (pos>0) {
+         scores[pos].value= value[ap];
+         scores[pos].pr= bucket;
+       }
+       fildebugf("@%2d", pos);
+      }
+    }
+  }
 
   fildebugf(" route");
 
@@ -122,11 +182,11 @@ static double process_route(int nports, int totaldist,
     fildebugf(" %d",ports[i]);
   fildebugf("\n");
 
-  return absolute;
+  return value[0];
 }
 
 static void recurse(int last_isle,
-                   int nports, /* excluding last_isle */
+                   int nports /* excluding last_isle */,
                    int totaldist /* including last_isle */,
                    double last_estimate) {
   ports[nports++]= last_isle;
@@ -142,11 +202,20 @@ static void recurse(int last_isle,
   }
 }
 
-void search(int start_isle, PotentialResult ****strat_base_io) {
-  strat_base= ONDEMAND(*strat_base_io, narches);
+void search(int start_isle, int final_isle_spec,
+           PotentialResult ****buckets_base_io[GRANUS]) {
+  int granui;
+  for (granui=0; granui<GRANUS; granui++)
+    buckets_base[granui]=
+      ONDEMAND(*buckets_base_io[granui], granusz_fin[granui]);
+
+  final_isle= final_isle_spec <= 0 ? 0 : final_isle_spec;
   recurse(start_isle,0,0,1e6);
 }
 
+int nhighscores[GRANUS][AP];
+HighScoreEntry *highscores[GRANUS][AP];
+int granus=GRANUS, granusz_fin[GRANUS], granusz_mid[GRANUS];
 
 int narches;
 char **archnames;
@@ -184,4 +253,8 @@ void setup_search(void) {
     islandid2arch[isle]= arch;
   }
   sqlite3_finalize(archs);
+
+  granusz_fin[0]=                granusz_mid[0]= narches;
+  granusz_fin[1]= islandtablesz; granusz_mid[1]= narches;
+  granusz_fin[2]=                granusz_mid[2]= islandtablesz;
 }