chiark / gitweb /
routesearch: three different bucket sizes for better result sets
[ypp-sc-tools.main.git] / yarrg / rssearch.c
index 646c796196d69a364034eed7f302a216fe31d040..bb84785eef12f3c6ac11819c00511246623482c9 100644 (file)
@@ -38,7 +38,7 @@ static Neighbour *get_neighbours(int isle) {
 }
 
 
-static PotentialResult ***strat_base;
+static PotentialResult ***strat_base[STRATS];
 
 
 static double process_route(int nports, int totaldist,
@@ -79,23 +79,38 @@ static double process_route(int nports, int totaldist,
       return guess[A];
     }
 
-    if (guess[A] <= highscores[A][0].value &&
-       guess[P] <= highscores[P][0].value) {
+    if (guess[A] <= highscores[minstrat][A][0].value &&
+       guess[P] <= highscores[minstrat][P][0].value) {
       ctr_routes_quickelim++;
       debugf(" QELIM %f %f\n", guess[A], guess[P]);
       return guess[A];
     }
   }
 
-  int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
+  int finisle= ports[nports-1];
+  int finarch= isle2arch(finisle);
+
+  int midisle= ports[nports/2];
   int midarch= route2midarch(ports,nports);
 
-  PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
-  PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
+  PotentialResult *strats[STRATS];
+  int strati;
+  for (strati=minstrat; strati<STRATS; strati++) {
+    PotentialResult **strat_fin;
+    int mid, fin;
+    switch (strati) {
+    case 0: fin=finisle; mid=midisle; break;
+    case 1: fin=finisle; mid=midarch; break;
+    case 2: fin=finarch; mid=midarch; break;
+    default: abort();
+    }
+    strat_fin= ONDEMAND(strat_base[strati][fin], stratsz_mid[strati]);
+    strats[strati]= ONDEMAND(strat_fin[mid], 1);
+  }
 
   if (nports>=2) {
-    if (guess[A] <= strat->value[A] &&
-       guess[P] <= strat->value[P]) {
+    if (guess[A] <= strats[minstrat]->value[A] &&
+       guess[P] <= strats[minstrat]->value[P]) {
       ctr_routes_stratelim++;
       debugf(" ELIM %f %f\n", guess[A], guess[P]);
       return guess[A];
@@ -114,43 +129,50 @@ static double process_route(int nports, int totaldist,
     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];
+  for (strati=minstrat; strati<STRATS; strati++) {
+    PotentialResult *strat= strats[strati];
+
+    if (value[A] <= strat->value[A] &&
+       value[P] <= strat->value[P])
+      continue;
+
+    debugf(" SOMEHOW %d BEST\n",strati);
+
+    fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
+
+    for (ap=0; ap<AP; ap++) {
+      HighScoreEntry *scores= highscores[strati][ap];
+      int *nscores= &nhighscores[strati][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 < *nscores; pos++)
+         if (scores[pos].pr == strat) 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= strat;
+       }
+       fildebugf("@%2d", pos);
       }
-      pos--;
-      if (pos>0) {
-       highscores[ap][pos].value= value[ap];
-       highscores[ap][pos].pr= strat;
-      }
-      fildebugf("@%2d", pos);
     }
   }
 
@@ -164,7 +186,7 @@ static double process_route(int nports, int totaldist,
 }
 
 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;
@@ -181,14 +203,18 @@ static void recurse(int last_isle,
 }
 
 void search(int start_isle, int final_isle_spec,
-           PotentialResult ****strat_base_io) {
-  strat_base= ONDEMAND(*strat_base_io, narches);
+           PotentialResult ****strat_base_io[STRATS]) {
+  int strati;
+  for (strati=0; strati<STRATS; strati++)
+    strat_base[strati]= ONDEMAND(*strat_base_io[strati], stratsz_fin[strati]);
+
   final_isle= final_isle_spec <= 0 ? 0 : final_isle_spec;
   recurse(start_isle,0,0,1e6);
 }
 
-int nhighscores[AP];
-HighScoreEntry *highscores[AP];
+int nhighscores[STRATS][AP];
+HighScoreEntry *highscores[STRATS][AP];
+int minstrat, stratsz_fin[STRATS], stratsz_mid[STRATS];
 
 int narches;
 char **archnames;
@@ -226,4 +252,8 @@ void setup_search(void) {
     islandid2arch[isle]= arch;
   }
   sqlite3_finalize(archs);
+
+  stratsz_fin[0]=                stratsz_mid[0]= islandtablesz;
+  stratsz_fin[1]= islandtablesz; stratsz_mid[1]= narches;
+  stratsz_fin[2]=                stratsz_mid[2]= narches;
 }