chiark / gitweb /
somewhat improve the jpctb docs
[ypp-sc-tools.db-test.git] / yarrg / rssearch.c
index bb84785eef12f3c6ac11819c00511246623482c9..245070e9ad6c34740f576707dee52dca95df03b1 100644 (file)
@@ -1,4 +1,29 @@
-/**/
+/*
+ * Route searcher - recursive iteration over all routes
+ */
+/*
+ *  This is part of the YARRG website, a tool for assisting
+ *  players of Yohoho Puzzle Pirates.
+ * 
+ *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *  
+ *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
+ *  are used without permission.  This program is not endorsed or
+ *  sponsored by Three Rings.
+ */
 
 #include "rscommon.h"
 
@@ -38,12 +63,12 @@ static Neighbour *get_neighbours(int isle) {
 }
 
 
-static PotentialResult ***strat_base[STRATS];
+static Bucket ***buckets_base[GRANUS];
 
 
 static double process_route(int nports, int totaldist,
                            double overestimate_excepting_tail) {
-  int i, ap;
+  int i, ap, granui;
   int leagues_divisor= totaldist + nports;
 
   ctr_routes_considered++;
@@ -79,12 +104,15 @@ static double process_route(int nports, int totaldist,
       return guess[A];
     }
 
-    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];
+    for (granui=0; granui<granus; granui++) {
+      if (guess[A] > highscores[granui][A][0].value ||
+         guess[P] > highscores[granui][P][0].value)
+       goto not_quickelim;
     }
+    ctr_routes_quickelim++;
+    debugf(" QELIM %f %f\n", guess[A], guess[P]);
+    return guess[A];
+  not_quickelim:;
   }
 
   int finisle= ports[nports-1];
@@ -93,28 +121,30 @@ static double process_route(int nports, int totaldist,
   int midisle= ports[nports/2];
   int midarch= route2midarch(ports,nports);
 
-  PotentialResult *strats[STRATS];
-  int strati;
-  for (strati=minstrat; strati<STRATS; strati++) {
-    PotentialResult **strat_fin;
+  Bucket *buckets[GRANUS];
+  for (granui=0; granui<granus; granui++) {
+    Bucket **buckets_fin;
     int mid, fin;
-    switch (strati) {
-    case 0: fin=finisle; mid=midisle; break;
+    switch (granui) {
+    case 0: fin=finarch; mid=midarch; break;
     case 1: fin=finisle; mid=midarch; break;
-    case 2: fin=finarch; mid=midarch; break;
+    case 2: fin=finisle; mid=midisle; break;
     default: abort();
     }
-    strat_fin= ONDEMAND(strat_base[strati][fin], stratsz_mid[strati]);
-    strats[strati]= ONDEMAND(strat_fin[mid], 1);
+    buckets_fin= ONDEMAND(buckets_base[granui][fin], granusz_mid[granui]);
+    buckets[granui]= ONDEMAND(buckets_fin[mid], 1);
   }
 
   if (nports>=2) {
-    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];
-    }
+    for (granui=0; granui<granus; granui++)
+      for (ap=0; ap<AP; ap++)
+       if (guess[ap] > buckets[granui]->prs[ap].value[ap] &&
+           guess[ap] > highscores[granui][ap][0].value)
+         goto not_bucketelim;
+    ctr_routes_bucketelim++;
+    debugf(" ELIM %f %f\n", guess[A], guess[P]);
+    return guess[A];
+  not_bucketelim:
     debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
   }
 
@@ -129,39 +159,41 @@ static double process_route(int nports, int totaldist,
     return value[0];
   }
 
-  for (strati=minstrat; strati<STRATS; strati++) {
-    PotentialResult *strat= strats[strati];
+  for (granui=granus-1; granui>=0; granui--) {
+    Bucket *bucket= buckets[granui];
 
-    if (value[A] <= strat->value[A] &&
-       value[P] <= strat->value[P])
+    if (value[A] <= bucket->prs[A].value[A] &&
+       value[P] <= bucket->prs[P].value[P])
       continue;
 
-    debugf(" SOMEHOW %d BEST\n",strati);
+    debugf(" SOMEHOW %d BEST\n",granui);
 
-    fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
+    fildebugf("granu %d f%d:%3d mid%d:%3d ",granui,
+             finarch,finisle,midarch,midisle);
 
     for (ap=0; ap<AP; ap++) {
-      HighScoreEntry *scores= highscores[strati][ap];
-      int *nscores= &nhighscores[strati][ap];
+      HighScoreEntry *scores= highscores[granui][ap];
+      int nscores= nhighscores[granui][ap];
 
       fildebugf("ap=%d %15f", ap, value[ap]);
-      if (value[ap] < strat->value[ap]) {
+      if (value[ap] < bucket->prs[ap].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;
+       ctr_newbests_granu[granui*AP+ap]++;
+       bucket->prs[ap].length= totaldist;
+       memcpy(bucket->prs[ap].value, value, sizeof(value));
+       memcpy(bucket->prs[ap].ports, ports, sizeof(*ports) * nports);
+       if (nports < MAX_ROUTELEN-1) bucket->prs[ap].ports[nports]= -1;
        fildebugf("** ");
-       for (pos=0; pos < *nscores; pos++)
-         if (scores[pos].pr == strat) goto found;
+       for (pos=0; pos < nscores; pos++)
+         if (scores[pos].bucket == bucket) goto found;
        /* not found */
        pos= -1;
       found:
        for (;;) {
          pos++;
-         if (pos >= *nscores-1) break; /* new top */
+         if (pos >= nscores) break; /* new top */
          if (scores[pos].value >= value[ap]) break; /* found spot */
          if (pos>0)
            scores[pos-1]= scores[pos];
@@ -169,12 +201,20 @@ static double process_route(int nports, int totaldist,
        pos--;
        if (pos>0) {
          scores[pos].value= value[ap];
-         scores[pos].pr= strat;
+         scores[pos].bucket= bucket;
+         if (granui < granus-1 &&
+             highscores[granui][A][0].bucket &&
+             highscores[granui][P][0].bucket) {
+           /* both absolute and perleague are full at this granularity,
+            * so we don't care about anything more granular */
+           fildebugf("\n                STOP-GRANU            ");
+           granus= granui+1;
+         }
        }
-       fildebugf("@%2d", pos);
-      }
-    }
-  }
+       fildebugf("@%2d/%2d ", pos, nscores);
+      } /* new best */
+    } /* ap */
+  } /* granui */
 
   fildebugf(" route");
 
@@ -203,18 +243,19 @@ static void recurse(int last_isle,
 }
 
 void search(int start_isle, int final_isle_spec,
-           PotentialResult ****strat_base_io[STRATS]) {
-  int strati;
-  for (strati=0; strati<STRATS; strati++)
-    strat_base[strati]= ONDEMAND(*strat_base_io[strati], stratsz_fin[strati]);
+           Bucket ****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[STRATS][AP];
-HighScoreEntry *highscores[STRATS][AP];
-int minstrat, stratsz_fin[STRATS], stratsz_mid[STRATS];
+int nhighscores[GRANUS][AP];
+HighScoreEntry *highscores[GRANUS][AP];
+int granus=GRANUS, granusz_fin[GRANUS], granusz_mid[GRANUS];
 
 int narches;
 char **archnames;
@@ -253,7 +294,7 @@ void setup_search(void) {
   }
   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;
+  granusz_fin[0]=                granusz_mid[0]= narches;
+  granusz_fin[1]= islandtablesz; granusz_mid[1]= narches;
+  granusz_fin[2]=                granusz_mid[2]= islandtablesz;
 }